j-templates 9.0.1 → 10.0.1
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/SYNTAX_PRIMER.md +181 -101
- package/Store/Diff/channelClient.d.ts +104 -0
- package/Store/Diff/channelClient.js +225 -0
- package/Store/Diff/defaultDiffTreeWorker.js +3 -4
- package/Store/Diff/diffTree.d.ts +56 -0
- package/Store/Diff/diffTree.js +110 -4
- package/Store/Diff/diffTree.types.d.ts +186 -21
- package/Store/Diff/diffTree.types.js +2 -10
- package/Store/Diff/diffTreeEndpoint.d.ts +101 -0
- package/Store/Diff/diffTreeEndpoint.js +173 -0
- package/Store/Diff/diffTreeEndpoint.types.d.ts +71 -0
- package/Store/Diff/diffTreeEndpoint.types.js +1 -0
- package/Store/Diff/diffTreeHub.d.ts +139 -0
- package/Store/Diff/diffTreeHub.js +346 -0
- package/Store/Diff/hostPublisher.d.ts +38 -0
- package/Store/Diff/hostPublisher.js +147 -0
- package/Store/Diff/index.d.ts +3 -0
- package/Store/Diff/index.js +3 -0
- package/Store/Diff/observableDiffTree.d.ts +93 -0
- package/Store/Diff/observableDiffTree.js +200 -0
- package/Store/Diff/onConnect.d.ts +33 -0
- package/Store/Diff/onConnect.js +83 -0
- package/Store/Diff/utils/diffTree.types.d.ts +9 -0
- package/Store/Diff/utils/homes.d.ts +12 -0
- package/Store/Diff/utils/homes.js +40 -0
- package/Store/Diff/utils/paths.d.ts +2 -1
- package/Store/Diff/utils/paths.js +3 -2
- package/Store/Diff/utils/routes.d.ts +18 -0
- package/Store/Diff/utils/routes.js +33 -2
- package/Store/Diff/utils/state.js +4 -3
- package/Store/Store/store.d.ts +41 -4
- package/Store/Store/store.js +62 -7
- package/Store/Store/storeAsync.d.ts +134 -25
- package/Store/Store/storeAsync.js +235 -33
- package/Store/Store/storeSync.d.ts +91 -11
- package/Store/Store/storeSync.js +107 -12
- package/Store/Tree/observableNode.d.ts +3 -0
- package/Store/Tree/observableNode.js +87 -13
- package/Store/Tree/observableScope.d.ts +3 -1
- package/Store/Tree/observableScope.js +12 -0
- package/Store/Tree/opaque.d.ts +30 -0
- package/Store/Tree/opaque.js +35 -0
- package/Store/Tree/snapshot.js +5 -4
- package/Store/index.d.ts +4 -1
- package/Store/index.js +1 -1
- package/Utils/decorators.d.ts +0 -98
- package/Utils/decorators.js +0 -156
- package/Utils/emitter.d.ts +11 -4
- package/Utils/emitter.js +25 -2
- package/Utils/index.d.ts +2 -0
- package/Utils/index.js +1 -0
- package/Utils/pattern.d.ts +14 -0
- package/Utils/pattern.js +20 -0
- package/Utils/subscriptionManager.d.ts +78 -0
- package/Utils/subscriptionManager.js +159 -0
- package/package.json +1 -1
- package/Store/Diff/diffTreeWorker.d.ts +0 -6
- package/Store/Diff/diffTreeWorker.js +0 -22
- package/Store/Diff/diffTreeWriter.d.ts +0 -92
- package/Store/Diff/diffTreeWriter.js +0 -137
- package/Store/Diff/workerQueue.d.ts +0 -7
- package/Store/Diff/workerQueue.js +0 -29
- package/Store/Store/pushStore.d.ts +0 -53
- package/Store/Store/pushStore.js +0 -77
package/Utils/emitter.d.ts
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
|
+
export declare enum EmitterEvent {
|
|
2
|
+
SUBSCRIBED = 0,
|
|
3
|
+
UNSUBSCRIBED = 1
|
|
4
|
+
}
|
|
5
|
+
export type EmitterEventCallback = (event: EmitterEvent, emitter: Emitter) => void;
|
|
1
6
|
export type EmitterCallback<T extends readonly any[] = any[]> = (...args: T) => void;
|
|
2
|
-
export type Emitter = [[number, number], ...EmitterCallback[]];
|
|
7
|
+
export type Emitter<T extends readonly any[] = any[]> = [[number, number, Emitter<[EmitterEvent, Emitter]> | undefined], ...EmitterCallback<T>[]];
|
|
3
8
|
export declare namespace Emitter {
|
|
4
|
-
function Create(): Emitter
|
|
5
|
-
function On(emitter: Emitter
|
|
6
|
-
function
|
|
9
|
+
function Create<T extends any[] = any[]>(): Emitter<T>;
|
|
10
|
+
function On<T extends any[]>(emitter: Emitter<T>, callback: EmitterCallback<T>): void;
|
|
11
|
+
function Watch(emitter: Emitter, callback: EmitterCallback<[EmitterEvent, Emitter]>): void;
|
|
12
|
+
function Unwatch(emitter: Emitter, callback: EmitterCallback<[EmitterEvent, Emitter]>): void;
|
|
13
|
+
function Emit<T extends any[]>(emitter: Emitter<T>, ...args: T): void;
|
|
7
14
|
function Remove(emitter: Emitter, callback: EmitterCallback): void;
|
|
8
15
|
function Clear(emitter: Emitter): void;
|
|
9
16
|
}
|
package/Utils/emitter.js
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
import { _requestIdleCallback } from "./scheduling";
|
|
2
|
+
export var EmitterEvent;
|
|
3
|
+
(function (EmitterEvent) {
|
|
4
|
+
EmitterEvent[EmitterEvent["SUBSCRIBED"] = 0] = "SUBSCRIBED";
|
|
5
|
+
EmitterEvent[EmitterEvent["UNSUBSCRIBED"] = 1] = "UNSUBSCRIBED";
|
|
6
|
+
})(EmitterEvent || (EmitterEvent = {}));
|
|
2
7
|
const pendingCompactEmitters = new Set();
|
|
3
8
|
function Compact(emitter) {
|
|
4
9
|
if (emitter.length === 1 || emitter[0][0] === 0)
|
|
@@ -29,13 +34,26 @@ function CompactEmitter(emitter) {
|
|
|
29
34
|
export var Emitter;
|
|
30
35
|
(function (Emitter) {
|
|
31
36
|
function Create() {
|
|
32
|
-
return [[0, -1]];
|
|
37
|
+
return [[0, -1, undefined]];
|
|
33
38
|
}
|
|
34
39
|
Emitter.Create = Create;
|
|
35
40
|
function On(emitter, callback) {
|
|
36
41
|
emitter.push(callback);
|
|
42
|
+
if (emitter.length - emitter[0][0] === 2)
|
|
43
|
+
emitter[0][2] && Emitter.Emit(emitter[0][2], EmitterEvent.SUBSCRIBED, emitter);
|
|
37
44
|
}
|
|
38
45
|
Emitter.On = On;
|
|
46
|
+
function Watch(emitter, callback) {
|
|
47
|
+
emitter[0][2] ??= Emitter.Create();
|
|
48
|
+
On(emitter[0][2], callback);
|
|
49
|
+
}
|
|
50
|
+
Emitter.Watch = Watch;
|
|
51
|
+
function Unwatch(emitter, callback) {
|
|
52
|
+
if (!emitter[0][2])
|
|
53
|
+
return;
|
|
54
|
+
Remove(emitter[0][2], callback);
|
|
55
|
+
}
|
|
56
|
+
Emitter.Unwatch = Unwatch;
|
|
39
57
|
// Re-entry during emit is not supported and considered a bug in client code
|
|
40
58
|
function Emit(emitter, ...args) {
|
|
41
59
|
if (emitter[0][1] !== -1)
|
|
@@ -73,11 +91,16 @@ export var Emitter;
|
|
|
73
91
|
RemoveFromBeginning(emitter, index);
|
|
74
92
|
else
|
|
75
93
|
RemoveFromEnd(emitter, index);
|
|
94
|
+
if (emitter.length - emitter[0][0] === 1)
|
|
95
|
+
emitter[0][2] && Emitter.Emit(emitter[0][2], EmitterEvent.UNSUBSCRIBED, emitter);
|
|
76
96
|
}
|
|
77
97
|
Emitter.Remove = Remove;
|
|
78
98
|
function Clear(emitter) {
|
|
79
|
-
emitter.length
|
|
99
|
+
if (emitter.length - emitter[0][0] === 1)
|
|
100
|
+
return;
|
|
101
|
+
emitter.splice(1);
|
|
80
102
|
emitter[0][0] = 0;
|
|
103
|
+
emitter[0][2] && Emitter.Emit(emitter[0][2], EmitterEvent.UNSUBSCRIBED, emitter);
|
|
81
104
|
}
|
|
82
105
|
Emitter.Clear = Clear;
|
|
83
106
|
})(Emitter || (Emitter = {}));
|
package/Utils/index.d.ts
CHANGED
|
@@ -2,3 +2,5 @@ export * from "./decorators";
|
|
|
2
2
|
export * from "./animation";
|
|
3
3
|
export { Injector } from "./injector";
|
|
4
4
|
export { IDestroyable } from "./utils.types";
|
|
5
|
+
export { SubscriptionManager } from "./subscriptionManager";
|
|
6
|
+
export type { KeyFilter, ISubscriptionMatch } from "./subscriptionManager";
|
package/Utils/index.js
CHANGED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Key patterns: an exact key, or a pattern ending in `*` that matches every key starting with the
|
|
3
|
+
* text before it (`task_*` matches `task_1`, and `task_` itself). A lone `*` matches every key.
|
|
4
|
+
* Store roots, subscriptions' key filters and route segments all match this way. The text is
|
|
5
|
+
* compared as it is: no character but a final `*` is special.
|
|
6
|
+
*/
|
|
7
|
+
/** A pattern's wildcard, its last character */
|
|
8
|
+
export declare const WILDCARD = "*";
|
|
9
|
+
/** Whether `pattern` is a pattern rather than an exact key: it ends in `*` */
|
|
10
|
+
export declare function IsPattern(pattern: string): boolean;
|
|
11
|
+
/** The text a pattern's keys start with: the pattern without its `*` */
|
|
12
|
+
export declare function PatternPrefix(pattern: string): string;
|
|
13
|
+
/** Whether `key` matches `pattern`: it is the exact key, or starts with the pattern's prefix */
|
|
14
|
+
export declare function PatternMatches(pattern: string, key: string): boolean;
|
package/Utils/pattern.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Key patterns: an exact key, or a pattern ending in `*` that matches every key starting with the
|
|
3
|
+
* text before it (`task_*` matches `task_1`, and `task_` itself). A lone `*` matches every key.
|
|
4
|
+
* Store roots, subscriptions' key filters and route segments all match this way. The text is
|
|
5
|
+
* compared as it is: no character but a final `*` is special.
|
|
6
|
+
*/
|
|
7
|
+
/** A pattern's wildcard, its last character */
|
|
8
|
+
export const WILDCARD = "*";
|
|
9
|
+
/** Whether `pattern` is a pattern rather than an exact key: it ends in `*` */
|
|
10
|
+
export function IsPattern(pattern) {
|
|
11
|
+
return pattern.endsWith(WILDCARD);
|
|
12
|
+
}
|
|
13
|
+
/** The text a pattern's keys start with: the pattern without its `*` */
|
|
14
|
+
export function PatternPrefix(pattern) {
|
|
15
|
+
return pattern.slice(0, -WILDCARD.length);
|
|
16
|
+
}
|
|
17
|
+
/** Whether `key` matches `pattern`: it is the exact key, or starts with the pattern's prefix */
|
|
18
|
+
export function PatternMatches(pattern, key) {
|
|
19
|
+
return IsPattern(pattern) ? key.startsWith(PatternPrefix(pattern)) : pattern === key;
|
|
20
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A key filter: exact keys, and patterns ending in `*` that match every key starting with the text
|
|
3
|
+
* before it (`task_*` matches `task_1`). Undefined matches every key; an empty array matches none.
|
|
4
|
+
*/
|
|
5
|
+
export type KeyFilter = readonly string[] | undefined;
|
|
6
|
+
/** A subscription that matched some changed keys, and the keys it matched */
|
|
7
|
+
export interface ISubscriptionMatch<S> {
|
|
8
|
+
id: number;
|
|
9
|
+
subscription: S;
|
|
10
|
+
keys: string[];
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Subscriptions to keys, for any use: it matches keys to subscriptions and counts who holds a
|
|
14
|
+
* key, and knows nothing about what a key's value is or how it's delivered.
|
|
15
|
+
*
|
|
16
|
+
* - **Exact keys hold a key; patterns don't.** `SubscriberCount(key)` counts the subscriptions
|
|
17
|
+
* naming the key exactly, and `OnReleased` reports when the last of them ends. A pattern
|
|
18
|
+
* subscription matches keys without holding them, or a `whatif_*` subscriber would keep every key.
|
|
19
|
+
* - **Groups end together.** A subscription can name a group (to a host, a channel), and
|
|
20
|
+
* `Disconnect(group)` ends every subscription in it.
|
|
21
|
+
* - Callbacks run synchronously, inside the call that caused them.
|
|
22
|
+
*
|
|
23
|
+
* @template S What a subscription carries, e.g. its callback and form.
|
|
24
|
+
*/
|
|
25
|
+
export declare class SubscriptionManager<S> {
|
|
26
|
+
private entries;
|
|
27
|
+
private nextId;
|
|
28
|
+
/** Exact key -> the subscriptions naming it */
|
|
29
|
+
private counts;
|
|
30
|
+
private groups;
|
|
31
|
+
private released;
|
|
32
|
+
/**
|
|
33
|
+
* Adds a subscription.
|
|
34
|
+
* @param keys The keys it matches, see `KeyFilter`.
|
|
35
|
+
* @param subscription What the subscription carries.
|
|
36
|
+
* @param options `group`: the group `Disconnect` ends it with.
|
|
37
|
+
* @returns Its id.
|
|
38
|
+
*/
|
|
39
|
+
Subscribe(keys: KeyFilter, subscription: S, options?: {
|
|
40
|
+
group?: unknown;
|
|
41
|
+
}): number;
|
|
42
|
+
/**
|
|
43
|
+
* Ends a subscription. Each exact key it was the last to name is released.
|
|
44
|
+
* @returns Whether it existed.
|
|
45
|
+
*/
|
|
46
|
+
Unsubscribe(id: number): boolean;
|
|
47
|
+
/** Ends every subscription in a group */
|
|
48
|
+
Disconnect(group: unknown): void;
|
|
49
|
+
/** Whether a subscription exists */
|
|
50
|
+
Has(id: number): boolean;
|
|
51
|
+
/** What a subscription carries, or undefined when it doesn't exist */
|
|
52
|
+
Get(id: number): S | undefined;
|
|
53
|
+
/**
|
|
54
|
+
* The keys a subscription matches, in the order given.
|
|
55
|
+
* @returns The keys, or an empty array when the subscription doesn't exist.
|
|
56
|
+
*/
|
|
57
|
+
MatchingKeys(id: number, keys: Iterable<string>): string[];
|
|
58
|
+
/**
|
|
59
|
+
* Each subscription that matches any of the keys, with the keys it matches, in subscription
|
|
60
|
+
* order. Subscriptions that match none are left out.
|
|
61
|
+
*/
|
|
62
|
+
Matching(keys: Iterable<string>): ISubscriptionMatch<S>[];
|
|
63
|
+
/**
|
|
64
|
+
* Whether a subscription matches every key, with no filter, so a caller can hand it the whole
|
|
65
|
+
* of something without filtering it.
|
|
66
|
+
*/
|
|
67
|
+
MatchesAll(id: number): boolean;
|
|
68
|
+
/**
|
|
69
|
+
* Calls back when a key matching a pattern loses its last exact subscription, inside the
|
|
70
|
+
* `Unsubscribe` or `Disconnect` that ended it. A key never named exactly is never released.
|
|
71
|
+
* @param pattern An exact key or a pattern ending in `*`.
|
|
72
|
+
* @returns A function that removes the callback.
|
|
73
|
+
*/
|
|
74
|
+
OnReleased(pattern: string, callback: (key: string) => void): () => void;
|
|
75
|
+
/** How many subscriptions name a key exactly now; patterns don't count */
|
|
76
|
+
SubscriberCount(key: string): number;
|
|
77
|
+
private Release;
|
|
78
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { IsPattern, PatternMatches, PatternPrefix } from "./pattern";
|
|
2
|
+
/**
|
|
3
|
+
* Subscriptions to keys, for any use: it matches keys to subscriptions and counts who holds a
|
|
4
|
+
* key, and knows nothing about what a key's value is or how it's delivered.
|
|
5
|
+
*
|
|
6
|
+
* - **Exact keys hold a key; patterns don't.** `SubscriberCount(key)` counts the subscriptions
|
|
7
|
+
* naming the key exactly, and `OnReleased` reports when the last of them ends. A pattern
|
|
8
|
+
* subscription matches keys without holding them, or a `whatif_*` subscriber would keep every key.
|
|
9
|
+
* - **Groups end together.** A subscription can name a group (to a host, a channel), and
|
|
10
|
+
* `Disconnect(group)` ends every subscription in it.
|
|
11
|
+
* - Callbacks run synchronously, inside the call that caused them.
|
|
12
|
+
*
|
|
13
|
+
* @template S What a subscription carries, e.g. its callback and form.
|
|
14
|
+
*/
|
|
15
|
+
export class SubscriptionManager {
|
|
16
|
+
constructor() {
|
|
17
|
+
this.entries = new Map();
|
|
18
|
+
this.nextId = 1;
|
|
19
|
+
/** Exact key -> the subscriptions naming it */
|
|
20
|
+
this.counts = new Map();
|
|
21
|
+
this.groups = new Map();
|
|
22
|
+
this.released = [];
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Adds a subscription.
|
|
26
|
+
* @param keys The keys it matches, see `KeyFilter`.
|
|
27
|
+
* @param subscription What the subscription carries.
|
|
28
|
+
* @param options `group`: the group `Disconnect` ends it with.
|
|
29
|
+
* @returns Its id.
|
|
30
|
+
*/
|
|
31
|
+
Subscribe(keys, subscription, options = {}) {
|
|
32
|
+
const id = this.nextId++;
|
|
33
|
+
const entry = { subscription, all: keys === undefined, exact: new Set(), prefixes: [], group: options.group };
|
|
34
|
+
if (keys)
|
|
35
|
+
for (let x = 0; x < keys.length; x++) {
|
|
36
|
+
const key = keys[x];
|
|
37
|
+
if (IsPattern(key))
|
|
38
|
+
entry.prefixes.push(PatternPrefix(key));
|
|
39
|
+
else
|
|
40
|
+
entry.exact.add(key);
|
|
41
|
+
}
|
|
42
|
+
this.entries.set(id, entry);
|
|
43
|
+
for (const key of entry.exact)
|
|
44
|
+
this.counts.set(key, (this.counts.get(key) ?? 0) + 1);
|
|
45
|
+
if (options.group !== undefined) {
|
|
46
|
+
let ids = this.groups.get(options.group);
|
|
47
|
+
ids || this.groups.set(options.group, ids = new Set());
|
|
48
|
+
ids.add(id);
|
|
49
|
+
}
|
|
50
|
+
return id;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Ends a subscription. Each exact key it was the last to name is released.
|
|
54
|
+
* @returns Whether it existed.
|
|
55
|
+
*/
|
|
56
|
+
Unsubscribe(id) {
|
|
57
|
+
const entry = this.entries.get(id);
|
|
58
|
+
if (!entry)
|
|
59
|
+
return false;
|
|
60
|
+
this.entries.delete(id);
|
|
61
|
+
if (entry.group !== undefined) {
|
|
62
|
+
const ids = this.groups.get(entry.group);
|
|
63
|
+
ids.delete(id);
|
|
64
|
+
ids.size === 0 && this.groups.delete(entry.group);
|
|
65
|
+
}
|
|
66
|
+
const releasedKeys = [];
|
|
67
|
+
for (const key of entry.exact) {
|
|
68
|
+
const count = this.counts.get(key) - 1;
|
|
69
|
+
if (count > 0)
|
|
70
|
+
this.counts.set(key, count);
|
|
71
|
+
else {
|
|
72
|
+
this.counts.delete(key);
|
|
73
|
+
releasedKeys.push(key);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
for (const key of releasedKeys)
|
|
77
|
+
this.Release(key);
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
/** Ends every subscription in a group */
|
|
81
|
+
Disconnect(group) {
|
|
82
|
+
const ids = this.groups.get(group);
|
|
83
|
+
if (ids)
|
|
84
|
+
for (const id of Array.from(ids))
|
|
85
|
+
this.Unsubscribe(id);
|
|
86
|
+
}
|
|
87
|
+
/** Whether a subscription exists */
|
|
88
|
+
Has(id) {
|
|
89
|
+
return this.entries.has(id);
|
|
90
|
+
}
|
|
91
|
+
/** What a subscription carries, or undefined when it doesn't exist */
|
|
92
|
+
Get(id) {
|
|
93
|
+
return this.entries.get(id)?.subscription;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* The keys a subscription matches, in the order given.
|
|
97
|
+
* @returns The keys, or an empty array when the subscription doesn't exist.
|
|
98
|
+
*/
|
|
99
|
+
MatchingKeys(id, keys) {
|
|
100
|
+
const entry = this.entries.get(id);
|
|
101
|
+
const matched = [];
|
|
102
|
+
if (entry)
|
|
103
|
+
for (const key of keys)
|
|
104
|
+
Matches(entry, key) && matched.push(key);
|
|
105
|
+
return matched;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Each subscription that matches any of the keys, with the keys it matches, in subscription
|
|
109
|
+
* order. Subscriptions that match none are left out.
|
|
110
|
+
*/
|
|
111
|
+
Matching(keys) {
|
|
112
|
+
const list = Array.isArray(keys) ? keys : Array.from(keys);
|
|
113
|
+
const matches = [];
|
|
114
|
+
for (const [id, entry] of this.entries) {
|
|
115
|
+
const matched = entry.all ? list : list.filter(key => Matches(entry, key));
|
|
116
|
+
matched.length > 0 && matches.push({ id, subscription: entry.subscription, keys: matched });
|
|
117
|
+
}
|
|
118
|
+
return matches;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Whether a subscription matches every key, with no filter, so a caller can hand it the whole
|
|
122
|
+
* of something without filtering it.
|
|
123
|
+
*/
|
|
124
|
+
MatchesAll(id) {
|
|
125
|
+
return this.entries.get(id)?.all === true;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Calls back when a key matching a pattern loses its last exact subscription, inside the
|
|
129
|
+
* `Unsubscribe` or `Disconnect` that ended it. A key never named exactly is never released.
|
|
130
|
+
* @param pattern An exact key or a pattern ending in `*`.
|
|
131
|
+
* @returns A function that removes the callback.
|
|
132
|
+
*/
|
|
133
|
+
OnReleased(pattern, callback) {
|
|
134
|
+
const entry = { pattern, callback };
|
|
135
|
+
this.released.push(entry);
|
|
136
|
+
return () => Remove(this.released, entry);
|
|
137
|
+
}
|
|
138
|
+
/** How many subscriptions name a key exactly now; patterns don't count */
|
|
139
|
+
SubscriberCount(key) {
|
|
140
|
+
return this.counts.get(key) ?? 0;
|
|
141
|
+
}
|
|
142
|
+
Release(key) {
|
|
143
|
+
for (const { pattern, callback } of this.released.slice())
|
|
144
|
+
if (PatternMatches(pattern, key))
|
|
145
|
+
callback(key);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
function Matches(entry, key) {
|
|
149
|
+
if (entry.all || entry.exact.has(key))
|
|
150
|
+
return true;
|
|
151
|
+
for (let x = 0; x < entry.prefixes.length; x++)
|
|
152
|
+
if (key.startsWith(entry.prefixes[x]))
|
|
153
|
+
return true;
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
function Remove(list, item) {
|
|
157
|
+
const index = list.indexOf(item);
|
|
158
|
+
index >= 0 && list.splice(index, 1);
|
|
159
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { DiffTree } from "./diffTree";
|
|
2
|
-
/**
|
|
3
|
-
* Runs each batch of method calls a `StoreAsync` sends on the tree, then its projections, and
|
|
4
|
-
* posts back the tree's `Commit()` diff for the store to apply.
|
|
5
|
-
*/
|
|
6
|
-
export declare function ConnectWorkerToDiffTree(diffTree: DiffTree, worker: Worker): void;
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Runs each batch of method calls a `StoreAsync` sends on the tree, then its projections, and
|
|
3
|
-
* posts back the tree's `Commit()` diff for the store to apply.
|
|
4
|
-
*/
|
|
5
|
-
export function ConnectWorkerToDiffTree(diffTree, worker) {
|
|
6
|
-
worker.onmessage = function (event) {
|
|
7
|
-
const data = event.data;
|
|
8
|
-
for (let x = 0; x < data.length; x++)
|
|
9
|
-
RunMethod(diffTree, data[x]);
|
|
10
|
-
diffTree.RunProjections();
|
|
11
|
-
worker.postMessage(diffTree.Commit());
|
|
12
|
-
};
|
|
13
|
-
}
|
|
14
|
-
function RunMethod(diffTree, call) {
|
|
15
|
-
const args = call.arguments;
|
|
16
|
-
switch (call.method) {
|
|
17
|
-
case "writepath": return diffTree.WritePath(args[0], args[1]);
|
|
18
|
-
case "splicepath": return diffTree.SplicePath(args[0], args[1], args[2], args[3]);
|
|
19
|
-
case "pushpath": return diffTree.PushPath(args[0], args[1]);
|
|
20
|
-
case "patchpath": return diffTree.PatchPath(args[0], args[1]);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import { JsonDiffResult } from "../../Utils/json";
|
|
2
|
-
import { DiffTree } from "./diffTree";
|
|
3
|
-
import { IKeyFunc, IRouteDefinitions } from "./utils/diffTree.types";
|
|
4
|
-
/**
|
|
5
|
-
* A `DiffTree` that pushes its changes to a `PushStore` instead of answering a store's
|
|
6
|
-
* requests. Use it inside a worker that produces data itself (for example, the result of a
|
|
7
|
-
* computation it runs): write with the usual `DiffTree` methods, then call `Commit()` to run
|
|
8
|
-
* projections and post everything since the last commit as one message.
|
|
9
|
-
*
|
|
10
|
-
* - Push only: don't pass a `DiffTreeWriter` to `ConnectWorkerToDiffTree`, which commits
|
|
11
|
-
* every batch itself and would post it to the wrong place.
|
|
12
|
-
* - After a change fails, every later change before `Commit()` throws that same error. The
|
|
13
|
-
* changes made before the failure are already in the tree, so `Commit()` still sends them,
|
|
14
|
-
* followed by a `DIFF_TREE_ERROR` message. Projections aren't run for that batch; the next
|
|
15
|
-
* `Commit()` brings them up to date.
|
|
16
|
-
* - A `PushStore` only receives the changes committed after it starts listening, so
|
|
17
|
-
* create it before the writer's first `Commit()`.
|
|
18
|
-
*
|
|
19
|
-
* @example
|
|
20
|
-
* // engine-worker.ts
|
|
21
|
-
* const tree = new DiffTreeWriter(self, undefined, { "/": { projections } });
|
|
22
|
-
* tree.WritePath("results", computeResults());
|
|
23
|
-
* tree.Commit();
|
|
24
|
-
*
|
|
25
|
-
* // main thread
|
|
26
|
-
* const store = new PushStore(worker);
|
|
27
|
-
*
|
|
28
|
-
* @see PushStore
|
|
29
|
-
*/
|
|
30
|
-
export declare class DiffTreeWriter extends DiffTree {
|
|
31
|
-
private worker;
|
|
32
|
-
private uncommittedError;
|
|
33
|
-
/**
|
|
34
|
-
* Creates a DiffTreeWriter.
|
|
35
|
-
* @param worker Where `Commit()` posts its messages: the worker's `self`, or a `MessagePort`.
|
|
36
|
-
* On the same thread as the store, anything whose `postMessage` dispatches a `"message"`
|
|
37
|
-
* event on the store's `EventTarget`. The store clones every value it applies, so it never
|
|
38
|
-
* shares objects with the writer's tree.
|
|
39
|
-
* @param keyFunc Optional function to extract a key from objects. Pass the same key logic to
|
|
40
|
-
* the `PushStore` that receives the changes.
|
|
41
|
-
* @param routes Optional route definitions. Projections run once per `Commit()`, after the
|
|
42
|
-
* batch's changes.
|
|
43
|
-
*/
|
|
44
|
-
constructor(worker: {
|
|
45
|
-
postMessage(message: unknown): void;
|
|
46
|
-
}, keyFunc?: IKeyFunc, routes?: IRouteDefinitions);
|
|
47
|
-
/**
|
|
48
|
-
* Not available on a DiffTreeWriter: `Commit()` runs projections once per batch, after its
|
|
49
|
-
* changes. Running them earlier would push a projection result computed from part of a batch.
|
|
50
|
-
* @throws Always.
|
|
51
|
-
*/
|
|
52
|
-
RunProjections(): void;
|
|
53
|
-
/**
|
|
54
|
-
* Writes a value at a path.
|
|
55
|
-
* @throws The change's error, or an earlier uncommitted change's error.
|
|
56
|
-
*/
|
|
57
|
-
WritePath(path: string, value: any): void;
|
|
58
|
-
/**
|
|
59
|
-
* Merges a patch into the current value at a path.
|
|
60
|
-
* @throws The change's error, or an earlier uncommitted change's error.
|
|
61
|
-
*/
|
|
62
|
-
PatchPath(path: string, patch: any): void;
|
|
63
|
-
/**
|
|
64
|
-
* Appends items to the array at a path.
|
|
65
|
-
* @throws The change's error, or an earlier uncommitted change's error.
|
|
66
|
-
*/
|
|
67
|
-
PushPath(path: string, items: unknown[]): void;
|
|
68
|
-
/**
|
|
69
|
-
* Splices the array at a path.
|
|
70
|
-
* @throws The change's error, or an earlier uncommitted change's error.
|
|
71
|
-
*/
|
|
72
|
-
SplicePath(path: string, start: number, deleteCount: number, items: any[]): void;
|
|
73
|
-
/**
|
|
74
|
-
* Deletes a route, its child routes and its route instances.
|
|
75
|
-
* @throws The change's error, or an earlier uncommitted change's error.
|
|
76
|
-
*/
|
|
77
|
-
DeleteRoute(routePath: string): void;
|
|
78
|
-
/**
|
|
79
|
-
* Runs projections and posts every change since the last commit as one `DIFF_TREE_EVENT`
|
|
80
|
-
* message, which a `PushStore` applies as a unit. Posts nothing when nothing changed.
|
|
81
|
-
*
|
|
82
|
-
* If a change in the batch failed, projections are skipped, the applied changes are still
|
|
83
|
-
* posted, and a `DIFF_TREE_ERROR` message follows with the error; `Commit()` itself doesn't
|
|
84
|
-
* throw, since the failed change already did. If a projection throws, the batch's changes
|
|
85
|
-
* and the error are posted the same way, then `Commit()` rethrows. Either way the next batch
|
|
86
|
-
* starts clean.
|
|
87
|
-
* @returns The diff that was posted.
|
|
88
|
-
* @throws A projection's error.
|
|
89
|
-
*/
|
|
90
|
-
Commit(): JsonDiffResult;
|
|
91
|
-
private Guard;
|
|
92
|
-
}
|
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
import { DiffTree } from "./diffTree";
|
|
2
|
-
import { DIFF_TREE_ERROR, DIFF_TREE_EVENT } from "./diffTree.types";
|
|
3
|
-
/**
|
|
4
|
-
* A `DiffTree` that pushes its changes to a `PushStore` instead of answering a store's
|
|
5
|
-
* requests. Use it inside a worker that produces data itself (for example, the result of a
|
|
6
|
-
* computation it runs): write with the usual `DiffTree` methods, then call `Commit()` to run
|
|
7
|
-
* projections and post everything since the last commit as one message.
|
|
8
|
-
*
|
|
9
|
-
* - Push only: don't pass a `DiffTreeWriter` to `ConnectWorkerToDiffTree`, which commits
|
|
10
|
-
* every batch itself and would post it to the wrong place.
|
|
11
|
-
* - After a change fails, every later change before `Commit()` throws that same error. The
|
|
12
|
-
* changes made before the failure are already in the tree, so `Commit()` still sends them,
|
|
13
|
-
* followed by a `DIFF_TREE_ERROR` message. Projections aren't run for that batch; the next
|
|
14
|
-
* `Commit()` brings them up to date.
|
|
15
|
-
* - A `PushStore` only receives the changes committed after it starts listening, so
|
|
16
|
-
* create it before the writer's first `Commit()`.
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* // engine-worker.ts
|
|
20
|
-
* const tree = new DiffTreeWriter(self, undefined, { "/": { projections } });
|
|
21
|
-
* tree.WritePath("results", computeResults());
|
|
22
|
-
* tree.Commit();
|
|
23
|
-
*
|
|
24
|
-
* // main thread
|
|
25
|
-
* const store = new PushStore(worker);
|
|
26
|
-
*
|
|
27
|
-
* @see PushStore
|
|
28
|
-
*/
|
|
29
|
-
export class DiffTreeWriter extends DiffTree {
|
|
30
|
-
/**
|
|
31
|
-
* Creates a DiffTreeWriter.
|
|
32
|
-
* @param worker Where `Commit()` posts its messages: the worker's `self`, or a `MessagePort`.
|
|
33
|
-
* On the same thread as the store, anything whose `postMessage` dispatches a `"message"`
|
|
34
|
-
* event on the store's `EventTarget`. The store clones every value it applies, so it never
|
|
35
|
-
* shares objects with the writer's tree.
|
|
36
|
-
* @param keyFunc Optional function to extract a key from objects. Pass the same key logic to
|
|
37
|
-
* the `PushStore` that receives the changes.
|
|
38
|
-
* @param routes Optional route definitions. Projections run once per `Commit()`, after the
|
|
39
|
-
* batch's changes.
|
|
40
|
-
*/
|
|
41
|
-
constructor(worker, keyFunc, routes) {
|
|
42
|
-
super(keyFunc, routes);
|
|
43
|
-
this.worker = worker;
|
|
44
|
-
this.uncommittedError = null;
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Not available on a DiffTreeWriter: `Commit()` runs projections once per batch, after its
|
|
48
|
-
* changes. Running them earlier would push a projection result computed from part of a batch.
|
|
49
|
-
* @throws Always.
|
|
50
|
-
*/
|
|
51
|
-
RunProjections() {
|
|
52
|
-
throw new Error("RunProjections is called during Commit()");
|
|
53
|
-
}
|
|
54
|
-
/**
|
|
55
|
-
* Writes a value at a path.
|
|
56
|
-
* @throws The change's error, or an earlier uncommitted change's error.
|
|
57
|
-
*/
|
|
58
|
-
WritePath(path, value) {
|
|
59
|
-
this.Guard(() => super.WritePath(path, value));
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Merges a patch into the current value at a path.
|
|
63
|
-
* @throws The change's error, or an earlier uncommitted change's error.
|
|
64
|
-
*/
|
|
65
|
-
PatchPath(path, patch) {
|
|
66
|
-
this.Guard(() => super.PatchPath(path, patch));
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Appends items to the array at a path.
|
|
70
|
-
* @throws The change's error, or an earlier uncommitted change's error.
|
|
71
|
-
*/
|
|
72
|
-
PushPath(path, items) {
|
|
73
|
-
this.Guard(() => super.PushPath(path, items));
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Splices the array at a path.
|
|
77
|
-
* @throws The change's error, or an earlier uncommitted change's error.
|
|
78
|
-
*/
|
|
79
|
-
SplicePath(path, start, deleteCount, items) {
|
|
80
|
-
this.Guard(() => super.SplicePath(path, start, deleteCount, items));
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Deletes a route, its child routes and its route instances.
|
|
84
|
-
* @throws The change's error, or an earlier uncommitted change's error.
|
|
85
|
-
*/
|
|
86
|
-
DeleteRoute(routePath) {
|
|
87
|
-
this.Guard(() => super.DeleteRoute(routePath));
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* Runs projections and posts every change since the last commit as one `DIFF_TREE_EVENT`
|
|
91
|
-
* message, which a `PushStore` applies as a unit. Posts nothing when nothing changed.
|
|
92
|
-
*
|
|
93
|
-
* If a change in the batch failed, projections are skipped, the applied changes are still
|
|
94
|
-
* posted, and a `DIFF_TREE_ERROR` message follows with the error; `Commit()` itself doesn't
|
|
95
|
-
* throw, since the failed change already did. If a projection throws, the batch's changes
|
|
96
|
-
* and the error are posted the same way, then `Commit()` rethrows. Either way the next batch
|
|
97
|
-
* starts clean.
|
|
98
|
-
* @returns The diff that was posted.
|
|
99
|
-
* @throws A projection's error.
|
|
100
|
-
*/
|
|
101
|
-
Commit() {
|
|
102
|
-
let diff = [];
|
|
103
|
-
try {
|
|
104
|
-
!this.uncommittedError && super.RunProjections();
|
|
105
|
-
}
|
|
106
|
-
catch (err) {
|
|
107
|
-
this.uncommittedError = err;
|
|
108
|
-
throw err;
|
|
109
|
-
}
|
|
110
|
-
finally {
|
|
111
|
-
diff = super.Commit();
|
|
112
|
-
diff.length > 0 && this.worker.postMessage({
|
|
113
|
-
type: DIFF_TREE_EVENT,
|
|
114
|
-
data: diff
|
|
115
|
-
});
|
|
116
|
-
if (this.uncommittedError) {
|
|
117
|
-
this.worker.postMessage({
|
|
118
|
-
type: DIFF_TREE_ERROR,
|
|
119
|
-
data: this.uncommittedError
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
this.uncommittedError = null;
|
|
123
|
-
}
|
|
124
|
-
return diff;
|
|
125
|
-
}
|
|
126
|
-
Guard(change) {
|
|
127
|
-
if (this.uncommittedError)
|
|
128
|
-
throw this.uncommittedError;
|
|
129
|
-
try {
|
|
130
|
-
change();
|
|
131
|
-
}
|
|
132
|
-
catch (err) {
|
|
133
|
-
this.uncommittedError = err;
|
|
134
|
-
throw err;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { List } from "../../Utils/list";
|
|
2
|
-
export class WorkerQueue {
|
|
3
|
-
constructor(worker) {
|
|
4
|
-
this.worker = worker;
|
|
5
|
-
this.callbacks = List.Create();
|
|
6
|
-
this.worker.onerror = (err) => {
|
|
7
|
-
const cb = List.Pop(this.callbacks);
|
|
8
|
-
cb && cb(null, err);
|
|
9
|
-
};
|
|
10
|
-
this.worker.onmessage = (message) => {
|
|
11
|
-
const cb = List.Pop(this.callbacks);
|
|
12
|
-
cb && cb(message.data);
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
Push(message) {
|
|
16
|
-
return new Promise((resolve, reject) => {
|
|
17
|
-
List.Add(this.callbacks, function (data, err) {
|
|
18
|
-
if (err)
|
|
19
|
-
reject(err);
|
|
20
|
-
else
|
|
21
|
-
resolve(data);
|
|
22
|
-
});
|
|
23
|
-
this.worker.postMessage(message);
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
Destroy() {
|
|
27
|
-
this.worker.terminate();
|
|
28
|
-
}
|
|
29
|
-
}
|