InstallationController.js 3.0 KB

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