vsn 0.1.129 → 0.1.130

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/Registry.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import {EventDispatcher} from "./EventDispatcher";
2
- import {IDeferred, IPromise, SimplePromise} from "./SimplePromise";
3
2
  import {ClassNode} from "./AST/ClassNode";
4
3
 
5
4
  export function register(store: string, key: string = null, setup: () => void = null) {
@@ -28,11 +27,10 @@ export class RegistryStore<T = any> extends EventDispatcher {
28
27
  this.dispatch(`registered:${key}`, item);
29
28
  }
30
29
 
31
- get(key: string): IPromise<any> {
32
- const deferred: IDeferred<any> = SimplePromise.defer();
33
-
34
- if (!!this.store[key]) {
35
- deferred.resolve(this.store[key]);
30
+ get(key: string): Promise<any> {
31
+ return new Promise((resolve, reject) => {
32
+ if (!!this.store[key]) {
33
+ resolve(this.store[key]);
36
34
  } else {
37
35
  console.warn(`Waiting for ${key} to be registered.`);
38
36
  this.timeouts[key] = setTimeout(() => {
@@ -40,11 +38,10 @@ export class RegistryStore<T = any> extends EventDispatcher {
40
38
  }, 5000);
41
39
  this.once(`registered:${key}`, (cls) => {
42
40
  clearTimeout(this.timeouts[key]);
43
- deferred.resolve(cls);
41
+ resolve(cls);
44
42
  })
45
43
  }
46
-
47
- return deferred.promise;
44
+ });
48
45
  }
49
46
 
50
47
  getSynchronous(key: string) {
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
- export const VERSION = '0.1.129';
1
+ export const VERSION = '0.1.130';
2
2
 
package/src/vsn.ts CHANGED
@@ -101,6 +101,5 @@ export {Model} from './Model';
101
101
  export {Service} from './Service';
102
102
  export {EventDispatcher} from './EventDispatcher';
103
103
  export {MessageList} from './MessageList';
104
- export {SimplePromise} from './SimplePromise';
105
104
  export {Tag} from './Tag';
106
105
  export const vision: Vision = Vision.instance;
@@ -11,4 +11,13 @@ describe('FunctionNode', () => {
11
11
  const v = await tree.evaluate(scope, null, null);
12
12
  expect(v).toBe(3);
13
13
  });
14
+
15
+ it("should take a multitude of arguments", async () => {
16
+ const tree = new Tree(`func add(a_int, a_dict, a_arr) { log(a_int, a_dict['a'], a_arr[0]); return a_int + a_dict['a'] + a_arr[0]; }; add(1, {'a':1, 'b': 2}, [1]);`);
17
+ const scope = new Scope();
18
+ await tree.prepare(scope, null, null);
19
+ expect(scope.get('add')).toBeDefined();
20
+ const v = await tree.evaluate(scope, null, null);
21
+ expect(v).toBe(3);
22
+ });
14
23
  });
package/test/AST.spec.ts CHANGED
@@ -2,7 +2,6 @@ import {WrappedArray} from '../src/Scope/WrappedArray';
2
2
  import {Scope} from "../src/Scope";
3
3
  import {Tree} from "../src/AST";
4
4
  import {DOM} from "../src/DOM";
5
- import {IDeferred, SimplePromise} from "../src/SimplePromise";
6
5
 
7
6
  describe('Tree', () => {
8
7
  let scope: Scope = null,
@@ -242,14 +241,13 @@ describe('Tree', () => {
242
241
 
243
242
  it("should be able to block properly with promises", async () => {
244
243
  scope.set('blockingFunction', async (num, toAdd, fin: boolean = false) => {
245
- const deferred: IDeferred<number> = SimplePromise.defer();
246
- expect(scope.get('test')).toBe(num);
244
+ return new Promise((resolve, reject) => {
245
+ expect(scope.get('test')).toBe(num);
247
246
 
248
- setTimeout(() => {
249
- deferred.resolve(num + toAdd);
250
- }, 1);
251
-
252
- return deferred.promise;
247
+ setTimeout(() => {
248
+ resolve(num + toAdd);
249
+ }, 1);
250
+ });
253
251
  });
254
252
 
255
253
  let tree: Tree = new Tree(`
@@ -1,6 +1,5 @@
1
1
  import {Controller} from '../src/Controller';
2
2
  import {DOM} from "../src/DOM";
3
- import {SimplePromise} from "../src/SimplePromise";
4
3
  import {Registry} from "../src/Registry";
5
4
  import {property} from "../src/Scope/properties/Property";
6
5
  import {Property, Scope, Tag} from "../src/vsn";
@@ -31,21 +30,21 @@ describe('Controller', () => {
31
30
  <div id="controller" vsn-controller:test="ControllerTestController" vsn-set:test.test="notTest" vsn-bind="test.test"></div>
32
31
  `;
33
32
  const dom = new DOM(document.body);
34
- const deferred = SimplePromise.defer();
35
- dom.once('built', async () => {
36
- const tag = await dom.exec('#controller');
37
- expect(tag).toBeInstanceOf(Tag);
38
- expect(tag.scope).toBeInstanceOf(Scope);
39
- /*
40
- expect(tag.scope.keys).toEqual(['test']);
41
- expect(tag.scope.get('test').wrapped).toBeInstanceOf(TestController);
42
- expect(await tag.exec('test.isValid()')).toBe(false);
43
- expect(await tag.exec('test.test')).toBe('notTest');
44
- await tag.exec('test.test = "test"');
45
- expect(await tag.exec('test.isValid()')).toBe(true);
46
- */
47
- deferred.resolve();
33
+ await new Promise((resolve, reject) => {
34
+ dom.once('built', async () => {
35
+ const tag = await dom.exec('#controller');
36
+ expect(tag).toBeInstanceOf(Tag);
37
+ expect(tag.scope).toBeInstanceOf(Scope);
38
+ /*
39
+ expect(tag.scope.keys).toEqual(['test']);
40
+ expect(tag.scope.get('test').wrapped).toBeInstanceOf(TestController);
41
+ expect(await tag.exec('test.isValid()')).toBe(false);
42
+ expect(await tag.exec('test.test')).toBe('notTest');
43
+ await tag.exec('test.test = "test"');
44
+ expect(await tag.exec('test.isValid()')).toBe(true);
45
+ */
46
+ resolve(null);
47
+ });
48
48
  });
49
- await deferred.promise;
50
49
  });
51
50
  });
@@ -1,42 +0,0 @@
1
- import { EventDispatcher } from "./EventDispatcher";
2
- export interface IDeferred<T> {
3
- [key: string]: any;
4
- promise: SimplePromise<T>;
5
- resolve(result?: T): void;
6
- reject(reason: string): void;
7
- }
8
- export declare type TResolve<T> = (result: T) => void;
9
- export declare type TReject = (reason: string) => void;
10
- export declare type TResult<T> = T | string | null;
11
- export declare enum EPromiseStates {
12
- PENDING = 0,
13
- FULFILLED = 1,
14
- REJECTED = 2
15
- }
16
- export interface IPromise<T> extends EventDispatcher {
17
- state: EPromiseStates;
18
- result: TResult<T>;
19
- then<X = T>(success?: (result?: T) => X, error?: (reason?: string) => string): IPromise<X>;
20
- catch(onRejected: (reason: string) => string): IPromise<string>;
21
- finally<X = T>(finallyCallback: (result: T | string) => X | string): IPromise<X>;
22
- }
23
- export declare function noop<T = any>(result?: T): T;
24
- export declare class SimplePromise<T> extends EventDispatcher implements IPromise<T> {
25
- protected _state: EPromiseStates;
26
- protected _result: TResult<T>;
27
- private promiseClass;
28
- constructor(executor: (resolve: TResolve<T>, reject: TReject) => void);
29
- get state(): EPromiseStates;
30
- get result(): TResult<T>;
31
- static defer<T>(): IDeferred<T>;
32
- static resolve<T>(result?: T): IPromise<T>;
33
- static reject(reason: string): IPromise<void>;
34
- static all<T>(iter: IPromise<T>[]): IPromise<T[]>;
35
- static poolResults<T>(iter: IPromise<T>[], deferred: IDeferred<T[]>): void;
36
- static race<T>(iter: IPromise<T>[]): IPromise<T>;
37
- then<X = T>(success?: (result: T) => X, error?: (reason: string) => string): IPromise<X>;
38
- catch(onRejected: (reason: string) => string): IPromise<string>;
39
- finally<X = T>(finallyCallback: (result: T | string) => X | string): IPromise<X>;
40
- protected _resolve(result: T): void;
41
- protected _reject(reason: string): void;
42
- }
@@ -1,258 +0,0 @@
1
- "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
- var __values = (this && this.__values) || function(o) {
18
- var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
19
- if (m) return m.call(o);
20
- if (o && typeof o.length === "number") return {
21
- next: function () {
22
- if (o && i >= o.length) o = void 0;
23
- return { value: o && o[i++], done: !o };
24
- }
25
- };
26
- throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
27
- };
28
- Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.SimplePromise = exports.noop = exports.EPromiseStates = void 0;
30
- var EventDispatcher_1 = require("./EventDispatcher");
31
- var EPromiseStates;
32
- (function (EPromiseStates) {
33
- EPromiseStates[EPromiseStates["PENDING"] = 0] = "PENDING";
34
- EPromiseStates[EPromiseStates["FULFILLED"] = 1] = "FULFILLED";
35
- EPromiseStates[EPromiseStates["REJECTED"] = 2] = "REJECTED";
36
- })(EPromiseStates = exports.EPromiseStates || (exports.EPromiseStates = {}));
37
- function noop(result) { return result; }
38
- exports.noop = noop;
39
- var SimplePromise = /** @class */ (function (_super) {
40
- __extends(SimplePromise, _super);
41
- function SimplePromise(executor) {
42
- var _this = _super.call(this) || this;
43
- _this._result = null;
44
- _this._state = EPromiseStates.PENDING;
45
- _this.promiseClass = (Object.getPrototypeOf(_this).constructor);
46
- try {
47
- executor(_this._resolve.bind(_this), _this._reject.bind(_this));
48
- }
49
- catch (e) {
50
- _this._reject(e);
51
- }
52
- return _this;
53
- }
54
- Object.defineProperty(SimplePromise.prototype, "state", {
55
- get: function () {
56
- return this._state;
57
- },
58
- enumerable: false,
59
- configurable: true
60
- });
61
- Object.defineProperty(SimplePromise.prototype, "result", {
62
- get: function () {
63
- return this._result;
64
- },
65
- enumerable: false,
66
- configurable: true
67
- });
68
- SimplePromise.defer = function () {
69
- var promise = new SimplePromise(function (res, rej) { });
70
- return {
71
- promise: promise,
72
- resolve: promise._resolve.bind(promise),
73
- reject: promise._reject.bind(promise)
74
- };
75
- };
76
- /*
77
- * Returns a Promise object that is resolved with the given value. If the value is a thenable (i.e. has a then
78
- * method), the returned promise will "follow" that thenable, adopting its eventual state; otherwise the returned
79
- * promise will be fulfilled with the value. Generally, if you don't know if a value is a promise or not,
80
- * Promise.resolve(value) it instead and work with the return value as a promise.
81
- */
82
- SimplePromise.resolve = function (result) {
83
- return new SimplePromise(function (resolve) {
84
- if (result instanceof SimplePromise) {
85
- result.then(function (innerResult) {
86
- resolve(innerResult);
87
- });
88
- }
89
- else {
90
- resolve(result);
91
- }
92
- });
93
- };
94
- /*
95
- * Returns a Promise object that is rejected with the given reason.
96
- */
97
- SimplePromise.reject = function (reason) {
98
- return new SimplePromise(function (resolve, reject) {
99
- reject(reason);
100
- });
101
- };
102
- /*
103
- * Returns a promise that either fulfills when all of the promises in the iterable argument have fulfilled or
104
- * rejects as soon as one of the promises in the iterable argument rejects. If the returned promise fulfills, it is
105
- * fulfilled with an array of the values from the fulfilled promises in the same order as defined in the iterable.
106
- * If the returned promise rejects, it is rejected with the reason from the first promise in the iterable that
107
- * rejected. This method can be useful for aggregating results of multiple promises.
108
- */
109
- SimplePromise.all = function (iter) {
110
- var e_1, _a;
111
- var deferred = SimplePromise.defer();
112
- var done = true;
113
- try {
114
- for (var iter_1 = __values(iter), iter_1_1 = iter_1.next(); !iter_1_1.done; iter_1_1 = iter_1.next()) {
115
- var promise = iter_1_1.value;
116
- if (promise.state == EPromiseStates.PENDING) {
117
- done = false;
118
- promise.once('fulfilled', function (result) {
119
- SimplePromise.poolResults(iter, deferred);
120
- });
121
- promise.once('rejected', function (reason) {
122
- deferred.reject(reason);
123
- });
124
- }
125
- else if (promise.state == EPromiseStates.REJECTED) {
126
- deferred.reject(promise.result);
127
- done = false;
128
- break;
129
- }
130
- }
131
- }
132
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
133
- finally {
134
- try {
135
- if (iter_1_1 && !iter_1_1.done && (_a = iter_1.return)) _a.call(iter_1);
136
- }
137
- finally { if (e_1) throw e_1.error; }
138
- }
139
- if (done)
140
- SimplePromise.poolResults(iter, deferred);
141
- return deferred.promise;
142
- };
143
- SimplePromise.poolResults = function (iter, deferred) {
144
- var e_2, _a;
145
- var done = true;
146
- var results = [];
147
- try {
148
- for (var iter_2 = __values(iter), iter_2_1 = iter_2.next(); !iter_2_1.done; iter_2_1 = iter_2.next()) {
149
- var p = iter_2_1.value;
150
- if (p.state === EPromiseStates.REJECTED) {
151
- deferred.reject(p.result);
152
- break;
153
- }
154
- else if (p.state === EPromiseStates.PENDING) {
155
- done = false;
156
- }
157
- results.push(p.result);
158
- }
159
- }
160
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
161
- finally {
162
- try {
163
- if (iter_2_1 && !iter_2_1.done && (_a = iter_2.return)) _a.call(iter_2);
164
- }
165
- finally { if (e_2) throw e_2.error; }
166
- }
167
- if (done)
168
- deferred.resolve(results);
169
- };
170
- /*
171
- * Returns a promise that fulfills or rejects as soon as one of the promises in the iterable fulfills or rejects,
172
- * with the value or reason from that promise.
173
- */
174
- SimplePromise.race = function (iter) {
175
- var e_3, _a;
176
- var deferred = SimplePromise.defer();
177
- try {
178
- for (var iter_3 = __values(iter), iter_3_1 = iter_3.next(); !iter_3_1.done; iter_3_1 = iter_3.next()) {
179
- var promise = iter_3_1.value;
180
- promise.once('fulfilled', function (result) {
181
- deferred.resolve(result);
182
- });
183
- promise.once('rejected', function (reason) {
184
- deferred.reject(reason);
185
- });
186
- }
187
- }
188
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
189
- finally {
190
- try {
191
- if (iter_3_1 && !iter_3_1.done && (_a = iter_3.return)) _a.call(iter_3);
192
- }
193
- finally { if (e_3) throw e_3.error; }
194
- }
195
- return deferred.promise;
196
- };
197
- /*
198
- * Appends fulfillment and rejection handlers to the promise, and returns a new promise resolving to the return
199
- * value of the called handler, or to its original settled value if the promise was not handled (i.e. if the
200
- * relevant handler onFulfilled or onRejected is not a function).
201
- */
202
- SimplePromise.prototype.then = function (success, error) {
203
- var _this = this;
204
- return new this.promiseClass(function (resolve, reject) {
205
- if (_this.state === EPromiseStates.FULFILLED) {
206
- if (success)
207
- resolve(success(_this.result));
208
- }
209
- else if (_this.state === EPromiseStates.REJECTED) {
210
- if (error)
211
- reject(error(_this.result));
212
- }
213
- else {
214
- _this.once('fulfilled', function (result) {
215
- if (success)
216
- resolve(success(result));
217
- });
218
- _this.once('rejected', function (reason) {
219
- if (error)
220
- reject(error(reason));
221
- });
222
- }
223
- });
224
- };
225
- /*
226
- * Appends a rejection handler callback to the promise, and returns a new promise resolving to the return value of
227
- * the callback if it is called, or to its original fulfillment value if the promise is instead fulfilled.
228
- */
229
- SimplePromise.prototype.catch = function (onRejected) {
230
- return this.then(undefined, onRejected);
231
- };
232
- /*
233
- * Appends a handler to the promise, and returns a new promise which is resolved when the original promise is
234
- * resolved. The handler is called when the promise is settled, whether fulfilled or rejected.
235
- */
236
- SimplePromise.prototype.finally = function (finallyCallback) {
237
- var success = function (result) { return finallyCallback(result); };
238
- var error = function (reason) { return finallyCallback(reason); };
239
- return this.then(success, error);
240
- };
241
- SimplePromise.prototype._resolve = function (result) {
242
- if (this.state !== EPromiseStates.PENDING)
243
- return;
244
- this._state = EPromiseStates.FULFILLED;
245
- this._result = result;
246
- this.dispatch('fulfilled', result);
247
- };
248
- SimplePromise.prototype._reject = function (reason) {
249
- if (this.state !== EPromiseStates.PENDING)
250
- return;
251
- this._state = EPromiseStates.REJECTED;
252
- this._result = reason;
253
- this.dispatch('rejected', reason);
254
- };
255
- return SimplePromise;
256
- }(EventDispatcher_1.EventDispatcher));
257
- exports.SimplePromise = SimplePromise;
258
- //# sourceMappingURL=SimplePromise.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"SimplePromise.js","sourceRoot":"","sources":["../src/SimplePromise.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,qDAAkD;AAalD,IAAY,cAIX;AAJD,WAAY,cAAc;IACtB,yDAAO,CAAA;IACP,6DAAS,CAAA;IACT,2DAAQ,CAAA;AACZ,CAAC,EAJW,cAAc,GAAd,sBAAc,KAAd,sBAAc,QAIzB;AAUD,SAAgB,IAAI,CAAU,MAAU,IAAO,OAAO,MAAW,CAAC,CAAC,CAAC;AAApE,oBAAoE;AAEpE;IAAsC,iCAAe;IAKj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;QACtB,IAAI,CAAC,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC,CAAC;IACvC,CAAC;IAES,+BAAO,GAAjB,UAAkB,MAAc;QAC5B,IAAI,IAAI,CAAC,KAAK,KAAK,cAAc,CAAC,OAAO;YAAE,OAAO;QAClD,IAAI,CAAC,MAAM,GAAG,cAAc,CAAC,QAAQ,CAAC;QACtC,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;QACtB,IAAI,CAAC,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;IACtC,CAAC;IACL,oBAAC;AAAD,CAAC,AA7LD,CAAsC,iCAAe,GA6LpD;AA7LY,sCAAa"}
@@ -1,219 +0,0 @@
1
- import {EventDispatcher} from "./EventDispatcher";
2
-
3
- export interface IDeferred<T> {
4
- [key: string]: any;
5
- promise: SimplePromise<T>;
6
- resolve(result?: T): void;
7
- reject(reason: string): void;
8
- }
9
-
10
- export type TResolve<T> = (result: T) => void;
11
- export type TReject = (reason: string) => void;
12
- export type TResult<T> = T | string | null;
13
-
14
- export enum EPromiseStates {
15
- PENDING,
16
- FULFILLED,
17
- REJECTED
18
- }
19
-
20
- export interface IPromise<T> extends EventDispatcher {
21
- state: EPromiseStates;
22
- result: TResult<T>;
23
- then<X = T>(success?: (result?: T) => X, error?: (reason?: string) => string): IPromise<X>;
24
- catch(onRejected: (reason: string) => string): IPromise<string>;
25
- finally<X = T>(finallyCallback: (result: T | string) => X | string): IPromise<X>;
26
- }
27
-
28
- export function noop<T = any>(result?: T): T { return result as T; }
29
-
30
- export class SimplePromise<T> extends EventDispatcher implements IPromise<T> {
31
- protected _state: EPromiseStates;
32
- protected _result: TResult<T> = null;
33
- private promiseClass: typeof SimplePromise;
34
-
35
- constructor(executor: (resolve: TResolve<T>, reject: TReject) => void) {
36
- super();
37
- this._state = EPromiseStates.PENDING;
38
- this.promiseClass = (Object.getPrototypeOf(this).constructor);
39
- try {
40
- executor(this._resolve.bind(this), this._reject.bind(this));
41
- } catch (e) {
42
- this._reject(e);
43
- }
44
- }
45
-
46
- public get state(): EPromiseStates {
47
- return this._state;
48
- }
49
-
50
- public get result(): TResult<T> {
51
- return this._result;
52
- }
53
-
54
- public static defer<T>(): IDeferred<T> {
55
- const promise: SimplePromise<T> = new SimplePromise<T>((res, rej) => {});
56
-
57
- return {
58
- promise: promise,
59
- resolve: promise._resolve.bind(promise),
60
- reject: promise._reject.bind(promise)
61
- };
62
- }
63
-
64
- /*
65
- * Returns a Promise object that is resolved with the given value. If the value is a thenable (i.e. has a then
66
- * method), the returned promise will "follow" that thenable, adopting its eventual state; otherwise the returned
67
- * promise will be fulfilled with the value. Generally, if you don't know if a value is a promise or not,
68
- * Promise.resolve(value) it instead and work with the return value as a promise.
69
- */
70
- public static resolve<T>(result?: T): IPromise<T> {
71
- return new SimplePromise<T>((resolve: TResolve<T>): void => {
72
- if (result instanceof SimplePromise) {
73
- result.then((innerResult: T) => {
74
- resolve(innerResult);
75
- });
76
- } else {
77
- resolve(result);
78
- }
79
- });
80
- }
81
-
82
- /*
83
- * Returns a Promise object that is rejected with the given reason.
84
- */
85
- public static reject(reason: string): IPromise<void> {
86
- return new SimplePromise<void>((resolve: TResolve<void>, reject: TReject): void => {
87
- reject(reason);
88
- });
89
- }
90
-
91
- /*
92
- * Returns a promise that either fulfills when all of the promises in the iterable argument have fulfilled or
93
- * rejects as soon as one of the promises in the iterable argument rejects. If the returned promise fulfills, it is
94
- * fulfilled with an array of the values from the fulfilled promises in the same order as defined in the iterable.
95
- * If the returned promise rejects, it is rejected with the reason from the first promise in the iterable that
96
- * rejected. This method can be useful for aggregating results of multiple promises.
97
- */
98
- public static all<T>(iter: IPromise<T>[]): IPromise<T[]> {
99
- const deferred: IDeferred<T[]> = SimplePromise.defer<T[]>();
100
- let done: boolean = true;
101
- for (const promise of iter) {
102
- if (promise.state == EPromiseStates.PENDING) {
103
- done = false;
104
- promise.once('fulfilled', (result: T): void => {
105
- SimplePromise.poolResults(iter, deferred);
106
- });
107
-
108
- promise.once('rejected', (reason: string): void => {
109
- deferred.reject(reason);
110
- });
111
- } else if(promise.state == EPromiseStates.REJECTED) {
112
- deferred.reject(promise.result as string);
113
- done = false;
114
- break;
115
- }
116
- }
117
-
118
- if (done)
119
- SimplePromise.poolResults(iter, deferred);
120
-
121
- return deferred.promise;
122
- }
123
-
124
- public static poolResults<T>(iter: IPromise<T>[], deferred: IDeferred<T[]>) {
125
- let done: boolean = true;
126
- const results: T[] = [];
127
- for (const p of iter) {
128
- if (p.state === EPromiseStates.REJECTED) {
129
- deferred.reject(p.result as string);
130
- break;
131
- } else if (p.state === EPromiseStates.PENDING) {
132
- done = false;
133
- }
134
- results.push(p.result as T);
135
- }
136
- if (done)
137
- deferred.resolve(results);
138
- }
139
-
140
- /*
141
- * Returns a promise that fulfills or rejects as soon as one of the promises in the iterable fulfills or rejects,
142
- * with the value or reason from that promise.
143
- */
144
- public static race<T>(iter: IPromise<T>[]): IPromise<T> {
145
- const deferred: IDeferred<T> = SimplePromise.defer<T>();
146
-
147
- for (const promise of iter) {
148
- promise.once('fulfilled', (result: T) => {
149
- deferred.resolve(result);
150
- });
151
-
152
- promise.once('rejected', (reason: string) => {
153
- deferred.reject(reason);
154
- });
155
- }
156
-
157
- return deferred.promise;
158
- }
159
-
160
- /*
161
- * Appends fulfillment and rejection handlers to the promise, and returns a new promise resolving to the return
162
- * value of the called handler, or to its original settled value if the promise was not handled (i.e. if the
163
- * relevant handler onFulfilled or onRejected is not a function).
164
- */
165
- public then<X = T>(success?: (result: T) => X, error?: (reason: string) => string): IPromise<X> {
166
- return new this.promiseClass<X>((resolve: TResolve<X>, reject: TReject): void => {
167
- if (this.state === EPromiseStates.FULFILLED) {
168
- if (success)
169
- resolve(success(this.result as T));
170
- } else if (this.state === EPromiseStates.REJECTED) {
171
- if (error)
172
- reject(error(this.result as string));
173
- } else {
174
- this.once('fulfilled', (result: T): void => {
175
- if (success)
176
- resolve(success(result));
177
- });
178
-
179
- this.once('rejected', (reason: string): void => {
180
- if (error)
181
- reject(error(reason));
182
- });
183
- }
184
- });
185
- }
186
-
187
- /*
188
- * Appends a rejection handler callback to the promise, and returns a new promise resolving to the return value of
189
- * the callback if it is called, or to its original fulfillment value if the promise is instead fulfilled.
190
- */
191
- public catch(onRejected: (reason: string) => string): IPromise<string> {
192
- return this.then<string>(undefined, onRejected);
193
- }
194
-
195
- /*
196
- * Appends a handler to the promise, and returns a new promise which is resolved when the original promise is
197
- * resolved. The handler is called when the promise is settled, whether fulfilled or rejected.
198
- */
199
- public finally<X = T>(finallyCallback: (result: T | string) => X | string): IPromise<X> {
200
- const success: (r: T) => X = (result: T): X => finallyCallback(result) as X;
201
- const error: (r: string) => string = (reason: string): string => finallyCallback(reason) as string;
202
-
203
- return this.then<X>(success, error);
204
- }
205
-
206
- protected _resolve(result: T): void {
207
- if (this.state !== EPromiseStates.PENDING) return;
208
- this._state = EPromiseStates.FULFILLED;
209
- this._result = result;
210
- this.dispatch('fulfilled', result);
211
- }
212
-
213
- protected _reject(reason: string): void {
214
- if (this.state !== EPromiseStates.PENDING) return;
215
- this._state = EPromiseStates.REJECTED;
216
- this._result = reason;
217
- this.dispatch('rejected', reason);
218
- }
219
- }