esnext.async-disposable-stack.constructor.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. 'use strict';
  2. // https://github.com/tc39/proposal-async-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 Promise = getBuiltIn('Promise');
  15. var SuppressedError = getBuiltIn('SuppressedError');
  16. var $ReferenceError = ReferenceError;
  17. var ASYNC_DISPOSE = wellKnownSymbol('asyncDispose');
  18. var TO_STRING_TAG = wellKnownSymbol('toStringTag');
  19. var ASYNC_DISPOSABLE_STACK = 'AsyncDisposableStack';
  20. var setInternalState = InternalStateModule.set;
  21. var getAsyncDisposableStackInternalState = InternalStateModule.getterFor(ASYNC_DISPOSABLE_STACK);
  22. var HINT = 'async-dispose';
  23. var DISPOSED = 'disposed';
  24. var PENDING = 'pending';
  25. var getPendingAsyncDisposableStackInternalState = function (stack) {
  26. var internalState = getAsyncDisposableStackInternalState(stack);
  27. if (internalState.state === DISPOSED) throw new $ReferenceError(ASYNC_DISPOSABLE_STACK + ' already disposed');
  28. return internalState;
  29. };
  30. var $AsyncDisposableStack = function AsyncDisposableStack() {
  31. setInternalState(anInstance(this, AsyncDisposableStackPrototype), {
  32. type: ASYNC_DISPOSABLE_STACK,
  33. state: PENDING,
  34. stack: []
  35. });
  36. if (!DESCRIPTORS) this.disposed = false;
  37. };
  38. var AsyncDisposableStackPrototype = $AsyncDisposableStack.prototype;
  39. defineBuiltIns(AsyncDisposableStackPrototype, {
  40. disposeAsync: function disposeAsync() {
  41. var asyncDisposableStack = this;
  42. return new Promise(function (resolve, reject) {
  43. var internalState = getAsyncDisposableStackInternalState(asyncDisposableStack);
  44. if (internalState.state === DISPOSED) return resolve(undefined);
  45. internalState.state = DISPOSED;
  46. if (!DESCRIPTORS) asyncDisposableStack.disposed = true;
  47. var stack = internalState.stack;
  48. var i = stack.length;
  49. var thrown = false;
  50. var suppressed;
  51. var handleError = function (result) {
  52. if (thrown) {
  53. suppressed = new SuppressedError(result, suppressed);
  54. } else {
  55. thrown = true;
  56. suppressed = result;
  57. }
  58. loop();
  59. };
  60. var loop = function () {
  61. if (i) {
  62. var disposeMethod = stack[--i];
  63. stack[i] = null;
  64. try {
  65. Promise.resolve(disposeMethod()).then(loop, handleError);
  66. } catch (error) {
  67. handleError(error);
  68. }
  69. } else {
  70. internalState.stack = null;
  71. thrown ? reject(suppressed) : resolve(undefined);
  72. }
  73. };
  74. loop();
  75. });
  76. },
  77. use: function use(value) {
  78. addDisposableResource(getPendingAsyncDisposableStackInternalState(this), value, HINT);
  79. return value;
  80. },
  81. adopt: function adopt(value, onDispose) {
  82. var internalState = getPendingAsyncDisposableStackInternalState(this);
  83. aCallable(onDispose);
  84. addDisposableResource(internalState, undefined, HINT, function () {
  85. return onDispose(value);
  86. });
  87. return value;
  88. },
  89. defer: function defer(onDispose) {
  90. var internalState = getPendingAsyncDisposableStackInternalState(this);
  91. aCallable(onDispose);
  92. addDisposableResource(internalState, undefined, HINT, onDispose);
  93. },
  94. move: function move() {
  95. var internalState = getPendingAsyncDisposableStackInternalState(this);
  96. var newAsyncDisposableStack = new $AsyncDisposableStack();
  97. getAsyncDisposableStackInternalState(newAsyncDisposableStack).stack = internalState.stack;
  98. internalState.stack = [];
  99. internalState.state = DISPOSED;
  100. if (!DESCRIPTORS) this.disposed = true;
  101. return newAsyncDisposableStack;
  102. }
  103. });
  104. if (DESCRIPTORS) defineBuiltInAccessor(AsyncDisposableStackPrototype, 'disposed', {
  105. configurable: true,
  106. get: function disposed() {
  107. return getAsyncDisposableStackInternalState(this).state === DISPOSED;
  108. }
  109. });
  110. defineBuiltIn(AsyncDisposableStackPrototype, ASYNC_DISPOSE, AsyncDisposableStackPrototype.disposeAsync, { name: 'disposeAsync' });
  111. defineBuiltIn(AsyncDisposableStackPrototype, TO_STRING_TAG, ASYNC_DISPOSABLE_STACK, { nonWritable: true });
  112. $({ global: true, constructor: true }, {
  113. AsyncDisposableStack: $AsyncDisposableStack
  114. });