InstallationController.js 1.5 KB

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