solarite 0.2.4 → 0.3.0

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.
@@ -1,189 +0,0 @@
1
- /**
2
- * Trying to be able to automatically watch primitive values.
3
- * TODO:
4
- * 1. Have get() return Proxies for nested updates.
5
- * 2. Override .map() for loops to capture changes.
6
- */
7
-
8
-
9
- // Example:
10
- /*
11
- class WatchExample extends Solarite {
12
-
13
- constructor(items = []) {
14
- super();
15
-
16
- this.items = items;
17
- watch(this, 'items');
18
-
19
- this.name = 'George';
20
- watch(this, 'name');
21
-
22
- this.render();
23
- }
24
-
25
- render() {
26
- r(this)`
27
- <watch-example>
28
- ${() => this.name + '!'}
29
-
30
- ${this.items.map(item => r`
31
- <div>${item.name}</div>
32
- `)}
33
- ${() => this.items.length}
34
- </watch-example>`;
35
- }
36
- }
37
- customElements.define('watch-example', WatchExample);
38
-
39
- let a = new WatchExample();
40
-
41
- // Items is a Proxy.
42
- // Calling push() will trigger the map'd ExprPaths to add another at the end.
43
- // And the .length expression to update.
44
- // Because accessing .items returns a proxy.
45
- a.items.push({name: 'Fred'});
46
- */
47
-
48
-
49
- import Globals from "./Globals.js";
50
- import Util from "../util/Util.js";
51
-
52
- let unusedArg = Symbol('unusedArg');
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
-
67
- /**
68
- *
69
- * @param root {HTMLElement}
70
- * @param field {string}
71
- * @param value {string|Symbol} */
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];
78
-
79
-
80
- // use a single object for both defineProperty and new Proxy's handler.
81
- const handler = {
82
- get(obj, prop, receiver) {
83
-
84
- let result = (obj === receiver && field === prop)
85
- ? value // top-level value.
86
- : Reflect.get(obj, prop, receiver); // avoid infinite recursion.
87
-
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
- }
97
-
98
- // Track which ExprPath is using this variable.
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);
111
-
112
- return result;
113
- },
114
-
115
-
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]) {
129
-
130
- // Update a single NodeGroup created by array.map()
131
- if (Array.isArray(obj) && parseInt(prop) == prop) {
132
- let exprsToRender = rootNg.exprsToRender.get(exprPath);
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);
142
- }
143
- return true;
144
- }
145
- }
146
-
147
- Object.defineProperty(root, field, {
148
- get: () => handler.get(root, field, root),
149
- set: (val) => handler.set(root, field, val, root)
150
- });
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
- }
@@ -1,33 +0,0 @@
1
- export default class WeakArray {
2
- constructor() {
3
- this.items = [];
4
- }
5
-
6
- push(...items) {
7
- for (let item of items) {
8
- if (typeof item === 'object' && item)
9
- this.items.push(new WeakRef(item));
10
- else
11
- throw new TypeError("Only objects can be added to a WeakArray");
12
- }
13
- }
14
-
15
- get(index) {
16
- const ref = this.items[index];
17
- if (ref)
18
- return ref.deref();
19
- return undefined;
20
- }
21
-
22
- cleanup() {
23
- this.items = this.items.filter(ref => ref.deref() !== undefined);
24
- }
25
-
26
- *[Symbol.iterator]() {
27
- for (const ref of this.items) {
28
- const value = ref.deref();
29
- if (value !== undefined)
30
- yield value;
31
- }
32
- }
33
- }