stillflow.engine 0.1.4 → 0.1.5

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
@@ -10,9 +10,9 @@ I built **Stillflow** to solve my own architectural problems. I wanted explicit
10
10
 
11
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
- 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.
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)) 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
 
15
- Also I think that **explicit reactivity is better than implicit reactivity**. So **Stillflow** has *`commit()` method that controls the update time. Before *commit* buffer is full, but changes are not applied to the *map-state*. It makes *state-between-state* an impossible occurense. It makes the **Stillflow** model transactional and atomic.
15
+ Also I think that **explicit reactivity is better than implicit reactivity**. So **Stillflow** has `commit()` method that controls the update time. Before *commit* buffer is full, but changes are not applied to the *map-state*. It makes *state-between-state* an impossible occurrence. It makes the **Stillflow** model transactional and atomic.
16
16
 
17
17
  ## Features
18
18
 
@@ -24,8 +24,8 @@ 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, while providing a full transactional state model.
28
- ![Benchmark scores of Stillflow](images/1.png)
27
+ **Stillflow** performs within *7,5-9% of VanillaJS* in the Krause benchmark on my notepad, while providing a full transactional state model. I run benchmark on JSX and DOM versions of my framework. You can see results below.
28
+ ![Benchmark scores of Stillflow](images/3.png)
29
29
 
30
30
  ## Quick Start
31
31
 
@@ -87,11 +87,11 @@ function TodoItem() {
87
87
  function App() {
88
88
  return (
89
89
  <jsxdom.div>
90
- <jsxdom.input sf: value={(context: TodoState) => context.input} on: change={change} />
91
- <jsxdom.button on: click={add}>
90
+ <jsxdom.input sf:value={(context: TodoState) => context.input} on: change={change} />
91
+ <jsxdom.button on:click={add}>
92
92
  Add
93
93
  </jsxdom.button>
94
- <jsxdom.div sf: for={(context: TodoState) => context.items} sf: each={() => <TodoItem />} />
94
+ <jsxdom.div sf:for={(context: TodoState) => context.items} sf:each={() => <TodoItem />} />
95
95
  </jsxdom.div>
96
96
  )
97
97
  }
@@ -1,8 +1,8 @@
1
1
  import { Compiler } from "./compile.js";
2
2
  import { transform } from "./transform.js";
3
- const SF_CACHED_TEXTNODE = '__stillflow_cache_textnode__';
4
3
  const SF_LISTENED = '__stillflow_listened__';
5
4
  const SF_CONTEXT = '__stillflow_context__';
5
+ const SF_CACHE_TEXT = '__stillflow_cache_text__';
6
6
  const SF_CACHE = '__stillflow_cache__';
7
7
  export class Appliers {
8
8
  _listeners = new WeakMap();
@@ -45,16 +45,9 @@ export class Appliers {
45
45
  apply_text(id, payload) {
46
46
  return (root, context) => {
47
47
  const text = payload(context);
48
- const nodes = root[SF_CACHE];
49
- const element = nodes[id];
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
- }
48
+ const nodes = root[SF_CACHE_TEXT];
49
+ const textnode = nodes[id];
50
+ textnode.nodeValue = text;
58
51
  };
59
52
  }
60
53
  apply_properties(id, payload) {
@@ -91,10 +84,13 @@ export class Appliers {
91
84
  const appliers = new Appliers();
92
85
  const template = transform(node);
93
86
  const compilation = compiler.compile(node);
94
- const applier = appliers.apply(compilation);
95
87
  let bindings = new Array();
96
88
  let pool = new Array();
89
+ let applier;
97
90
  return (root, context) => {
91
+ if (applier === undefined) {
92
+ applier = appliers.apply(compilation);
93
+ }
98
94
  const items = payload.select(context);
99
95
  const nodes = root[SF_CACHE];
100
96
  const target = nodes[id];
@@ -133,11 +129,10 @@ export class Appliers {
133
129
  }
134
130
  if (addes > 0) {
135
131
  const fragment = document.createDocumentFragment();
136
- const old_size = bindings.length;
137
132
  const elements = new Array(addes);
138
133
  for (let index = 0; index < addes; index++) {
139
134
  const element = template.cloneNode(true);
140
- applier(element, items.at(index + old_size));
135
+ applier(element, items.at(index));
141
136
  elements[index] = element;
142
137
  fragment.appendChild(element);
143
138
  }
@@ -164,10 +159,13 @@ export class Appliers {
164
159
  return node[SF_CACHE];
165
160
  }
166
161
  const cache = new Array(paths.length);
162
+ const text_cache = new Array(paths.length);
167
163
  for (let id = 0; id < paths.length; id++) {
168
164
  cache[id] = this.find_element_by(root, paths[id]);
165
+ text_cache[id] = cache[id].firstChild;
169
166
  }
170
167
  node[SF_CACHE] = cache;
168
+ node[SF_CACHE_TEXT] = text_cache;
171
169
  return cache;
172
170
  }
173
171
  apply(template) {
@@ -4,7 +4,7 @@ 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:";
6
6
  type ElementStyles<K extends keyof HTMLElementTagNameMap> = {
7
- styles: HTMLElementTagNameMap[K]['style'];
7
+ styles: Partial<HTMLElementTagNameMap[K]['style']>;
8
8
  };
9
9
  type ElementPropertiesWithoutStyles<K extends keyof HTMLElementTagNameMap> = Omit<HTMLElementTagNameMap[K], 'style'>;
10
10
  type ElementServicePropertiesWithoutPrefix<V> = {
@@ -6,19 +6,21 @@ const DYNAMIC_PROPERTY_CYCLES = [`${DYNAMIC_PROPERTY_PREFIX}for`, `${DYNAMIC_PRO
6
6
  const DYNAMIC_PROPERTY_CONDITIONAL = `${DYNAMIC_PROPERTY_PREFIX}if`;
7
7
  const DELEGATE_EVENT_PROPERTY_PREFIX = `${DYNAMIC_PROPERTY_PREFIX}on-`;
8
8
  const COMMON_EVENT_PROPERTY_PREFIX = `on:`;
9
+ const COMMON_STYLES_PROPERTY = 'styles';
9
10
  class JSXDOMUtilities {
10
11
  static patch(object, key, value) {
11
12
  object[key] = value;
12
13
  }
13
14
  static outsort_children(all) {
14
- const content = new Array();
15
+ const dynamic_content = new Array();
16
+ const static_content = new Array();
15
17
  const children = new Array();
16
18
  for (const child of all) {
17
19
  if (typeof child === 'string' || typeof child === 'number') {
18
- content.push(() => child.toString());
20
+ static_content.push(child.toString());
19
21
  }
20
22
  else if (typeof child === 'function') {
21
- content.push(child);
23
+ dynamic_content.push(child);
22
24
  }
23
25
  else if (typeof child === 'object') {
24
26
  children.push(child);
@@ -27,7 +29,7 @@ class JSXDOMUtilities {
27
29
  throw new Error(`JSX does not support typeof of ${typeof child} element`);
28
30
  }
29
31
  }
30
- return { content, children };
32
+ return { static_content, dynamic_content, children };
31
33
  }
32
34
  static map_properties(jsx) {
33
35
  const jsx_properties = jsx;
@@ -58,6 +60,9 @@ class JSXDOMUtilities {
58
60
  dom_properties.modifiers.properties = dom_properties.modifiers.properties || {};
59
61
  this.patch(dom_properties.modifiers.properties, property.slice(DYNAMIC_PROPERTY_PREFIX.length), jsx_properties[property]);
60
62
  }
63
+ else if (property === COMMON_STYLES_PROPERTY) {
64
+ this.patch(dom_properties, property, jsx_properties[property]);
65
+ }
61
66
  else {
62
67
  this.patch(dom_properties.properties, property, jsx_properties[property]);
63
68
  }
@@ -68,9 +73,18 @@ class JSXDOMUtilities {
68
73
  export const JSXDOM_REGISTRY = Object.create(null);
69
74
  for (const tag of HTML_TAGS) {
70
75
  JSXDOM_REGISTRY[tag] = (context, all) => {
71
- const { content, children } = JSXDOMUtilities.outsort_children(all);
76
+ const { static_content, dynamic_content, children } = JSXDOMUtilities.outsort_children(all);
72
77
  const properties = JSXDOMUtilities.map_properties(context);
73
- properties.modifiers.text = (context) => content.map(site => site(context)).join(String());
78
+ properties.text = static_content.join(String());
79
+ if (dynamic_content.length > 0) {
80
+ properties.modifiers.text = (context) => {
81
+ let contents = String();
82
+ for (const site of dynamic_content) {
83
+ contents += site(context);
84
+ }
85
+ return contents;
86
+ };
87
+ }
74
88
  properties.name = tag;
75
89
  return describe(properties, children);
76
90
  };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,29 @@
1
+ import { Stillflow, State, adapters, FlushableCollection, FenwickCollection } from "../index.js";
2
+ const { jsxdom, dom } = adapters;
3
+ const core = new Stillflow(new State());
4
+ const factory = core.factory();
5
+ factory.add('collection', () => new FlushableCollection(new FenwickCollection(100)));
6
+ function FileView({ depth }) {
7
+ return (adapters.jsx(jsxdom.div, { styles: { 'padding': `${depth * 4}px` } },
8
+ adapters.jsx(jsxdom.span, null, (context) => context.name),
9
+ adapters.jsx(jsxdom.div, { "sf:for": (context) => context.files, "sf:each": () => adapters.jsx(FileView, { depth: depth + 1 }) })));
10
+ }
11
+ const root = {
12
+ name: 'test',
13
+ files: factory.get('collection', {})
14
+ };
15
+ root.files.add({
16
+ name: 'test-0',
17
+ files: factory.get('collection', {})
18
+ });
19
+ root.files.at(0).files.add({
20
+ name: 'test-0-1',
21
+ files: factory.get('collection', {})
22
+ });
23
+ root.files.add({
24
+ name: 'test-1',
25
+ files: factory.get('collection', {})
26
+ });
27
+ const { element, update } = dom.$(adapters.jsx(FileView, { depth: 0 }));
28
+ update(root);
29
+ document.body.append(element);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stillflow.engine",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "A headless, transaction-based UI engine with AOT compilation.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",