j-templates 5.0.51 → 5.0.52
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/DOM/createEventAssignment.d.ts +3 -0
- package/DOM/createEventAssignment.js +29 -0
- package/DOM/createPropertyAssignment.d.ts +4 -0
- package/DOM/createPropertyAssignment.js +74 -0
- package/DOM/domNodeConfig.d.ts +1 -1
- package/DOM/domNodeConfig.js +27 -12
- package/DOM/elements.d.ts +1 -0
- package/DOM/elements.js +27 -24
- package/DOM/index.d.ts +2 -1
- package/DOM/index.js +2 -1
- package/DOM/svgElements.js +3 -4
- package/Node/boundNode.js +16 -34
- package/Node/boundNode.types.d.ts +10 -4
- package/Node/component.js +1 -0
- package/Node/componentNode.js +0 -1
- package/Node/elementNode.js +12 -13
- package/Node/nodeConfig.d.ts +10 -1
- package/Node/nodeRef.d.ts +1 -1
- package/Node/nodeRef.js +58 -26
- package/Node/nodeRef.types.d.ts +2 -4
- package/Store/Diff/diffTree.js +1 -2
- package/Store/Tree/observableComputed.d.ts +4 -0
- package/Store/Tree/observableComputed.js +45 -0
- package/Store/Tree/observableNode.d.ts +3 -0
- package/Store/Tree/observableNode.js +195 -0
- package/Store/Tree/observableScope.d.ts +7 -12
- package/Store/Tree/observableScope.js +38 -34
- package/Store/index.d.ts +2 -0
- package/Store/index.js +5 -1
- package/Utils/decorators.d.ts +3 -1
- package/Utils/decorators.js +36 -39
- package/Utils/emitter.d.ts +2 -6
- package/Utils/emitter.js +8 -1
- package/Utils/json.d.ts +7 -0
- package/Utils/json.js +109 -0
- package/Utils/list.js +7 -7
- package/Utils/thread.d.ts +9 -15
- package/Utils/thread.js +36 -29
- package/jTemplates.js +1 -3176
- package/jTemplates.js.map +1 -1
- package/package.json +1 -1
- package/web.export.d.ts +1 -1
- package/web.export.js +3 -1
- package/DOM/utils.d.ts +0 -1
- package/DOM/utils.js +0 -47
package/Store/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ObservableScope = exports.StoreAsync = exports.StoreSync = exports.Store = void 0;
|
|
3
|
+
exports.ObservableComputed = exports.ObservableNode = exports.ObservableScope = exports.StoreAsync = exports.StoreSync = exports.Store = void 0;
|
|
4
4
|
var store_1 = require("./Store/store");
|
|
5
5
|
Object.defineProperty(exports, "Store", { enumerable: true, get: function () { return store_1.Store; } });
|
|
6
6
|
var storeSync_1 = require("./Store/storeSync");
|
|
@@ -9,3 +9,7 @@ var storeAsync_1 = require("./Store/storeAsync");
|
|
|
9
9
|
Object.defineProperty(exports, "StoreAsync", { enumerable: true, get: function () { return storeAsync_1.StoreAsync; } });
|
|
10
10
|
var observableScope_1 = require("./Tree/observableScope");
|
|
11
11
|
Object.defineProperty(exports, "ObservableScope", { enumerable: true, get: function () { return observableScope_1.ObservableScope; } });
|
|
12
|
+
var observableNode_1 = require("./Tree/observableNode");
|
|
13
|
+
Object.defineProperty(exports, "ObservableNode", { enumerable: true, get: function () { return observableNode_1.ObservableNode; } });
|
|
14
|
+
var observableComputed_1 = require("./Tree/observableComputed");
|
|
15
|
+
Object.defineProperty(exports, "ObservableComputed", { enumerable: true, get: function () { return observableComputed_1.ObservableComputed; } });
|
package/Utils/decorators.d.ts
CHANGED
|
@@ -22,7 +22,9 @@ export declare namespace Destroy {
|
|
|
22
22
|
declare function DestroyDecorator<T extends Component<any, any, any> & Record<K, IDestroyable>, K extends string>(target: T, propertyKey: K): any;
|
|
23
23
|
export declare function PreReqTemplate(template: {
|
|
24
24
|
(): NodeRefTypes | NodeRefTypes[];
|
|
25
|
-
}): <T extends Component<any, any, any>>(target:
|
|
25
|
+
}): <T extends Component<any, any, any>>(target: {
|
|
26
|
+
new (...args: Array<any>): T;
|
|
27
|
+
}) => any;
|
|
26
28
|
export declare namespace PreReqTemplate {
|
|
27
29
|
function Get(value: any): NodeRefTypes[];
|
|
28
30
|
}
|
package/Utils/decorators.js
CHANGED
|
@@ -1,70 +1,75 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
|
|
3
|
+
exports.State = State;
|
|
4
|
+
exports.StateSync = StateSync;
|
|
5
|
+
exports.StateAsync = StateAsync;
|
|
6
|
+
exports.Scope = Scope;
|
|
7
|
+
exports.DestroyScope = DestroyScope;
|
|
8
|
+
exports.Computed = Computed;
|
|
9
|
+
exports.ComputedAsync = ComputedAsync;
|
|
10
|
+
exports.Inject = Inject;
|
|
11
|
+
exports.Destroy = Destroy;
|
|
12
|
+
exports.PreReqTemplate = PreReqTemplate;
|
|
13
|
+
exports.PreReq = PreReq;
|
|
5
14
|
const Store_1 = require("../Store");
|
|
6
15
|
const observableScope_1 = require("../Store/Tree/observableScope");
|
|
7
16
|
const observableTree_1 = require("../Store/Tree/observableTree");
|
|
17
|
+
const observableNode_1 = require("../Store/Tree/observableNode");
|
|
8
18
|
function State() {
|
|
9
19
|
return StateDecorator;
|
|
10
20
|
}
|
|
11
|
-
exports.State = State;
|
|
12
21
|
function StateDecorator(target, propertyKey) {
|
|
13
22
|
const propKey = `StoreDecorator_${propertyKey}`;
|
|
14
|
-
DestroyDecorator(target, propKey);
|
|
15
23
|
return {
|
|
16
24
|
configurable: false,
|
|
17
25
|
enumerable: true,
|
|
18
26
|
get: function () {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
if (observableScope_1.ObservableScope.Watching())
|
|
23
|
-
return value;
|
|
24
|
-
return observableTree_1.ObservableTree.UnwrapProxyValues(value);
|
|
27
|
+
const map = this.DecoratorMap;
|
|
28
|
+
const node = map.get(propKey);
|
|
29
|
+
return node;
|
|
25
30
|
},
|
|
26
31
|
set: function (val) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
if (!
|
|
30
|
-
map.set(propKey,
|
|
31
|
-
else
|
|
32
|
-
|
|
32
|
+
const map = this.DecoratorMap;
|
|
33
|
+
const node = map.get(propKey);
|
|
34
|
+
if (!node)
|
|
35
|
+
map.set(propKey, observableNode_1.ObservableNode.Create(val));
|
|
36
|
+
else {
|
|
37
|
+
const keys = Object.keys(val);
|
|
38
|
+
for (let x = 0; x < keys.length; x++)
|
|
39
|
+
node[keys[x]] = val[keys[x]];
|
|
40
|
+
}
|
|
33
41
|
}
|
|
34
42
|
};
|
|
35
43
|
}
|
|
36
44
|
function StateSync() {
|
|
37
45
|
return StateSyncDecorator;
|
|
38
46
|
}
|
|
39
|
-
exports.StateSync = StateSync;
|
|
40
47
|
function StateSyncDecorator(target, propertyKey) {
|
|
41
48
|
const propKey = `StoreSyncDecorator_${propertyKey}`;
|
|
42
|
-
DestroyDecorator(target, propKey);
|
|
43
49
|
return {
|
|
44
50
|
configurable: false,
|
|
45
51
|
enumerable: true,
|
|
46
52
|
get: function () {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
if (observableScope_1.ObservableScope.Watching())
|
|
51
|
-
return value;
|
|
52
|
-
return observableTree_1.ObservableTree.UnwrapProxyValues(value);
|
|
53
|
+
const map = this.DecoratorMap;
|
|
54
|
+
const node = map.get(propKey);
|
|
55
|
+
return node;
|
|
53
56
|
},
|
|
54
57
|
set: function (val) {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
if (!
|
|
58
|
-
map.set(propKey,
|
|
59
|
-
else
|
|
60
|
-
|
|
58
|
+
const map = this.DecoratorMap;
|
|
59
|
+
const node = map.get(propKey);
|
|
60
|
+
if (!node)
|
|
61
|
+
map.set(propKey, observableNode_1.ObservableNode.Create(val));
|
|
62
|
+
else {
|
|
63
|
+
const keys = Object.keys(val);
|
|
64
|
+
for (let x = 0; x < keys.length; x++)
|
|
65
|
+
node[keys[x]] = val[keys[x]];
|
|
66
|
+
}
|
|
61
67
|
}
|
|
62
68
|
};
|
|
63
69
|
}
|
|
64
70
|
function StateAsync() {
|
|
65
71
|
return StateAsyncDecorator;
|
|
66
72
|
}
|
|
67
|
-
exports.StateAsync = StateAsync;
|
|
68
73
|
function StateAsyncDecorator(target, propertyKey) {
|
|
69
74
|
const propKey = `StoreAsyncDecorator_${propertyKey}`;
|
|
70
75
|
const scopeKey = `StoreAsyncDecorator_Scope_${propertyKey}`;
|
|
@@ -97,7 +102,6 @@ function StateAsyncDecorator(target, propertyKey) {
|
|
|
97
102
|
function Scope() {
|
|
98
103
|
return ScopeDecorator;
|
|
99
104
|
}
|
|
100
|
-
exports.Scope = Scope;
|
|
101
105
|
function ScopeDecorator(target, propertyKey, descriptor) {
|
|
102
106
|
if (!(descriptor && descriptor.get))
|
|
103
107
|
throw "Scope decorator requires a getter";
|
|
@@ -123,7 +127,6 @@ function ScopeDecorator(target, propertyKey, descriptor) {
|
|
|
123
127
|
function DestroyScope() {
|
|
124
128
|
return DestroyScopeDecorator;
|
|
125
129
|
}
|
|
126
|
-
exports.DestroyScope = DestroyScope;
|
|
127
130
|
function DestroyScopeDecorator(target, propertyKey, descriptor) {
|
|
128
131
|
if (!(descriptor && descriptor.get))
|
|
129
132
|
throw "Destroy Scope decorator requires a getter";
|
|
@@ -156,7 +159,6 @@ function DestroyScopeDecorator(target, propertyKey, descriptor) {
|
|
|
156
159
|
function Computed() {
|
|
157
160
|
return ComputedDecorator;
|
|
158
161
|
}
|
|
159
|
-
exports.Computed = Computed;
|
|
160
162
|
function ComputedDecorator(target, propertyKey, descriptor) {
|
|
161
163
|
if (!(descriptor && descriptor.get))
|
|
162
164
|
throw "Computed decorator requires a getter";
|
|
@@ -190,7 +192,6 @@ function ComputedDecorator(target, propertyKey, descriptor) {
|
|
|
190
192
|
function ComputedAsync(idFunc) {
|
|
191
193
|
return ComputedAsyncDecorator.bind(null, idFunc);
|
|
192
194
|
}
|
|
193
|
-
exports.ComputedAsync = ComputedAsync;
|
|
194
195
|
function ComputedAsyncDecorator(idFunc, target, propertyKey, descriptor) {
|
|
195
196
|
if (!(descriptor && descriptor.get))
|
|
196
197
|
throw "ComputedAsync decorator requires a getter";
|
|
@@ -233,7 +234,6 @@ function ComputedAsyncDecorator(idFunc, target, propertyKey, descriptor) {
|
|
|
233
234
|
function Inject(type) {
|
|
234
235
|
return InjectorDecorator.bind(null, type);
|
|
235
236
|
}
|
|
236
|
-
exports.Inject = Inject;
|
|
237
237
|
function InjectorDecorator(type, target, propertyKey, descriptor) {
|
|
238
238
|
return {
|
|
239
239
|
configurable: false,
|
|
@@ -249,7 +249,6 @@ function InjectorDecorator(type, target, propertyKey, descriptor) {
|
|
|
249
249
|
function Destroy() {
|
|
250
250
|
return DestroyDecorator;
|
|
251
251
|
}
|
|
252
|
-
exports.Destroy = Destroy;
|
|
253
252
|
(function (Destroy) {
|
|
254
253
|
function Get(value) {
|
|
255
254
|
return value && value.DestroyDecorator_Destroys || [];
|
|
@@ -270,7 +269,6 @@ function DestroyDecorator(target, propertyKey) {
|
|
|
270
269
|
function PreReqTemplate(template) {
|
|
271
270
|
return PreReqTemplateDecorator.bind(null, template);
|
|
272
271
|
}
|
|
273
|
-
exports.PreReqTemplate = PreReqTemplate;
|
|
274
272
|
(function (PreReqTemplate) {
|
|
275
273
|
function Get(value) {
|
|
276
274
|
var func = value && value.PreReqTemplateDecorator_Template;
|
|
@@ -288,7 +286,6 @@ function PreReqTemplateDecorator(template, target) {
|
|
|
288
286
|
function PreReq() {
|
|
289
287
|
return PreReqDecorator;
|
|
290
288
|
}
|
|
291
|
-
exports.PreReq = PreReq;
|
|
292
289
|
(function (PreReq) {
|
|
293
290
|
function Get(value) {
|
|
294
291
|
return value && value.PreReqDecorator_PreReqs || [];
|
package/Utils/emitter.d.ts
CHANGED
|
@@ -1,9 +1,5 @@
|
|
|
1
|
-
export type EmitterCallback =
|
|
2
|
-
|
|
3
|
-
};
|
|
4
|
-
export type Emitter = Set<{
|
|
5
|
-
(...args: any[]): void;
|
|
6
|
-
}>;
|
|
1
|
+
export type EmitterCallback<T extends readonly any[] = any[]> = (...args: T) => boolean | void;
|
|
2
|
+
export type Emitter = Set<EmitterCallback>;
|
|
7
3
|
export declare namespace Emitter {
|
|
8
4
|
function Create(): Emitter;
|
|
9
5
|
function On(emitter: Emitter, callback: EmitterCallback): void;
|
package/Utils/emitter.js
CHANGED
|
@@ -12,9 +12,16 @@ var Emitter;
|
|
|
12
12
|
}
|
|
13
13
|
Emitter.On = On;
|
|
14
14
|
function Emit(emitter, ...args) {
|
|
15
|
+
let cleanup;
|
|
15
16
|
emitter.forEach(function (cb) {
|
|
16
|
-
cb(...args);
|
|
17
|
+
const result = cb(...args);
|
|
18
|
+
if (result === true) {
|
|
19
|
+
cleanup ??= [];
|
|
20
|
+
cleanup.push(cb);
|
|
21
|
+
}
|
|
17
22
|
});
|
|
23
|
+
for (let x = 0; cleanup !== undefined && x < cleanup.length; x++)
|
|
24
|
+
Remove(emitter, cleanup[x]);
|
|
18
25
|
}
|
|
19
26
|
Emitter.Emit = Emit;
|
|
20
27
|
function Remove(emitter, callback) {
|
package/Utils/json.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export type JsonDiffResult<T = unknown> = {
|
|
2
|
+
path: (string | number)[];
|
|
3
|
+
value: unknown;
|
|
4
|
+
}[];
|
|
5
|
+
export declare function JsonType(value: any): "value" | "array" | "object";
|
|
6
|
+
export declare function JsonDeepClone<T>(value: T): T;
|
|
7
|
+
export declare function JsonDiff<T>(newValue: T, oldValue: T): JsonDiffResult<T>;
|
package/Utils/json.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.JsonType = JsonType;
|
|
4
|
+
exports.JsonDeepClone = JsonDeepClone;
|
|
5
|
+
exports.JsonDiff = JsonDiff;
|
|
6
|
+
const jsonProto = Object.getPrototypeOf({});
|
|
7
|
+
function JsonType(value) {
|
|
8
|
+
if (value === null || value === undefined)
|
|
9
|
+
return 'value';
|
|
10
|
+
if (Array.isArray(value))
|
|
11
|
+
return 'array';
|
|
12
|
+
if (jsonProto === Object.getPrototypeOf(value))
|
|
13
|
+
return 'object';
|
|
14
|
+
return 'value';
|
|
15
|
+
}
|
|
16
|
+
function JsonDeepClone(value) {
|
|
17
|
+
const type = JsonType(value);
|
|
18
|
+
switch (type) {
|
|
19
|
+
case 'array':
|
|
20
|
+
return value.map(JsonDeepClone);
|
|
21
|
+
case 'object': {
|
|
22
|
+
const ret = {};
|
|
23
|
+
const keys = Object.keys(value);
|
|
24
|
+
for (let x = 0; x < keys.length; x++)
|
|
25
|
+
ret[keys[x]] = JsonDeepClone(value[keys[x]]);
|
|
26
|
+
return ret;
|
|
27
|
+
}
|
|
28
|
+
default:
|
|
29
|
+
return value;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
function JsonDiff(newValue, oldValue) {
|
|
33
|
+
const result = [];
|
|
34
|
+
JsonDiffRecursive([], newValue, oldValue, result);
|
|
35
|
+
return result;
|
|
36
|
+
}
|
|
37
|
+
function JsonDiffRecursive(path, newValue, oldValue, resp) {
|
|
38
|
+
if (newValue === oldValue)
|
|
39
|
+
return false;
|
|
40
|
+
const newType = JsonType(newValue);
|
|
41
|
+
const oldType = JsonType(oldValue);
|
|
42
|
+
const changedPathLength = resp.length;
|
|
43
|
+
let allChildrenChanged = true;
|
|
44
|
+
if (newType === oldType)
|
|
45
|
+
switch (newType) {
|
|
46
|
+
case 'array': {
|
|
47
|
+
allChildrenChanged = JsonDiffArrays(path, newValue, oldValue, resp);
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
case 'object': {
|
|
51
|
+
allChildrenChanged = JsonDiffObjects(path, newValue, oldValue, resp);
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
if (allChildrenChanged) {
|
|
56
|
+
resp.splice(changedPathLength);
|
|
57
|
+
resp.push({
|
|
58
|
+
path,
|
|
59
|
+
value: newValue
|
|
60
|
+
});
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
function JsonDiffArrays(path, newValue, oldValue, resp) {
|
|
66
|
+
let allChildrenChanged = true;
|
|
67
|
+
if (newValue.length !== oldValue.length)
|
|
68
|
+
resp.push({
|
|
69
|
+
path: path.concat('length'),
|
|
70
|
+
value: newValue.length
|
|
71
|
+
});
|
|
72
|
+
if (newValue.length > 0 || oldValue.length > 0) {
|
|
73
|
+
for (let y = 0; y < newValue.length; y++) {
|
|
74
|
+
const arrayPath = path.concat(y);
|
|
75
|
+
const oldValueElem = oldValue[y];
|
|
76
|
+
allChildrenChanged = JsonDiffRecursive(arrayPath, newValue[y], oldValueElem, resp) && allChildrenChanged;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
else
|
|
80
|
+
allChildrenChanged = false;
|
|
81
|
+
return allChildrenChanged;
|
|
82
|
+
}
|
|
83
|
+
function JsonDiffObjects(path, newValue, oldValue, resp) {
|
|
84
|
+
const newKeys = Object.keys(newValue).sort();
|
|
85
|
+
const oldKeys = Object.keys(oldValue).sort();
|
|
86
|
+
if (newKeys.length === 0 && oldKeys.length === 0)
|
|
87
|
+
return false;
|
|
88
|
+
if (newKeys.length < oldKeys.length)
|
|
89
|
+
return true;
|
|
90
|
+
let newKeyIndex = 0;
|
|
91
|
+
let oldKeyIndex = 0;
|
|
92
|
+
while (newKeyIndex < newKeys.length) {
|
|
93
|
+
const childPath = path.concat(newKeys[newKeyIndex]);
|
|
94
|
+
if (oldKeyIndex < oldKeys.length && newKeys[newKeyIndex] === oldKeys[oldKeyIndex]) {
|
|
95
|
+
JsonDiffRecursive(childPath, newValue[newKeys[newKeyIndex]], oldValue[oldKeys[oldKeyIndex]], resp);
|
|
96
|
+
oldKeyIndex++;
|
|
97
|
+
}
|
|
98
|
+
else if (newValue[newKeys[newKeyIndex]] !== undefined) {
|
|
99
|
+
resp.push({
|
|
100
|
+
path: childPath,
|
|
101
|
+
value: newValue[newKeys[newKeyIndex]]
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
newKeyIndex++;
|
|
105
|
+
}
|
|
106
|
+
if (oldKeyIndex < oldKeys.length)
|
|
107
|
+
return true;
|
|
108
|
+
return false;
|
|
109
|
+
}
|
package/Utils/list.js
CHANGED
|
@@ -18,7 +18,7 @@ var List;
|
|
|
18
18
|
}
|
|
19
19
|
List.Clear = Clear;
|
|
20
20
|
function Push(list, data) {
|
|
21
|
-
|
|
21
|
+
const node = { previous: null, next: null, data: data };
|
|
22
22
|
if (list.size === 0) {
|
|
23
23
|
list.head = node;
|
|
24
24
|
list.tail = node;
|
|
@@ -36,7 +36,7 @@ var List;
|
|
|
36
36
|
function Pop(list) {
|
|
37
37
|
if (list.size === 0)
|
|
38
38
|
return null;
|
|
39
|
-
|
|
39
|
+
const node = list.head;
|
|
40
40
|
list.head = node.next;
|
|
41
41
|
if (list.head)
|
|
42
42
|
list.head.previous = null;
|
|
@@ -69,8 +69,8 @@ var List;
|
|
|
69
69
|
function AddBefore(list, node, data) {
|
|
70
70
|
if (!node)
|
|
71
71
|
return List.Add(list, data);
|
|
72
|
-
|
|
73
|
-
|
|
72
|
+
const newNode = { previous: null, next: null, data: data };
|
|
73
|
+
const prevNode = node.previous;
|
|
74
74
|
newNode.next = node;
|
|
75
75
|
node.previous = newNode;
|
|
76
76
|
if (list.head === node)
|
|
@@ -86,8 +86,8 @@ var List;
|
|
|
86
86
|
function AddAfter(list, node, data) {
|
|
87
87
|
if (!node)
|
|
88
88
|
return List.Push(list, data);
|
|
89
|
-
|
|
90
|
-
|
|
89
|
+
const newNode = { previous: null, next: null, data: data };
|
|
90
|
+
const nextNode = node.next;
|
|
91
91
|
node.next = newNode;
|
|
92
92
|
newNode.previous = node;
|
|
93
93
|
if (list.tail === node)
|
|
@@ -133,7 +133,7 @@ var List;
|
|
|
133
133
|
}
|
|
134
134
|
List.RemoveNode = RemoveNode;
|
|
135
135
|
function ForEach(list, callback) {
|
|
136
|
-
|
|
136
|
+
let node = list.head;
|
|
137
137
|
while (node) {
|
|
138
138
|
callback(node.data);
|
|
139
139
|
node = node.next;
|
package/Utils/thread.d.ts
CHANGED
|
@@ -1,16 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
(): void;
|
|
3
|
-
}
|
|
4
|
-
export declare function
|
|
5
|
-
|
|
6
|
-
}): void;
|
|
1
|
+
type ThreadCallback = {
|
|
2
|
+
(async: boolean): void;
|
|
3
|
+
};
|
|
4
|
+
export declare function Schedule(callback: ThreadCallback): void;
|
|
5
|
+
export declare function After(callback: ThreadCallback): void;
|
|
7
6
|
export declare function Callback<A = void, B = void, C = void, D = void>(callback: (a: A, b: B, c: C, d: D) => void): (a?: A, b?: B, c?: C, d?: D) => void;
|
|
8
|
-
export declare function Synch(callback:
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
export
|
|
12
|
-
(): void;
|
|
13
|
-
}): void;
|
|
14
|
-
export declare function ThreadAsync(callback: {
|
|
15
|
-
(): void;
|
|
16
|
-
}): Promise<void>;
|
|
7
|
+
export declare function Synch(callback: ThreadCallback): void;
|
|
8
|
+
export declare function Thread(callback: ThreadCallback): void;
|
|
9
|
+
export declare function ThreadAsync(callback: ThreadCallback): Promise<boolean>;
|
|
10
|
+
export {};
|
package/Utils/thread.js
CHANGED
|
@@ -1,18 +1,32 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.Schedule = Schedule;
|
|
4
|
+
exports.After = After;
|
|
5
|
+
exports.Callback = Callback;
|
|
6
|
+
exports.Synch = Synch;
|
|
7
|
+
exports.Thread = Thread;
|
|
8
|
+
exports.ThreadAsync = ThreadAsync;
|
|
4
9
|
const list_1 = require("./list");
|
|
5
10
|
const workTimeMs = 16;
|
|
6
11
|
const contextQueue = list_1.List.Create();
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
let threadContext = null;
|
|
13
|
+
let timeoutRunning = false;
|
|
14
|
+
const scheduleCallback = setTimeout;
|
|
15
|
+
function timeRemaining() {
|
|
16
|
+
return this.end - Date.now();
|
|
17
|
+
}
|
|
18
|
+
function createDeadline() {
|
|
19
|
+
return {
|
|
20
|
+
end: Date.now() + workTimeMs,
|
|
21
|
+
timeRemaining
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
function ProcessQueue(deadline = createDeadline()) {
|
|
25
|
+
let ctx;
|
|
26
|
+
while (deadline.timeRemaining() > 0 && (ctx = list_1.List.Pop(contextQueue)))
|
|
27
|
+
DoWork(ctx, deadline);
|
|
14
28
|
if (contextQueue.size > 0)
|
|
15
|
-
|
|
29
|
+
scheduleCallback(ProcessQueue);
|
|
16
30
|
else
|
|
17
31
|
timeoutRunning = false;
|
|
18
32
|
}
|
|
@@ -21,20 +35,20 @@ function ScheduleWork(ctx) {
|
|
|
21
35
|
if (timeoutRunning)
|
|
22
36
|
return;
|
|
23
37
|
timeoutRunning = true;
|
|
24
|
-
|
|
38
|
+
scheduleCallback(ProcessQueue);
|
|
25
39
|
}
|
|
26
40
|
function Invoke(ctx, callback) {
|
|
27
|
-
|
|
41
|
+
const parent = ctx.workEndNode;
|
|
28
42
|
ctx.workEndNode = ctx.workList.head;
|
|
29
|
-
callback();
|
|
43
|
+
callback(true);
|
|
30
44
|
ctx.workEndNode = parent;
|
|
31
45
|
}
|
|
32
|
-
function DoWork(ctx,
|
|
33
|
-
|
|
46
|
+
function DoWork(ctx, deadline = createDeadline()) {
|
|
47
|
+
const parentContext = threadContext;
|
|
34
48
|
threadContext = ctx;
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
while (async === ctx.async &&
|
|
49
|
+
const async = ctx.async;
|
|
50
|
+
let callback;
|
|
51
|
+
while (async === ctx.async && deadline.timeRemaining() > 0 && (callback = list_1.List.Pop(ctx.workList)))
|
|
38
52
|
Invoke(ctx, callback);
|
|
39
53
|
if (ctx.workList.size > 0)
|
|
40
54
|
ScheduleWork(ctx);
|
|
@@ -58,51 +72,44 @@ function ScheduleCallback(callback, before, async) {
|
|
|
58
72
|
threadContext.workEndNode = list_1.List.Add(threadContext.workList, callback);
|
|
59
73
|
}
|
|
60
74
|
function SynchWithoutThread(callback) {
|
|
61
|
-
|
|
62
|
-
callback();
|
|
75
|
+
callback(false);
|
|
63
76
|
if (threadContext)
|
|
64
77
|
if (threadContext.async)
|
|
65
78
|
ScheduleWork(threadContext);
|
|
66
79
|
else
|
|
67
|
-
DoWork(threadContext
|
|
80
|
+
DoWork(threadContext);
|
|
68
81
|
threadContext = null;
|
|
69
82
|
}
|
|
70
83
|
function Schedule(callback) {
|
|
71
84
|
ScheduleCallback(callback, true, true);
|
|
72
85
|
}
|
|
73
|
-
exports.Schedule = Schedule;
|
|
74
86
|
function After(callback) {
|
|
75
87
|
ScheduleCallback(callback, false, false);
|
|
76
88
|
}
|
|
77
|
-
exports.After = After;
|
|
78
89
|
function Callback(callback) {
|
|
79
90
|
return function (a, b, c, d) {
|
|
80
91
|
Schedule(function () { callback(a, b, c, d); });
|
|
81
92
|
};
|
|
82
93
|
}
|
|
83
|
-
exports.Callback = Callback;
|
|
84
94
|
var inSynchCallback = false;
|
|
85
95
|
function Synch(callback) {
|
|
86
96
|
if (threadContext || inSynchCallback)
|
|
87
|
-
callback();
|
|
97
|
+
callback(false);
|
|
88
98
|
else {
|
|
89
99
|
inSynchCallback = true;
|
|
90
100
|
SynchWithoutThread(callback);
|
|
91
101
|
inSynchCallback = false;
|
|
92
102
|
}
|
|
93
103
|
}
|
|
94
|
-
exports.Synch = Synch;
|
|
95
104
|
function Thread(callback) {
|
|
96
105
|
if (threadContext)
|
|
97
106
|
ScheduleCallback(callback, true, false);
|
|
98
107
|
else
|
|
99
108
|
Synch(callback);
|
|
100
109
|
}
|
|
101
|
-
exports.Thread = Thread;
|
|
102
110
|
function ThreadAsync(callback) {
|
|
103
|
-
return new Promise(resolve => Thread(function () {
|
|
104
|
-
callback();
|
|
111
|
+
return new Promise(resolve => Thread(function (async) {
|
|
112
|
+
callback(async);
|
|
105
113
|
Thread(resolve);
|
|
106
114
|
}));
|
|
107
115
|
}
|
|
108
|
-
exports.ThreadAsync = ThreadAsync;
|