1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = _usingCtx;
|
---|
7 | function _usingCtx() {
|
---|
8 | var _disposeSuppressedError = typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed) {
|
---|
9 | var err = new Error();
|
---|
10 | err.name = "SuppressedError";
|
---|
11 | err.error = error;
|
---|
12 | err.suppressed = suppressed;
|
---|
13 | return err;
|
---|
14 | },
|
---|
15 | empty = {},
|
---|
16 | stack = [];
|
---|
17 | function using(isAwait, value) {
|
---|
18 | if (value != null) {
|
---|
19 | if (Object(value) !== value) {
|
---|
20 | throw new TypeError("using declarations can only be used with objects, functions, null, or undefined.");
|
---|
21 | }
|
---|
22 | if (isAwait) {
|
---|
23 | var dispose = value[Symbol.asyncDispose || Symbol.for("Symbol.asyncDispose")];
|
---|
24 | }
|
---|
25 | if (dispose === undefined) {
|
---|
26 | dispose = value[Symbol.dispose || Symbol.for("Symbol.dispose")];
|
---|
27 | if (isAwait) {
|
---|
28 | var inner = dispose;
|
---|
29 | }
|
---|
30 | }
|
---|
31 | if (typeof dispose !== "function") {
|
---|
32 | throw new TypeError("Object is not disposable.");
|
---|
33 | }
|
---|
34 | if (inner) {
|
---|
35 | dispose = function () {
|
---|
36 | try {
|
---|
37 | inner.call(value);
|
---|
38 | } catch (e) {
|
---|
39 | return Promise.reject(e);
|
---|
40 | }
|
---|
41 | };
|
---|
42 | }
|
---|
43 | stack.push({
|
---|
44 | v: value,
|
---|
45 | d: dispose,
|
---|
46 | a: isAwait
|
---|
47 | });
|
---|
48 | } else if (isAwait) {
|
---|
49 | stack.push({
|
---|
50 | d: value,
|
---|
51 | a: isAwait
|
---|
52 | });
|
---|
53 | }
|
---|
54 | return value;
|
---|
55 | }
|
---|
56 | return {
|
---|
57 | e: empty,
|
---|
58 | u: using.bind(null, false),
|
---|
59 | a: using.bind(null, true),
|
---|
60 | d: function () {
|
---|
61 | var error = this.e,
|
---|
62 | state = 0,
|
---|
63 | resource;
|
---|
64 | function next() {
|
---|
65 | while (resource = stack.pop()) {
|
---|
66 | try {
|
---|
67 | if (!resource.a && state === 1) {
|
---|
68 | state = 0;
|
---|
69 | stack.push(resource);
|
---|
70 | return Promise.resolve().then(next);
|
---|
71 | }
|
---|
72 | if (resource.d) {
|
---|
73 | var disposalResult = resource.d.call(resource.v);
|
---|
74 | if (resource.a) {
|
---|
75 | state |= 2;
|
---|
76 | return Promise.resolve(disposalResult).then(next, err);
|
---|
77 | }
|
---|
78 | } else {
|
---|
79 | state |= 1;
|
---|
80 | }
|
---|
81 | } catch (e) {
|
---|
82 | return err(e);
|
---|
83 | }
|
---|
84 | }
|
---|
85 | if (state === 1) {
|
---|
86 | if (error !== empty) {
|
---|
87 | return Promise.reject(error);
|
---|
88 | } else {
|
---|
89 | return Promise.resolve();
|
---|
90 | }
|
---|
91 | }
|
---|
92 | if (error !== empty) throw error;
|
---|
93 | }
|
---|
94 | function err(e) {
|
---|
95 | error = error !== empty ? new _disposeSuppressedError(e, error) : e;
|
---|
96 | return next();
|
---|
97 | }
|
---|
98 | return next();
|
---|
99 | }
|
---|
100 | };
|
---|
101 | }
|
---|
102 |
|
---|
103 | //# sourceMappingURL=usingCtx.js.map
|
---|