InstallationController.js 1.2 KB

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