stillflow.engine 0.1.9 → 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.
Files changed (68) hide show
  1. package/README.md +10 -0
  2. package/dist/index.d.ts +2 -4
  3. package/dist/index.js +2 -3
  4. package/dist/stillflow-adapters/dom/aot/appliers/apply.d.ts +6 -0
  5. package/dist/stillflow-adapters/dom/{compiler/apply.generator.js → aot/appliers/apply.js} +40 -57
  6. package/dist/stillflow-adapters/dom/aot/compiler/compile.d.ts +11 -0
  7. package/dist/stillflow-adapters/dom/aot/compiler/compile.js +71 -0
  8. package/dist/stillflow-adapters/dom/aot/dom.delegator.d.ts +6 -0
  9. package/dist/stillflow-adapters/dom/aot/dom.delegator.js +39 -0
  10. package/dist/stillflow-adapters/dom/aot/dom.directives.d.ts +16 -0
  11. package/dist/stillflow-adapters/dom/aot/dom.operations.d.ts +3 -0
  12. package/dist/stillflow-adapters/dom/aot/dom.operations.js +1 -0
  13. package/dist/stillflow-adapters/dom/aot/dom.pool.d.ts +11 -0
  14. package/dist/stillflow-adapters/dom/aot/dom.pool.js +47 -0
  15. package/dist/stillflow-adapters/dom/aot/dom.resources.d.ts +3 -0
  16. package/dist/stillflow-adapters/dom/aot/dom.resources.js +3 -0
  17. package/dist/stillflow-adapters/dom/aot/dom.symbols.d.ts +5 -0
  18. package/dist/stillflow-adapters/dom/aot/dom.symbols.js +5 -0
  19. package/dist/stillflow-adapters/dom/{dom.d.ts → aot/dom.types.d.ts} +2 -2
  20. package/dist/stillflow-adapters/dom/aot/transformers/transform.d.ts +3 -0
  21. package/dist/stillflow-adapters/dom/aot/transformers/transform.js +42 -0
  22. package/dist/stillflow-adapters/dom/base.dom.d.ts +5 -3
  23. package/dist/stillflow-adapters/dom/base.dom.js +20 -12
  24. package/dist/stillflow-adapters/dom/description/describe.d.ts +1 -1
  25. package/dist/stillflow-adapters/dom/jsx.dom.d.ts +3 -3
  26. package/dist/stillflow-adapters/dom/properties/modifiers.d.ts +3 -4
  27. package/dist/stillflow-collection/index.d.ts +3 -0
  28. package/dist/stillflow-collection/index.js +2 -0
  29. package/dist/stillflow-examples/app.js +7 -5
  30. package/package.json +1 -1
  31. package/dist/stillflow-adapters/dom/compiler/apply.d.ts +0 -37
  32. package/dist/stillflow-adapters/dom/compiler/apply.generator.d.ts +0 -5
  33. package/dist/stillflow-adapters/dom/compiler/apply.js +0 -289
  34. package/dist/stillflow-adapters/dom/compiler/compile.d.ts +0 -18
  35. package/dist/stillflow-adapters/dom/compiler/compile.js +0 -47
  36. package/dist/stillflow-adapters/dom/compiler/pool.d.ts +0 -21
  37. package/dist/stillflow-adapters/dom/compiler/pool.js +0 -91
  38. package/dist/stillflow-adapters/dom/compiler/test.d.ts +0 -1
  39. package/dist/stillflow-adapters/dom/compiler/test.js +0 -59
  40. package/dist/stillflow-adapters/dom/compiler/transform.d.ts +0 -2
  41. package/dist/stillflow-adapters/dom/compiler/transform.js +0 -41
  42. package/dist/stillflow-adapters/dom/dom.js +0 -1
  43. package/dist/stillflow-adapters/dom/index.d.ts +0 -15
  44. package/dist/stillflow-adapters/dom/index.js +0 -22
  45. package/dist/stillflow-adapters/dom/types.d.ts +0 -4
  46. package/dist/stillflow-adapters/dom/types.js +0 -1
  47. package/dist/stillflow-adapters/dom/vm/compile.d.ts +0 -8
  48. package/dist/stillflow-adapters/dom/vm/compile.js +0 -8
  49. package/dist/stillflow-adapters/dom/vm/instructions/instructions.d.ts +0 -1
  50. package/dist/stillflow-adapters/dom/vm/instructions/instructions.js +0 -1
  51. package/dist/stillflow-adapters/dom/vm/resources.d.ts +0 -1
  52. package/dist/stillflow-adapters/dom/vm/resources.js +0 -1
  53. package/dist/stillflow-examples/files.d.ts +0 -1
  54. package/dist/stillflow-examples/files.js +0 -29
  55. package/dist/stillflow-examples/test.d.ts +0 -1
  56. package/dist/stillflow-examples/test.js +0 -35
  57. package/dist/stillflow-vm/analyze.d.ts +0 -7
  58. package/dist/stillflow-vm/analyze.js +0 -22
  59. package/dist/stillflow-vm/buffer.d.ts +0 -1
  60. package/dist/stillflow-vm/buffer.js +0 -1
  61. package/dist/stillflow-vm/instruction.d.ts +0 -1
  62. package/dist/stillflow-vm/instruction.js +0 -1
  63. package/dist/stillflow-vm/pool.d.ts +0 -1
  64. package/dist/stillflow-vm/pool.js +0 -1
  65. package/dist/stillflow-vm/resources.d.ts +0 -14
  66. package/dist/stillflow-vm/resources.js +0 -16
  67. /package/dist/stillflow-adapters/dom/{compiler/apply-generator.d.ts → aot/dom.directives.js} +0 -0
  68. /package/dist/stillflow-adapters/dom/{compiler/apply-generator.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 };
@@ -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,34 @@
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;
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);
31
6
  const payload = (id) => `p${id}`;
32
- const payloads = [markup, paths];
7
+ const payloads = [markup_template(node)];
33
8
  let code = `return function(root, context) {\n`;
34
- code += `${payload(0)}(root, ${payload(1)});`;
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};`;
35
13
  for (let id = 0; id < instructions.length; id++) {
36
14
  const instruction = instructions[id];
37
15
  const index = instruction.id;
38
16
  if (instruction.text !== undefined) {
39
17
  const pIndex = payloads.length;
40
18
  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 += `})();`;
19
+ code += `iter_${labels++}: {`;
20
+ code += `const textnode = sf_text_cache[${index}];`;
21
+ code += `textnode.data = ${payload(pIndex)}(context);`;
22
+ code += `}`;
45
23
  }
46
24
  if (instruction.events !== undefined) {
47
- code += `(function() {`;
48
- code += `const element = root.${SF_CACHE}[${index}];`;
25
+ code += `iter_${labels++}: {`;
26
+ code += `const element = sf_node_cache[${index}];`;
49
27
  code += `if (!element.${SF_LISTENED}) {`;
50
28
  code += `element.${SF_LISTENED} = true;`;
51
29
  code += `element.${SF_CONTEXT} = context;`;
52
30
  const pIndexRoot = payloads.length;
53
- payloads.push(pool.listeners);
31
+ payloads.push(delegator);
54
32
  for (const key in instruction.events) {
55
33
  const pIndex = payloads.length;
56
34
  payloads.push(instruction.events[key]);
@@ -60,46 +38,45 @@ export function generate_applier(template, pool) {
60
38
  code += `else {`;
61
39
  code += `element.${SF_CONTEXT} = context;`;
62
40
  code += '}';
63
- code += '})();';
41
+ code += '}';
64
42
  }
65
43
  if (instruction.properties !== undefined) {
66
- code += `(function() {`;
67
- code += `const element = root.${SF_CACHE}[${index}];`;
68
- code += `if (element !== undefined) {`;
44
+ code += `iter_${labels++}: {`;
45
+ code += `const element = sf_node_cache[${index}];`;
69
46
  for (const key in instruction.properties) {
70
47
  const pIndex = payloads.length;
71
48
  payloads.push(instruction.properties[key]);
72
49
  code += `element.${key} = ${payload(pIndex)}(context);`;
73
50
  }
74
- code += `}`;
75
- code += '})();';
51
+ code += '}';
76
52
  }
77
53
  if (instruction.conditional !== undefined) {
78
54
  const pIndex = payloads.length;
79
- payloads.push(instruction.text);
80
- code += `(function () {`;
81
- code += `const element = root.${SF_CACHE}[${index}];`;
55
+ payloads.push(instruction.conditional);
56
+ code += `iter_${labels++}: {`;
57
+ code += `const element = sf_node_cache[${index}];`;
82
58
  code += `const condition = ${payload(pIndex)}(context);`;
83
59
  code += `element.style.display = condition ? String() : 'none'`;
84
- code += '})();';
60
+ code += '}';
85
61
  }
86
62
  if (instruction.iterate !== undefined) {
87
63
  const node = instruction.iterate.each();
88
- const compiler = new Compiler();
89
64
  const template = transform(node);
90
65
  const pIndex = payloads.length;
91
66
  payloads.push({
92
- compilation: compiler.compile(node),
67
+ node,
93
68
  elements: [],
94
69
  template,
95
70
  applier: undefined,
96
71
  select: instruction.iterate.select,
97
- generator: generate_applier,
72
+ generator: apply,
73
+ delegator,
98
74
  pool
99
75
  });
100
- code += `(function () {`;
76
+ code += `iter_${labels++}: {`;
101
77
  code += `if (${payload(pIndex)}.applier === undefined) {`;
102
- code += `${payload(pIndex)}.applier = ${payload(pIndex)}.generator(${payload(pIndex)}.compilation, ${payload(pIndex)}.pool)`;
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);`;
103
80
  code += '}';
104
81
  code += `const items = ${payload(pIndex)}.select(context);`;
105
82
  code += `const target = root.${SF_CACHE}[${index}];`;
@@ -123,6 +100,8 @@ export function generate_applier(template, pool) {
123
100
  code += `const elements = new Array(items_count - child_count);`;
124
101
  code += `for (let index = child_count; index < items_count; index++) {`;
125
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);`;
126
105
  code += `${payload(pIndex)}.applier(element, items.at(index));`;
127
106
  code += `elements[index - child_count] = element;`;
128
107
  code += `fragment.appendChild(element);`;
@@ -130,11 +109,12 @@ export function generate_applier(template, pool) {
130
109
  code += `${payload(pIndex)}.elements = ${payload(pIndex)}.elements.concat(elements);`;
131
110
  code += `target.appendChild(fragment);`;
132
111
  code += `}`;
133
- code += `return;`;
112
+ code += `break iter_${labels - 1};`;
134
113
  code += `}`;
135
114
  code += `if (clears) {`;
136
115
  code += `const free = ${payload(pIndex)}.elements.splice(0);`;
137
116
  code += `for (const element of free) {`;
117
+ code += `element.${SF_HANDLERS} = undefined;`;
138
118
  code += `${payload(pIndex)}.pool.add(element);`;
139
119
  code += `}`;
140
120
  code += `target.innerHTML = String();`;
@@ -143,7 +123,9 @@ export function generate_applier(template, pool) {
143
123
  code += `const fragment = document.createDocumentFragment();`;
144
124
  code += `const elements = new Array(addes);`;
145
125
  code += `for (let index = 0; index < addes; index++) {`;
146
- code += `const element = ${payload(pIndex)}.template.cloneNode(true);`;
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);`;
147
129
  code += `${payload(pIndex)}.applier(element, items.at(index));`;
148
130
  code += `elements[index] = element;`;
149
131
  code += `fragment.appendChild(element);`;
@@ -157,13 +139,14 @@ export function generate_applier(template, pool) {
157
139
  code += `}`;
158
140
  code += `for (const id of removes) {`;
159
141
  code += `const element = ${payload(pIndex)}.elements[id];`;
142
+ code += `element.${SF_HANDLERS} = undefined;`;
160
143
  code += `${payload(pIndex)}.pool.add(element);`;
161
144
  code += `element.remove();`;
162
145
  code += `}`;
163
146
  code += `for (const id of updates) {`;
164
147
  code += `${payload(pIndex)}.applier(${payload(pIndex)}.elements[id], items.at(id));`;
165
148
  code += `}`;
166
- code += `})();`;
149
+ code += `}`;
167
150
  }
168
151
  }
169
152
  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,6 @@
1
+ export declare class EventDelegator {
2
+ private _actives;
3
+ private ensure;
4
+ remove(root: HTMLElement): void;
5
+ add(root: HTMLElement, type: string, listener: (event: Event, context: any) => void): void;
6
+ }
@@ -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,3 @@
1
+ type Operation = (bytecode: Uint32Array, offset: number) => number;
2
+ export declare const OPERATIONS: Operation[];
3
+ export { Operation };
@@ -0,0 +1 @@
1
+ export const OPERATIONS = [];
@@ -0,0 +1,11 @@
1
+ export declare class Pool {
2
+ private static TIME_TO_EVICT;
3
+ private _hots;
4
+ private _colds;
5
+ private _frame;
6
+ private evict;
7
+ private retrieve;
8
+ get(): HTMLElement | undefined;
9
+ add(root: HTMLElement): void;
10
+ tick(): void;
11
+ }
@@ -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,3 @@
1
+ export declare const STRINGS: string[];
2
+ export declare const BUFFERS: Uint32Array[];
3
+ export declare const FUNCTIONS: Function[];
@@ -0,0 +1,3 @@
1
+ export const STRINGS = [];
2
+ export const BUFFERS = [];
3
+ export const FUNCTIONS = [];
@@ -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__";
@@ -0,0 +1,5 @@
1
+ export const SF_LISTENED = '__stillflow_listened__';
2
+ export const SF_CONTEXT = '__stillflow_context__';
3
+ export const SF_CACHE_TEXT = '__stillflow_cache_text__';
4
+ export const SF_CACHE = '__stillflow_cache__';
5
+ export const SF_HANDLERS = `__stillflow_handlers__`;
@@ -1,4 +1,4 @@
1
- import { Binding } from "../binding.js";
2
- import { Properties } from "./properties/properties.js";
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,3 @@
1
+ import { DOM } from "../dom.types.js";
2
+ declare function transform<V, K extends keyof HTMLElementTagNameMap>(description: DOM<V, K>): HTMLElementTagNameMap[K];
3
+ export { transform };
@@ -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 { Compiler } from "./compiler/compile.js";
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 { generate_applier } from "./compiler/apply.generator.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();
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 pool = new Pool(new Listeners());
11
- const compiler = new Compiler();
12
- const compilation = compiler.compile(root);
13
- const applier = generate_applier(compilation, pool);
14
- const update = (context) => applier(element, context);
15
- const clean = () => pool.tick();
16
- return { element, update, clean };
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 { FlushableCollection } from "../../index.js";
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) => FlushableCollection<unknown>;
12
+ 'for': (context: V) => collection.Flushable<unknown>;
13
13
  'each': () => AnyDOM<V>;
14
14
  };
15
15
  type ElementServicePropertiesWithPrefix<V> = {
@@ -1,8 +1,7 @@
1
- import { FlushableCollection } from "../../../index.js";
2
- import { AnyDOM } from "../types.js";
1
+ import { collection } from "../../../index.js";
2
+ import { AnyDOM } from "../aot/dom.types.js";
3
3
  export type Modifiers<Context, K extends keyof HTMLElementTagNameMap = any> = Partial<{
4
4
  text: (context: Context) => string;
5
- lens: <Another>(context: Context) => Another;
6
5
  conditional: (context: Context) => boolean;
7
6
  events: Partial<{
8
7
  [L in keyof HTMLElementEventMap]: (event: HTMLElementEventMap[L], context: Context) => void;
@@ -11,7 +10,7 @@ export type Modifiers<Context, K extends keyof HTMLElementTagNameMap = any> = Pa
11
10
  [L in keyof HTMLElementTagNameMap[K]]: (context: Context) => string;
12
11
  }>;
13
12
  iterate: {
14
- select: <V>(context: Context) => FlushableCollection<V>;
13
+ select: <V>(context: Context) => collection.Flushable<V>;
15
14
  each: () => AnyDOM<any>;
16
15
  };
17
16
  }>;
@@ -0,0 +1,3 @@
1
+ export { FlushableCollection as Flushable } from "./flushable.js";
2
+ export { FenwickCollection as Fenwick } from "./fenwick.js";
3
+ export { AbstractCollection as Abstract } from './abstract/collection.js';
@@ -0,0 +1,2 @@
1
+ export { FlushableCollection as Flushable } from "./flushable.js";
2
+ export { FenwickCollection as Fenwick } from "./fenwick.js";