-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTransform.js
425 lines (425 loc) · 15.2 KB
/
Transform.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
import { MountObserver } from 'mount-observer/MountObserver.js';
import { arr0 } from './arr.js';
export { arr0 } from './arr.js';
import { stdEvt } from './asmr/stdEvt.js';
export async function Transform(target, model, xform, options) {
const xformer = new Transformer(target, model, xform, options);
await xformer.do();
return xformer;
}
export class Transformer extends EventTarget {
target;
xform;
options;
#mountOrchestrators = [];
#model;
initializedMods = new Set();
get model() {
return this.#model;
}
async updateModel(newModel) {
const { options } = this;
const { propagator } = options;
const { ___props, ___nestedProps } = propagator;
if (___props === undefined) {
this.#model = newModel;
return;
}
if (___nestedProps === undefined) {
Object.assign(this.#model, newModel);
return;
}
else {
for (const prop in newModel) {
if (___nestedProps.has(prop)) {
const tbd = ___nestedProps.get(prop);
const newVal = newModel[prop];
if (tbd instanceof Transformer) {
await tbd.updateModel(newVal);
}
else {
Object.assign(tbd, newVal);
}
}
}
}
}
constructor(target, model, xform, options) {
super();
this.target = target;
this.xform = xform;
this.options = options;
this.#model = model;
}
async do() {
const { target, model, xform } = this;
const info = xform;
let { options } = this;
if (options === undefined) {
options = {};
this.options = options;
}
let { propagator } = options;
if (propagator === undefined) {
propagator = new EventTarget();
options.propagator = propagator;
}
if (propagator.___props === undefined) {
propagator.___props = new Set();
}
const uows = [];
for (const key in xform) {
if (key === '411')
continue;
let rhs = (xform[key]);
switch (typeof rhs) {
case 'number': {
if (rhs !== 0)
throw 'NI';
const qi = await this.calcQI(key);
const { hostPropToAttrMap, localPropCamelCase } = qi;
const uow = {
o: hostPropToAttrMap.map(x => x.name),
d: 0,
qi,
q: key,
s: localPropCamelCase
};
uows.push(uow);
break;
}
case 'string':
{
if (typeof model[rhs] === 'function') {
const qi = await this.calcQI(key);
const { hostPropToAttrMap } = qi;
const uow = {
o: hostPropToAttrMap.map(x => x.name),
d: rhs,
qi,
q: key
};
uows.push(uow);
}
else {
const uow = {
o: [rhs],
d: 0,
q: key
};
uows.push(uow);
}
}
break;
case 'object':
{
const rhses = arr0(rhs);
for (const rhsPart of rhses) {
const uow = {
//d: 0,
...rhsPart,
q: key
};
const { m, e, a, f } = uow;
if ((m || e || a || f) === undefined) {
if (uow.o === undefined) {
const qi = await this.calcQI(key);
const { hostPropToAttrMap } = qi;
if (hostPropToAttrMap !== undefined) {
uow.o = hostPropToAttrMap.filter(x => x.name in model).map(x => x.name);
}
}
const { o } = uow;
if (o !== undefined && !(Array.isArray(o) && o.length === 0) && uow.d === undefined)
uow.d = 0;
}
uows.push(uow);
}
}
break;
}
}
for (const uow of uows) {
let { q, qi, y } = uow;
if (qi === undefined)
qi = await this.calcQI(q);
//qi.w = w;
const { o, s } = qi;
if (o !== undefined) {
uow.o = o;
}
if (s !== undefined) {
uow.s = s[0];
}
if (y !== undefined) {
uow.d = 0;
}
const newProcessor = new MountOrchestrator(this, uow, qi);
await newProcessor.do();
this.#mountOrchestrators.push(newProcessor);
await newProcessor.subscribe();
}
}
async calcQI(pqe) {
if (pqe.startsWith('* ')) {
const cssQuery = pqe.substring(2);
return {
css: cssQuery,
cssQuery
};
}
if (pqe === ':root') {
return {
isRootQry: true
};
}
if (!pqe.includes(' ')) {
return {
css: pqe,
localName: pqe
};
}
//TODO: dynamic import of the rest of this method, including other methods it calls.
const qi = {};
const asterSplit = pqe.split('*');
if (asterSplit.length === 2) {
qi.cssQuery = asterSplit[1].trim();
}
const tokens = pqe.split(' ');
if (tokens.length === 1)
throw 'NI';
let currentTokens = tokens;
while (currentTokens.length > 0) {
const [first, second, ...rest] = currentTokens;
switch (first) {
case '-s': {
qi.localPropCamelCase = second;
//qi.s = [second];
break;
}
default: {
if (qi.hostPropToAttrMap === undefined) {
qi.hostPropToAttrMap = [];
}
qi.hostPropToAttrMap.push({
type: first,
name: second,
});
}
}
currentTokens = rest;
}
qi.css = await this.#calcCSS(qi);
return qi;
}
async #calcCSS(qi) {
const { cssQuery } = qi;
if (cssQuery !== undefined)
return cssQuery;
const { localName } = qi;
if (localName !== undefined)
return localName;
const { hostPropToAttrMap, localPropCamelCase } = qi;
;
if (hostPropToAttrMap === undefined)
throw 'NI';
qi.o = hostPropToAttrMap.map(x => x.name);
if (localPropCamelCase !== undefined) {
qi.s = [localPropCamelCase];
}
let returnStr = hostPropToAttrMap.map(x => {
const { name, type } = x;
switch (type) {
case '#':
return `#${name}`;
case '|':
return `[itemprop~="${name}"]`;
case '%':
return `[part~="${name}"]`;
case '@':
return `[name="${name}"]`;
case '.':
return `.${name}`;
case '$':
return `[itemscope][itemprop~="${name}"]`;
case '-o':
return `[-o~="${name}"]`;
}
}).join('');
if (localPropCamelCase !== undefined) {
returnStr += `[-s~="${localPropCamelCase}"]`;
}
return returnStr;
}
async doUpdate(matchingElement, uow) {
const { doUpdate } = await import('./trHelpers/doUpdate.js');
await doUpdate(this, matchingElement, uow);
}
async doIfs(matchingElement, uow, i) {
const { doIfs } = await import('./trHelpers/doIfs.js');
return await doIfs(this, matchingElement, uow, i);
}
// async doYield(matchingElement: Element, uow: UnitOfWork<TProps, TMethods, TElement>, y: YieldSettings<TProps>){
// const {doYield} = await import('./trHelpers/doYield.js');
// return await doYield(this, matchingElement, uow, y);
// }
async engage(matchingElement, type, uow, observer, mountContext) {
const { e } = uow;
if (e === undefined)
return;
const { Engage } = await import('./trHelpers/Engage.js');
await Engage(this, matchingElement, type, uow, mountContext);
}
async getDerivedVal(uow, d, matchingElement) {
const { getDerivedVal } = await import('./trHelpers/getDerivedVal.js');
return await getDerivedVal(this, uow, d, matchingElement);
}
async getArrayVal(uow, u) {
const { getArrayVal } = await import('./trHelpers/getArrayVal.js');
return getArrayVal(this, uow, u);
}
async getComplexDerivedVal(uow, dc) {
const { getComplexDerivedVal } = await import('./trHelpers/getComplexDerivedVal.js');
return await getComplexDerivedVal(this, uow, dc);
}
getNumberUVal(uow, d) {
const { o } = uow;
const arrO = arr0(o);
const propName = this.#getPropName(arrO, d);
const pOrC = arrO[d];
const model = this.model;
let val = model[propName];
if (Array.isArray(pOrC)) {
const c = pOrC[1];
if (typeof c === 'function') {
val = c(val);
}
else {
val = model[c](val);
}
}
return val;
}
#getPropName(p, n) {
const pOrC = p[n];
if (Array.isArray(pOrC))
return pOrC[0];
return pOrC;
}
}
export class MountOrchestrator extends EventTarget {
transformer;
queryInfo;
#mountObserver;
#matchingElements = [];
#unitsOfWork;
constructor(transformer, uows, queryInfo) {
super();
this.transformer = transformer;
this.queryInfo = queryInfo;
this.#unitsOfWork = arr0(uows);
}
async do() {
const { transformer, queryInfo } = this;
const { options, xform } = transformer;
const { skipInit } = options;
const { isRootQry } = queryInfo;
if (isRootQry) {
const target = transformer.target;
this.#matchingElements.push(new WeakRef(target));
const { onMount } = await import('./trHelpers/onMount.js');
await onMount(transformer, this, target, this.#unitsOfWork, !!skipInit, { initializing: true }, this.#matchingElements);
return;
}
const info = xform;
const w = info?.[411]?.w;
const x = w || '';
const on = queryInfo.css + x; // transformer.calcCSS(queryInfo);
this.#mountObserver = new MountObserver({
on,
do: {
mount: async (matchingElement, observer, ctx) => {
this.#matchingElements.push(new WeakRef(matchingElement));
const { onMount } = await import('./trHelpers/onMount.js');
await onMount(transformer, this, matchingElement, this.#unitsOfWork, !!skipInit, ctx, this.#matchingElements, observer, this.#mountObserver);
},
dismount: async (matchingElement, ctx, stage) => {
for (const uow of this.#unitsOfWork) {
this.#cleanUp(matchingElement);
await transformer.engage(matchingElement, 'onDismount', uow, ctx, stage);
}
//TODO remove weak ref from matching elements;
},
disconnect: async (matchingElement, ctx, stage) => {
for (const uow of this.#unitsOfWork) {
this.#cleanUp(matchingElement);
await transformer.engage(matchingElement, 'onDisconnect', uow, ctx, stage);
}
}
}
});
}
async subscribe() {
for (const uow of this.#unitsOfWork) {
let { o } = uow;
const p = arr0(o);
const { target, options, model } = this.transformer;
const propagator = (model.propagator || options.propagator);
const propagatorIsReady = model.propagator ? true : options.propagatorIsReady;
for (const propName of p) {
if (typeof propName !== 'string')
throw 'NI';
if (!(propName in model))
continue;
if (!propagatorIsReady) {
const propsSet = propagator.___props;
if (propsSet instanceof Set) {
if (!propsSet.has(propName)) {
const { subscribe } = await import('./lib/subscribe2.js');
await subscribe(model, propName, propagator, true);
}
}
}
//I'm thinking this event handler doesn't access any memory, hence
//risk of memory leaks seems really low.
propagator.addEventListener(propName, e => {
const all = this.#cleanUp();
for (const matchingElement of all) {
this.doUpdate(matchingElement, uow);
}
});
}
if (Array.isArray(target)) {
throw 'NI';
}
else {
this.#mountObserver?.observe(target);
}
}
}
#cleanUp(matchingElement) {
const newRefs = [];
const all = [];
for (const ref of this.#matchingElements) {
const deref = ref.deref();
if (deref !== undefined && deref !== matchingElement) {
newRefs.push(ref);
all.push(deref);
}
}
return all;
}
async doUpdate(matchingElement, uow) {
const { d, s, sa, ss, f } = uow;
if (d !== undefined || s !== undefined || sa !== undefined || ss !== undefined || f !== undefined) {
await this.transformer.doUpdate(matchingElement, uow);
}
}
toStdEvt(a, matchingElement) {
const on = stdEvt(matchingElement);
return {
on,
do: a,
};
}
}