stillflow.engine 0.1.9 → 0.2.1
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 +14 -4
- package/dist/index.d.ts +2 -4
- package/dist/index.js +2 -3
- package/dist/stillflow-adapters/dom/aot/appliers/apply.d.ts +6 -0
- package/dist/stillflow-adapters/dom/{compiler/apply.generator.js → aot/appliers/apply.js} +52 -57
- 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/jsx.dom.js +1 -1
- 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/dist/stillflow-examples/test.js +4 -4
- package/package.json +1 -1
- package/dist/stillflow-adapters/dom/compiler/apply.d.ts +0 -37
- package/dist/stillflow-adapters/dom/compiler/apply.generator.d.ts +0 -5
- 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/pool.d.ts +0 -21
- package/dist/stillflow-adapters/dom/compiler/pool.js +0 -91
- package/dist/stillflow-adapters/dom/compiler/test.d.ts +0 -1
- package/dist/stillflow-adapters/dom/compiler/test.js +0 -59
- 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/dom.js +0 -1
- 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-adapters/dom/types.js +0 -1
- package/dist/stillflow-adapters/dom/vm/compile.d.ts +0 -8
- package/dist/stillflow-adapters/dom/vm/compile.js +0 -8
- package/dist/stillflow-adapters/dom/vm/instructions/instructions.d.ts +0 -1
- package/dist/stillflow-adapters/dom/vm/instructions/instructions.js +0 -1
- package/dist/stillflow-adapters/dom/vm/resources.d.ts +0 -1
- package/dist/stillflow-adapters/dom/vm/resources.js +0 -1
- package/dist/stillflow-examples/files.d.ts +0 -1
- package/dist/stillflow-examples/files.js +0 -29
- 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/{compiler/apply-generator.d.ts → aot/dom.directives.js} +0 -0
- /package/dist/stillflow-adapters/dom/{compiler/apply-generator.js → aot/dom.types.js} +0 -0
package/README.md
CHANGED
|
@@ -42,8 +42,18 @@ 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
|
-
import { Stillflow, State,
|
|
56
|
+
import { Stillflow, State, collection, operators, adapters } from 'stillflow.engine'
|
|
47
57
|
|
|
48
58
|
type AppState = {
|
|
49
59
|
input: string
|
|
@@ -55,7 +65,7 @@ type TodoItem = {
|
|
|
55
65
|
|
|
56
66
|
type TodoState = {
|
|
57
67
|
input: string,
|
|
58
|
-
items:
|
|
68
|
+
items: collection.Flushable<TodoItem>
|
|
59
69
|
}
|
|
60
70
|
|
|
61
71
|
const { jsxdom, dom } = adapters
|
|
@@ -64,11 +74,11 @@ const state = new State()
|
|
|
64
74
|
const stillflow = new Stillflow<AppState>(state)
|
|
65
75
|
const factory = stillflow.factory()
|
|
66
76
|
|
|
67
|
-
factory.add(
|
|
77
|
+
factory.add(collection.Flushable.name, () => new collection.Flushable(new collection.Fenwick(100)))
|
|
68
78
|
|
|
69
79
|
const source = {
|
|
70
80
|
input: stillflow.variable('input').backref(),
|
|
71
|
-
items: operators.is(
|
|
81
|
+
items: operators.is(collection.Flushable.name)
|
|
72
82
|
}
|
|
73
83
|
|
|
74
84
|
const view = stillflow.compile(source)
|
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 };
|
|
@@ -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 {};
|
|
@@ -1,56 +1,45 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
function markup(root, paths) {
|
|
15
|
-
const node = root;
|
|
16
|
-
if (node[SF_CACHE]) {
|
|
17
|
-
return node[SF_CACHE];
|
|
18
|
-
}
|
|
19
|
-
const cache = new Array(paths.length);
|
|
20
|
-
const text_cache = new Array(paths.length);
|
|
21
|
-
for (let id = 0; id < paths.length; id++) {
|
|
22
|
-
cache[id] = find_element_by(root, paths[id]);
|
|
23
|
-
text_cache[id] = cache[id].firstChild;
|
|
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
|
+
const ANY = new Proxy({}, {
|
|
5
|
+
get(target, name) {
|
|
6
|
+
if (name === 'toString') {
|
|
7
|
+
return () => String();
|
|
8
|
+
}
|
|
9
|
+
return ANY;
|
|
10
|
+
},
|
|
11
|
+
apply(target, thisArg, argArray) {
|
|
12
|
+
return ANY;
|
|
24
13
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
29
|
-
export function generate_applier(template, pool) {
|
|
30
|
-
const { instructions, paths } = template;
|
|
14
|
+
});
|
|
15
|
+
export function apply(node, delegator, pool) {
|
|
16
|
+
const instructions = compile(node);
|
|
31
17
|
const payload = (id) => `p${id}`;
|
|
32
|
-
const payloads = [
|
|
18
|
+
const payloads = [markup_template(node)];
|
|
33
19
|
let code = `return function(root, context) {\n`;
|
|
34
|
-
|
|
20
|
+
let labels = 0;
|
|
21
|
+
code += `${payload(0)}(root);`;
|
|
22
|
+
code += `let sf_node_cache = root.${SF_CACHE};`;
|
|
23
|
+
code += `let sf_text_cache = root.${SF_CACHE_TEXT};`;
|
|
35
24
|
for (let id = 0; id < instructions.length; id++) {
|
|
36
25
|
const instruction = instructions[id];
|
|
37
26
|
const index = instruction.id;
|
|
38
27
|
if (instruction.text !== undefined) {
|
|
39
28
|
const pIndex = payloads.length;
|
|
40
29
|
payloads.push(instruction.text);
|
|
41
|
-
code += `
|
|
42
|
-
code += `const textnode =
|
|
43
|
-
code += `
|
|
44
|
-
code += `}
|
|
30
|
+
code += `iter_${labels++}: {`;
|
|
31
|
+
code += `const textnode = sf_text_cache[${index}];`;
|
|
32
|
+
code += `textnode.data = ${payload(pIndex)}(context);`;
|
|
33
|
+
code += `}`;
|
|
45
34
|
}
|
|
46
35
|
if (instruction.events !== undefined) {
|
|
47
|
-
code += `
|
|
48
|
-
code += `const element =
|
|
36
|
+
code += `iter_${labels++}: {`;
|
|
37
|
+
code += `const element = sf_node_cache[${index}];`;
|
|
49
38
|
code += `if (!element.${SF_LISTENED}) {`;
|
|
50
39
|
code += `element.${SF_LISTENED} = true;`;
|
|
51
40
|
code += `element.${SF_CONTEXT} = context;`;
|
|
52
41
|
const pIndexRoot = payloads.length;
|
|
53
|
-
payloads.push(
|
|
42
|
+
payloads.push(delegator);
|
|
54
43
|
for (const key in instruction.events) {
|
|
55
44
|
const pIndex = payloads.length;
|
|
56
45
|
payloads.push(instruction.events[key]);
|
|
@@ -60,48 +49,48 @@ export function generate_applier(template, pool) {
|
|
|
60
49
|
code += `else {`;
|
|
61
50
|
code += `element.${SF_CONTEXT} = context;`;
|
|
62
51
|
code += '}';
|
|
63
|
-
code += '}
|
|
52
|
+
code += '}';
|
|
64
53
|
}
|
|
65
54
|
if (instruction.properties !== undefined) {
|
|
66
|
-
code += `
|
|
67
|
-
code += `const element =
|
|
68
|
-
code += `if (element !== undefined) {`;
|
|
55
|
+
code += `iter_${labels++}: {`;
|
|
56
|
+
code += `const element = sf_node_cache[${index}];`;
|
|
69
57
|
for (const key in instruction.properties) {
|
|
70
58
|
const pIndex = payloads.length;
|
|
71
59
|
payloads.push(instruction.properties[key]);
|
|
72
60
|
code += `element.${key} = ${payload(pIndex)}(context);`;
|
|
73
61
|
}
|
|
74
|
-
code +=
|
|
75
|
-
code += '})();';
|
|
62
|
+
code += '}';
|
|
76
63
|
}
|
|
77
64
|
if (instruction.conditional !== undefined) {
|
|
78
65
|
const pIndex = payloads.length;
|
|
79
|
-
payloads.push(instruction.
|
|
80
|
-
code += `
|
|
81
|
-
code += `const element =
|
|
66
|
+
payloads.push(instruction.conditional);
|
|
67
|
+
code += `iter_${labels++}: {`;
|
|
68
|
+
code += `const element = sf_node_cache[${index}];`;
|
|
82
69
|
code += `const condition = ${payload(pIndex)}(context);`;
|
|
83
70
|
code += `element.style.display = condition ? String() : 'none'`;
|
|
84
|
-
code += '}
|
|
71
|
+
code += '}';
|
|
85
72
|
}
|
|
86
73
|
if (instruction.iterate !== undefined) {
|
|
87
74
|
const node = instruction.iterate.each();
|
|
88
|
-
const compiler = new Compiler();
|
|
89
75
|
const template = transform(node);
|
|
90
76
|
const pIndex = payloads.length;
|
|
91
77
|
payloads.push({
|
|
92
|
-
|
|
78
|
+
node,
|
|
79
|
+
any: ANY,
|
|
93
80
|
elements: [],
|
|
94
81
|
template,
|
|
95
82
|
applier: undefined,
|
|
96
83
|
select: instruction.iterate.select,
|
|
97
|
-
generator:
|
|
84
|
+
generator: apply,
|
|
85
|
+
delegator,
|
|
98
86
|
pool
|
|
99
87
|
});
|
|
100
|
-
code += `
|
|
88
|
+
code += `iter_${labels++}: {`;
|
|
89
|
+
code += `const items = ${payload(pIndex)}.select(context);`;
|
|
101
90
|
code += `if (${payload(pIndex)}.applier === undefined) {`;
|
|
102
|
-
code += `${payload(pIndex)}.applier = ${payload(pIndex)}.generator(${payload(pIndex)}.
|
|
91
|
+
code += `${payload(pIndex)}.applier = ${payload(pIndex)}.generator(${payload(pIndex)}.node, ${payload(pIndex)}.delegator, ${payload(pIndex)}.pool);`;
|
|
92
|
+
code += `${payload(pIndex)}.applier(${payload(pIndex)}.template, ${payload(pIndex)}.any);`;
|
|
103
93
|
code += '}';
|
|
104
|
-
code += `const items = ${payload(pIndex)}.select(context);`;
|
|
105
94
|
code += `const target = root.${SF_CACHE}[${index}];`;
|
|
106
95
|
code += `const { replaces, clears, addes, swaps, removes, updates } = items.flush();`;
|
|
107
96
|
code += `if (replaces) {`;
|
|
@@ -123,6 +112,8 @@ export function generate_applier(template, pool) {
|
|
|
123
112
|
code += `const elements = new Array(items_count - child_count);`;
|
|
124
113
|
code += `for (let index = child_count; index < items_count; index++) {`;
|
|
125
114
|
code += `const element = ${payload(pIndex)}.pool.get() || ${payload(pIndex)}.template.cloneNode(true);`;
|
|
115
|
+
code += `element.${SF_HANDLERS} = ${payload(pIndex)}.template.${SF_HANDLERS};`;
|
|
116
|
+
code += `element.${SF_CONTEXT} = items.at(index);`;
|
|
126
117
|
code += `${payload(pIndex)}.applier(element, items.at(index));`;
|
|
127
118
|
code += `elements[index - child_count] = element;`;
|
|
128
119
|
code += `fragment.appendChild(element);`;
|
|
@@ -130,11 +121,12 @@ export function generate_applier(template, pool) {
|
|
|
130
121
|
code += `${payload(pIndex)}.elements = ${payload(pIndex)}.elements.concat(elements);`;
|
|
131
122
|
code += `target.appendChild(fragment);`;
|
|
132
123
|
code += `}`;
|
|
133
|
-
code += `
|
|
124
|
+
code += `break iter_${labels - 1};`;
|
|
134
125
|
code += `}`;
|
|
135
126
|
code += `if (clears) {`;
|
|
136
127
|
code += `const free = ${payload(pIndex)}.elements.splice(0);`;
|
|
137
128
|
code += `for (const element of free) {`;
|
|
129
|
+
code += `element.${SF_HANDLERS} = undefined;`;
|
|
138
130
|
code += `${payload(pIndex)}.pool.add(element);`;
|
|
139
131
|
code += `}`;
|
|
140
132
|
code += `target.innerHTML = String();`;
|
|
@@ -143,7 +135,9 @@ export function generate_applier(template, pool) {
|
|
|
143
135
|
code += `const fragment = document.createDocumentFragment();`;
|
|
144
136
|
code += `const elements = new Array(addes);`;
|
|
145
137
|
code += `for (let index = 0; index < addes; index++) {`;
|
|
146
|
-
code += `const element = ${payload(pIndex)}.template.cloneNode(true);`;
|
|
138
|
+
code += `const element = ${payload(pIndex)}.pool.get() || ${payload(pIndex)}.template.cloneNode(true);`;
|
|
139
|
+
code += `element.${SF_HANDLERS} = ${payload(pIndex)}.template.${SF_HANDLERS};`;
|
|
140
|
+
code += `element.${SF_CONTEXT} = items.at(index);`;
|
|
147
141
|
code += `${payload(pIndex)}.applier(element, items.at(index));`;
|
|
148
142
|
code += `elements[index] = element;`;
|
|
149
143
|
code += `fragment.appendChild(element);`;
|
|
@@ -157,13 +151,14 @@ export function generate_applier(template, pool) {
|
|
|
157
151
|
code += `}`;
|
|
158
152
|
code += `for (const id of removes) {`;
|
|
159
153
|
code += `const element = ${payload(pIndex)}.elements[id];`;
|
|
154
|
+
code += `element.${SF_HANDLERS} = undefined;`;
|
|
160
155
|
code += `${payload(pIndex)}.pool.add(element);`;
|
|
161
156
|
code += `element.remove();`;
|
|
162
157
|
code += `}`;
|
|
163
158
|
code += `for (const id of updates) {`;
|
|
164
159
|
code += `${payload(pIndex)}.applier(${payload(pIndex)}.elements[id], items.at(id));`;
|
|
165
160
|
code += `}`;
|
|
166
|
-
code += `}
|
|
161
|
+
code += `}`;
|
|
167
162
|
}
|
|
168
163
|
}
|
|
169
164
|
code += '}';
|
|
@@ -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 { Listeners, Pool } from "./compiler/pool.js";
|
|
4
|
-
import { transform } from "./compiler/transform.js";
|
|
2
|
+
import { transform } from "./aot/transformers/transform.js";
|
|
5
3
|
import { describe } from "./description/describe.js";
|
|
6
|
-
import {
|
|
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();
|
|
7
8
|
export const DOM_REGISTRY = Object.create(null);
|
|
8
|
-
DOM_REGISTRY.$ = function $(root) {
|
|
9
|
+
DOM_REGISTRY.$ = function $(root, options = { gc: site => site() }) {
|
|
10
|
+
const pool = new Pool();
|
|
9
11
|
const element = transform(root);
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
const
|
|
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>;
|