stillflow.engine 0.1.8 → 0.2.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.
- package/README.md +10 -0
- package/dist/index.d.ts +2 -4
- package/dist/index.js +2 -3
- package/dist/stillflow/kernel/buffer.d.ts +1 -0
- package/dist/stillflow/kernel/buffer.js +3 -0
- package/dist/stillflow/tools/batcher.d.ts +1 -0
- package/dist/stillflow/tools/batcher.js +5 -1
- package/dist/stillflow-adapters/dom/aot/appliers/apply.d.ts +6 -0
- package/dist/stillflow-adapters/dom/aot/appliers/apply.js +155 -0
- package/dist/stillflow-adapters/dom/aot/compiler/compile.d.ts +11 -0
- package/dist/stillflow-adapters/dom/aot/compiler/compile.js +71 -0
- package/dist/stillflow-adapters/dom/aot/dom.delegator.d.ts +6 -0
- package/dist/stillflow-adapters/dom/aot/dom.delegator.js +39 -0
- package/dist/stillflow-adapters/dom/aot/dom.directives.d.ts +16 -0
- package/dist/stillflow-adapters/dom/aot/dom.operations.d.ts +3 -0
- package/dist/stillflow-adapters/dom/aot/dom.operations.js +1 -0
- package/dist/stillflow-adapters/dom/aot/dom.pool.d.ts +11 -0
- package/dist/stillflow-adapters/dom/aot/dom.pool.js +47 -0
- package/dist/stillflow-adapters/dom/aot/dom.resources.d.ts +3 -0
- package/dist/stillflow-adapters/dom/aot/dom.resources.js +3 -0
- package/dist/stillflow-adapters/dom/aot/dom.symbols.d.ts +5 -0
- package/dist/stillflow-adapters/dom/aot/dom.symbols.js +5 -0
- package/dist/stillflow-adapters/dom/{dom.d.ts → aot/dom.types.d.ts} +2 -2
- package/dist/stillflow-adapters/dom/aot/transformers/transform.d.ts +3 -0
- package/dist/stillflow-adapters/dom/aot/transformers/transform.js +42 -0
- package/dist/stillflow-adapters/dom/base.dom.d.ts +5 -3
- package/dist/stillflow-adapters/dom/base.dom.js +20 -12
- package/dist/stillflow-adapters/dom/description/describe.d.ts +1 -1
- package/dist/stillflow-adapters/dom/jsx.dom.d.ts +3 -3
- package/dist/stillflow-adapters/dom/properties/modifiers.d.ts +3 -4
- package/dist/stillflow-collection/index.d.ts +3 -0
- package/dist/stillflow-collection/index.js +2 -0
- package/dist/stillflow-examples/app.js +7 -5
- package/package.json +1 -1
- package/dist/stillflow-adapters/dom/compiler/apply.d.ts +0 -37
- package/dist/stillflow-adapters/dom/compiler/apply.js +0 -289
- package/dist/stillflow-adapters/dom/compiler/compile.d.ts +0 -18
- package/dist/stillflow-adapters/dom/compiler/compile.js +0 -47
- package/dist/stillflow-adapters/dom/compiler/transform.d.ts +0 -2
- package/dist/stillflow-adapters/dom/compiler/transform.js +0 -41
- package/dist/stillflow-adapters/dom/index.d.ts +0 -15
- package/dist/stillflow-adapters/dom/index.js +0 -22
- package/dist/stillflow-adapters/dom/types.d.ts +0 -4
- package/dist/stillflow-examples/files.d.ts +0 -1
- package/dist/stillflow-examples/files.js +0 -29
- package/dist/stillflow-examples/test.d.ts +0 -1
- package/dist/stillflow-examples/test.js +0 -35
- package/dist/stillflow-vm/analyze.d.ts +0 -7
- package/dist/stillflow-vm/analyze.js +0 -22
- package/dist/stillflow-vm/buffer.d.ts +0 -1
- package/dist/stillflow-vm/buffer.js +0 -1
- package/dist/stillflow-vm/instruction.d.ts +0 -1
- package/dist/stillflow-vm/instruction.js +0 -1
- package/dist/stillflow-vm/pool.d.ts +0 -1
- package/dist/stillflow-vm/pool.js +0 -1
- package/dist/stillflow-vm/resources.d.ts +0 -14
- package/dist/stillflow-vm/resources.js +0 -16
- /package/dist/stillflow-adapters/dom/{dom.js → aot/dom.directives.js} +0 -0
- /package/dist/stillflow-adapters/dom/{types.js → aot/dom.types.js} +0 -0
package/README.md
CHANGED
|
@@ -42,6 +42,16 @@ Also I think that **explicit reactivity is better than implicit reactivity**. So
|
|
|
42
42
|
|
|
43
43
|
For a starter you can play with [it in **CodeSandBox**](https://codesandbox.io/p/sandbox/stillflow-engine-starter-3qv9mh). Simple demonstration of the features of **Stillflow**.
|
|
44
44
|
|
|
45
|
+
You can test some benchmarks on [**CodeSandBox**](https://codesandbox.io/p/sandbox/43vn9g). It shows following results:
|
|
46
|
+
|
|
47
|
+
| Operation | Dataset Size | Execution Time (State + DOM) |
|
|
48
|
+
| :--- | :--- | :--- |
|
|
49
|
+
| **Append rows** | 10,000 rows | **~57ms** |
|
|
50
|
+
| **Full list swap (sort)** | 10,000 rows | **~40ms max** |
|
|
51
|
+
| **Partial random swaps** | 1,000 pairs | **~6-8ms max** |
|
|
52
|
+
| **Lazy remove every 2nd row** | 10,000 rows (5k removed) | **~24ms max** |
|
|
53
|
+
| **Lazy remove every 2nd row** | 10,000 rows (60k removed) | **~242ms max** ($O(k \log n)$ scaling) |
|
|
54
|
+
|
|
45
55
|
```ts
|
|
46
56
|
import { Stillflow, State, FenwickCollection, FlushableCollection, operators, adapters } from 'stillflow.engine'
|
|
47
57
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import * as adapters from './stillflow-adapters/index.js';
|
|
2
|
+
import * as collection from './stillflow-collection/index.js';
|
|
2
3
|
import operators from './stillflow/operators.js';
|
|
3
|
-
export { FlushableCollection } from './stillflow-collection/flushable.js';
|
|
4
|
-
export { AbstractCollection } from './stillflow-collection/abstract/collection.js';
|
|
5
|
-
export { FenwickCollection } from './stillflow-collection/fenwick.js';
|
|
6
4
|
export { Stillflow } from './stillflow/stillflow.js';
|
|
7
5
|
export { State } from './stillflow/state.js';
|
|
8
6
|
export { Batcher } from './stillflow/tools/batcher.js';
|
|
9
|
-
export { operators, adapters };
|
|
7
|
+
export { operators, adapters, collection };
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import * as adapters from './stillflow-adapters/index.js';
|
|
2
|
+
import * as collection from './stillflow-collection/index.js';
|
|
2
3
|
import operators from './stillflow/operators.js';
|
|
3
|
-
export { FlushableCollection } from './stillflow-collection/flushable.js';
|
|
4
|
-
export { FenwickCollection } from './stillflow-collection/fenwick.js';
|
|
5
4
|
export { Stillflow } from './stillflow/stillflow.js';
|
|
6
5
|
export { State } from './stillflow/state.js';
|
|
7
6
|
export { Batcher } from './stillflow/tools/batcher.js';
|
|
8
|
-
export { operators, adapters };
|
|
7
|
+
export { operators, adapters, collection };
|
|
@@ -1,16 +1,20 @@
|
|
|
1
1
|
export class Batcher {
|
|
2
2
|
_strategy;
|
|
3
3
|
_commiting = false;
|
|
4
|
+
_tasks = new Set();
|
|
4
5
|
constructor(_strategy) {
|
|
5
6
|
this._strategy = _strategy;
|
|
6
7
|
}
|
|
7
8
|
task(task) {
|
|
9
|
+
this._tasks.add(task);
|
|
8
10
|
if (this._commiting) {
|
|
9
11
|
return;
|
|
10
12
|
}
|
|
11
13
|
this._commiting = true;
|
|
12
14
|
this._strategy(() => {
|
|
13
|
-
task
|
|
15
|
+
for (const task of this._tasks) {
|
|
16
|
+
task();
|
|
17
|
+
}
|
|
14
18
|
this._commiting = false;
|
|
15
19
|
});
|
|
16
20
|
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { Pool } from "../dom.pool.js";
|
|
2
|
+
import { EventDelegator } from "../dom.delegator.js";
|
|
3
|
+
import { AnyDOM } from "../dom.types.js";
|
|
4
|
+
type Applier<T> = (root: HTMLElement, context: T) => void;
|
|
5
|
+
export declare function apply<V>(node: AnyDOM<V>, delegator: EventDelegator, pool: Pool): Applier<V>;
|
|
6
|
+
export {};
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { transform } from "../transformers/transform.js";
|
|
2
|
+
import { compile, markup_template } from "../compiler/compile.js";
|
|
3
|
+
import { SF_CACHE, SF_CACHE_TEXT, SF_LISTENED, SF_CONTEXT, SF_HANDLERS } from "../dom.symbols.js";
|
|
4
|
+
export function apply(node, delegator, pool) {
|
|
5
|
+
const instructions = compile(node);
|
|
6
|
+
const payload = (id) => `p${id}`;
|
|
7
|
+
const payloads = [markup_template(node)];
|
|
8
|
+
let code = `return function(root, context) {\n`;
|
|
9
|
+
let labels = 0;
|
|
10
|
+
code += `${payload(0)}(root);`;
|
|
11
|
+
code += `let sf_node_cache = root.${SF_CACHE};`;
|
|
12
|
+
code += `let sf_text_cache = root.${SF_CACHE_TEXT};`;
|
|
13
|
+
for (let id = 0; id < instructions.length; id++) {
|
|
14
|
+
const instruction = instructions[id];
|
|
15
|
+
const index = instruction.id;
|
|
16
|
+
if (instruction.text !== undefined) {
|
|
17
|
+
const pIndex = payloads.length;
|
|
18
|
+
payloads.push(instruction.text);
|
|
19
|
+
code += `iter_${labels++}: {`;
|
|
20
|
+
code += `const textnode = sf_text_cache[${index}];`;
|
|
21
|
+
code += `textnode.data = ${payload(pIndex)}(context);`;
|
|
22
|
+
code += `}`;
|
|
23
|
+
}
|
|
24
|
+
if (instruction.events !== undefined) {
|
|
25
|
+
code += `iter_${labels++}: {`;
|
|
26
|
+
code += `const element = sf_node_cache[${index}];`;
|
|
27
|
+
code += `if (!element.${SF_LISTENED}) {`;
|
|
28
|
+
code += `element.${SF_LISTENED} = true;`;
|
|
29
|
+
code += `element.${SF_CONTEXT} = context;`;
|
|
30
|
+
const pIndexRoot = payloads.length;
|
|
31
|
+
payloads.push(delegator);
|
|
32
|
+
for (const key in instruction.events) {
|
|
33
|
+
const pIndex = payloads.length;
|
|
34
|
+
payloads.push(instruction.events[key]);
|
|
35
|
+
code += `${payload(pIndexRoot)}.add(element, "${key}", ${payload(pIndex)});`;
|
|
36
|
+
}
|
|
37
|
+
code += `}`;
|
|
38
|
+
code += `else {`;
|
|
39
|
+
code += `element.${SF_CONTEXT} = context;`;
|
|
40
|
+
code += '}';
|
|
41
|
+
code += '}';
|
|
42
|
+
}
|
|
43
|
+
if (instruction.properties !== undefined) {
|
|
44
|
+
code += `iter_${labels++}: {`;
|
|
45
|
+
code += `const element = sf_node_cache[${index}];`;
|
|
46
|
+
for (const key in instruction.properties) {
|
|
47
|
+
const pIndex = payloads.length;
|
|
48
|
+
payloads.push(instruction.properties[key]);
|
|
49
|
+
code += `element.${key} = ${payload(pIndex)}(context);`;
|
|
50
|
+
}
|
|
51
|
+
code += '}';
|
|
52
|
+
}
|
|
53
|
+
if (instruction.conditional !== undefined) {
|
|
54
|
+
const pIndex = payloads.length;
|
|
55
|
+
payloads.push(instruction.conditional);
|
|
56
|
+
code += `iter_${labels++}: {`;
|
|
57
|
+
code += `const element = sf_node_cache[${index}];`;
|
|
58
|
+
code += `const condition = ${payload(pIndex)}(context);`;
|
|
59
|
+
code += `element.style.display = condition ? String() : 'none'`;
|
|
60
|
+
code += '}';
|
|
61
|
+
}
|
|
62
|
+
if (instruction.iterate !== undefined) {
|
|
63
|
+
const node = instruction.iterate.each();
|
|
64
|
+
const template = transform(node);
|
|
65
|
+
const pIndex = payloads.length;
|
|
66
|
+
payloads.push({
|
|
67
|
+
node,
|
|
68
|
+
elements: [],
|
|
69
|
+
template,
|
|
70
|
+
applier: undefined,
|
|
71
|
+
select: instruction.iterate.select,
|
|
72
|
+
generator: apply,
|
|
73
|
+
delegator,
|
|
74
|
+
pool
|
|
75
|
+
});
|
|
76
|
+
code += `iter_${labels++}: {`;
|
|
77
|
+
code += `if (${payload(pIndex)}.applier === undefined) {`;
|
|
78
|
+
code += `${payload(pIndex)}.applier = ${payload(pIndex)}.generator(${payload(pIndex)}.node, ${payload(pIndex)}.delegator, ${payload(pIndex)}.pool);`;
|
|
79
|
+
code += `${payload(pIndex)}.applier(${payload(pIndex)}.template);`;
|
|
80
|
+
code += '}';
|
|
81
|
+
code += `const items = ${payload(pIndex)}.select(context);`;
|
|
82
|
+
code += `const target = root.${SF_CACHE}[${index}];`;
|
|
83
|
+
code += `const { replaces, clears, addes, swaps, removes, updates } = items.flush();`;
|
|
84
|
+
code += `if (replaces) {`;
|
|
85
|
+
code += `const items_count = items.size;`;
|
|
86
|
+
code += `const child_count = ${payload(pIndex)}.elements.length;`;
|
|
87
|
+
code += `const common_count = Math.min(items_count, child_count);`;
|
|
88
|
+
code += `for (let index = 0; index < common_count; index++) {`;
|
|
89
|
+
code += `${payload(pIndex)}.applier(${payload(pIndex)}.elements[index], items.at(index));`;
|
|
90
|
+
code += `}`;
|
|
91
|
+
code += `if (items_count < child_count) {`;
|
|
92
|
+
code += `for (let index = items_count; index < child_count; index++) {`;
|
|
93
|
+
code += `const element = ${payload(pIndex)}.elements[index];`;
|
|
94
|
+
code += `${payload(pIndex)}.pool.add(element);`;
|
|
95
|
+
code += `element.remove();`;
|
|
96
|
+
code += `}`;
|
|
97
|
+
code += `${payload(pIndex)}.elements.length = items_count;`;
|
|
98
|
+
code += `} else if (child_count < items_count) {`;
|
|
99
|
+
code += `const fragment = document.createDocumentFragment();`;
|
|
100
|
+
code += `const elements = new Array(items_count - child_count);`;
|
|
101
|
+
code += `for (let index = child_count; index < items_count; index++) {`;
|
|
102
|
+
code += `const element = ${payload(pIndex)}.pool.get() || ${payload(pIndex)}.template.cloneNode(true);`;
|
|
103
|
+
code += `element.${SF_HANDLERS} = ${payload(pIndex)}.template.${SF_HANDLERS};`;
|
|
104
|
+
code += `element.${SF_CONTEXT} = items.at(index);`;
|
|
105
|
+
code += `${payload(pIndex)}.applier(element, items.at(index));`;
|
|
106
|
+
code += `elements[index - child_count] = element;`;
|
|
107
|
+
code += `fragment.appendChild(element);`;
|
|
108
|
+
code += `}`;
|
|
109
|
+
code += `${payload(pIndex)}.elements = ${payload(pIndex)}.elements.concat(elements);`;
|
|
110
|
+
code += `target.appendChild(fragment);`;
|
|
111
|
+
code += `}`;
|
|
112
|
+
code += `break iter_${labels - 1};`;
|
|
113
|
+
code += `}`;
|
|
114
|
+
code += `if (clears) {`;
|
|
115
|
+
code += `const free = ${payload(pIndex)}.elements.splice(0);`;
|
|
116
|
+
code += `for (const element of free) {`;
|
|
117
|
+
code += `element.${SF_HANDLERS} = undefined;`;
|
|
118
|
+
code += `${payload(pIndex)}.pool.add(element);`;
|
|
119
|
+
code += `}`;
|
|
120
|
+
code += `target.innerHTML = String();`;
|
|
121
|
+
code += `}`;
|
|
122
|
+
code += `if (addes > 0) {`;
|
|
123
|
+
code += `const fragment = document.createDocumentFragment();`;
|
|
124
|
+
code += `const elements = new Array(addes);`;
|
|
125
|
+
code += `for (let index = 0; index < addes; index++) {`;
|
|
126
|
+
code += `const element = ${payload(pIndex)}.pool.get() || ${payload(pIndex)}.template.cloneNode(true);`;
|
|
127
|
+
code += `element.${SF_HANDLERS} = ${payload(pIndex)}.template.${SF_HANDLERS};`;
|
|
128
|
+
code += `element.${SF_CONTEXT} = items.at(index);`;
|
|
129
|
+
code += `${payload(pIndex)}.applier(element, items.at(index));`;
|
|
130
|
+
code += `elements[index] = element;`;
|
|
131
|
+
code += `fragment.appendChild(element);`;
|
|
132
|
+
code += `}`;
|
|
133
|
+
code += `${payload(pIndex)}.elements = ${payload(pIndex)}.elements.concat(elements);`;
|
|
134
|
+
code += `target.appendChild(fragment);`;
|
|
135
|
+
code += `}`;
|
|
136
|
+
code += `for (const [i, j] of swaps.entries()) {`;
|
|
137
|
+
code += `${payload(pIndex)}.applier(${payload(pIndex)}.elements[i], items.at(i));`;
|
|
138
|
+
code += `${payload(pIndex)}.applier(${payload(pIndex)}.elements[j], items.at(j));`;
|
|
139
|
+
code += `}`;
|
|
140
|
+
code += `for (const id of removes) {`;
|
|
141
|
+
code += `const element = ${payload(pIndex)}.elements[id];`;
|
|
142
|
+
code += `element.${SF_HANDLERS} = undefined;`;
|
|
143
|
+
code += `${payload(pIndex)}.pool.add(element);`;
|
|
144
|
+
code += `element.remove();`;
|
|
145
|
+
code += `}`;
|
|
146
|
+
code += `for (const id of updates) {`;
|
|
147
|
+
code += `${payload(pIndex)}.applier(${payload(pIndex)}.elements[id], items.at(id));`;
|
|
148
|
+
code += `}`;
|
|
149
|
+
code += `}`;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
code += '}';
|
|
153
|
+
const generator = new Function(...payloads.map((_, id) => payload(id)), code);
|
|
154
|
+
return generator(...payloads);
|
|
155
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { AnyDOM } from "../dom.types.js";
|
|
2
|
+
import { Directives } from "../dom.directives.js";
|
|
3
|
+
type Instructions<V> = Partial<{
|
|
4
|
+
[L in keyof Directives<V>]: Directives<V>[L];
|
|
5
|
+
}> & {
|
|
6
|
+
id: number;
|
|
7
|
+
};
|
|
8
|
+
declare function markup_template<V>(node: AnyDOM<V>): (root: HTMLElement) => void;
|
|
9
|
+
declare function compile<V>(node: AnyDOM<V>): Instructions<V>[];
|
|
10
|
+
export { compile, markup_template };
|
|
11
|
+
export { Instructions };
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { SF_CACHE, SF_CACHE_TEXT } from "../dom.symbols.js";
|
|
2
|
+
function markup_template_code(node, path) {
|
|
3
|
+
let { modifiers } = node.properties;
|
|
4
|
+
let code = String();
|
|
5
|
+
const has_modifiers = modifiers?.text !== undefined ||
|
|
6
|
+
modifiers?.properties !== undefined ||
|
|
7
|
+
modifiers?.events !== undefined ||
|
|
8
|
+
modifiers?.iterate !== undefined ||
|
|
9
|
+
modifiers?.conditional !== undefined;
|
|
10
|
+
if (has_modifiers) {
|
|
11
|
+
code += `sf_markup_path.push(root${path.map(x => `.children[${x}]`).join(String())});`;
|
|
12
|
+
}
|
|
13
|
+
for (let index = 0; index < node.children.length; index++) {
|
|
14
|
+
code += markup_template_code(node.children[index], [...path, index]);
|
|
15
|
+
}
|
|
16
|
+
return code;
|
|
17
|
+
}
|
|
18
|
+
function markup_template(node) {
|
|
19
|
+
let code = String();
|
|
20
|
+
code += `return function(root) {`;
|
|
21
|
+
code += `if (root.${SF_CACHE}) {`;
|
|
22
|
+
code += `return;`;
|
|
23
|
+
code += `}`;
|
|
24
|
+
code += `let sf_markup_path = [];`;
|
|
25
|
+
code += markup_template_code(node, []);
|
|
26
|
+
code += `root.${SF_CACHE} = sf_markup_path;`;
|
|
27
|
+
code += `root.${SF_CACHE_TEXT} = sf_markup_path.map(node => node.firstChild);`;
|
|
28
|
+
code += `}`;
|
|
29
|
+
const generator = new Function(code);
|
|
30
|
+
return generator();
|
|
31
|
+
}
|
|
32
|
+
function compile_template(node, state) {
|
|
33
|
+
const modifiers = node.properties.modifiers || {};
|
|
34
|
+
const has_modifiers = modifiers.text !== undefined ||
|
|
35
|
+
modifiers.properties !== undefined ||
|
|
36
|
+
modifiers.events !== undefined ||
|
|
37
|
+
modifiers.iterate !== undefined ||
|
|
38
|
+
modifiers.conditional !== undefined;
|
|
39
|
+
if (has_modifiers) {
|
|
40
|
+
const current_id = state.id++;
|
|
41
|
+
const instruction = { id: current_id };
|
|
42
|
+
if (modifiers.text !== undefined) {
|
|
43
|
+
instruction.text = modifiers.text;
|
|
44
|
+
}
|
|
45
|
+
if (modifiers.properties !== undefined) {
|
|
46
|
+
instruction.properties = modifiers.properties;
|
|
47
|
+
}
|
|
48
|
+
if (modifiers.events !== undefined) {
|
|
49
|
+
instruction.events = modifiers.events;
|
|
50
|
+
}
|
|
51
|
+
if (modifiers.iterate !== undefined) {
|
|
52
|
+
instruction.iterate = modifiers.iterate;
|
|
53
|
+
}
|
|
54
|
+
if (modifiers.conditional !== undefined) {
|
|
55
|
+
instruction.conditional = modifiers.conditional;
|
|
56
|
+
}
|
|
57
|
+
state.instructions.push(instruction);
|
|
58
|
+
}
|
|
59
|
+
for (let index = 0; index < node.children.length; index++) {
|
|
60
|
+
compile_template(node.children[index], state);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
function compile(node) {
|
|
64
|
+
const state = {
|
|
65
|
+
instructions: [],
|
|
66
|
+
id: 0
|
|
67
|
+
};
|
|
68
|
+
compile_template(node, state);
|
|
69
|
+
return state.instructions;
|
|
70
|
+
}
|
|
71
|
+
export { compile, markup_template };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { SF_CONTEXT, SF_HANDLERS } from "./dom.symbols.js";
|
|
2
|
+
export class EventDelegator {
|
|
3
|
+
_actives = new Set();
|
|
4
|
+
ensure(type) {
|
|
5
|
+
if (this._actives.has(type)) {
|
|
6
|
+
return;
|
|
7
|
+
}
|
|
8
|
+
this._actives.add(type);
|
|
9
|
+
document.body.addEventListener(type, event => {
|
|
10
|
+
let target = event.target;
|
|
11
|
+
while (target && target !== document.body) {
|
|
12
|
+
const listeners = target[SF_HANDLERS];
|
|
13
|
+
if (listeners !== undefined) {
|
|
14
|
+
const callback = listeners[type];
|
|
15
|
+
callback?.(event, target[SF_CONTEXT]);
|
|
16
|
+
if (event.cancelBubble) {
|
|
17
|
+
break;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
target = target.parentElement;
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
remove(root) {
|
|
25
|
+
const element = root;
|
|
26
|
+
element[SF_CONTEXT] = null;
|
|
27
|
+
element[SF_HANDLERS] = undefined;
|
|
28
|
+
}
|
|
29
|
+
add(root, type, listener) {
|
|
30
|
+
const element = root;
|
|
31
|
+
if (element[SF_HANDLERS] !== undefined) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
const listeners = Object.create(null);
|
|
35
|
+
listeners[type] = listener;
|
|
36
|
+
element[SF_HANDLERS] = listeners;
|
|
37
|
+
this.ensure(type);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { collection } from "../../../index.js";
|
|
2
|
+
import { AnyDOM } from "./dom.types.js";
|
|
3
|
+
export type Directives<V, K extends keyof HTMLElementTagNameMap = any> = Partial<{
|
|
4
|
+
text: (context: V) => string;
|
|
5
|
+
events: Partial<{
|
|
6
|
+
[L in keyof HTMLElementEventMap]: (event: HTMLElementEventMap[L], context: V) => void;
|
|
7
|
+
}>;
|
|
8
|
+
conditional: (context: V) => boolean;
|
|
9
|
+
properties: Partial<{
|
|
10
|
+
[L in keyof HTMLElementTagNameMap[K]]: (context: V) => string;
|
|
11
|
+
}>;
|
|
12
|
+
iterate: {
|
|
13
|
+
select: <W>(context: V) => collection.Flushable<W>;
|
|
14
|
+
each: () => AnyDOM<any>;
|
|
15
|
+
};
|
|
16
|
+
}>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const OPERATIONS = [];
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { SF_CONTEXT, SF_LISTENED } from "./dom.symbols.js";
|
|
2
|
+
export class Pool {
|
|
3
|
+
static TIME_TO_EVICT = 2;
|
|
4
|
+
_hots = [];
|
|
5
|
+
_colds = [];
|
|
6
|
+
_frame = 0;
|
|
7
|
+
evict() {
|
|
8
|
+
for (let id = this._hots.length - 1; id >= 0; id--) {
|
|
9
|
+
const hot = this._hots[id];
|
|
10
|
+
if (this._frame - hot.frame >= Pool.TIME_TO_EVICT) {
|
|
11
|
+
const element = hot.root;
|
|
12
|
+
element[SF_CONTEXT] = null;
|
|
13
|
+
element[SF_LISTENED] = false;
|
|
14
|
+
this._colds.push(new WeakRef(hot.root));
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
this._hots = this._hots.filter(hot => this._frame - hot.frame < Pool.TIME_TO_EVICT);
|
|
18
|
+
}
|
|
19
|
+
retrieve() {
|
|
20
|
+
if (this._hots.length <= 0) {
|
|
21
|
+
while (this._colds.length > 0) {
|
|
22
|
+
const cold = this._colds.pop();
|
|
23
|
+
const element = cold.deref();
|
|
24
|
+
if (element !== undefined) {
|
|
25
|
+
this._hots.push({ root: element, frame: this._frame });
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
get() {
|
|
32
|
+
this.retrieve();
|
|
33
|
+
const element = this._hots.pop();
|
|
34
|
+
if (element === undefined) {
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
element.frame = this._frame;
|
|
38
|
+
return element.root;
|
|
39
|
+
}
|
|
40
|
+
add(root) {
|
|
41
|
+
this._hots.push({ root, frame: this._frame });
|
|
42
|
+
}
|
|
43
|
+
tick() {
|
|
44
|
+
this.evict();
|
|
45
|
+
this._frame++;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare const SF_LISTENED = "__stillflow_listened__";
|
|
2
|
+
export declare const SF_CONTEXT = "__stillflow_context__";
|
|
3
|
+
export declare const SF_CACHE_TEXT = "__stillflow_cache_text__";
|
|
4
|
+
export declare const SF_CACHE = "__stillflow_cache__";
|
|
5
|
+
export declare const SF_HANDLERS = "__stillflow_handlers__";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Binding } from "
|
|
2
|
-
import { Properties } from "
|
|
1
|
+
import { Binding } from "../../binding.js";
|
|
2
|
+
import { Properties } from "../properties/properties.js";
|
|
3
3
|
export type DOM<V, K extends keyof HTMLElementTagNameMap> = Binding<Properties<V, K>>;
|
|
4
4
|
export type AnyDOM<V> = Binding<Properties<V, keyof HTMLElementTagNameMap>>;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
function transform_code(description, payloads, depth = 1) {
|
|
2
|
+
let { name, text, properties, styles, events, modifiers } = description.properties;
|
|
3
|
+
let code = String();
|
|
4
|
+
styles = styles || Object.create(null);
|
|
5
|
+
events = events || Object.create(null);
|
|
6
|
+
properties = properties || Object.create(null);
|
|
7
|
+
code += `let sf_child_node_${depth} = document.createElement("${name}");`;
|
|
8
|
+
if (text !== undefined || modifiers?.text !== undefined) {
|
|
9
|
+
code += `let sf_text_node_${depth} = document.createTextNode("${text}");`;
|
|
10
|
+
code += `sf_child_node_${depth}.appendChild(sf_text_node_${depth});`;
|
|
11
|
+
}
|
|
12
|
+
code += `sf_child_node_${depth - 1}.appendChild(sf_child_node_${depth});`;
|
|
13
|
+
for (const key in styles) {
|
|
14
|
+
code += `sf_child_node_${depth}.style.${key} = "${styles[key]}";`;
|
|
15
|
+
}
|
|
16
|
+
for (const key in events) {
|
|
17
|
+
const pIndex = payloads.length;
|
|
18
|
+
const event = events[key];
|
|
19
|
+
payloads.push(event);
|
|
20
|
+
code += `sf_child_node_${depth}.addEventListener("${key}", p${pIndex});`;
|
|
21
|
+
}
|
|
22
|
+
for (const key in properties) {
|
|
23
|
+
const property = properties[key];
|
|
24
|
+
code += `sf_child_node_${depth}.${key} = "${property}";`;
|
|
25
|
+
}
|
|
26
|
+
for (const child of description.children) {
|
|
27
|
+
code += `{${transform_code(child, payloads, depth + 1)}}`;
|
|
28
|
+
}
|
|
29
|
+
return code;
|
|
30
|
+
}
|
|
31
|
+
function transform(description) {
|
|
32
|
+
const payloads = [];
|
|
33
|
+
const fragment = document.createDocumentFragment();
|
|
34
|
+
let code = String();
|
|
35
|
+
code += `let sf_child_node_0 = root;`;
|
|
36
|
+
code += transform_code(description, payloads);
|
|
37
|
+
code += `return sf_child_node_1; `;
|
|
38
|
+
const transformer = new Function(...payloads.map((_, id) => `p${id} `), 'root', code);
|
|
39
|
+
const root = transformer(...payloads, fragment);
|
|
40
|
+
return root;
|
|
41
|
+
}
|
|
42
|
+
export { transform };
|
|
@@ -1,14 +1,16 @@
|
|
|
1
|
-
import { AnyDOM } from "./types.js";
|
|
1
|
+
import { AnyDOM } from "./aot/dom.types.js";
|
|
2
2
|
import { Properties } from "./properties/properties.js";
|
|
3
3
|
import { Binding } from "../binding.js";
|
|
4
4
|
type PropertiesWithoutServices<V, K extends keyof HTMLElementTagNameMap> = Omit<Properties<V, K>, 'name'>;
|
|
5
|
+
type Options = {
|
|
6
|
+
gc: (site: () => void) => void;
|
|
7
|
+
};
|
|
5
8
|
export type DOMRegistry = {
|
|
6
9
|
[K in keyof HTMLElementTagNameMap]: <V>(properties: Partial<PropertiesWithoutServices<V, K>>, children?: Binding<any>[]) => AnyDOM<V>;
|
|
7
10
|
} & {
|
|
8
|
-
$: <V>(root: AnyDOM<V
|
|
11
|
+
$: <V>(root: AnyDOM<V>, options?: Options) => {
|
|
9
12
|
element: HTMLElement;
|
|
10
13
|
update: (context: V) => void;
|
|
11
|
-
clean: () => void;
|
|
12
14
|
};
|
|
13
15
|
};
|
|
14
16
|
export declare const DOM_REGISTRY: DOMRegistry;
|
|
@@ -1,19 +1,27 @@
|
|
|
1
1
|
import { HTML_TAGS } from "./tags.js";
|
|
2
|
-
import {
|
|
3
|
-
import { Appliers, Listeners, Pool } from "./compiler/apply.js";
|
|
4
|
-
import { transform } from "./compiler/transform.js";
|
|
2
|
+
import { transform } from "./aot/transformers/transform.js";
|
|
5
3
|
import { describe } from "./description/describe.js";
|
|
4
|
+
import { apply } from "./aot/appliers/apply.js";
|
|
5
|
+
import { EventDelegator } from "./aot/dom.delegator.js";
|
|
6
|
+
import { Pool } from "./aot/dom.pool.js";
|
|
7
|
+
const DELEGATOR = new EventDelegator();
|
|
6
8
|
export const DOM_REGISTRY = Object.create(null);
|
|
7
|
-
DOM_REGISTRY.$ = function $(root) {
|
|
9
|
+
DOM_REGISTRY.$ = function $(root, options = { gc: site => site() }) {
|
|
10
|
+
const pool = new Pool();
|
|
8
11
|
const element = transform(root);
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
12
|
+
const applier = apply(root, DELEGATOR, pool);
|
|
13
|
+
let cleaning = false;
|
|
14
|
+
const update = (context) => {
|
|
15
|
+
if (!cleaning) {
|
|
16
|
+
cleaning = true;
|
|
17
|
+
options.gc(() => {
|
|
18
|
+
pool.tick();
|
|
19
|
+
cleaning = false;
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
applier(element, context);
|
|
23
|
+
};
|
|
24
|
+
return { element, update };
|
|
17
25
|
};
|
|
18
26
|
for (const tag of HTML_TAGS) {
|
|
19
27
|
DOM_REGISTRY[tag] = (properties, children = []) => {
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { AnyDOM } from "../types.js";
|
|
1
|
+
import { AnyDOM } from "../aot/dom.types.js";
|
|
2
2
|
import { Properties } from "../properties/properties.js";
|
|
3
3
|
export declare function describe<V, K extends keyof HTMLElementTagNameMap>(properties: Properties<V, K>, children: AnyDOM<V>[]): AnyDOM<V>;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { AnyDOM } from "./types.js";
|
|
2
|
-
import {
|
|
1
|
+
import { AnyDOM } from "./aot/dom.types.js";
|
|
2
|
+
import { collection } from "../../index.js";
|
|
3
3
|
declare const DYNAMIC_PROPERTY_PREFIX = "sf:";
|
|
4
4
|
declare const DELEGATE_EVENT_PROPERTY_PREFIX = "sf:on-";
|
|
5
5
|
declare const COMMON_EVENT_PROPERTY_PREFIX = "on:";
|
|
@@ -9,7 +9,7 @@ type ElementStyles<K extends keyof HTMLElementTagNameMap> = {
|
|
|
9
9
|
type ElementPropertiesWithoutStyles<K extends keyof HTMLElementTagNameMap> = Omit<HTMLElementTagNameMap[K], 'style'>;
|
|
10
10
|
type ElementServicePropertiesWithoutPrefix<V> = {
|
|
11
11
|
'if': (context: V) => boolean;
|
|
12
|
-
'for': (context: V) =>
|
|
12
|
+
'for': (context: V) => collection.Flushable<unknown>;
|
|
13
13
|
'each': () => AnyDOM<V>;
|
|
14
14
|
};
|
|
15
15
|
type ElementServicePropertiesWithPrefix<V> = {
|