stillflow.engine 0.1.1 → 0.1.3

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
@@ -8,7 +8,7 @@ I built **Stillflow** to solve my own architectural problems. I wanted explicit
8
8
 
9
9
  > UI = State + Projections + Bindings + Feedback loop.
10
10
 
11
- I think it is a **misconception** that `UI = f(state)`, because every `UI` has *user and document events* (`click`, `input`, `load` and *etc.*). So I derive that `state = g(UI)`, where `g` **is not** and inverse function of `f`, because we cannot restore *state* from `UI` fully in the most cases, because **UI is a subset of state**.
11
+ I think it is a **misconception** that `UI = f(state)`, because every `UI` has *user and document events* (`click`, `input`, `load` and *etc.*). So I derive that `state = g(UI)`, where `g` **is not** an inverse function of `f`, because we cannot restore *state* from `UI` fully in the most cases, because **UI is a subset of state**.
12
12
 
13
13
  Also I think of a `state` as a `global state` and a simple `"map"` ([Read about it here](http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map)) with filled with `primitives` (`number`, `string`, `object` and `array`, but not `symbol`, `function` or `class instances`) as I call it `"map-state"`. You could have thought that it is antipattern and a bad practice so it is out of consideration. But it makes the `state` the **single source of truth** so we can restore `UI` from the *state* fully [`UI = f(state)`]. The *map-state* makes the *serialization* of *state* easier and predictable.
14
14
 
@@ -24,29 +24,32 @@ Also I think that **explicit reactivity is better than implicit reactivity**. So
24
24
 
25
25
  ## Benchmarks
26
26
 
27
- **Stillflow** performs within *1% of VanillaJS* in the Krause benchmark on my notepad in the Krause benchmark, while providing a full transactional state model.
27
+ **Stillflow** performs within *1% of VanillaJS* in the Krause benchmark on my notepad, while providing a full transactional state model.
28
28
  ![Benchmark scores of Stillflow](images/1.png)
29
29
 
30
30
  ## Quick Start
31
31
 
32
32
  ### Let's make some simple Todo application
33
+
34
+ For a starter you can play with (it)[https://codesandbox.io/p/sandbox/stillflow-engine-starter-3qv9mh]. Simple demonstration of the features of **Stillflow**.
35
+
33
36
  ```ts
34
37
  import { Stillflow, State, FenwickCollection, FlushableCollection, operators, adapters } from 'stillflow.engine'
35
38
 
36
39
  type AppState = {
37
- input: string
40
+ input: string
38
41
  }
39
42
 
40
43
  type TodoItem = {
41
- label: string
44
+ label: string
42
45
  }
43
46
 
44
47
  type TodoState = {
45
- input: string,
46
- items: FlushableCollection<TodoItem>
48
+ input: string,
49
+ items: FlushableCollection<TodoItem>
47
50
  }
48
51
 
49
- const { jsxdom, DOM } = adapters
52
+ const { jsxdom, dom } = adapters
50
53
 
51
54
  const state = new State()
52
55
  const stillflow = new Stillflow<AppState>(state)
@@ -55,45 +58,45 @@ const factory = stillflow.factory()
55
58
  factory.add(FlushableCollection.name, () => new FlushableCollection(new FenwickCollection(100)))
56
59
 
57
60
  const source = {
58
- input: stillflow.variable('input').backref(),
59
- items: operators.is(FlushableCollection.name)
61
+ input: stillflow.variable('input').backref(),
62
+ items: operators.is(FlushableCollection.name)
60
63
  }
61
64
 
62
65
  const view = stillflow.compile(source)
63
66
  const instance: TodoState = stillflow.instantiate(view, factory)
64
67
 
65
68
  function change(event: any) {
66
- instance.input = event.target.value
67
- stillflow.commit()
69
+ instance.input = event.target.value
70
+ stillflow.commit()
68
71
  }
69
72
 
70
73
  function add() {
71
- instance.items.add({ label: instance.input })
72
- instance.input = String()
73
- stillflow.commit()
74
+ instance.items.add({ label: instance.input })
75
+ instance.input = String()
76
+ stillflow.commit()
74
77
  }
75
78
 
76
79
  function TodoItem() {
77
- return (
78
- <jsxdom.div>
79
- {(context: TodoItem) => context.label}
80
- </jsxdom.div>
81
- )
80
+ return (
81
+ <jsxdom.div>
82
+ {(context: TodoItem) => context.label}
83
+ </jsxdom.div>
84
+ )
82
85
  }
83
86
 
84
87
  function App() {
85
- return (
86
- <jsxdom.div>
87
- <jsxdom.input sf:value={(context: TodoState) => context.input} on:change={change}/>
88
- <jsxdom.button on:click={add}>
89
- Add
90
- </jsxdom.button>
91
- <jsxdom.div sf:for={(context: TodoState) => context.items} sf:each={() => <TodoItem />}/>
92
- </jsxdom.div>
93
- )
88
+ return (
89
+ <jsxdom.div>
90
+ <jsxdom.input sf: value={(context: TodoState) => context.input} on: change={change} />
91
+ <jsxdom.button on: click={add}>
92
+ Add
93
+ </jsxdom.button>
94
+ <jsxdom.div sf: for={(context: TodoState) => context.items} sf: each={() => <TodoItem />} />
95
+ </jsxdom.div>
96
+ )
94
97
  }
95
98
 
96
- const { element: app, update } = new DOM().$(<App />)
99
+ const { element: app, update } = dom.$(<App />)
97
100
 
98
101
  stillflow.on(() => update(instance))
99
102
 
@@ -0,0 +1,14 @@
1
+ import { AnyDOM } from "./types.js";
2
+ import { Properties } from "./properties/properties.js";
3
+ import { Binding } from "../binding.js";
4
+ type PropertiesWithoutServices<V, K extends keyof HTMLElementTagNameMap> = Omit<Properties<V, K>, 'name'>;
5
+ export type DOMRegistry = {
6
+ [K in keyof HTMLElementTagNameMap]: <V>(properties: Partial<PropertiesWithoutServices<V, K>>, children?: Binding<any>[]) => AnyDOM<V>;
7
+ } & {
8
+ $: <V>(root: AnyDOM<V>) => {
9
+ element: HTMLElement;
10
+ update: (context: V) => void;
11
+ };
12
+ };
13
+ export declare const DOM_REGISTRY: DOMRegistry;
14
+ export {};
@@ -0,0 +1,20 @@
1
+ import { HTML_TAGS } from "./tags.js";
2
+ import { Compiler } from "./compiler/compile.js";
3
+ import { Appliers } from "./compiler/apply.js";
4
+ import { transform } from "./compiler/transform.js";
5
+ import { describe } from "./description/describe.js";
6
+ export const DOM_REGISTRY = Object.create(null);
7
+ DOM_REGISTRY.$ = function $(root) {
8
+ const element = transform(root);
9
+ const compiler = new Compiler();
10
+ const compilation = compiler.compile(root);
11
+ const appliers = new Appliers();
12
+ const applier = appliers.apply(compilation);
13
+ const update = (context) => applier(element, context);
14
+ return { element, update };
15
+ };
16
+ for (const tag of HTML_TAGS) {
17
+ DOM_REGISTRY[tag] = (properties, children = []) => {
18
+ return describe({ ...properties, name: tag }, children);
19
+ };
20
+ }
@@ -1,5 +1,6 @@
1
1
  import { Compiler } from "./compile.js";
2
2
  import { transform } from "./transform.js";
3
+ const SF_CACHED_TEXTNODE = '__stillflow_cache_textnode__';
3
4
  const SF_LISTENED = '__stillflow_listened__';
4
5
  const SF_CONTEXT = '__stillflow_context__';
5
6
  const SF_CACHE = '__stillflow_cache__';
@@ -46,7 +47,14 @@ export class Appliers {
46
47
  const text = payload(context);
47
48
  const nodes = root[SF_CACHE];
48
49
  const element = nodes[id];
49
- element.firstChild.nodeValue = text;
50
+ let textnode = element[SF_CACHED_TEXTNODE];
51
+ if (textnode === undefined) {
52
+ textnode = element.firstChild;
53
+ element[SF_CACHED_TEXTNODE] = textnode;
54
+ }
55
+ if (textnode !== null) {
56
+ textnode.nodeValue = text;
57
+ }
50
58
  };
51
59
  }
52
60
  apply_properties(id, payload) {
@@ -77,7 +85,7 @@ export class Appliers {
77
85
  };
78
86
  }
79
87
  apply_iterate(id, payload) {
80
- const SPACE = " ";
88
+ const SPACE = String();
81
89
  const node = payload.each();
82
90
  const compiler = new Compiler();
83
91
  const appliers = new Appliers();
@@ -164,47 +172,30 @@ export class Appliers {
164
172
  }
165
173
  apply(template) {
166
174
  const { instructions, paths } = template;
167
- const appliers = {
168
- text: new Array(),
169
- iterate: new Array(),
170
- events: new Array(),
171
- properties: new Array(),
172
- conditional: new Array()
173
- };
175
+ const appliers = new Array(instructions.length);
176
+ let fill_index = 0;
174
177
  for (let index = 0; index < instructions.length; index++) {
175
178
  const instruction = instructions[index];
176
179
  const id = instruction.id;
177
180
  if (instruction.text !== undefined) {
178
- appliers.text.push(this.apply_text(id, instruction.text));
181
+ appliers[fill_index++] = this.apply_text(id, instruction.text);
179
182
  }
180
183
  if (instruction.iterate !== undefined) {
181
- appliers.iterate.push(this.apply_iterate(id, instruction.iterate));
184
+ appliers[fill_index++] = this.apply_iterate(id, instruction.iterate);
182
185
  }
183
186
  if (instruction.events !== undefined) {
184
- appliers.events.push(this.apply_events(id, instruction.events));
187
+ appliers[fill_index++] = this.apply_events(id, instruction.events);
185
188
  }
186
189
  if (instruction.properties !== undefined) {
187
- appliers.properties.push(this.apply_properties(id, instruction.properties));
190
+ appliers[fill_index++] = this.apply_properties(id, instruction.properties);
188
191
  }
189
192
  if (instruction.conditional !== undefined) {
190
- appliers.conditional.push(this.apply_conditional(id, instruction.conditional));
193
+ appliers[fill_index++] = this.apply_conditional(id, instruction.conditional);
191
194
  }
192
195
  }
193
196
  return (root, context) => {
194
197
  this.markup(root, paths);
195
- for (const applier of appliers.text) {
196
- applier(root, context);
197
- }
198
- for (const applier of appliers.events) {
199
- applier(root, context);
200
- }
201
- for (const applier of appliers.properties) {
202
- applier(root, context);
203
- }
204
- for (const applier of appliers.iterate) {
205
- applier(root, context);
206
- }
207
- for (const applier of appliers.conditional) {
198
+ for (const applier of appliers) {
208
199
  applier(root, context);
209
200
  }
210
201
  };
@@ -1,4 +1,4 @@
1
- import { AnyDOM } from "../dom.js";
1
+ import { AnyDOM } from "../types.js";
2
2
  import { Modifiers } from "../properties/modifiers.js";
3
3
  export type Instructions<V> = Partial<{
4
4
  [L in keyof Modifiers<V>]: Modifiers<V>[L];
@@ -1,2 +1,2 @@
1
- import { DOM } from "../dom.js";
1
+ import { DOM } from "../types.js";
2
2
  export declare function transform<V, K extends keyof HTMLElementTagNameMap>(description: DOM<V, K>): HTMLElementTagNameMap[K];
@@ -1,3 +1,3 @@
1
- import { AnyDOM } from "../dom.js";
1
+ import { AnyDOM } from "../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,4 +1,4 @@
1
- import { AnyDOM } from "./dom.js";
1
+ import { AnyDOM } from "./types.js";
2
2
  import { FlushableCollection } from "../../index.js";
3
3
  declare const DYNAMIC_PROPERTY_PREFIX = "sf:";
4
4
  declare const DELEGATE_EVENT_PROPERTY_PREFIX = "sf:on-";
@@ -1,5 +1,5 @@
1
1
  import { FlushableCollection } from "../../../index.js";
2
- import { AnyDOM } from "../dom.js";
2
+ import { AnyDOM } from "../types.js";
3
3
  export type Modifiers<Context, K extends keyof HTMLElementTagNameMap = any> = Partial<{
4
4
  text: (context: Context) => string;
5
5
  lens: <Another>(context: Context) => Another;
@@ -0,0 +1,4 @@
1
+ import { Binding } from "../binding.js";
2
+ import { Properties } from "./properties/properties.js";
3
+ export type DOM<V, K extends keyof HTMLElementTagNameMap> = Binding<Properties<V, K>>;
4
+ export type AnyDOM<V> = Binding<Properties<V, keyof HTMLElementTagNameMap>>;
@@ -0,0 +1 @@
1
+ export {};
@@ -1,3 +1,3 @@
1
1
  export { jsx } from './runtime/jsx.runtime.js';
2
2
  export { JSXDOM_REGISTRY as jsxdom } from './dom/jsx.dom.js';
3
- export { DOMAdapter as DOM, DOM_REGISTRY as dom } from './dom/index.js';
3
+ export { DOM_REGISTRY as dom } from './dom/base.dom.js';
@@ -1,3 +1,3 @@
1
1
  export { jsx } from './runtime/jsx.runtime.js';
2
2
  export { JSXDOM_REGISTRY as jsxdom } from './dom/jsx.dom.js';
3
- export { DOMAdapter as DOM, DOM_REGISTRY as dom } from './dom/index.js';
3
+ export { DOM_REGISTRY as dom } from './dom/base.dom.js';
@@ -1,9 +1,9 @@
1
1
  import { Stillflow, State, Batcher, operators, adapters, FlushableCollection, FenwickCollection } from "../index.js";
2
2
  import { buildData } from "./data.js";
3
- const { jsxdom, jsx, DOM } = adapters;
3
+ const { jsxdom, dom } = adapters;
4
4
  class BenchmarkApplicationController {
5
- _stillflow = new Stillflow(new State(), { strategy: site => requestAnimationFrame(site) });
6
- _clearing = new Batcher(site => requestIdleCallback(site));
5
+ _stillflow = new Stillflow(new State());
6
+ _clearing = new Batcher(site => site());
7
7
  _factory = this._stillflow.factory();
8
8
  _view;
9
9
  schedule_clear() {
@@ -13,7 +13,6 @@ class BenchmarkApplicationController {
13
13
  deselect() {
14
14
  this._view.selection.current_id = undefined;
15
15
  this._view.selection.previous_id = undefined;
16
- this._stillflow.commit();
17
16
  }
18
17
  create(count = 1000) {
19
18
  this.schedule_clear();
@@ -134,7 +133,7 @@ function BenchmarkApplicationView(context) {
134
133
  adapters.jsx(jsxdom.span, { ariaHidden: "true", className: "preloadicon glyphicon glyphicon-remove" })));
135
134
  }
136
135
  const controller = new BenchmarkApplicationController();
137
- const { element, update } = new DOM().$(adapters.jsx(BenchmarkApplicationView, { controller: controller }));
136
+ const { element, update } = dom.$(adapters.jsx(BenchmarkApplicationView, { controller: controller }));
138
137
  controller.core.on(() => update({}));
139
138
  document.body.appendChild(element);
140
139
  update({});
@@ -1,5 +1,5 @@
1
1
  import { Stillflow, State, FenwickCollection, FlushableCollection, operators, adapters } from '../index.js';
2
- const { jsxdom, DOM } = adapters;
2
+ const { jsxdom, dom } = adapters;
3
3
  const state = new State();
4
4
  const stillflow = new Stillflow(state);
5
5
  const factory = stillflow.factory();
@@ -28,7 +28,7 @@ function App() {
28
28
  adapters.jsx(jsxdom.button, { "on:click": add }, "Add"),
29
29
  adapters.jsx(jsxdom.div, { "sf:for": (context) => context.items, "sf:each": () => adapters.jsx(TodoItem, null) })));
30
30
  }
31
- const { element: app, update } = new DOM().$(adapters.jsx(App, null));
31
+ const { element: app, update } = dom.$(adapters.jsx(App, null));
32
32
  stillflow.on(() => update(instance));
33
33
  instance.input = String();
34
34
  document.body.appendChild(app);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stillflow.engine",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "A headless, transaction-based UI engine with AOT compilation.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",