esnext.disposable-stack.constructor.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. 'use strict';
  2. // https://github.com/tc39/proposal-explicit-resource-management
  3. var $ = require('../internals/export');
  4. var DESCRIPTORS = require('../internals/descriptors');
  5. var getBuiltIn = require('../internals/get-built-in');
  6. var aCallable = require('../internals/a-callable');
  7. var anInstance = require('../internals/an-instance');
  8. var defineBuiltIn = require('../internals/define-built-in');
  9. var defineBuiltIns = require('../internals/define-built-ins');
  10. var defineBuiltInAccessor = require('../internals/define-built-in-accessor');
  11. var wellKnownSymbol = require('../internals/well-known-symbol');
  12. var InternalStateModule = require('../internals/internal-state');
  13. var addDisposableResource = require('../internals/add-disposable-resource');
  14. var SuppressedError = getBuiltIn('SuppressedError');
  15. var $ReferenceError = ReferenceError;
  16. var DISPOSE = wellKnownSymbol('dispose');
  17. var TO_STRING_TAG = wellKnownSymbol('toStringTag');
  18. var DISPOSABLE_STACK = 'DisposableStack';
  19. var setInternalState = InternalStateModule.set;
  20. var getDisposableStackInternalState = InternalStateModule.getterFor(DISPOSABLE_STACK);
  21. var HINT = 'sync-dispose';
  22. var DISPOSED = 'disposed';
  23. var PENDING = 'pending';
  24. var getPendingDisposableStackInternalState = function (stack) {
  25. var internalState = getDisposableStackInternalState(stack);
  26. if (internalState.state === DISPOSED) throw new $ReferenceError(DISPOSABLE_STACK + ' already disposed');
  27. return internalState;
  28. };
  29. var $DisposableStack = function DisposableStack() {
  30. setInternalState(anInstance(this, DisposableStackPrototype), {
  31. type: DISPOSABLE_STACK,
  32. state: PENDING,
  33. stack: []
  34. });
  35. if (!DESCRIPTORS) this.disposed = false;
  36. };
  37. var DisposableStackPrototype = $DisposableStack.prototype;
  38. defineBuiltIns(DisposableStackPrototype, {
  39. dispose: function dispose() {
  40. var internalState = getDisposableStackInternalState(this);
  41. if (internalState.state === DISPOSED) return;
  42. internalState.state = DISPOSED;
  43. if (!DESCRIPTORS) this.disposed = true;
  44. var stack = internalState.stack;
  45. var i = stack.length;
  46. var thrown = false;
  47. var suppressed;
  48. while (i) {
  49. var disposeMethod = stack[--i];
  50. stack[i] = null;
  51. try {
  52. disposeMethod();
  53. } catch (errorResult) {
  54. if (thrown) {
  55. suppressed = new SuppressedError(errorResult, suppressed);
  56. } else {
  57. thrown = true;
  58. suppressed = errorResult;
  59. }
  60. }
  61. }
  62. internalState.stack = null;
  63. if (thrown) throw suppressed;
  64. },
  65. use: function use(value) {
  66. addDisposableResource(getPendingDisposableStackInternalState(this), value, HINT);
  67. return value;
  68. },
  69. adopt: function adopt(value, onDispose) {
  70. var internalState = getPendingDisposableStackInternalState(this);
  71. aCallable(onDispose);
  72. addDisposableResource(internalState, undefined, HINT, function () {
  73. onDispose(value);
  74. });
  75. return value;
  76. },
  77. defer: function defer(onDispose) {
  78. var internalState = getPendingDisposableStackInternalState(this);
  79. aCallable(onDispose);
  80. addDisposableResource(internalState, undefined, HINT, onDispose);
  81. },
  82. move: function move() {
  83. var internalState = getPendingDisposableStackInternalState(this);
  84. var newDisposableStack = new $DisposableStack();
  85. getDisposableStackInternalState(newDisposableStack).stack = internalState.stack;
  86. internalState.stack = [];
  87. internalState.state = DISPOSED;
  88. if (!DESCRIPTORS) this.disposed = true;
  89. return newDisposableStack;
  90. }
  91. });
  92. if (DESCRIPTORS) defineBuiltInAccessor(DisposableStackPrototype, 'disposed', {
  93. configurable: true,
  94. get: function disposed() {
  95. return getDisposableStackInternalState(this).state === DISPOSED;
  96. }
  97. });
  98. defineBuiltIn(DisposableStackPrototype, DISPOSE, DisposableStackPrototype.dispose, { name: 'dispose' });
  99. defineBuiltIn(DisposableStackPrototype, TO_STRING_TAG, DISPOSABLE_STACK, { nonWritable: true });
  100. $({ global: true, constructor: true }, {
  101. DisposableStack: $DisposableStack
  102. });