UNPKG

6.61 kBJavaScriptView Raw
1Object.defineProperty(exports, "__esModule", { value: true });
2/* eslint-disable @typescript-eslint/explicit-function-return-type */
3/* eslint-disable @typescript-eslint/typedef */
4/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
5/* eslint-disable @typescript-eslint/no-explicit-any */
6var is_1 = require("./is");
7/** SyncPromise internal states */
8var States;
9(function (States) {
10 /** Pending */
11 States["PENDING"] = "PENDING";
12 /** Resolved / OK */
13 States["RESOLVED"] = "RESOLVED";
14 /** Rejected / Error */
15 States["REJECTED"] = "REJECTED";
16})(States || (States = {}));
17/**
18 * Thenable class that behaves like a Promise and follows it's interface
19 * but is not async internally
20 */
21var SyncPromise = /** @class */ (function () {
22 function SyncPromise(executor) {
23 var _this = this;
24 this._state = States.PENDING;
25 this._handlers = [];
26 /** JSDoc */
27 this._resolve = function (value) {
28 _this._setResult(States.RESOLVED, value);
29 };
30 /** JSDoc */
31 this._reject = function (reason) {
32 _this._setResult(States.REJECTED, reason);
33 };
34 /** JSDoc */
35 this._setResult = function (state, value) {
36 if (_this._state !== States.PENDING) {
37 return;
38 }
39 if (is_1.isThenable(value)) {
40 void value.then(_this._resolve, _this._reject);
41 return;
42 }
43 _this._state = state;
44 _this._value = value;
45 _this._executeHandlers();
46 };
47 // TODO: FIXME
48 /** JSDoc */
49 this._attachHandler = function (handler) {
50 _this._handlers = _this._handlers.concat(handler);
51 _this._executeHandlers();
52 };
53 /** JSDoc */
54 this._executeHandlers = function () {
55 if (_this._state === States.PENDING) {
56 return;
57 }
58 var cachedHandlers = _this._handlers.slice();
59 _this._handlers = [];
60 cachedHandlers.forEach(function (handler) {
61 if (handler.done) {
62 return;
63 }
64 if (_this._state === States.RESOLVED) {
65 if (handler.onfulfilled) {
66 // eslint-disable-next-line @typescript-eslint/no-floating-promises
67 handler.onfulfilled(_this._value);
68 }
69 }
70 if (_this._state === States.REJECTED) {
71 if (handler.onrejected) {
72 handler.onrejected(_this._value);
73 }
74 }
75 handler.done = true;
76 });
77 };
78 try {
79 executor(this._resolve, this._reject);
80 }
81 catch (e) {
82 this._reject(e);
83 }
84 }
85 /** JSDoc */
86 SyncPromise.resolve = function (value) {
87 return new SyncPromise(function (resolve) {
88 resolve(value);
89 });
90 };
91 /** JSDoc */
92 SyncPromise.reject = function (reason) {
93 return new SyncPromise(function (_, reject) {
94 reject(reason);
95 });
96 };
97 /** JSDoc */
98 SyncPromise.all = function (collection) {
99 return new SyncPromise(function (resolve, reject) {
100 if (!Array.isArray(collection)) {
101 reject(new TypeError("Promise.all requires an array as input."));
102 return;
103 }
104 if (collection.length === 0) {
105 resolve([]);
106 return;
107 }
108 var counter = collection.length;
109 var resolvedCollection = [];
110 collection.forEach(function (item, index) {
111 void SyncPromise.resolve(item)
112 .then(function (value) {
113 resolvedCollection[index] = value;
114 counter -= 1;
115 if (counter !== 0) {
116 return;
117 }
118 resolve(resolvedCollection);
119 })
120 .then(null, reject);
121 });
122 });
123 };
124 /** JSDoc */
125 SyncPromise.prototype.then = function (onfulfilled, onrejected) {
126 var _this = this;
127 return new SyncPromise(function (resolve, reject) {
128 _this._attachHandler({
129 done: false,
130 onfulfilled: function (result) {
131 if (!onfulfilled) {
132 // TODO: ¯\_(ツ)_/¯
133 // TODO: FIXME
134 resolve(result);
135 return;
136 }
137 try {
138 resolve(onfulfilled(result));
139 return;
140 }
141 catch (e) {
142 reject(e);
143 return;
144 }
145 },
146 onrejected: function (reason) {
147 if (!onrejected) {
148 reject(reason);
149 return;
150 }
151 try {
152 resolve(onrejected(reason));
153 return;
154 }
155 catch (e) {
156 reject(e);
157 return;
158 }
159 },
160 });
161 });
162 };
163 /** JSDoc */
164 SyncPromise.prototype.catch = function (onrejected) {
165 return this.then(function (val) { return val; }, onrejected);
166 };
167 /** JSDoc */
168 SyncPromise.prototype.finally = function (onfinally) {
169 var _this = this;
170 return new SyncPromise(function (resolve, reject) {
171 var val;
172 var isRejected;
173 return _this.then(function (value) {
174 isRejected = false;
175 val = value;
176 if (onfinally) {
177 onfinally();
178 }
179 }, function (reason) {
180 isRejected = true;
181 val = reason;
182 if (onfinally) {
183 onfinally();
184 }
185 }).then(function () {
186 if (isRejected) {
187 reject(val);
188 return;
189 }
190 resolve(val);
191 });
192 });
193 };
194 /** JSDoc */
195 SyncPromise.prototype.toString = function () {
196 return '[object SyncPromise]';
197 };
198 return SyncPromise;
199}());
200exports.SyncPromise = SyncPromise;
201//# sourceMappingURL=syncpromise.js.map
\No newline at end of file