solarite 0.2.3 → 0.2.4

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.
@@ -30,7 +30,7 @@ class WatchExample extends Solarite {
30
30
  ${this.items.map(item => r`
31
31
  <div>${item.name}</div>
32
32
  `)}
33
- ${this.items.length}
33
+ ${() => this.items.length}
34
34
  </watch-example>`;
35
35
  }
36
36
  }
@@ -47,52 +47,143 @@ a.items.push({name: 'Fred'});
47
47
 
48
48
 
49
49
  import Globals from "./Globals.js";
50
- import delve from "../util/delve.js";
51
- //import NodeGroupManager from "./NodeGroupManager.js";
50
+ import Util from "../util/Util.js";
52
51
 
53
52
  let unusedArg = Symbol('unusedArg');
54
53
 
54
+ /**
55
+ * Custom map function triggers the get() Proxy.
56
+ * @param array {Array}
57
+ * @param callback {function}
58
+ * @returns {*[]} */
59
+ function map(array, callback) {
60
+ let result = [];
61
+ for (let i=0; i<array.length; i++)
62
+ result.push(callback(array[i], i, array));
63
+ return result;
64
+ }
65
+
66
+
55
67
  /**
56
68
  *
57
- * @param root {Object}
58
- * @param path {string}
69
+ * @param root {HTMLElement}
70
+ * @param field {string}
59
71
  * @param value {string|Symbol} */
60
- export default function watch3(root, path, value=unusedArg) {
72
+ export default function watch3(root, field, value=unusedArg) {
73
+ // Store internal value used by get/set.
74
+ if (value !== unusedArg)
75
+ root[field] = value;
76
+ else
77
+ value = root[field];
61
78
 
62
- /** @type {ExprPath} The ExprPath that's using this variable.*/
63
- let exprPath;
64
79
 
65
- /** @type {function} Function evaluated by the ExprPath. */
66
- let exprFunction;
80
+ // use a single object for both defineProperty and new Proxy's handler.
81
+ const handler = {
82
+ get(obj, prop, receiver) {
67
83
 
68
- if (value !== unusedArg)
69
- root[path] = value;
84
+ let result = (obj === receiver && field === prop)
85
+ ? value // top-level value.
86
+ : Reflect.get(obj, prop, receiver); // avoid infinite recursion.
70
87
 
71
- // Store internal value used by get/set.
72
- else
73
- value = root[path];
88
+ if (prop === 'map')
89
+
90
+ // Double function so the ExprPath calls it as a function,
91
+ // instead of it being evaluated immediately when the Templat eis created.
92
+ return (callback) => () => {
93
+ let rootNg = Globals.nodeGroups.get(root);
94
+ rootNg.mapCallbacks.set(obj, callback);
95
+ return map(new Proxy(obj, handler), callback);
96
+ }
74
97
 
75
- Object.defineProperty(root, path, {
76
- get() {
77
98
  // Track which ExprPath is using this variable.
78
- if (Globals.currentExprPath)
79
- [exprPath, exprFunction] = Globals.currentExprPath;
99
+ if (Globals.currentExprPath) {
100
+ let [exprPath, exprFunction] = Globals.currentExprPath; // Set in ExprPath.applyExact()
101
+
102
+ let rootNg = Globals.nodeGroups.get(root);
103
+
104
+ // Init for field.
105
+ rootNg.watchedExprPaths[field] = rootNg.watchedExprPaths[field] || new Set();
106
+ rootNg.watchedExprPaths[field].add(exprPath);
107
+ }
108
+
109
+ if (result && typeof result === 'object')
110
+ return new Proxy(result, handler);
80
111
 
81
- // TODO: Return Proxy for non-primitive value, to track path changed.
82
- return value;
112
+ return result;
83
113
  },
84
- set(val) {
85
- value = val;
86
114
 
87
- if (exprFunction) {
88
115
 
89
- // TODO: Will fail for attribute w/ a value having multiple ExprPaths.
90
- // TODO: This won't update a component's expressions.
91
- exprPath.apply(exprFunction);
116
+ // TODO: Will fail for attribute w/ a value having multiple ExprPaths.
117
+ // TODO: This won't update a component's expressions.
118
+ set(obj, prop, val, receiver) {
119
+
120
+ // 1. Set the value.
121
+ if (obj === receiver && field === prop)
122
+ value = val; // top-level value.
123
+ else // avoid infinite recursion.
124
+ Reflect.set(obj, prop, val, receiver);
125
+
126
+ // 2. Add to the list of ExprPaths to re-render.
127
+ let rootNg = Globals.nodeGroups.get(root);
128
+ for (let exprPath of rootNg.watchedExprPaths[field]) {
92
129
 
93
- exprPath.freeNodeGroups(); // TODO: This could be skipped if applyExprs() never marked them as in-use.
130
+ // Update a single NodeGroup created by array.map()
131
+ if (Array.isArray(obj) && parseInt(prop) == prop) {
132
+ let exprsToRender = rootNg.exprsToRender.get(exprPath);
94
133
 
134
+ // If we're not re-rendering the whole thing.
135
+ if (exprsToRender !== true)
136
+ Util.mapAdd(rootNg.exprsToRender, exprPath, [obj, prop, val]);
137
+ }
138
+
139
+ // Reapply the whole expression.
140
+ else
141
+ rootNg.exprsToRender.set(exprPath, true);
95
142
  }
143
+ return true;
96
144
  }
145
+ }
146
+
147
+ Object.defineProperty(root, field, {
148
+ get: () => handler.get(root, field, root),
149
+ set: (val) => handler.set(root, field, val, root)
97
150
  });
98
151
  }
152
+
153
+ /**
154
+ * TODO: Rename so we have watch.add() and watch.render() ?
155
+ * @param root
156
+ * @returns {*[]} */
157
+ export function renderWatched(root) {
158
+ let rootNg = Globals.nodeGroups.get(root);
159
+ let modified = [];
160
+
161
+ for (let [exprPath, params] of rootNg.exprsToRender) {
162
+
163
+ // Reapply the whole expression.
164
+ if (params === true) {
165
+ exprPath.apply(exprPath.watchFunction);
166
+
167
+ // TODO: freeNodeGroups() could be skipped if applyExprs() never marked them as in-use.
168
+ exprPath.freeNodeGroups();
169
+
170
+ modified.push(...exprPath.getNodes());
171
+ }
172
+
173
+ // Update a single NodeGroup created by array.map()
174
+ else {
175
+ for (let row of params) {
176
+ let [obj, prop, value] = row;
177
+ let callback = rootNg.mapCallbacks.get(obj);
178
+ let template = callback(value);
179
+ exprPath.applyLoopItemUpdate(prop, template);
180
+
181
+ modified.push(...exprPath.nodeGroups[prop].getNodes());
182
+ }
183
+ }
184
+ }
185
+
186
+ rootNg.exprsToRender = new Map(); // clear
187
+
188
+ return modified;
189
+ }
@@ -20,7 +20,10 @@ export default class MultiValueMap {
20
20
  return false;
21
21
  }
22
22
 
23
- // Get all values for a key
23
+ /**
24
+ * Get all values for a key.
25
+ * @param key {string}
26
+ * @returns {Set|*[]} */
24
27
  getAll(key) {
25
28
  return this.data[key] || [];
26
29
  }
@@ -29,7 +32,7 @@ export default class MultiValueMap {
29
32
  * Remove one value from a key, and return it.
30
33
  * @param key {string}
31
34
  * @param val If specified, make sure we delete this specific value, if a key exists more than once.
32
- * @returns {*} */
35
+ * @returns {*|undefined} The deleted item. */
33
36
  delete(key, val=undefined) {
34
37
  // if (key === '["Html2",[[["Html3",["F1","A"]],["Html3",["F1","B"]]]]]')
35
38
  // debugger;
@@ -65,6 +68,36 @@ export default class MultiValueMap {
65
68
  return result;
66
69
  }
67
70
 
71
+ /**
72
+ * Try to delete an item that matches the key and the isPreferred function.
73
+ * if not the latter, just delete any item that matches the key.
74
+ * @param key {string}
75
+ * @param isPreferred {function}
76
+ * @returns {*|undefined} The deleted item. */
77
+ deletePreferred(key, isPreferred) {
78
+ let result;
79
+ let data = this.data;
80
+ let set = data[key];
81
+ if (!set)
82
+ return undefined;
83
+
84
+ for (let val of set)
85
+ if (isPreferred(val)) {
86
+ set.delete(val);
87
+ result = val;
88
+ break;
89
+ }
90
+ if (!result) {
91
+ [result] = set;
92
+ set.delete(result);
93
+ }
94
+
95
+ if (set.size === 0)
96
+ delete data[key];
97
+
98
+ return result;
99
+ }
100
+
68
101
  hasValue(val) {
69
102
  let data = this.data;
70
103
  let names = [];
package/src/util/Util.js CHANGED
@@ -79,10 +79,35 @@ var Util = {
79
79
 
80
80
  return result;
81
81
  },
82
-
83
82
 
83
+ /**
84
+ * @param map {Map|WeakMap|Object}
85
+ * @param key
86
+ * @param value */
87
+ mapAdd(map, key, value) {
88
+ let isMap = map instanceof Map || map instanceof WeakMap;
89
+ let result = isMap ? map.get(key) : map[key];
90
+ if (!result) {
91
+ result = [value];
92
+ if (isMap)
93
+ map.set(key, result);
94
+ else
95
+ map[key] = result;
96
+ }
97
+ else
98
+ result.push(value);
99
+ },
84
100
 
101
+ weakMemoize(obj, callback) {
102
+ let result = weakMemoizeInputs.get(obj);
103
+ if (!result) {
104
+ result = callback(obj);
105
+ weakMemoizeInputs.set(obj, result);
106
+ }
107
+ return result;
108
+ }
85
109
  };
86
110
 
111
+ let weakMemoizeInputs = new WeakMap();
87
112
 
88
113
  export default Util;