Skip to content

Commit

Permalink
move validation of pending stacks to helper
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Mar 23, 2023
1 parent 3493e88 commit 096cb45
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ var HINT = 'async-dispose';
var DISPOSED = 'disposed';
var PENDING = 'pending';

var ALREADY_DISPOSED = ASYNC_DISPOSABLE_STACK + ' already disposed';
var getPendingAsyncDisposableStackInternalState = function (stack) {
var internalState = getAsyncDisposableStackInternalState(stack);
if (internalState.state == DISPOSED) throw $ReferenceError(ASYNC_DISPOSABLE_STACK + ' already disposed');
return internalState;
};

var $AsyncDisposableStack = function AsyncDisposableStack() {
setInternalState(anInstance(this, AsyncDisposableStackPrototype), {
Expand Down Expand Up @@ -84,29 +88,24 @@ defineBuiltIns(AsyncDisposableStackPrototype, {
});
},
use: function use(value) {
var internalState = getAsyncDisposableStackInternalState(this);
if (internalState.state == DISPOSED) throw $ReferenceError(ALREADY_DISPOSED);
addDisposableResource(internalState, value, HINT);
addDisposableResource(getPendingAsyncDisposableStackInternalState(this), value, HINT);
return value;
},
adopt: function adopt(value, onDispose) {
var internalState = getAsyncDisposableStackInternalState(this);
if (internalState.state == DISPOSED) throw $ReferenceError(ALREADY_DISPOSED);
var internalState = getPendingAsyncDisposableStackInternalState(this);
aCallable(onDispose);
addDisposableResource(internalState, undefined, HINT, function () {
onDispose(value);
});
return value;
},
defer: function defer(onDispose) {
var internalState = getAsyncDisposableStackInternalState(this);
if (internalState.state == DISPOSED) throw $ReferenceError(ALREADY_DISPOSED);
var internalState = getPendingAsyncDisposableStackInternalState(this);
aCallable(onDispose);
addDisposableResource(internalState, undefined, HINT, onDispose);
},
move: function move() {
var internalState = getAsyncDisposableStackInternalState(this);
if (internalState.state == DISPOSED) throw $ReferenceError(ALREADY_DISPOSED);
var internalState = getPendingAsyncDisposableStackInternalState(this);
var newAsyncDisposableStack = new $AsyncDisposableStack();
getAsyncDisposableStackInternalState(newAsyncDisposableStack).stack = internalState.stack;
internalState.stack = [];
Expand Down
19 changes: 9 additions & 10 deletions packages/core-js/modules/esnext.disposable-stack.constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ var HINT = 'sync-dispose';
var DISPOSED = 'disposed';
var PENDING = 'pending';

var ALREADY_DISPOSED = DISPOSABLE_STACK + ' already disposed';
var getPendingDisposableStackInternalState = function (stack) {
var internalState = getDisposableStackInternalState(stack);
if (internalState.state == DISPOSED) throw $ReferenceError(DISPOSABLE_STACK + ' already disposed');
return internalState;
};

var $DisposableStack = function DisposableStack() {
setInternalState(anInstance(this, DisposableStackPrototype), {
Expand Down Expand Up @@ -68,29 +72,24 @@ defineBuiltIns(DisposableStackPrototype, {
if (thrown) throw suppressed;
},
use: function use(value) {
var internalState = getDisposableStackInternalState(this);
if (internalState.state == DISPOSED) throw $ReferenceError(ALREADY_DISPOSED);
addDisposableResource(internalState, value, HINT);
addDisposableResource(getPendingDisposableStackInternalState(this), value, HINT);
return value;
},
adopt: function adopt(value, onDispose) {
var internalState = getDisposableStackInternalState(this);
if (internalState.state == DISPOSED) throw $ReferenceError(ALREADY_DISPOSED);
var internalState = getPendingDisposableStackInternalState(this);
aCallable(onDispose);
addDisposableResource(internalState, undefined, HINT, function () {
onDispose(value);
});
return value;
},
defer: function defer(onDispose) {
var internalState = getDisposableStackInternalState(this);
if (internalState.state == DISPOSED) throw $ReferenceError(ALREADY_DISPOSED);
var internalState = getPendingDisposableStackInternalState(this);
aCallable(onDispose);
addDisposableResource(internalState, undefined, HINT, onDispose);
},
move: function move() {
var internalState = getDisposableStackInternalState(this);
if (internalState.state == DISPOSED) throw $ReferenceError(ALREADY_DISPOSED);
var internalState = getPendingDisposableStackInternalState(this);
var newDisposableStack = new $DisposableStack();
getDisposableStackInternalState(newDisposableStack).stack = internalState.stack;
internalState.stack = [];
Expand Down

0 comments on commit 096cb45

Please sign in to comment.