stillflow.engine 0.1.6 → 0.1.8

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 CHANGED
@@ -32,6 +32,10 @@ Also I think that **explicit reactivity is better than implicit reactivity**. So
32
32
 
33
33
  ![Benchmark scores of Stillflow](images/1.png)
34
34
 
35
+ **Stillflow** memory usage score is below
36
+
37
+ ![Memory usage of Stillflow](images/m1.png)
38
+
35
39
  ## Quick Start
36
40
 
37
41
  ### Let's make some simple Todo application
@@ -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 { Appliers, Listeners, Pool } from "./compiler/apply.js";
4
4
  import { transform } from "./compiler/transform.js";
5
5
  import { describe } from "./description/describe.js";
6
6
  export const DOM_REGISTRY = Object.create(null);
7
7
  DOM_REGISTRY.$ = function $(root) {
8
8
  const element = transform(root);
9
+ const pool = new Pool(new Listeners());
9
10
  const compiler = new Compiler();
10
11
  const compilation = compiler.compile(root);
11
- const appliers = new Appliers();
12
+ const appliers = new Appliers(pool);
12
13
  const applier = appliers.apply(compilation);
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,9 +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;
5
- private _actives;
6
- private ensure_listener;
27
+ constructor(_pool: Pool);
7
28
  private find_element_by;
8
29
  private apply_conditional;
9
30
  private apply_text;
@@ -4,20 +4,20 @@ 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
- export class Appliers {
7
+ export class Listeners {
8
8
  _listeners = new WeakMap();
9
9
  _actives = new Set();
10
- ensure_listener(name) {
11
- if (this._actives.has(name)) {
10
+ ensure(type) {
11
+ if (this._actives.has(type)) {
12
12
  return;
13
13
  }
14
- this._actives.add(name);
15
- document.body.addEventListener(name, event => {
14
+ this._actives.add(type);
15
+ document.body.addEventListener(type, event => {
16
16
  let target = event.target;
17
17
  while (target && target !== document.body) {
18
18
  const listeners = this._listeners.get(target);
19
19
  if (listeners !== undefined) {
20
- const callback = listeners.get(name);
20
+ const callback = listeners.get(type);
21
21
  callback?.(event, target[SF_CONTEXT]);
22
22
  if (event.cancelBubble) {
23
23
  break;
@@ -27,6 +27,169 @@ export class Appliers {
27
27
  }
28
28
  });
29
29
  }
30
+ remove(root) {
31
+ this._listeners.delete(root);
32
+ }
33
+ add(root, type, listener) {
34
+ const listeners = this._listeners.get(root) || new Map();
35
+ listeners.set(type, listener);
36
+ this._listeners.set(root, listeners);
37
+ this.ensure(type);
38
+ }
39
+ }
40
+ export class Pool {
41
+ _listeners;
42
+ static TIME_TO_EVICT = 2;
43
+ _hots = [];
44
+ _colds = [];
45
+ _frame = 0;
46
+ constructor(_listeners) {
47
+ this._listeners = _listeners;
48
+ }
49
+ evict() {
50
+ for (let id = this._hots.length - 1; id >= 0; id--) {
51
+ const hot = this._hots[id];
52
+ if (this._frame - hot.frame >= Pool.TIME_TO_EVICT) {
53
+ this._listeners.remove(hot.root);
54
+ const element = hot.root;
55
+ element[SF_CONTEXT] = null;
56
+ element[SF_LISTENED] = false;
57
+ this._colds.push(new WeakRef(hot.root));
58
+ this._hots.splice(id, 1);
59
+ }
60
+ }
61
+ }
62
+ retrieve() {
63
+ if (this._hots.length <= 0) {
64
+ while (this._colds.length > 0) {
65
+ const cold = this._colds.pop();
66
+ const element = cold.deref();
67
+ if (element !== undefined) {
68
+ this._hots.push({ root: element, frame: this._frame });
69
+ return;
70
+ }
71
+ }
72
+ }
73
+ }
74
+ get() {
75
+ this.retrieve();
76
+ const element = this._hots.pop();
77
+ if (element === undefined) {
78
+ return undefined;
79
+ }
80
+ element.frame = this._frame;
81
+ return element.root;
82
+ }
83
+ add(root) {
84
+ this._hots.push({ root, frame: this._frame });
85
+ }
86
+ tick() {
87
+ this.evict();
88
+ this._frame++;
89
+ }
90
+ get listeners() {
91
+ return this._listeners;
92
+ }
93
+ }
94
+ class CollectionApplier {
95
+ _pool;
96
+ _id;
97
+ _payload;
98
+ _compilation;
99
+ _template;
100
+ _elements = [];
101
+ _appliers;
102
+ _applier;
103
+ constructor(_pool, _id, _payload) {
104
+ this._pool = _pool;
105
+ this._id = _id;
106
+ this._payload = _payload;
107
+ const node = this._payload.each();
108
+ const compiler = new Compiler();
109
+ const appliers = new Appliers(this._pool);
110
+ const template = transform(node);
111
+ const compilation = compiler.compile(node);
112
+ this._appliers = appliers;
113
+ this._compilation = compilation;
114
+ this._template = template;
115
+ }
116
+ apply(root, context) {
117
+ if (this._applier === undefined) {
118
+ this._applier = this._appliers.apply(this._compilation);
119
+ }
120
+ const items = this._payload.select(context);
121
+ const nodes = root[SF_CACHE];
122
+ const target = nodes[this._id];
123
+ const { replaces, clears, addes, swaps, removes, updates } = items.flush();
124
+ if (replaces) {
125
+ const items_count = items.size;
126
+ const child_count = this._elements.length;
127
+ const common_count = Math.min(items_count, child_count);
128
+ for (let index = 0; index < common_count; index++) {
129
+ this._applier(this._elements[index], items.at(index));
130
+ }
131
+ if (items_count < child_count) {
132
+ for (let index = items_count; index < child_count; index++) {
133
+ const element = this._elements[index];
134
+ this._pool.add(element);
135
+ element.remove();
136
+ }
137
+ this._elements.length = items_count;
138
+ }
139
+ else if (child_count < items_count) {
140
+ const fragment = document.createDocumentFragment();
141
+ const elements = new Array(items_count - child_count);
142
+ for (let index = child_count; index < items_count; index++) {
143
+ const element = this._pool.get() || this._template.cloneNode(true);
144
+ this._applier(element, items.at(index));
145
+ elements[index - child_count] = element;
146
+ fragment.appendChild(element);
147
+ }
148
+ this._elements = this._elements.concat(elements);
149
+ target.appendChild(fragment);
150
+ }
151
+ return;
152
+ }
153
+ if (clears) {
154
+ const free = this._elements.splice(0);
155
+ for (const element of free) {
156
+ this._pool.add(element);
157
+ }
158
+ target.innerHTML = String();
159
+ }
160
+ if (addes > 0) {
161
+ const fragment = document.createDocumentFragment();
162
+ const elements = new Array(addes);
163
+ for (let index = 0; index < addes; index++) {
164
+ const element = this._template.cloneNode(true);
165
+ this._applier(element, items.at(index));
166
+ elements[index] = element;
167
+ fragment.appendChild(element);
168
+ }
169
+ this._elements = this._elements.concat(elements);
170
+ target.appendChild(fragment);
171
+ }
172
+ for (const [i, j] of swaps.entries()) {
173
+ this._applier(this._elements[i], items.at(i));
174
+ this._applier(this._elements[j], items.at(j));
175
+ }
176
+ for (const id of removes) {
177
+ const element = this._elements[id];
178
+ this._pool.add(element);
179
+ element.remove();
180
+ }
181
+ for (const id of updates) {
182
+ this._applier(this._elements[id], items.at(id));
183
+ }
184
+ }
185
+ }
186
+ export class Appliers {
187
+ _pool;
188
+ _listeners;
189
+ constructor(_pool) {
190
+ this._pool = _pool;
191
+ this._listeners = this._pool.listeners;
192
+ }
30
193
  find_element_by(root, path) {
31
194
  let current = root;
32
195
  for (let id = 0; id < path.length; id++) {
@@ -69,89 +232,14 @@ export class Appliers {
69
232
  }
70
233
  element[SF_LISTENED] = true;
71
234
  element[SF_CONTEXT] = context;
72
- const listeners = this._listeners.get(element) || new Map();
73
235
  for (let key in payload) {
74
- listeners.set(key, payload[key]);
75
- this.ensure_listener(key);
236
+ this._listeners.add(element, key, payload[key]);
76
237
  }
77
- this._listeners.set(element, listeners);
78
238
  };
79
239
  }
80
240
  apply_iterate(id, payload) {
81
- const SPACE = String();
82
- const node = payload.each();
83
- const compiler = new Compiler();
84
- const appliers = new Appliers();
85
- const template = transform(node);
86
- const compilation = compiler.compile(node);
87
- let bindings = new Array();
88
- let pool = new Array();
89
- let applier;
90
- return (root, context) => {
91
- if (applier === undefined) {
92
- applier = appliers.apply(compilation);
93
- }
94
- const items = payload.select(context);
95
- const nodes = root[SF_CACHE];
96
- const target = nodes[id];
97
- const { replaces, clears, addes, swaps, removes, updates } = items.flush();
98
- if (replaces) {
99
- const items_count = items.size;
100
- const child_count = bindings.length;
101
- const common_count = Math.min(items_count, child_count);
102
- for (let index = 0; index < common_count; index++) {
103
- applier(bindings[index], items.at(index));
104
- }
105
- if (items_count < child_count) {
106
- for (let index = items_count; index < child_count; index++) {
107
- pool.push(bindings[index]);
108
- bindings[index].remove();
109
- }
110
- bindings.length = items_count;
111
- }
112
- else if (child_count < items_count) {
113
- const fragment = document.createDocumentFragment();
114
- const elements = new Array(items_count - child_count);
115
- for (let index = child_count; index < items_count; index++) {
116
- const element = pool.pop() || template.cloneNode(true);
117
- applier(element, items.at(index));
118
- elements[index - child_count] = element;
119
- fragment.appendChild(element);
120
- }
121
- bindings = bindings.concat(elements);
122
- target.appendChild(fragment);
123
- }
124
- return;
125
- }
126
- if (clears) {
127
- pool = pool.concat(bindings.splice(0));
128
- target.innerHTML = SPACE;
129
- }
130
- if (addes > 0) {
131
- const fragment = document.createDocumentFragment();
132
- const elements = new Array(addes);
133
- for (let index = 0; index < addes; index++) {
134
- const element = template.cloneNode(true);
135
- applier(element, items.at(index));
136
- elements[index] = element;
137
- fragment.appendChild(element);
138
- }
139
- bindings = bindings.concat(elements);
140
- target.appendChild(fragment);
141
- }
142
- for (const [i, j] of swaps.entries()) {
143
- applier(bindings[i], items.at(i));
144
- applier(bindings[j], items.at(j));
145
- }
146
- for (const id of removes) {
147
- const element = bindings[id];
148
- pool.push(element);
149
- element.remove();
150
- }
151
- for (const id of updates) {
152
- applier(bindings[id], items.at(id));
153
- }
154
- };
241
+ const appliers = new CollectionApplier(this._pool, id, payload);
242
+ return (root, context) => appliers.apply(root, context);
155
243
  }
156
244
  markup(root, paths) {
157
245
  const node = root;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stillflow.engine",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
4
4
  "description": "A headless, transaction-based UI engine with AOT compilation.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",