stillflow.engine 0.1.8 → 0.1.9
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/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/base.dom.js +3 -3
- package/dist/stillflow-adapters/dom/compiler/apply-generator.d.ts +1 -0
- package/dist/stillflow-adapters/dom/compiler/apply-generator.js +1 -0
- package/dist/stillflow-adapters/dom/compiler/apply.generator.d.ts +5 -0
- package/dist/stillflow-adapters/dom/compiler/apply.generator.js +172 -0
- package/dist/stillflow-adapters/dom/compiler/pool.d.ts +21 -0
- package/dist/stillflow-adapters/dom/compiler/pool.js +91 -0
- package/dist/stillflow-adapters/dom/compiler/test.d.ts +1 -0
- package/dist/stillflow-adapters/dom/compiler/test.js +59 -0
- package/dist/stillflow-adapters/dom/vm/compile.d.ts +8 -0
- package/dist/stillflow-adapters/dom/vm/compile.js +8 -0
- package/dist/stillflow-adapters/dom/vm/instructions/instructions.d.ts +1 -0
- package/dist/stillflow-adapters/dom/vm/instructions/instructions.js +1 -0
- package/dist/stillflow-adapters/dom/vm/resources.d.ts +1 -0
- package/dist/stillflow-adapters/dom/vm/resources.js +1 -0
- package/package.json +1 -1
|
@@ -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
|
}
|
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import { HTML_TAGS } from "./tags.js";
|
|
2
2
|
import { Compiler } from "./compiler/compile.js";
|
|
3
|
-
import {
|
|
3
|
+
import { Listeners, Pool } from "./compiler/pool.js";
|
|
4
4
|
import { transform } from "./compiler/transform.js";
|
|
5
5
|
import { describe } from "./description/describe.js";
|
|
6
|
+
import { generate_applier } from "./compiler/apply.generator.js";
|
|
6
7
|
export const DOM_REGISTRY = Object.create(null);
|
|
7
8
|
DOM_REGISTRY.$ = function $(root) {
|
|
8
9
|
const element = transform(root);
|
|
9
10
|
const pool = new Pool(new Listeners());
|
|
10
11
|
const compiler = new Compiler();
|
|
11
12
|
const compilation = compiler.compile(root);
|
|
12
|
-
const
|
|
13
|
-
const applier = appliers.apply(compilation);
|
|
13
|
+
const applier = generate_applier(compilation, pool);
|
|
14
14
|
const update = (context) => applier(element, context);
|
|
15
15
|
const clean = () => pool.tick();
|
|
16
16
|
return { element, update, clean };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { Compiler } from "./compile.js";
|
|
2
|
+
import { transform } from "./transform.js";
|
|
3
|
+
const SF_LISTENED = '__stillflow_listened__';
|
|
4
|
+
const SF_CONTEXT = '__stillflow_context__';
|
|
5
|
+
const SF_CACHE_TEXT = '__stillflow_cache_text__';
|
|
6
|
+
const SF_CACHE = '__stillflow_cache__';
|
|
7
|
+
function find_element_by(root, path) {
|
|
8
|
+
let current = root;
|
|
9
|
+
for (let id = 0; id < path.length; id++) {
|
|
10
|
+
current = current.children[path[id]];
|
|
11
|
+
}
|
|
12
|
+
return current;
|
|
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;
|
|
24
|
+
}
|
|
25
|
+
node[SF_CACHE] = cache;
|
|
26
|
+
node[SF_CACHE_TEXT] = text_cache;
|
|
27
|
+
return cache;
|
|
28
|
+
}
|
|
29
|
+
export function generate_applier(template, pool) {
|
|
30
|
+
const { instructions, paths } = template;
|
|
31
|
+
const payload = (id) => `p${id}`;
|
|
32
|
+
const payloads = [markup, paths];
|
|
33
|
+
let code = `return function(root, context) {\n`;
|
|
34
|
+
code += `${payload(0)}(root, ${payload(1)});`;
|
|
35
|
+
for (let id = 0; id < instructions.length; id++) {
|
|
36
|
+
const instruction = instructions[id];
|
|
37
|
+
const index = instruction.id;
|
|
38
|
+
if (instruction.text !== undefined) {
|
|
39
|
+
const pIndex = payloads.length;
|
|
40
|
+
payloads.push(instruction.text);
|
|
41
|
+
code += `(function() {`;
|
|
42
|
+
code += `const textnode = root.${SF_CACHE_TEXT}[${index}];`;
|
|
43
|
+
code += `if (textnode !== null) textnode.nodeValue = ${payload(pIndex)}(context);`;
|
|
44
|
+
code += `})();`;
|
|
45
|
+
}
|
|
46
|
+
if (instruction.events !== undefined) {
|
|
47
|
+
code += `(function() {`;
|
|
48
|
+
code += `const element = root.${SF_CACHE}[${index}];`;
|
|
49
|
+
code += `if (!element.${SF_LISTENED}) {`;
|
|
50
|
+
code += `element.${SF_LISTENED} = true;`;
|
|
51
|
+
code += `element.${SF_CONTEXT} = context;`;
|
|
52
|
+
const pIndexRoot = payloads.length;
|
|
53
|
+
payloads.push(pool.listeners);
|
|
54
|
+
for (const key in instruction.events) {
|
|
55
|
+
const pIndex = payloads.length;
|
|
56
|
+
payloads.push(instruction.events[key]);
|
|
57
|
+
code += `${payload(pIndexRoot)}.add(element, "${key}", ${payload(pIndex)});`;
|
|
58
|
+
}
|
|
59
|
+
code += `}`;
|
|
60
|
+
code += `else {`;
|
|
61
|
+
code += `element.${SF_CONTEXT} = context;`;
|
|
62
|
+
code += '}';
|
|
63
|
+
code += '})();';
|
|
64
|
+
}
|
|
65
|
+
if (instruction.properties !== undefined) {
|
|
66
|
+
code += `(function() {`;
|
|
67
|
+
code += `const element = root.${SF_CACHE}[${index}];`;
|
|
68
|
+
code += `if (element !== undefined) {`;
|
|
69
|
+
for (const key in instruction.properties) {
|
|
70
|
+
const pIndex = payloads.length;
|
|
71
|
+
payloads.push(instruction.properties[key]);
|
|
72
|
+
code += `element.${key} = ${payload(pIndex)}(context);`;
|
|
73
|
+
}
|
|
74
|
+
code += `}`;
|
|
75
|
+
code += '})();';
|
|
76
|
+
}
|
|
77
|
+
if (instruction.conditional !== undefined) {
|
|
78
|
+
const pIndex = payloads.length;
|
|
79
|
+
payloads.push(instruction.text);
|
|
80
|
+
code += `(function () {`;
|
|
81
|
+
code += `const element = root.${SF_CACHE}[${index}];`;
|
|
82
|
+
code += `const condition = ${payload(pIndex)}(context);`;
|
|
83
|
+
code += `element.style.display = condition ? String() : 'none'`;
|
|
84
|
+
code += '})();';
|
|
85
|
+
}
|
|
86
|
+
if (instruction.iterate !== undefined) {
|
|
87
|
+
const node = instruction.iterate.each();
|
|
88
|
+
const compiler = new Compiler();
|
|
89
|
+
const template = transform(node);
|
|
90
|
+
const pIndex = payloads.length;
|
|
91
|
+
payloads.push({
|
|
92
|
+
compilation: compiler.compile(node),
|
|
93
|
+
elements: [],
|
|
94
|
+
template,
|
|
95
|
+
applier: undefined,
|
|
96
|
+
select: instruction.iterate.select,
|
|
97
|
+
generator: generate_applier,
|
|
98
|
+
pool
|
|
99
|
+
});
|
|
100
|
+
code += `(function () {`;
|
|
101
|
+
code += `if (${payload(pIndex)}.applier === undefined) {`;
|
|
102
|
+
code += `${payload(pIndex)}.applier = ${payload(pIndex)}.generator(${payload(pIndex)}.compilation, ${payload(pIndex)}.pool)`;
|
|
103
|
+
code += '}';
|
|
104
|
+
code += `const items = ${payload(pIndex)}.select(context);`;
|
|
105
|
+
code += `const target = root.${SF_CACHE}[${index}];`;
|
|
106
|
+
code += `const { replaces, clears, addes, swaps, removes, updates } = items.flush();`;
|
|
107
|
+
code += `if (replaces) {`;
|
|
108
|
+
code += `const items_count = items.size;`;
|
|
109
|
+
code += `const child_count = ${payload(pIndex)}.elements.length;`;
|
|
110
|
+
code += `const common_count = Math.min(items_count, child_count);`;
|
|
111
|
+
code += `for (let index = 0; index < common_count; index++) {`;
|
|
112
|
+
code += `${payload(pIndex)}.applier(${payload(pIndex)}.elements[index], items.at(index));`;
|
|
113
|
+
code += `}`;
|
|
114
|
+
code += `if (items_count < child_count) {`;
|
|
115
|
+
code += `for (let index = items_count; index < child_count; index++) {`;
|
|
116
|
+
code += `const element = ${payload(pIndex)}.elements[index];`;
|
|
117
|
+
code += `${payload(pIndex)}.pool.add(element);`;
|
|
118
|
+
code += `element.remove();`;
|
|
119
|
+
code += `}`;
|
|
120
|
+
code += `${payload(pIndex)}.elements.length = items_count;`;
|
|
121
|
+
code += `} else if (child_count < items_count) {`;
|
|
122
|
+
code += `const fragment = document.createDocumentFragment();`;
|
|
123
|
+
code += `const elements = new Array(items_count - child_count);`;
|
|
124
|
+
code += `for (let index = child_count; index < items_count; index++) {`;
|
|
125
|
+
code += `const element = ${payload(pIndex)}.pool.get() || ${payload(pIndex)}.template.cloneNode(true);`;
|
|
126
|
+
code += `${payload(pIndex)}.applier(element, items.at(index));`;
|
|
127
|
+
code += `elements[index - child_count] = element;`;
|
|
128
|
+
code += `fragment.appendChild(element);`;
|
|
129
|
+
code += `}`;
|
|
130
|
+
code += `${payload(pIndex)}.elements = ${payload(pIndex)}.elements.concat(elements);`;
|
|
131
|
+
code += `target.appendChild(fragment);`;
|
|
132
|
+
code += `}`;
|
|
133
|
+
code += `return;`;
|
|
134
|
+
code += `}`;
|
|
135
|
+
code += `if (clears) {`;
|
|
136
|
+
code += `const free = ${payload(pIndex)}.elements.splice(0);`;
|
|
137
|
+
code += `for (const element of free) {`;
|
|
138
|
+
code += `${payload(pIndex)}.pool.add(element);`;
|
|
139
|
+
code += `}`;
|
|
140
|
+
code += `target.innerHTML = String();`;
|
|
141
|
+
code += `}`;
|
|
142
|
+
code += `if (addes > 0) {`;
|
|
143
|
+
code += `const fragment = document.createDocumentFragment();`;
|
|
144
|
+
code += `const elements = new Array(addes);`;
|
|
145
|
+
code += `for (let index = 0; index < addes; index++) {`;
|
|
146
|
+
code += `const element = ${payload(pIndex)}.template.cloneNode(true);`;
|
|
147
|
+
code += `${payload(pIndex)}.applier(element, items.at(index));`;
|
|
148
|
+
code += `elements[index] = element;`;
|
|
149
|
+
code += `fragment.appendChild(element);`;
|
|
150
|
+
code += `}`;
|
|
151
|
+
code += `${payload(pIndex)}.elements = ${payload(pIndex)}.elements.concat(elements);`;
|
|
152
|
+
code += `target.appendChild(fragment);`;
|
|
153
|
+
code += `}`;
|
|
154
|
+
code += `for (const [i, j] of swaps.entries()) {`;
|
|
155
|
+
code += `${payload(pIndex)}.applier(${payload(pIndex)}.elements[i], items.at(i));`;
|
|
156
|
+
code += `${payload(pIndex)}.applier(${payload(pIndex)}.elements[j], items.at(j));`;
|
|
157
|
+
code += `}`;
|
|
158
|
+
code += `for (const id of removes) {`;
|
|
159
|
+
code += `const element = ${payload(pIndex)}.elements[id];`;
|
|
160
|
+
code += `${payload(pIndex)}.pool.add(element);`;
|
|
161
|
+
code += `element.remove();`;
|
|
162
|
+
code += `}`;
|
|
163
|
+
code += `for (const id of updates) {`;
|
|
164
|
+
code += `${payload(pIndex)}.applier(${payload(pIndex)}.elements[id], items.at(id));`;
|
|
165
|
+
code += `}`;
|
|
166
|
+
code += `})();`;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
code += '}';
|
|
170
|
+
const generator = new Function(...payloads.map((_, id) => payload(id)), code);
|
|
171
|
+
return generator(...payloads);
|
|
172
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export declare class Listeners {
|
|
2
|
+
private _listeners;
|
|
3
|
+
private _actives;
|
|
4
|
+
private ensure;
|
|
5
|
+
remove(root: HTMLElement): void;
|
|
6
|
+
add(root: HTMLElement, type: string, listener: (event: Event, context: any) => void): void;
|
|
7
|
+
}
|
|
8
|
+
export declare class Pool {
|
|
9
|
+
private _listeners;
|
|
10
|
+
private static TIME_TO_EVICT;
|
|
11
|
+
private _hots;
|
|
12
|
+
private _colds;
|
|
13
|
+
private _frame;
|
|
14
|
+
constructor(_listeners: Listeners);
|
|
15
|
+
private evict;
|
|
16
|
+
private retrieve;
|
|
17
|
+
get(): HTMLElement | undefined;
|
|
18
|
+
add(root: HTMLElement): void;
|
|
19
|
+
tick(): void;
|
|
20
|
+
get listeners(): Listeners;
|
|
21
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
const SF_LISTENED = '__stillflow_listened__';
|
|
2
|
+
const SF_CONTEXT = '__stillflow_context__';
|
|
3
|
+
const SF_CACHE_TEXT = '__stillflow_cache_text__';
|
|
4
|
+
const SF_CACHE = '__stillflow_cache__';
|
|
5
|
+
export class Listeners {
|
|
6
|
+
_listeners = new WeakMap();
|
|
7
|
+
_actives = new Set();
|
|
8
|
+
ensure(type) {
|
|
9
|
+
if (this._actives.has(type)) {
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
this._actives.add(type);
|
|
13
|
+
document.body.addEventListener(type, event => {
|
|
14
|
+
let target = event.target;
|
|
15
|
+
while (target && target !== document.body) {
|
|
16
|
+
const listeners = this._listeners.get(target);
|
|
17
|
+
if (listeners !== undefined) {
|
|
18
|
+
const callback = listeners.get(type);
|
|
19
|
+
callback?.(event, target[SF_CONTEXT]);
|
|
20
|
+
if (event.cancelBubble) {
|
|
21
|
+
break;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
target = target.parentElement;
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
remove(root) {
|
|
29
|
+
this._listeners.delete(root);
|
|
30
|
+
}
|
|
31
|
+
add(root, type, listener) {
|
|
32
|
+
const listeners = this._listeners.get(root) || new Map();
|
|
33
|
+
listeners.set(type, listener);
|
|
34
|
+
this._listeners.set(root, listeners);
|
|
35
|
+
this.ensure(type);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
export class Pool {
|
|
39
|
+
_listeners;
|
|
40
|
+
static TIME_TO_EVICT = 2;
|
|
41
|
+
_hots = [];
|
|
42
|
+
_colds = [];
|
|
43
|
+
_frame = 0;
|
|
44
|
+
constructor(_listeners) {
|
|
45
|
+
this._listeners = _listeners;
|
|
46
|
+
}
|
|
47
|
+
evict() {
|
|
48
|
+
for (let id = this._hots.length - 1; id >= 0; id--) {
|
|
49
|
+
const hot = this._hots[id];
|
|
50
|
+
if (this._frame - hot.frame >= Pool.TIME_TO_EVICT) {
|
|
51
|
+
this._listeners.remove(hot.root);
|
|
52
|
+
const element = hot.root;
|
|
53
|
+
element[SF_CONTEXT] = null;
|
|
54
|
+
element[SF_LISTENED] = false;
|
|
55
|
+
this._colds.push(new WeakRef(hot.root));
|
|
56
|
+
this._hots.splice(id, 1);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
retrieve() {
|
|
61
|
+
if (this._hots.length <= 0) {
|
|
62
|
+
while (this._colds.length > 0) {
|
|
63
|
+
const cold = this._colds.pop();
|
|
64
|
+
const element = cold.deref();
|
|
65
|
+
if (element !== undefined) {
|
|
66
|
+
this._hots.push({ root: element, frame: this._frame });
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
get() {
|
|
73
|
+
this.retrieve();
|
|
74
|
+
const element = this._hots.pop();
|
|
75
|
+
if (element === undefined) {
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
element.frame = this._frame;
|
|
79
|
+
return element.root;
|
|
80
|
+
}
|
|
81
|
+
add(root) {
|
|
82
|
+
this._hots.push({ root, frame: this._frame });
|
|
83
|
+
}
|
|
84
|
+
tick() {
|
|
85
|
+
this.evict();
|
|
86
|
+
this._frame++;
|
|
87
|
+
}
|
|
88
|
+
get listeners() {
|
|
89
|
+
return this._listeners;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
return function (root, context) {
|
|
2
|
+
p0(root, p1);
|
|
3
|
+
(function () { const items = p2.select(context); const target = root.__stillflow_cache__[0]; const { replaces, clears, addes, swaps, removes, updates } = items.flush(); if (replaces) {
|
|
4
|
+
const items_count = items.size;
|
|
5
|
+
const child_count = this._elements.length;
|
|
6
|
+
const common_count = Math.min(items_count, child_count);
|
|
7
|
+
for (let index = 0; index < common_count; index++) {
|
|
8
|
+
p2.applier(p2.elements[index], items.at(index));
|
|
9
|
+
}
|
|
10
|
+
if (items_count < child_count) {
|
|
11
|
+
for (let index = items_count; index < child_count; index++) {
|
|
12
|
+
const element = p2.elements[index];
|
|
13
|
+
this._pool.add(element);
|
|
14
|
+
element.remove();
|
|
15
|
+
}
|
|
16
|
+
p2.elements.length = items_count;
|
|
17
|
+
}
|
|
18
|
+
else if (child_count < items_count) {
|
|
19
|
+
const fragment = document.createDocumentFragment();
|
|
20
|
+
const elements = new Array(items_count - child_count);
|
|
21
|
+
for (let index = child_count; index < items_count; index++) {
|
|
22
|
+
const element = p2.pool.get() || p2.template.cloneNode(true);
|
|
23
|
+
p2.applier(element, items.at(index));
|
|
24
|
+
p2.elements[index - child_count] = element;
|
|
25
|
+
fragment.appendChild(element);
|
|
26
|
+
}
|
|
27
|
+
p2.elements = p2.elements.concat(elements);
|
|
28
|
+
target.appendChild(fragment);
|
|
29
|
+
}
|
|
30
|
+
return;
|
|
31
|
+
} if (clears) {
|
|
32
|
+
const free = p2.elements.splice(0);
|
|
33
|
+
for (const element of free) {
|
|
34
|
+
p2.pool.add(element);
|
|
35
|
+
}
|
|
36
|
+
target.innerHTML = String();
|
|
37
|
+
} if (addes > 0) {
|
|
38
|
+
const fragment = document.createDocumentFragment();
|
|
39
|
+
const elements = new Array(addes);
|
|
40
|
+
for (let index = 0; index < addes; index++) {
|
|
41
|
+
const element = p2.template.cloneNode(true);
|
|
42
|
+
p2.applier(element, items.at(index));
|
|
43
|
+
elements[index] = element;
|
|
44
|
+
fragment.appendChild(element);
|
|
45
|
+
}
|
|
46
|
+
p2.elements = p2.elements.concat(elements);
|
|
47
|
+
target.appendChild(fragment);
|
|
48
|
+
} for (const [i, j] of swaps.entries()) {
|
|
49
|
+
p2.applier(p2.elements[i], items.at(i));
|
|
50
|
+
p2.applier(p2.elements[j], items.at(j));
|
|
51
|
+
} for (const id of removes) {
|
|
52
|
+
const element = p2.elements[id];
|
|
53
|
+
this._pool.add(element);
|
|
54
|
+
element.remove();
|
|
55
|
+
} for (const id of updates) {
|
|
56
|
+
p2.applier(p2.elements[id], items.at(id));
|
|
57
|
+
} })();
|
|
58
|
+
};
|
|
59
|
+
export {};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { AnyDOM } from "../types.js";
|
|
2
|
+
export type Instructions = Uint32Array;
|
|
3
|
+
export type Template = {
|
|
4
|
+
paths: number[][];
|
|
5
|
+
stream: Instructions;
|
|
6
|
+
};
|
|
7
|
+
export declare function traverse(node: AnyDOM<any>, path: number[], site: (node: AnyDOM<any>, path: number[]) => void): void;
|
|
8
|
+
export declare function compile(node: AnyDOM<any>): Template;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|