j-templates 5.0.33 → 5.0.36

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.
@@ -24,6 +24,7 @@ export declare class Component<D = void, T = void, E = void> {
24
24
  }
25
25
  export declare namespace Component {
26
26
  function ToFunction<D = void, T = void, E = void>(type: any, namespace: any, constructor: ComponentConstructor<D, T, E>): import("./componentNode.types").ComponentNodeFunction<D, T, E>;
27
+ function Register<D = void, T = void, E = void>(name: string, constructor: ComponentConstructor<D, T, E>): void;
27
28
  function Attach(node: Node, nodeRef: NodeRefTypes): void;
28
29
  }
29
30
  export declare type ComponentConstructor<D, T, E> = {
package/Node/component.js CHANGED
@@ -52,6 +52,19 @@ exports.Component = Component;
52
52
  return componentNode_1.ComponentNode.ToFunction(type, namespace, constructor);
53
53
  }
54
54
  Component.ToFunction = ToFunction;
55
+ function Register(name, constructor) {
56
+ const componentFunction = ToFunction(`${name}-component`, undefined, constructor);
57
+ class WebComponent extends HTMLElement {
58
+ constructor() {
59
+ super();
60
+ const shadowRoot = this.attachShadow({ mode: 'open' });
61
+ const node = componentFunction({});
62
+ Attach(shadowRoot, node);
63
+ }
64
+ }
65
+ customElements.define(name, WebComponent);
66
+ }
67
+ Component.Register = Register;
55
68
  function Attach(node, nodeRef) {
56
69
  nodeRef_1.NodeRef.Init(nodeRef);
57
70
  var rootRef = nodeRef_1.NodeRef.Wrap(node);
@@ -12,5 +12,6 @@ export declare class DiffAsync {
12
12
  value: any;
13
13
  }>): Promise<IDiffResponse>;
14
14
  UpdatePath(path: string, value: any): Promise<void>;
15
+ GetPath(path: string): Promise<any>;
15
16
  Destroy(): void;
16
17
  }
@@ -24,6 +24,9 @@ class DiffAsync {
24
24
  async UpdatePath(path, value) {
25
25
  await this.workerQueue.Push({ method: "updatepath", arguments: [path, value] });
26
26
  }
27
+ async GetPath(path) {
28
+ return await this.workerQueue.Push({ method: "getpath", arguments: [path] });
29
+ }
27
30
  Destroy() {
28
31
  this.workerQueue.Destroy();
29
32
  }
@@ -1,14 +1,11 @@
1
1
  export interface IDiffMethod {
2
- method: "create" | "diffpath" | "diffbatch" | "updatepath";
2
+ method: "create" | "diffpath" | "diffbatch" | "updatepath" | "getpath";
3
3
  arguments: Array<any>;
4
4
  }
5
- export interface IDiffResponse {
6
- changedPaths: Array<{
7
- path: string;
8
- value: any;
9
- }>;
10
- deletedPaths: Array<string>;
11
- }
5
+ export declare type IDiffResponse = {
6
+ path: string;
7
+ value: any;
8
+ }[];
12
9
  export interface IDiffTree {
13
10
  DiffBatch(data: Array<{
14
11
  path: string;
@@ -24,6 +24,10 @@ function DiffTreeScope(worker) {
24
24
  diffTree.UpdatePath(data.arguments[0], data.arguments[1]);
25
25
  ctx.postMessage(null);
26
26
  break;
27
+ case "getpath":
28
+ var ret = diffTree.GetPath(data.arguments[0]);
29
+ ctx.postMessage(ret);
30
+ break;
27
31
  }
28
32
  };
29
33
  }
@@ -50,43 +54,43 @@ function DiffTreeScope(worker) {
50
54
  return matches[1];
51
55
  }
52
56
  DiffBatch(data) {
53
- var resp = {
54
- changedPaths: [],
55
- deletedPaths: []
56
- };
57
+ var resp = [];
57
58
  ;
58
59
  for (var x = 0; x < data.length; x++)
59
60
  this.RunDiff(data[x].path, data[x].value, resp);
60
61
  return resp;
61
62
  }
62
63
  DiffPath(path, value) {
63
- var resp = {
64
- changedPaths: [],
65
- deletedPaths: []
66
- };
64
+ var resp = [];
67
65
  this.RunDiff(path, value, resp);
68
66
  return resp;
69
67
  }
70
68
  UpdatePath(path, value) {
71
69
  this.SetPathValue(path, value);
72
70
  }
71
+ GetPath(path) {
72
+ return this.GetPathValue(path);
73
+ }
73
74
  RunDiff(path, value, diffResp) {
74
75
  var breakupMap = this.GetBreakUpMap(path, value);
75
- var resp = diffResp || {
76
- changedPaths: [],
77
- deletedPaths: []
78
- };
76
+ var resp = diffResp || [];
79
77
  breakupMap.forEach((value, key) => {
80
78
  var currentValue = key.split(".").reduce((pre, curr, index) => {
81
79
  if (index === 0)
82
80
  return this.rootStateMap.get(curr);
83
81
  return pre && pre[curr];
84
82
  }, null);
85
- this.DiffValues(key, value, currentValue, resp);
86
- });
87
- resp.changedPaths.forEach(val => {
88
- this.SetPathValue(val.path, val.value);
83
+ this.DiffJson(key, value, currentValue, resp);
89
84
  });
85
+ for (var x = 0; x < resp.length; x++)
86
+ this.SetPathValue(resp[x].path, resp[x].value);
87
+ }
88
+ GetPathValue(path) {
89
+ var parts = path.split(".");
90
+ var curr = this.rootStateMap.get(parts[0]);
91
+ for (var x = 1; x < parts.length; x++)
92
+ curr = curr && curr[parts[x]];
93
+ return curr;
90
94
  }
91
95
  SetPathValue(path, value) {
92
96
  var parts = path.split(".");
@@ -129,61 +133,79 @@ function DiffTreeScope(worker) {
129
133
  map.set(path, key === path ? value : keyRef || value);
130
134
  return map;
131
135
  }
132
- DiffValues(path, newValue, oldValue, resp) {
133
- var changedPathLength = resp.changedPaths.length;
134
- var oldIsValue = IsValue(oldValue);
135
- if (oldIsValue) {
136
+ DiffJson(path, newValue, oldValue, resp) {
137
+ const oldIsValue = IsValue(oldValue);
138
+ const newIsValue = IsValue(newValue);
139
+ if (oldIsValue || newIsValue) {
136
140
  if (oldValue !== newValue) {
137
- resp.changedPaths.push({
138
- path: path,
141
+ resp.push({
142
+ path,
139
143
  value: newValue
140
144
  });
141
145
  return true;
142
146
  }
143
147
  return false;
144
148
  }
145
- var oldKeys = Array.isArray(oldValue) ? null : Object.keys(oldValue);
146
- var newIsValue = IsValue(newValue);
147
- if (newIsValue) {
148
- resp.changedPaths.push({
149
- path: path,
149
+ let allChildrenChanged = true;
150
+ let childDeleted = false;
151
+ const oldIsArray = Array.isArray(oldValue);
152
+ const newIsArray = Array.isArray(newValue);
153
+ if (oldIsArray !== newIsArray) {
154
+ resp.push({
155
+ path,
150
156
  value: newValue
151
157
  });
152
- for (var x = 0; x < (oldKeys || oldValue).length; x++)
153
- resp.deletedPaths.push(`${path}.${oldKeys ? oldKeys[x] : x}`);
154
158
  return true;
155
159
  }
156
- var deleted = false;
157
- var allChanged = true;
158
- var newKeys = Object.keys(newValue);
159
- var newKeysSet = new Set(newKeys);
160
- for (var x = 0; x < (oldKeys || oldValue).length; x++) {
161
- var oldKey = oldKeys ? oldKeys[x] : x.toString();
162
- var childPath = `${path}.${oldKey}`;
163
- var stays = newKeysSet.delete(oldKey);
164
- if (stays)
165
- allChanged = this.DiffValues(childPath, newValue[oldKey], oldValue[oldKey], resp) && allChanged;
166
- else {
167
- deleted = true;
168
- resp.deletedPaths.push(childPath);
160
+ const changedPathLength = resp.length;
161
+ if (oldIsArray && newIsArray) {
162
+ if (oldValue.length === 0 && newValue.length === 0)
163
+ return false;
164
+ for (let y = 0; y < newValue.length; y++) {
165
+ const arrayPath = path ? `${path}.${y}` : `${y}`;
166
+ allChildrenChanged = this.DiffJson(arrayPath, newValue[y], oldValue[y], resp) && allChildrenChanged;
169
167
  }
168
+ if (!allChildrenChanged && newValue.length < oldValue.length)
169
+ resp.push({
170
+ path: path ? `${path}.length` : 'length',
171
+ value: newValue.length
172
+ });
170
173
  }
171
- if (allChanged || deleted) {
172
- resp.changedPaths.splice(changedPathLength);
173
- resp.changedPaths.push({
174
- path: path,
175
- value: newValue
176
- });
177
- return true;
178
- }
179
- else if (newKeysSet.size > 0) {
180
- newKeysSet.forEach(function (key) {
181
- resp.changedPaths.push({
182
- path: `${path}.${key}`,
174
+ else {
175
+ const oldKeys = Reflect.ownKeys(oldValue);
176
+ const newKeys = Reflect.ownKeys(newValue);
177
+ if (oldKeys && oldKeys.length === 0 && newKeys.length === 0)
178
+ return false;
179
+ const newKeysSet = new Set(newKeys);
180
+ for (let x = 0; x < oldKeys.length && !childDeleted; x++) {
181
+ const oldKey = oldKeys[x];
182
+ const childPath = path ? `${path}.${oldKey}` : `${oldKey}`;
183
+ if (newKeysSet.delete(oldKey))
184
+ allChildrenChanged = this.DiffJson(childPath, newValue[oldKey], oldValue[oldKey], resp) && allChildrenChanged;
185
+ else if (path)
186
+ childDeleted = true;
187
+ else
188
+ resp.push({
189
+ path: childPath,
190
+ value: undefined
191
+ });
192
+ }
193
+ newKeysSet.forEach(key => {
194
+ const childPath = path ? `${path}.${key}` : `${key}`;
195
+ resp.push({
196
+ path: childPath,
183
197
  value: newValue[key]
184
198
  });
185
199
  });
186
200
  }
201
+ if (path && (allChildrenChanged || childDeleted)) {
202
+ resp.splice(changedPathLength);
203
+ resp.push({
204
+ path,
205
+ value: newValue
206
+ });
207
+ return true;
208
+ }
187
209
  return false;
188
210
  }
189
211
  }
@@ -4,6 +4,7 @@ export declare class StoreAsync {
4
4
  private diffAsync;
5
5
  private observableTree;
6
6
  private asyncWriter;
7
+ private asyncQueue;
7
8
  constructor(idFunc: {
8
9
  (val: any): string;
9
10
  }, init?: any);
@@ -3,12 +3,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const observableTree_1 = require("../Tree/observableTree");
4
4
  const diffAsync_1 = require("../Diff/diffAsync");
5
5
  const storeAsyncWriter_1 = require("./storeAsyncWriter");
6
+ const asyncQueue_1 = require("../../Utils/asyncQueue");
6
7
  class StoreAsync {
7
8
  constructor(idFunc, init) {
8
9
  this.idFunc = idFunc;
9
10
  this.diffAsync = new diffAsync_1.DiffAsync(this.idFunc);
10
11
  this.observableTree = new observableTree_1.ObservableTree(diffAsync_1.DiffAsync.ReadKeyRef);
11
12
  this.asyncWriter = new storeAsyncWriter_1.StoreAsyncWriter(this.idFunc, this.diffAsync, this.observableTree);
13
+ this.asyncQueue = new asyncQueue_1.AsyncQueue();
12
14
  if (init) {
13
15
  var id = this.idFunc(init);
14
16
  this.observableTree.Write(id, init);
@@ -19,10 +21,12 @@ class StoreAsync {
19
21
  return this.observableTree.Scope(id, func);
20
22
  }
21
23
  async Action(id, action) {
22
- var node;
23
- if (id)
24
- node = this.observableTree.GetNode(id);
25
- await action(node && node.Proxy, this.asyncWriter);
24
+ await this.asyncQueue.Next(async () => {
25
+ var node;
26
+ if (id)
27
+ node = this.observableTree.GetNode(id);
28
+ await action(node && node.Proxy, this.asyncWriter);
29
+ });
26
30
  }
27
31
  async Write(data) {
28
32
  await this.Action(null, async (val, writer) => {
@@ -35,6 +39,7 @@ class StoreAsync {
35
39
  });
36
40
  }
37
41
  Destroy() {
42
+ this.asyncQueue.Stop();
38
43
  this.diffAsync.Destroy();
39
44
  this.observableTree.Destroy();
40
45
  }
@@ -29,26 +29,24 @@ class StoreAsyncWriter {
29
29
  this.ApplyChanges(diff);
30
30
  }
31
31
  async Push(source, data) {
32
- var array = source;
33
32
  var proxy = source;
34
33
  var rootPath = proxy.___node.Path;
35
- var length = array.length;
34
+ var lengthPath = `${rootPath}.length`;
35
+ var length = await this.diffAsync.GetPath(lengthPath);
36
36
  var diff = await this.diffAsync.DiffPath(`${rootPath}.${length}`, data);
37
37
  this.ApplyChanges(diff);
38
38
  }
39
39
  async Splice(source, start, deleteCount, ...items) {
40
40
  var proxy = source;
41
41
  var rootPath = proxy.___node.Path;
42
- var array = this.observableTree.Get(rootPath);
42
+ var array = await this.diffAsync.GetPath(rootPath);
43
43
  array = array.slice();
44
44
  array.splice(start, deleteCount, ...items);
45
45
  var diff = await this.diffAsync.DiffPath(rootPath, array);
46
46
  this.ApplyChanges(diff);
47
47
  }
48
48
  ApplyChanges(diff) {
49
- for (var x = 0; x < diff.deletedPaths.length; x++)
50
- this.observableTree.Delete(diff.deletedPaths[x]);
51
- this.observableTree.WriteAll(diff.changedPaths);
49
+ this.observableTree.WriteAll(diff);
52
50
  }
53
51
  }
54
52
  exports.StoreAsyncWriter = StoreAsyncWriter;
@@ -37,9 +37,7 @@ class StoreSyncWriter {
37
37
  return proxy.___node.Splice(start, deleteCount, ...items);
38
38
  }
39
39
  ApplyChanges(diff) {
40
- for (var x = 0; x < diff.deletedPaths.length; x++)
41
- this.observableTree.Delete(diff.deletedPaths[x]);
42
- this.observableTree.WriteAll(diff.changedPaths);
40
+ this.observableTree.WriteAll(diff);
43
41
  }
44
42
  }
45
43
  exports.StoreSyncWriter = StoreSyncWriter;
@@ -10,6 +10,7 @@ export declare class ObservableNode {
10
10
  private path;
11
11
  private scope;
12
12
  private arrayScope;
13
+ get Key(): string;
13
14
  get Path(): string;
14
15
  get Value(): any;
15
16
  get Type(): Type;
@@ -29,6 +29,9 @@ class ObservableNode {
29
29
  };
30
30
  });
31
31
  }
32
+ get Key() {
33
+ return this.key;
34
+ }
32
35
  get Path() {
33
36
  if (this.path === undefined)
34
37
  this.path = (this.parent ? this.parent.Path + "." : "") + this.key;
@@ -16,7 +16,7 @@ export declare class ObservableTree {
16
16
  GetNode(path: string): ObservableNode;
17
17
  Delete(path: string): void;
18
18
  Destroy(): void;
19
- Scope<O, R>(path: string, func?: {
19
+ Scope<O, R = O>(path: string, func?: {
20
20
  (val: O): R;
21
21
  }): ObservableScope<R>;
22
22
  private WritePath;
@@ -74,8 +74,18 @@ class ObservableTree {
74
74
  UpdatePathNode(path) {
75
75
  var node = this.GetNode(path);
76
76
  node.Update();
77
- if (node.Parent && node.Parent.Type === observableProxy_1.Type.Array)
77
+ if (node.Parent && node.Parent.Type === observableProxy_1.Type.Array) {
78
78
  node.Parent.ArrayUpdate();
79
+ if (node.Key === 'length') {
80
+ var index = node.Value;
81
+ var childNode = node.Parent.Children.get(index.toString());
82
+ while (childNode) {
83
+ childNode.Destroy();
84
+ index++;
85
+ childNode = node.Parent.Children.get(index.toString());
86
+ }
87
+ }
88
+ }
79
89
  }
80
90
  }
81
91
  exports.ObservableTree = ObservableTree;
@@ -0,0 +1,8 @@
1
+ export declare class AsyncQueue {
2
+ private running;
3
+ private queue;
4
+ Next<T>(callback: () => Promise<T>): Promise<T>;
5
+ Stop(): void;
6
+ private Start;
7
+ private ExecuteQueue;
8
+ }
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const list_1 = require("./list");
4
+ class AsyncQueue {
5
+ constructor() {
6
+ this.running = false;
7
+ this.queue = list_1.List.Create();
8
+ }
9
+ Next(callback) {
10
+ const ret = new Promise(async function (resolve, reject) {
11
+ list_1.List.Add(this.queue, async function () {
12
+ try {
13
+ const ret = await callback();
14
+ resolve(ret);
15
+ }
16
+ catch (e) {
17
+ reject(e);
18
+ }
19
+ });
20
+ });
21
+ this.Start();
22
+ return ret;
23
+ }
24
+ Stop() {
25
+ list_1.List.Clear(this.queue);
26
+ }
27
+ Start() {
28
+ if (this.running)
29
+ return;
30
+ this.running = true;
31
+ this.ExecuteQueue();
32
+ }
33
+ async ExecuteQueue() {
34
+ const callback = list_1.List.Pop(this.queue);
35
+ if (callback !== null) {
36
+ await callback();
37
+ this.ExecuteQueue();
38
+ }
39
+ else
40
+ this.running = false;
41
+ }
42
+ }
43
+ exports.AsyncQueue = AsyncQueue;