-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
add-disposable-resource.js
56 lines (49 loc) · 2.11 KB
/
add-disposable-resource.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use strict';
var call = require('../internals/function-call');
var uncurryThis = require('../internals/function-uncurry-this');
var bind = require('../internals/function-bind-context');
var anObject = require('../internals/an-object');
var aCallable = require('../internals/a-callable');
var isNullOrUndefined = require('../internals/is-null-or-undefined');
var getMethod = require('../internals/get-method');
var wellKnownSymbol = require('../internals/well-known-symbol');
var ASYNC_DISPOSE = wellKnownSymbol('asyncDispose');
var DISPOSE = wellKnownSymbol('dispose');
var push = uncurryThis([].push);
// `GetDisposeMethod` abstract operation
// https://tc39.es/proposal-explicit-resource-management/#sec-getdisposemethod
var getDisposeMethod = function (V, hint) {
if (hint === 'async-dispose') {
var method = getMethod(V, ASYNC_DISPOSE);
if (method !== undefined) return method;
method = getMethod(V, DISPOSE);
if (method === undefined) return method;
return function () {
call(method, this);
};
} return getMethod(V, DISPOSE);
};
// `CreateDisposableResource` abstract operation
// https://tc39.es/proposal-explicit-resource-management/#sec-createdisposableresource
var createDisposableResource = function (V, hint, method) {
if (arguments.length < 3 && !isNullOrUndefined(V)) {
method = aCallable(getDisposeMethod(anObject(V), hint));
}
return method === undefined ? function () {
return undefined;
} : bind(method, V);
};
// `AddDisposableResource` abstract operation
// https://tc39.es/proposal-explicit-resource-management/#sec-adddisposableresource
module.exports = function (disposable, V, hint, method) {
var resource;
if (arguments.length < 4) {
// When `V`` is either `null` or `undefined` and hint is `async-dispose`,
// we record that the resource was evaluated to ensure we will still perform an `Await` when resources are later disposed.
if (isNullOrUndefined(V) && hint === 'sync-dispose') return;
resource = createDisposableResource(V, hint);
} else {
resource = createDisposableResource(undefined, hint, method);
}
push(disposable.stack, resource);
};