InstallationController.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. let iidCache = null;
  19. function hexOctet() {
  20. return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
  21. }
  22. function generateId() {
  23. return hexOctet() + hexOctet() + '-' + hexOctet() + '-' + hexOctet() + '-' + hexOctet() + '-' + hexOctet() + hexOctet() + hexOctet();
  24. }
  25. const InstallationController = {
  26. currentInstallationId()
  27. /*: Promise<string>*/
  28. {
  29. if (typeof iidCache === 'string') {
  30. return Promise.resolve(iidCache);
  31. }
  32. const path = _Storage.default.generatePath('installationId');
  33. return _Storage.default.getItemAsync(path).then(iid => {
  34. if (!iid) {
  35. iid = generateId();
  36. return _Storage.default.setItemAsync(path, iid).then(() => {
  37. iidCache = iid;
  38. return iid;
  39. });
  40. }
  41. iidCache = iid;
  42. return iid;
  43. });
  44. },
  45. _clearCache() {
  46. iidCache = null;
  47. },
  48. _setInstallationIdCache(iid
  49. /*: string*/
  50. ) {
  51. iidCache = iid;
  52. }
  53. };
  54. module.exports = InstallationController;