InstallationController.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 {
  19. v4: uuidv4
  20. } = require('uuid');
  21. let iidCache = null;
  22. const InstallationController = {
  23. currentInstallationId()
  24. /*: Promise<string>*/
  25. {
  26. if (typeof iidCache === 'string') {
  27. return Promise.resolve(iidCache);
  28. }
  29. const path = _Storage.default.generatePath('installationId');
  30. return _Storage.default.getItemAsync(path).then(iid => {
  31. if (!iid) {
  32. iid = uuidv4();
  33. return _Storage.default.setItemAsync(path, iid).then(() => {
  34. iidCache = iid;
  35. return iid;
  36. });
  37. }
  38. iidCache = iid;
  39. return iid;
  40. });
  41. },
  42. _clearCache() {
  43. iidCache = null;
  44. },
  45. _setInstallationIdCache(iid
  46. /*: string*/
  47. ) {
  48. iidCache = iid;
  49. }
  50. };
  51. module.exports = InstallationController;