Node-Red configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

internal-state.js 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. 'use strict';
  2. var NATIVE_WEAK_MAP = require('../internals/weak-map-basic-detection');
  3. var globalThis = require('../internals/global-this');
  4. var isObject = require('../internals/is-object');
  5. var createNonEnumerableProperty = require('../internals/create-non-enumerable-property');
  6. var hasOwn = require('../internals/has-own-property');
  7. var shared = require('../internals/shared-store');
  8. var sharedKey = require('../internals/shared-key');
  9. var hiddenKeys = require('../internals/hidden-keys');
  10. var OBJECT_ALREADY_INITIALIZED = 'Object already initialized';
  11. var TypeError = globalThis.TypeError;
  12. var WeakMap = globalThis.WeakMap;
  13. var set, get, has;
  14. var enforce = function (it) {
  15. return has(it) ? get(it) : set(it, {});
  16. };
  17. var getterFor = function (TYPE) {
  18. return function (it) {
  19. var state;
  20. if (!isObject(it) || (state = get(it)).type !== TYPE) {
  21. throw new TypeError('Incompatible receiver, ' + TYPE + ' required');
  22. } return state;
  23. };
  24. };
  25. if (NATIVE_WEAK_MAP || shared.state) {
  26. var store = shared.state || (shared.state = new WeakMap());
  27. /* eslint-disable no-self-assign -- prototype methods protection */
  28. store.get = store.get;
  29. store.has = store.has;
  30. store.set = store.set;
  31. /* eslint-enable no-self-assign -- prototype methods protection */
  32. set = function (it, metadata) {
  33. if (store.has(it)) throw new TypeError(OBJECT_ALREADY_INITIALIZED);
  34. metadata.facade = it;
  35. store.set(it, metadata);
  36. return metadata;
  37. };
  38. get = function (it) {
  39. return store.get(it) || {};
  40. };
  41. has = function (it) {
  42. return store.has(it);
  43. };
  44. } else {
  45. var STATE = sharedKey('state');
  46. hiddenKeys[STATE] = true;
  47. set = function (it, metadata) {
  48. if (hasOwn(it, STATE)) throw new TypeError(OBJECT_ALREADY_INITIALIZED);
  49. metadata.facade = it;
  50. createNonEnumerableProperty(it, STATE, metadata);
  51. return metadata;
  52. };
  53. get = function (it) {
  54. return hasOwn(it, STATE) ? it[STATE] : {};
  55. };
  56. has = function (it) {
  57. return hasOwn(it, STATE);
  58. };
  59. }
  60. module.exports = {
  61. set: set,
  62. get: get,
  63. has: has,
  64. enforce: enforce,
  65. getterFor: getterFor
  66. };