stillflow.engine 0.1.7 → 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.
@@ -4,6 +4,7 @@ export declare class Buffer<T extends Mapping = Mapping> {
4
4
  private _messages;
5
5
  constructor();
6
6
  write<K extends keyof T>(path: K, value: T[K]): void;
7
+ discard(): void;
7
8
  commit(state: AbstractState<T>): void;
8
9
  }
9
10
  export {};
@@ -4,6 +4,9 @@ export class Buffer {
4
4
  write(path, value) {
5
5
  this._messages.set(path, value);
6
6
  }
7
+ discard() {
8
+ this._messages.clear();
9
+ }
7
10
  commit(state) {
8
11
  for (const [path, value] of this._messages.entries()) {
9
12
  state.write(path, value);
@@ -2,6 +2,7 @@ type Strategy = (site: () => void) => void;
2
2
  export declare class Batcher {
3
3
  private _strategy;
4
4
  private _commiting;
5
+ private _tasks;
5
6
  constructor(_strategy: Strategy);
6
7
  task(task: () => void): void;
7
8
  }
@@ -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
  }
@@ -8,6 +8,7 @@ export type DOMRegistry = {
8
8
  $: <V>(root: AnyDOM<V>) => {
9
9
  element: HTMLElement;
10
10
  update: (context: V) => void;
11
+ clean: () => void;
11
12
  };
12
13
  };
13
14
  export declare const DOM_REGISTRY: DOMRegistry;
@@ -1,17 +1,19 @@
1
1
  import { HTML_TAGS } from "./tags.js";
2
2
  import { Compiler } from "./compiler/compile.js";
3
- import { Appliers } from "./compiler/apply.js";
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);
10
+ const pool = new Pool(new Listeners());
9
11
  const compiler = new Compiler();
10
12
  const compilation = compiler.compile(root);
11
- const appliers = new Appliers();
12
- const applier = appliers.apply(compilation);
13
+ const applier = generate_applier(compilation, pool);
13
14
  const update = (context) => applier(element, context);
14
- return { element, update };
15
+ const clean = () => pool.tick();
16
+ return { element, update, clean };
15
17
  };
16
18
  for (const tag of HTML_TAGS) {
17
19
  DOM_REGISTRY[tag] = (properties, children = []) => {
@@ -1,7 +1,30 @@
1
1
  import { Template } from "./compile.js";
2
+ export declare class Listeners {
3
+ private _listeners;
4
+ private _actives;
5
+ private ensure;
6
+ remove(root: HTMLElement): void;
7
+ add(root: HTMLElement, type: string, listener: (event: Event, context: any) => void): void;
8
+ }
9
+ export declare class Pool {
10
+ private _listeners;
11
+ private static TIME_TO_EVICT;
12
+ private _hots;
13
+ private _colds;
14
+ private _frame;
15
+ constructor(_listeners: Listeners);
16
+ private evict;
17
+ private retrieve;
18
+ get(): HTMLElement | undefined;
19
+ add(root: HTMLElement): void;
20
+ tick(): void;
21
+ get listeners(): Listeners;
22
+ }
2
23
  type Applier<V> = (root: HTMLElement, context: V) => void;
3
24
  export declare class Appliers<V> {
25
+ private _pool;
4
26
  private _listeners;
27
+ constructor(_pool: Pool);
5
28
  private find_element_by;
6
29
  private apply_conditional;
7
30
  private apply_text;
@@ -0,0 +1,5 @@
1
+ import { Pool } from "./pool.js";
2
+ import { Template } from "./compile.js";
3
+ type Applier<T> = (root: HTMLElement, context: T) => void;
4
+ export declare function generate_applier<V>(template: Template<V>, pool: Pool): Applier<V>;
5
+ 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
+ }
@@ -4,7 +4,7 @@ const SF_LISTENED = '__stillflow_listened__';
4
4
  const SF_CONTEXT = '__stillflow_context__';
5
5
  const SF_CACHE_TEXT = '__stillflow_cache_text__';
6
6
  const SF_CACHE = '__stillflow_cache__';
7
- class Listeners {
7
+ export class Listeners {
8
8
  _listeners = new WeakMap();
9
9
  _actives = new Set();
10
10
  ensure(type) {
@@ -37,7 +37,7 @@ class Listeners {
37
37
  this.ensure(type);
38
38
  }
39
39
  }
40
- class Pool {
40
+ export class Pool {
41
41
  _listeners;
42
42
  static TIME_TO_EVICT = 2;
43
43
  _hots = [];
@@ -85,9 +85,11 @@ class Pool {
85
85
  }
86
86
  tick() {
87
87
  this.evict();
88
- console.log(this._frame);
89
88
  this._frame++;
90
89
  }
90
+ get listeners() {
91
+ return this._listeners;
92
+ }
91
93
  }
92
94
  class CollectionApplier {
93
95
  _pool;
@@ -104,22 +106,12 @@ class CollectionApplier {
104
106
  this._payload = _payload;
105
107
  const node = this._payload.each();
106
108
  const compiler = new Compiler();
107
- const appliers = new Appliers();
109
+ const appliers = new Appliers(this._pool);
108
110
  const template = transform(node);
109
111
  const compilation = compiler.compile(node);
110
112
  this._appliers = appliers;
111
113
  this._compilation = compilation;
112
114
  this._template = template;
113
- const pool = this._pool;
114
- function schedule() {
115
- requestIdleCallback(function clean(deadline) {
116
- if (deadline.timeRemaining() > 0 || deadline.didTimeout) {
117
- pool.tick();
118
- }
119
- schedule();
120
- }, { timeout: 1000 });
121
- }
122
- schedule();
123
115
  }
124
116
  apply(root, context) {
125
117
  if (this._applier === undefined) {
@@ -192,7 +184,12 @@ class CollectionApplier {
192
184
  }
193
185
  }
194
186
  export class Appliers {
195
- _listeners = new Listeners();
187
+ _pool;
188
+ _listeners;
189
+ constructor(_pool) {
190
+ this._pool = _pool;
191
+ this._listeners = this._pool.listeners;
192
+ }
196
193
  find_element_by(root, path) {
197
194
  let current = root;
198
195
  for (let id = 0; id < path.length; id++) {
@@ -241,8 +238,7 @@ export class Appliers {
241
238
  };
242
239
  }
243
240
  apply_iterate(id, payload) {
244
- const pool = new Pool(this._listeners);
245
- const appliers = new CollectionApplier(pool, id, payload);
241
+ const appliers = new CollectionApplier(this._pool, id, payload);
246
242
  return (root, context) => appliers.apply(root, context);
247
243
  }
248
244
  markup(root, paths) {
@@ -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,8 @@
1
+ export function traverse(node, path, site) {
2
+ site(node, path);
3
+ for (let id = 0; id < node.children.length; id++) {
4
+ traverse(node.children[id], [...path, id], site);
5
+ }
6
+ }
7
+ export function compile(node) {
8
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stillflow.engine",
3
- "version": "0.1.7",
3
+ "version": "0.1.9",
4
4
  "description": "A headless, transaction-based UI engine with AOT compilation.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",