vsn 0.1.129 → 0.1.131

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.
Files changed (56) hide show
  1. package/demo/examples/cascading-function-sheets.html +2 -2
  2. package/demo/examples/events.html +1 -1
  3. package/demo/examples/persist.html +17 -0
  4. package/demo/resources/xhr-persist.html +40 -0
  5. package/demo/vsn.js +2 -2
  6. package/dist/AST/ClassNode.js +2 -0
  7. package/dist/AST/ClassNode.js.map +1 -1
  8. package/dist/AST/FunctionNode.js +8 -1
  9. package/dist/AST/FunctionNode.js.map +1 -1
  10. package/dist/AST/IndexNode.js +7 -1
  11. package/dist/AST/IndexNode.js.map +1 -1
  12. package/dist/AST.d.ts +20 -0
  13. package/dist/AST.js +82 -23
  14. package/dist/AST.js.map +1 -1
  15. package/dist/Attribute.js.map +1 -1
  16. package/dist/DOM/AbstractDOM.d.ts +1 -0
  17. package/dist/DOM/AbstractDOM.js +114 -91
  18. package/dist/DOM/AbstractDOM.js.map +1 -1
  19. package/dist/Registry.d.ts +1 -2
  20. package/dist/Registry.js +15 -16
  21. package/dist/Registry.js.map +1 -1
  22. package/dist/Tag.d.ts +1 -0
  23. package/dist/Tag.js +7 -0
  24. package/dist/Tag.js.map +1 -1
  25. package/dist/attributes/PersistAttribute.d.ts +15 -0
  26. package/dist/attributes/PersistAttribute.js +215 -0
  27. package/dist/attributes/PersistAttribute.js.map +1 -0
  28. package/dist/attributes/_imports.d.ts +1 -0
  29. package/dist/attributes/_imports.js +3 -1
  30. package/dist/attributes/_imports.js.map +1 -1
  31. package/dist/version.d.ts +1 -1
  32. package/dist/version.js +1 -1
  33. package/dist/vsn.d.ts +0 -1
  34. package/dist/vsn.js +1 -3
  35. package/dist/vsn.js.map +1 -1
  36. package/package.json +1 -1
  37. package/src/AST/ClassNode.ts +2 -0
  38. package/src/AST/FunctionNode.ts +8 -1
  39. package/src/AST/IndexNode.ts +4 -0
  40. package/src/AST.ts +72 -24
  41. package/src/Attribute.ts +0 -1
  42. package/src/DOM/AbstractDOM.ts +17 -3
  43. package/src/Registry.ts +6 -9
  44. package/src/Tag.ts +4 -0
  45. package/src/attributes/PersistAttribute.ts +83 -0
  46. package/src/attributes/_imports.ts +1 -0
  47. package/src/version.ts +1 -1
  48. package/src/vsn.ts +0 -1
  49. package/test/AST/FunctionNode.spec.ts +9 -0
  50. package/test/AST.spec.ts +6 -8
  51. package/test/Controller.spec.ts +15 -16
  52. package/dist/SimplePromise.d.ts +0 -42
  53. package/dist/SimplePromise.js +0 -258
  54. package/dist/SimplePromise.js.map +0 -1
  55. package/src/SimplePromise.ts +0 -219
  56. package/test/SimplePromise.spec.ts +0 -271
@@ -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
- }
@@ -1,271 +0,0 @@
1
- import {IDeferred, SimplePromise} from "../src/SimplePromise";
2
-
3
-
4
- class Test {
5
- constructor(
6
- public value: string = 'test'
7
- ) {}
8
- }
9
- class Testing {
10
- constructor(
11
- public test: Test
12
- ) {}
13
- }
14
-
15
-
16
- describe('Promise', () => {
17
- let dummy: any = null,
18
- dummy2: any = null;
19
-
20
- beforeEach(() => {
21
- dummy = jasmine.createSpyObj('dummy', ['callback']);
22
- dummy2 = jasmine.createSpyObj('dummy2', ['callback']);
23
- });
24
-
25
- it("defer should work properly with resolve", () => {
26
- const d: IDeferred<Test> = SimplePromise.defer<Test>();
27
- const t: Test = new Test();
28
- expect(dummy.callback).not.toHaveBeenCalled();
29
- d.promise.then(dummy.callback.bind(dummy));
30
- d.resolve(t);
31
- expect(dummy.callback).toHaveBeenCalled();
32
- expect(d.promise['result']).toBe(t);
33
- });
34
-
35
- it("defer should work properly with reject", () => {
36
- const d: IDeferred<Test> = SimplePromise.defer<Test>();
37
- const t: Test = new Test();
38
- expect(dummy.callback).not.toHaveBeenCalled();
39
- expect(dummy2.callback).not.toHaveBeenCalled();
40
- d.promise.then(dummy.callback.bind(dummy), dummy2.callback.bind(dummy2));
41
- d.reject("failed");
42
- expect(dummy.callback).not.toHaveBeenCalled();
43
- expect(dummy2.callback).toHaveBeenCalled();
44
- expect(d.promise['result']).toBe("failed");
45
- });
46
-
47
- it("defer should work properly with finally", () => {
48
- const d: IDeferred<Test> = SimplePromise.defer<Test>();
49
- expect(dummy.callback).not.toHaveBeenCalled();
50
- expect(dummy2.callback).not.toHaveBeenCalled();
51
- d.promise.then((result: Test) => {
52
- expect(false).toBe(true); // should never be called
53
- return result;
54
- }, (reason: string) => {
55
- expect(dummy2.callback).not.toHaveBeenCalled();
56
- return reason;
57
- }).finally(dummy2.callback.bind(dummy2));
58
-
59
- d.reject("failed");
60
- expect(dummy2.callback).toHaveBeenCalled();
61
- expect(d.promise['result']).toBe("failed");
62
- });
63
-
64
- it("test race with reject", () => {
65
- const d: IDeferred<Test> = SimplePromise.defer<Test>();
66
- const d2: IDeferred<Test> = SimplePromise.defer<Test>();
67
- const t: Test = new Test();
68
- expect(dummy.callback).not.toHaveBeenCalled();
69
- expect(dummy2.callback).not.toHaveBeenCalled();
70
- SimplePromise.race([
71
- d.promise,
72
- d2.promise
73
- ]).then(dummy.callback.bind(dummy), dummy2.callback.bind(dummy2));
74
-
75
- d.reject("failed");
76
- d2.resolve(t);
77
- expect(dummy.callback).not.toHaveBeenCalled();
78
- expect(dummy2.callback).toHaveBeenCalled();
79
- expect(d.promise['result']).toBe("failed");
80
- });
81
-
82
- it("test race with resolve", () => {
83
- const d: IDeferred<Test> = SimplePromise.defer<Test>();
84
- const d2: IDeferred<Test> = SimplePromise.defer<Test>();
85
- const t: Test = new Test();
86
- expect(dummy.callback).not.toHaveBeenCalled();
87
- expect(dummy2.callback).not.toHaveBeenCalled();
88
- SimplePromise.race([
89
- d.promise,
90
- d2.promise
91
- ]).then(dummy.callback.bind(dummy), dummy2.callback.bind(dummy2));
92
-
93
- d.resolve(t);
94
- d.reject("failed");
95
- expect(dummy.callback).toHaveBeenCalled();
96
- expect(dummy2.callback).not.toHaveBeenCalled();
97
- expect(d.promise['result']).toBe(t);
98
- });
99
-
100
- it("test all with resolve", () => {
101
- const d: IDeferred<Test> = SimplePromise.defer<Test>();
102
- const d2: IDeferred<Test> = SimplePromise.defer<Test>();
103
- const t: Test = new Test();
104
- expect(dummy.callback).not.toHaveBeenCalled();
105
- expect(dummy2.callback).not.toHaveBeenCalled();
106
- SimplePromise.all([
107
- d.promise,
108
- d2.promise
109
- ]).then(dummy.callback.bind(dummy), dummy2.callback.bind(dummy2));
110
-
111
- d.resolve(t);
112
- expect(dummy.callback).not.toHaveBeenCalled();
113
- expect(dummy2.callback).not.toHaveBeenCalled();
114
- d2.resolve(t);
115
- expect(dummy.callback).toHaveBeenCalled();
116
- expect(dummy2.callback).not.toHaveBeenCalled();
117
- expect(d.promise['result']).toBe(t);
118
- });
119
-
120
- it("test all with reject", () => {
121
- const d: IDeferred<Test> = SimplePromise.defer<Test>();
122
- const d2: IDeferred<Test> = SimplePromise.defer<Test>();
123
- const t: Test = new Test();
124
- expect(dummy.callback).not.toHaveBeenCalled();
125
- expect(dummy2.callback).not.toHaveBeenCalled();
126
- SimplePromise.all([
127
- d.promise,
128
- d2.promise
129
- ]).then(dummy.callback.bind(dummy), dummy2.callback.bind(dummy2));
130
-
131
- d.resolve(t);
132
- expect(dummy.callback).not.toHaveBeenCalled();
133
- expect(dummy2.callback).not.toHaveBeenCalled();
134
- d2.reject("failed");
135
- expect(dummy.callback).not.toHaveBeenCalled();
136
- expect(dummy2.callback).toHaveBeenCalled();
137
- expect(d2.promise['result']).toBe("failed");
138
- });
139
-
140
- it("resolve static function should work properly", () => {
141
- const t: Test = new Test();
142
- expect(dummy.callback).not.toHaveBeenCalled();
143
- expect(dummy2.callback).not.toHaveBeenCalled();
144
- SimplePromise.resolve(t).then(dummy.callback.bind(dummy), dummy2.callback.bind(dummy2));
145
- expect(dummy.callback).toHaveBeenCalled();
146
- expect(dummy2.callback).not.toHaveBeenCalled();
147
- });
148
-
149
- it("reject static function should work properly", () => {
150
- const t: Test = new Test();
151
- expect(dummy.callback).not.toHaveBeenCalled();
152
- expect(dummy2.callback).not.toHaveBeenCalled();
153
- SimplePromise.reject("failed").then(dummy.callback.bind(dummy), dummy2.callback.bind(dummy2));
154
- expect(dummy.callback).not.toHaveBeenCalled();
155
- expect(dummy2.callback).toHaveBeenCalled();
156
- });
157
-
158
- it("should be rejected if error is thrown", () => {
159
- const t: Test = new Test();
160
- expect(dummy.callback).not.toHaveBeenCalled();
161
- expect(dummy2.callback).not.toHaveBeenCalled();
162
- new SimplePromise<Test>((resolve, reject) => {
163
- throw Error('failed');
164
- }).then(dummy.callback.bind(dummy), dummy2.callback.bind(dummy2));
165
- expect(dummy.callback).not.toHaveBeenCalled();
166
- expect(dummy2.callback).toHaveBeenCalled();
167
- });
168
-
169
- it("should process multiple then statements in the correct order", () => {
170
- const d: IDeferred<Test> = SimplePromise.defer<Test>();
171
- const t: Test = new Test();
172
- d.promise.then<Test>((result: Test): Test => {
173
- expect(result.value).toBe('test');
174
- result.value = 'then 1';
175
- return result;
176
- });
177
- d.promise.then<Test>((result: Test): Test => {
178
- expect(result.value).toBe('then 1');
179
- result.value = 'then 2';
180
- return result;
181
- });
182
- d.promise.then<Test>((result: Test): Test => {
183
- expect(result.value).toBe('then 2');
184
- result.value = 'then 3';
185
- return result;
186
- });
187
- d.promise.then<Test>((result: Test): Test => {
188
- expect(result.value).toBe('then 3');
189
- result.value = 'then 4';
190
- return result;
191
- });
192
-
193
- d.resolve(t);
194
- });
195
-
196
- it("should process chained then statements in the correct order", () => {
197
- const d: IDeferred<Test> = SimplePromise.defer<Test>();
198
- const t: Test = new Test();
199
- d.promise.then<Testing>((result: Test) => {
200
- expect(d.promise['result']).toBe(result);
201
- return new Testing(result);
202
- }).then((result: Testing | undefined): Testing => {
203
- expect((result as any as Testing).test).toBe(t);
204
- return result as any as Testing;
205
- });
206
- d.resolve(t);
207
- });
208
-
209
- it("should process callback on previously resolved promise", (done) => {
210
- const d: IDeferred<Test> = SimplePromise.defer<Test>();
211
- const t: Test = new Test();
212
- d.resolve(t);
213
-
214
- d.promise.then<Testing>((result: Test) => {
215
- expect(d.promise['result']).toBe(result);
216
- done();
217
- return new Testing(result);
218
- });
219
- });
220
-
221
- it("should process callback on previously rejected promise", (done) => {
222
- const d: IDeferred<Test> = SimplePromise.defer<Test>();
223
- d.reject('Failure.');
224
-
225
- d.promise.then<Testing>((result: Test) => {
226
- expect(false).toBe(true);
227
- return new Testing(result);
228
- }, (reason?: string): string => {
229
- expect(reason).toBe('Failure.');
230
- done();
231
-
232
- return reason as string;
233
- });
234
- });
235
-
236
- it("should process Promise.all callbacks with previously resolved promises", (done) => {
237
- const d: IDeferred<Test> = SimplePromise.defer<Test>();
238
- const d2: IDeferred<Test> = SimplePromise.defer<Test>();
239
- const t: Test = new Test();
240
- const t2: Test = new Test();
241
- d2.resolve(t2);
242
-
243
- SimplePromise.all<Test>([d.promise, d2.promise]).then((results?: Test[]) => {
244
- if (results === undefined) return;
245
- expect(d.promise['result']).toBe(results[0]);
246
- expect(d2.promise['result']).toBe(results[1]);
247
- done();
248
- });
249
-
250
- d.resolve(t);
251
- });
252
-
253
- it("should process Promise.all callbacks with previously rejected promises", (done) => {
254
- const d: IDeferred<Test> = SimplePromise.defer<Test>();
255
- const d2: IDeferred<Test> = SimplePromise.defer<Test>();
256
- const t: Test = new Test();
257
- const t2: Test = new Test();
258
- d.reject('Failure.');
259
-
260
- SimplePromise.all<Test>([d.promise, d2.promise]).then((results?: Test[]) => {
261
- expect(false).toBe(true);
262
- }, (reason?: string): string => {
263
- expect(reason).toBe('Failure.');
264
- done();
265
-
266
- return reason as string;
267
- });
268
-
269
- d2.resolve(t2);
270
- });
271
- });