id.js 769 B

123456789101112131415161718192021222324252627
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. 'use strict';
  5. module.metadata = {
  6. 'stability': 'experimental'
  7. };
  8. const method = require('../../method/core');
  9. const { uuid } = require('../util/uuid');
  10. // NOTE: use lang/functional memoize when it is updated to use WeakMap
  11. function memoize(f) {
  12. const memo = new WeakMap();
  13. return function memoizer(o) {
  14. let key = o;
  15. if (!memo.has(key))
  16. memo.set(key, f.apply(this, arguments));
  17. return memo.get(key);
  18. };
  19. }
  20. let identify = method('identify');
  21. identify.define(Object, memoize(function() { return uuid(); }));
  22. exports.identify = identify;