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