InstallationController.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. "use strict";
  2. var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");
  3. var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
  4. _Object$defineProperty(exports, "__esModule", {
  5. value: true
  6. });
  7. exports.default = void 0;
  8. var _stringify = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/json/stringify"));
  9. var _CoreManager = _interopRequireDefault(require("./CoreManager"));
  10. var _Storage = _interopRequireDefault(require("./Storage"));
  11. var _ParseInstallation = _interopRequireDefault(require("./ParseInstallation"));
  12. var _uuid = _interopRequireDefault(require("./uuid"));
  13. const CURRENT_INSTALLATION_KEY = 'currentInstallation';
  14. const CURRENT_INSTALLATION_ID_KEY = 'currentInstallationId';
  15. let iidCache = null;
  16. let currentInstallationCache = null;
  17. let currentInstallationCacheMatchesDisk = false;
  18. const InstallationController = {
  19. async updateInstallationOnDisk(installation) {
  20. const path = _Storage.default.generatePath(CURRENT_INSTALLATION_KEY);
  21. await _Storage.default.setItemAsync(path, (0, _stringify.default)(installation.toJSON()));
  22. this._setCurrentInstallationCache(installation);
  23. },
  24. async currentInstallationId() {
  25. if (typeof iidCache === 'string') {
  26. return iidCache;
  27. }
  28. const path = _Storage.default.generatePath(CURRENT_INSTALLATION_ID_KEY);
  29. let iid = await _Storage.default.getItemAsync(path);
  30. if (!iid) {
  31. iid = (0, _uuid.default)();
  32. return _Storage.default.setItemAsync(path, iid).then(() => {
  33. iidCache = iid;
  34. return iid;
  35. });
  36. }
  37. iidCache = iid;
  38. return iid;
  39. },
  40. async currentInstallation() {
  41. if (currentInstallationCache) {
  42. return currentInstallationCache;
  43. }
  44. if (currentInstallationCacheMatchesDisk) {
  45. return null;
  46. }
  47. const path = _Storage.default.generatePath(CURRENT_INSTALLATION_KEY);
  48. let installationData = await _Storage.default.getItemAsync(path);
  49. currentInstallationCacheMatchesDisk = true;
  50. if (installationData) {
  51. installationData = JSON.parse(installationData);
  52. installationData.className = '_Installation';
  53. const current = _ParseInstallation.default.fromJSON(installationData);
  54. currentInstallationCache = current;
  55. return current;
  56. }
  57. const installationId = await this.currentInstallationId();
  58. const installation = new _ParseInstallation.default();
  59. installation.set('deviceType', _ParseInstallation.default.DEVICE_TYPES.WEB);
  60. installation.set('installationId', installationId);
  61. installation.set('parseVersion', _CoreManager.default.get('VERSION'));
  62. currentInstallationCache = installation;
  63. await _Storage.default.setItemAsync(path, (0, _stringify.default)(installation.toJSON()));
  64. return installation;
  65. },
  66. _clearCache() {
  67. iidCache = null;
  68. currentInstallationCache = null;
  69. currentInstallationCacheMatchesDisk = false;
  70. },
  71. _setInstallationIdCache(iid) {
  72. iidCache = iid;
  73. },
  74. _setCurrentInstallationCache(installation) {
  75. let matchesDisk = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
  76. currentInstallationCache = installation;
  77. currentInstallationCacheMatchesDisk = matchesDisk;
  78. }
  79. };
  80. module.exports = InstallationController;
  81. var _default = exports.default = InstallationController;