InstallationController.js 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
  7. var _CoreManager = _interopRequireDefault(require("./CoreManager"));
  8. var _Storage = _interopRequireDefault(require("./Storage"));
  9. var _ParseInstallation = _interopRequireDefault(require("./ParseInstallation"));
  10. var _uuid = _interopRequireDefault(require("./uuid"));
  11. var CURRENT_INSTALLATION_KEY = 'currentInstallation';
  12. var CURRENT_INSTALLATION_ID_KEY = 'currentInstallationId';
  13. var iidCache = null;
  14. var currentInstallationCache = null;
  15. var currentInstallationCacheMatchesDisk = false;
  16. var InstallationController = {
  17. updateInstallationOnDisk: function () {
  18. var _updateInstallationOnDisk = (0, _asyncToGenerator2.default)(function* (installation) {
  19. var path = _Storage.default.generatePath(CURRENT_INSTALLATION_KEY);
  20. yield _Storage.default.setItemAsync(path, JSON.stringify(installation.toJSON()));
  21. this._setCurrentInstallationCache(installation);
  22. });
  23. function updateInstallationOnDisk() {
  24. return _updateInstallationOnDisk.apply(this, arguments);
  25. }
  26. return updateInstallationOnDisk;
  27. }(),
  28. currentInstallationId: function () {
  29. var _currentInstallationId = (0, _asyncToGenerator2.default)(function* () {
  30. if (typeof iidCache === 'string') {
  31. return iidCache;
  32. }
  33. var path = _Storage.default.generatePath(CURRENT_INSTALLATION_ID_KEY);
  34. var iid = yield _Storage.default.getItemAsync(path);
  35. if (!iid) {
  36. iid = (0, _uuid.default)();
  37. return _Storage.default.setItemAsync(path, iid).then(function () {
  38. iidCache = iid;
  39. return iid;
  40. });
  41. }
  42. iidCache = iid;
  43. return iid;
  44. });
  45. function currentInstallationId() {
  46. return _currentInstallationId.apply(this, arguments);
  47. }
  48. return currentInstallationId;
  49. }(),
  50. currentInstallation: function () {
  51. var _currentInstallation = (0, _asyncToGenerator2.default)(function* () {
  52. if (currentInstallationCache) {
  53. return currentInstallationCache;
  54. }
  55. if (currentInstallationCacheMatchesDisk) {
  56. return null;
  57. }
  58. var path = _Storage.default.generatePath(CURRENT_INSTALLATION_KEY);
  59. var installationData = yield _Storage.default.getItemAsync(path);
  60. currentInstallationCacheMatchesDisk = true;
  61. if (installationData) {
  62. installationData = JSON.parse(installationData);
  63. installationData.className = '_Installation';
  64. var current = _ParseInstallation.default.fromJSON(installationData);
  65. currentInstallationCache = current;
  66. return current;
  67. }
  68. var installationId = yield this.currentInstallationId();
  69. var installation = new _ParseInstallation.default();
  70. installation.set('deviceType', _ParseInstallation.default.DEVICE_TYPES.WEB);
  71. installation.set('installationId', installationId);
  72. installation.set('parseVersion', _CoreManager.default.get('VERSION'));
  73. currentInstallationCache = installation;
  74. yield _Storage.default.setItemAsync(path, JSON.stringify(installation.toJSON()));
  75. return installation;
  76. });
  77. function currentInstallation() {
  78. return _currentInstallation.apply(this, arguments);
  79. }
  80. return currentInstallation;
  81. }(),
  82. _clearCache: function () {
  83. iidCache = null;
  84. currentInstallationCache = null;
  85. currentInstallationCacheMatchesDisk = false;
  86. },
  87. _setInstallationIdCache: function (iid) {
  88. iidCache = iid;
  89. },
  90. _setCurrentInstallationCache: function (installation) {
  91. var matchesDisk = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
  92. currentInstallationCache = installation;
  93. currentInstallationCacheMatchesDisk = matchesDisk;
  94. }
  95. };
  96. module.exports = InstallationController;
  97. var _default = exports.default = InstallationController;