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.
package/jTemplates.js CHANGED
@@ -564,6 +564,19 @@ exports.Component = Component;
564
564
  return componentNode_1.ComponentNode.ToFunction(type, namespace, constructor);
565
565
  }
566
566
  Component.ToFunction = ToFunction;
567
+ function Register(name, constructor) {
568
+ const componentFunction = ToFunction(`${name}-component`, undefined, constructor);
569
+ class WebComponent extends HTMLElement {
570
+ constructor() {
571
+ super();
572
+ const shadowRoot = this.attachShadow({ mode: 'open' });
573
+ const node = componentFunction({});
574
+ Attach(shadowRoot, node);
575
+ }
576
+ }
577
+ customElements.define(name, WebComponent);
578
+ }
579
+ Component.Register = Register;
567
580
  function Attach(node, nodeRef) {
568
581
  nodeRef_1.NodeRef.Init(nodeRef);
569
582
  var rootRef = nodeRef_1.NodeRef.Wrap(node);
@@ -1096,6 +1109,9 @@ class DiffAsync {
1096
1109
  async UpdatePath(path, value) {
1097
1110
  await this.workerQueue.Push({ method: "updatepath", arguments: [path, value] });
1098
1111
  }
1112
+ async GetPath(path) {
1113
+ return await this.workerQueue.Push({ method: "getpath", arguments: [path] });
1114
+ }
1099
1115
  Destroy() {
1100
1116
  this.workerQueue.Destroy();
1101
1117
  }
@@ -1176,6 +1192,10 @@ function DiffTreeScope(worker) {
1176
1192
  diffTree.UpdatePath(data.arguments[0], data.arguments[1]);
1177
1193
  ctx.postMessage(null);
1178
1194
  break;
1195
+ case "getpath":
1196
+ var ret = diffTree.GetPath(data.arguments[0]);
1197
+ ctx.postMessage(ret);
1198
+ break;
1179
1199
  }
1180
1200
  };
1181
1201
  }
@@ -1202,43 +1222,43 @@ function DiffTreeScope(worker) {
1202
1222
  return matches[1];
1203
1223
  }
1204
1224
  DiffBatch(data) {
1205
- var resp = {
1206
- changedPaths: [],
1207
- deletedPaths: []
1208
- };
1225
+ var resp = [];
1209
1226
  ;
1210
1227
  for (var x = 0; x < data.length; x++)
1211
1228
  this.RunDiff(data[x].path, data[x].value, resp);
1212
1229
  return resp;
1213
1230
  }
1214
1231
  DiffPath(path, value) {
1215
- var resp = {
1216
- changedPaths: [],
1217
- deletedPaths: []
1218
- };
1232
+ var resp = [];
1219
1233
  this.RunDiff(path, value, resp);
1220
1234
  return resp;
1221
1235
  }
1222
1236
  UpdatePath(path, value) {
1223
1237
  this.SetPathValue(path, value);
1224
1238
  }
1239
+ GetPath(path) {
1240
+ return this.GetPathValue(path);
1241
+ }
1225
1242
  RunDiff(path, value, diffResp) {
1226
1243
  var breakupMap = this.GetBreakUpMap(path, value);
1227
- var resp = diffResp || {
1228
- changedPaths: [],
1229
- deletedPaths: []
1230
- };
1244
+ var resp = diffResp || [];
1231
1245
  breakupMap.forEach((value, key) => {
1232
1246
  var currentValue = key.split(".").reduce((pre, curr, index) => {
1233
1247
  if (index === 0)
1234
1248
  return this.rootStateMap.get(curr);
1235
1249
  return pre && pre[curr];
1236
1250
  }, null);
1237
- this.DiffValues(key, value, currentValue, resp);
1238
- });
1239
- resp.changedPaths.forEach(val => {
1240
- this.SetPathValue(val.path, val.value);
1251
+ this.DiffJson(key, value, currentValue, resp);
1241
1252
  });
1253
+ for (var x = 0; x < resp.length; x++)
1254
+ this.SetPathValue(resp[x].path, resp[x].value);
1255
+ }
1256
+ GetPathValue(path) {
1257
+ var parts = path.split(".");
1258
+ var curr = this.rootStateMap.get(parts[0]);
1259
+ for (var x = 1; x < parts.length; x++)
1260
+ curr = curr && curr[parts[x]];
1261
+ return curr;
1242
1262
  }
1243
1263
  SetPathValue(path, value) {
1244
1264
  var parts = path.split(".");
@@ -1281,61 +1301,79 @@ function DiffTreeScope(worker) {
1281
1301
  map.set(path, key === path ? value : keyRef || value);
1282
1302
  return map;
1283
1303
  }
1284
- DiffValues(path, newValue, oldValue, resp) {
1285
- var changedPathLength = resp.changedPaths.length;
1286
- var oldIsValue = IsValue(oldValue);
1287
- if (oldIsValue) {
1304
+ DiffJson(path, newValue, oldValue, resp) {
1305
+ const oldIsValue = IsValue(oldValue);
1306
+ const newIsValue = IsValue(newValue);
1307
+ if (oldIsValue || newIsValue) {
1288
1308
  if (oldValue !== newValue) {
1289
- resp.changedPaths.push({
1290
- path: path,
1309
+ resp.push({
1310
+ path,
1291
1311
  value: newValue
1292
1312
  });
1293
1313
  return true;
1294
1314
  }
1295
1315
  return false;
1296
1316
  }
1297
- var oldKeys = Array.isArray(oldValue) ? null : Object.keys(oldValue);
1298
- var newIsValue = IsValue(newValue);
1299
- if (newIsValue) {
1300
- resp.changedPaths.push({
1301
- path: path,
1317
+ let allChildrenChanged = true;
1318
+ let childDeleted = false;
1319
+ const oldIsArray = Array.isArray(oldValue);
1320
+ const newIsArray = Array.isArray(newValue);
1321
+ if (oldIsArray !== newIsArray) {
1322
+ resp.push({
1323
+ path,
1302
1324
  value: newValue
1303
1325
  });
1304
- for (var x = 0; x < (oldKeys || oldValue).length; x++)
1305
- resp.deletedPaths.push(`${path}.${oldKeys ? oldKeys[x] : x}`);
1306
1326
  return true;
1307
1327
  }
1308
- var deleted = false;
1309
- var allChanged = true;
1310
- var newKeys = Object.keys(newValue);
1311
- var newKeysSet = new Set(newKeys);
1312
- for (var x = 0; x < (oldKeys || oldValue).length; x++) {
1313
- var oldKey = oldKeys ? oldKeys[x] : x.toString();
1314
- var childPath = `${path}.${oldKey}`;
1315
- var stays = newKeysSet.delete(oldKey);
1316
- if (stays)
1317
- allChanged = this.DiffValues(childPath, newValue[oldKey], oldValue[oldKey], resp) && allChanged;
1318
- else {
1319
- deleted = true;
1320
- resp.deletedPaths.push(childPath);
1328
+ const changedPathLength = resp.length;
1329
+ if (oldIsArray && newIsArray) {
1330
+ if (oldValue.length === 0 && newValue.length === 0)
1331
+ return false;
1332
+ for (let y = 0; y < newValue.length; y++) {
1333
+ const arrayPath = path ? `${path}.${y}` : `${y}`;
1334
+ allChildrenChanged = this.DiffJson(arrayPath, newValue[y], oldValue[y], resp) && allChildrenChanged;
1321
1335
  }
1336
+ if (!allChildrenChanged && newValue.length < oldValue.length)
1337
+ resp.push({
1338
+ path: path ? `${path}.length` : 'length',
1339
+ value: newValue.length
1340
+ });
1322
1341
  }
1323
- if (allChanged || deleted) {
1324
- resp.changedPaths.splice(changedPathLength);
1325
- resp.changedPaths.push({
1326
- path: path,
1327
- value: newValue
1328
- });
1329
- return true;
1330
- }
1331
- else if (newKeysSet.size > 0) {
1332
- newKeysSet.forEach(function (key) {
1333
- resp.changedPaths.push({
1334
- path: `${path}.${key}`,
1342
+ else {
1343
+ const oldKeys = Reflect.ownKeys(oldValue);
1344
+ const newKeys = Reflect.ownKeys(newValue);
1345
+ if (oldKeys && oldKeys.length === 0 && newKeys.length === 0)
1346
+ return false;
1347
+ const newKeysSet = new Set(newKeys);
1348
+ for (let x = 0; x < oldKeys.length && !childDeleted; x++) {
1349
+ const oldKey = oldKeys[x];
1350
+ const childPath = path ? `${path}.${oldKey}` : `${oldKey}`;
1351
+ if (newKeysSet.delete(oldKey))
1352
+ allChildrenChanged = this.DiffJson(childPath, newValue[oldKey], oldValue[oldKey], resp) && allChildrenChanged;
1353
+ else if (path)
1354
+ childDeleted = true;
1355
+ else
1356
+ resp.push({
1357
+ path: childPath,
1358
+ value: undefined
1359
+ });
1360
+ }
1361
+ newKeysSet.forEach(key => {
1362
+ const childPath = path ? `${path}.${key}` : `${key}`;
1363
+ resp.push({
1364
+ path: childPath,
1335
1365
  value: newValue[key]
1336
1366
  });
1337
1367
  });
1338
1368
  }
1369
+ if (path && (allChildrenChanged || childDeleted)) {
1370
+ resp.splice(changedPathLength);
1371
+ resp.push({
1372
+ path,
1373
+ value: newValue
1374
+ });
1375
+ return true;
1376
+ }
1339
1377
  return false;
1340
1378
  }
1341
1379
  }
@@ -1476,12 +1514,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
1476
1514
  const observableTree_1 = __webpack_require__(/*! ../Tree/observableTree */ "./src/Store/Tree/observableTree.ts");
1477
1515
  const diffAsync_1 = __webpack_require__(/*! ../Diff/diffAsync */ "./src/Store/Diff/diffAsync.ts");
1478
1516
  const storeAsyncWriter_1 = __webpack_require__(/*! ./storeAsyncWriter */ "./src/Store/Store/storeAsyncWriter.ts");
1517
+ const asyncQueue_1 = __webpack_require__(/*! ../../Utils/asyncQueue */ "./src/Utils/asyncQueue.ts");
1479
1518
  class StoreAsync {
1480
1519
  constructor(idFunc, init) {
1481
1520
  this.idFunc = idFunc;
1482
1521
  this.diffAsync = new diffAsync_1.DiffAsync(this.idFunc);
1483
1522
  this.observableTree = new observableTree_1.ObservableTree(diffAsync_1.DiffAsync.ReadKeyRef);
1484
1523
  this.asyncWriter = new storeAsyncWriter_1.StoreAsyncWriter(this.idFunc, this.diffAsync, this.observableTree);
1524
+ this.asyncQueue = new asyncQueue_1.AsyncQueue();
1485
1525
  if (init) {
1486
1526
  var id = this.idFunc(init);
1487
1527
  this.observableTree.Write(id, init);
@@ -1492,10 +1532,12 @@ class StoreAsync {
1492
1532
  return this.observableTree.Scope(id, func);
1493
1533
  }
1494
1534
  async Action(id, action) {
1495
- var node;
1496
- if (id)
1497
- node = this.observableTree.GetNode(id);
1498
- await action(node && node.Proxy, this.asyncWriter);
1535
+ await this.asyncQueue.Next(async () => {
1536
+ var node;
1537
+ if (id)
1538
+ node = this.observableTree.GetNode(id);
1539
+ await action(node && node.Proxy, this.asyncWriter);
1540
+ });
1499
1541
  }
1500
1542
  async Write(data) {
1501
1543
  await this.Action(null, async (val, writer) => {
@@ -1508,6 +1550,7 @@ class StoreAsync {
1508
1550
  });
1509
1551
  }
1510
1552
  Destroy() {
1553
+ this.asyncQueue.Stop();
1511
1554
  this.diffAsync.Destroy();
1512
1555
  this.observableTree.Destroy();
1513
1556
  }
@@ -1556,26 +1599,24 @@ class StoreAsyncWriter {
1556
1599
  this.ApplyChanges(diff);
1557
1600
  }
1558
1601
  async Push(source, data) {
1559
- var array = source;
1560
1602
  var proxy = source;
1561
1603
  var rootPath = proxy.___node.Path;
1562
- var length = array.length;
1604
+ var lengthPath = `${rootPath}.length`;
1605
+ var length = await this.diffAsync.GetPath(lengthPath);
1563
1606
  var diff = await this.diffAsync.DiffPath(`${rootPath}.${length}`, data);
1564
1607
  this.ApplyChanges(diff);
1565
1608
  }
1566
1609
  async Splice(source, start, deleteCount, ...items) {
1567
1610
  var proxy = source;
1568
1611
  var rootPath = proxy.___node.Path;
1569
- var array = this.observableTree.Get(rootPath);
1612
+ var array = await this.diffAsync.GetPath(rootPath);
1570
1613
  array = array.slice();
1571
1614
  array.splice(start, deleteCount, ...items);
1572
1615
  var diff = await this.diffAsync.DiffPath(rootPath, array);
1573
1616
  this.ApplyChanges(diff);
1574
1617
  }
1575
1618
  ApplyChanges(diff) {
1576
- for (var x = 0; x < diff.deletedPaths.length; x++)
1577
- this.observableTree.Delete(diff.deletedPaths[x]);
1578
- this.observableTree.WriteAll(diff.changedPaths);
1619
+ this.observableTree.WriteAll(diff);
1579
1620
  }
1580
1621
  }
1581
1622
  exports.StoreAsyncWriter = StoreAsyncWriter;
@@ -1675,9 +1716,7 @@ class StoreSyncWriter {
1675
1716
  return proxy.___node.Splice(start, deleteCount, ...items);
1676
1717
  }
1677
1718
  ApplyChanges(diff) {
1678
- for (var x = 0; x < diff.deletedPaths.length; x++)
1679
- this.observableTree.Delete(diff.deletedPaths[x]);
1680
- this.observableTree.WriteAll(diff.changedPaths);
1719
+ this.observableTree.WriteAll(diff);
1681
1720
  }
1682
1721
  }
1683
1722
  exports.StoreSyncWriter = StoreSyncWriter;
@@ -1767,6 +1806,9 @@ class ObservableNode {
1767
1806
  };
1768
1807
  });
1769
1808
  }
1809
+ get Key() {
1810
+ return this.key;
1811
+ }
1770
1812
  get Path() {
1771
1813
  if (this.path === undefined)
1772
1814
  this.path = (this.parent ? this.parent.Path + "." : "") + this.key;
@@ -2273,8 +2315,18 @@ class ObservableTree {
2273
2315
  UpdatePathNode(path) {
2274
2316
  var node = this.GetNode(path);
2275
2317
  node.Update();
2276
- if (node.Parent && node.Parent.Type === observableProxy_1.Type.Array)
2318
+ if (node.Parent && node.Parent.Type === observableProxy_1.Type.Array) {
2277
2319
  node.Parent.ArrayUpdate();
2320
+ if (node.Key === 'length') {
2321
+ var index = node.Value;
2322
+ var childNode = node.Parent.Children.get(index.toString());
2323
+ while (childNode) {
2324
+ childNode.Destroy();
2325
+ index++;
2326
+ childNode = node.Parent.Children.get(index.toString());
2327
+ }
2328
+ }
2329
+ }
2278
2330
  }
2279
2331
  }
2280
2332
  exports.ObservableTree = ObservableTree;
@@ -2412,6 +2464,61 @@ class Animation {
2412
2464
  exports.Animation = Animation;
2413
2465
 
2414
2466
 
2467
+ /***/ }),
2468
+
2469
+ /***/ "./src/Utils/asyncQueue.ts":
2470
+ /*!*********************************!*\
2471
+ !*** ./src/Utils/asyncQueue.ts ***!
2472
+ \*********************************/
2473
+ /*! no static exports found */
2474
+ /***/ (function(module, exports, __webpack_require__) {
2475
+
2476
+ "use strict";
2477
+
2478
+ Object.defineProperty(exports, "__esModule", { value: true });
2479
+ const list_1 = __webpack_require__(/*! ./list */ "./src/Utils/list.ts");
2480
+ class AsyncQueue {
2481
+ constructor() {
2482
+ this.running = false;
2483
+ this.queue = list_1.List.Create();
2484
+ }
2485
+ Next(callback) {
2486
+ const ret = new Promise(async function (resolve, reject) {
2487
+ list_1.List.Add(this.queue, async function () {
2488
+ try {
2489
+ const ret = await callback();
2490
+ resolve(ret);
2491
+ }
2492
+ catch (e) {
2493
+ reject(e);
2494
+ }
2495
+ });
2496
+ });
2497
+ this.Start();
2498
+ return ret;
2499
+ }
2500
+ Stop() {
2501
+ list_1.List.Clear(this.queue);
2502
+ }
2503
+ Start() {
2504
+ if (this.running)
2505
+ return;
2506
+ this.running = true;
2507
+ this.ExecuteQueue();
2508
+ }
2509
+ async ExecuteQueue() {
2510
+ const callback = list_1.List.Pop(this.queue);
2511
+ if (callback !== null) {
2512
+ await callback();
2513
+ this.ExecuteQueue();
2514
+ }
2515
+ else
2516
+ this.running = false;
2517
+ }
2518
+ }
2519
+ exports.AsyncQueue = AsyncQueue;
2520
+
2521
+
2415
2522
  /***/ }),
2416
2523
 
2417
2524
  /***/ "./src/Utils/decorators.ts":