stillflow.engine 0.1.6 → 0.1.7
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
|

|
|
34
34
|
|
|
35
|
+
**Stillflow** memory usage score is below
|
|
36
|
+
|
|
37
|
+

|
|
38
|
+
|
|
35
39
|
## Quick Start
|
|
36
40
|
|
|
37
41
|
### Let's make some simple Todo application
|
|
@@ -2,8 +2,6 @@ import { Template } from "./compile.js";
|
|
|
2
2
|
type Applier<V> = (root: HTMLElement, context: V) => void;
|
|
3
3
|
export declare class Appliers<V> {
|
|
4
4
|
private _listeners;
|
|
5
|
-
private _actives;
|
|
6
|
-
private ensure_listener;
|
|
7
5
|
private find_element_by;
|
|
8
6
|
private apply_conditional;
|
|
9
7
|
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
|
-
|
|
7
|
+
class Listeners {
|
|
8
8
|
_listeners = new WeakMap();
|
|
9
9
|
_actives = new Set();
|
|
10
|
-
|
|
11
|
-
if (this._actives.has(
|
|
10
|
+
ensure(type) {
|
|
11
|
+
if (this._actives.has(type)) {
|
|
12
12
|
return;
|
|
13
13
|
}
|
|
14
|
-
this._actives.add(
|
|
15
|
-
document.body.addEventListener(
|
|
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(
|
|
20
|
+
const callback = listeners.get(type);
|
|
21
21
|
callback?.(event, target[SF_CONTEXT]);
|
|
22
22
|
if (event.cancelBubble) {
|
|
23
23
|
break;
|
|
@@ -27,6 +27,172 @@ 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
|
+
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
|
+
console.log(this._frame);
|
|
89
|
+
this._frame++;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
class CollectionApplier {
|
|
93
|
+
_pool;
|
|
94
|
+
_id;
|
|
95
|
+
_payload;
|
|
96
|
+
_compilation;
|
|
97
|
+
_template;
|
|
98
|
+
_elements = [];
|
|
99
|
+
_appliers;
|
|
100
|
+
_applier;
|
|
101
|
+
constructor(_pool, _id, _payload) {
|
|
102
|
+
this._pool = _pool;
|
|
103
|
+
this._id = _id;
|
|
104
|
+
this._payload = _payload;
|
|
105
|
+
const node = this._payload.each();
|
|
106
|
+
const compiler = new Compiler();
|
|
107
|
+
const appliers = new Appliers();
|
|
108
|
+
const template = transform(node);
|
|
109
|
+
const compilation = compiler.compile(node);
|
|
110
|
+
this._appliers = appliers;
|
|
111
|
+
this._compilation = compilation;
|
|
112
|
+
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
|
+
}
|
|
124
|
+
apply(root, context) {
|
|
125
|
+
if (this._applier === undefined) {
|
|
126
|
+
this._applier = this._appliers.apply(this._compilation);
|
|
127
|
+
}
|
|
128
|
+
const items = this._payload.select(context);
|
|
129
|
+
const nodes = root[SF_CACHE];
|
|
130
|
+
const target = nodes[this._id];
|
|
131
|
+
const { replaces, clears, addes, swaps, removes, updates } = items.flush();
|
|
132
|
+
if (replaces) {
|
|
133
|
+
const items_count = items.size;
|
|
134
|
+
const child_count = this._elements.length;
|
|
135
|
+
const common_count = Math.min(items_count, child_count);
|
|
136
|
+
for (let index = 0; index < common_count; index++) {
|
|
137
|
+
this._applier(this._elements[index], items.at(index));
|
|
138
|
+
}
|
|
139
|
+
if (items_count < child_count) {
|
|
140
|
+
for (let index = items_count; index < child_count; index++) {
|
|
141
|
+
const element = this._elements[index];
|
|
142
|
+
this._pool.add(element);
|
|
143
|
+
element.remove();
|
|
144
|
+
}
|
|
145
|
+
this._elements.length = items_count;
|
|
146
|
+
}
|
|
147
|
+
else if (child_count < items_count) {
|
|
148
|
+
const fragment = document.createDocumentFragment();
|
|
149
|
+
const elements = new Array(items_count - child_count);
|
|
150
|
+
for (let index = child_count; index < items_count; index++) {
|
|
151
|
+
const element = this._pool.get() || this._template.cloneNode(true);
|
|
152
|
+
this._applier(element, items.at(index));
|
|
153
|
+
elements[index - child_count] = element;
|
|
154
|
+
fragment.appendChild(element);
|
|
155
|
+
}
|
|
156
|
+
this._elements = this._elements.concat(elements);
|
|
157
|
+
target.appendChild(fragment);
|
|
158
|
+
}
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
if (clears) {
|
|
162
|
+
const free = this._elements.splice(0);
|
|
163
|
+
for (const element of free) {
|
|
164
|
+
this._pool.add(element);
|
|
165
|
+
}
|
|
166
|
+
target.innerHTML = String();
|
|
167
|
+
}
|
|
168
|
+
if (addes > 0) {
|
|
169
|
+
const fragment = document.createDocumentFragment();
|
|
170
|
+
const elements = new Array(addes);
|
|
171
|
+
for (let index = 0; index < addes; index++) {
|
|
172
|
+
const element = this._template.cloneNode(true);
|
|
173
|
+
this._applier(element, items.at(index));
|
|
174
|
+
elements[index] = element;
|
|
175
|
+
fragment.appendChild(element);
|
|
176
|
+
}
|
|
177
|
+
this._elements = this._elements.concat(elements);
|
|
178
|
+
target.appendChild(fragment);
|
|
179
|
+
}
|
|
180
|
+
for (const [i, j] of swaps.entries()) {
|
|
181
|
+
this._applier(this._elements[i], items.at(i));
|
|
182
|
+
this._applier(this._elements[j], items.at(j));
|
|
183
|
+
}
|
|
184
|
+
for (const id of removes) {
|
|
185
|
+
const element = this._elements[id];
|
|
186
|
+
this._pool.add(element);
|
|
187
|
+
element.remove();
|
|
188
|
+
}
|
|
189
|
+
for (const id of updates) {
|
|
190
|
+
this._applier(this._elements[id], items.at(id));
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
export class Appliers {
|
|
195
|
+
_listeners = new Listeners();
|
|
30
196
|
find_element_by(root, path) {
|
|
31
197
|
let current = root;
|
|
32
198
|
for (let id = 0; id < path.length; id++) {
|
|
@@ -69,89 +235,15 @@ export class Appliers {
|
|
|
69
235
|
}
|
|
70
236
|
element[SF_LISTENED] = true;
|
|
71
237
|
element[SF_CONTEXT] = context;
|
|
72
|
-
const listeners = this._listeners.get(element) || new Map();
|
|
73
238
|
for (let key in payload) {
|
|
74
|
-
|
|
75
|
-
this.ensure_listener(key);
|
|
239
|
+
this._listeners.add(element, key, payload[key]);
|
|
76
240
|
}
|
|
77
|
-
this._listeners.set(element, listeners);
|
|
78
241
|
};
|
|
79
242
|
}
|
|
80
243
|
apply_iterate(id, payload) {
|
|
81
|
-
const
|
|
82
|
-
const
|
|
83
|
-
|
|
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
|
-
};
|
|
244
|
+
const pool = new Pool(this._listeners);
|
|
245
|
+
const appliers = new CollectionApplier(pool, id, payload);
|
|
246
|
+
return (root, context) => appliers.apply(root, context);
|
|
155
247
|
}
|
|
156
248
|
markup(root, paths) {
|
|
157
249
|
const node = root;
|