InstallationController.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
  3. var _promise = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/promise"));
  4. var _Storage = _interopRequireDefault(require("./Storage"));
  5. /**
  6. * Copyright (c) 2015-present, Parse, LLC.
  7. * All rights reserved.
  8. *
  9. * This source code is licensed under the BSD-style license found in the
  10. * LICENSE file in the root directory of this source tree. An additional grant
  11. * of patent rights can be found in the PATENTS file in the same directory.
  12. *
  13. * @flow
  14. */
  15. var uuidv4 = require('./uuid');
  16. var iidCache = null;
  17. var InstallationController = {
  18. currentInstallationId: function ()
  19. /*: Promise<string>*/
  20. {
  21. if (typeof iidCache === 'string') {
  22. return _promise.default.resolve(iidCache);
  23. }
  24. var path = _Storage.default.generatePath('installationId');
  25. return _Storage.default.getItemAsync(path).then(function (iid) {
  26. if (!iid) {
  27. iid = uuidv4();
  28. return _Storage.default.setItemAsync(path, iid).then(function () {
  29. iidCache = iid;
  30. return iid;
  31. });
  32. }
  33. iidCache = iid;
  34. return iid;
  35. });
  36. },
  37. _clearCache: function () {
  38. iidCache = null;
  39. },
  40. _setInstallationIdCache: function (iid
  41. /*: string*/
  42. ) {
  43. iidCache = iid;
  44. }
  45. };
  46. module.exports = InstallationController;