nesquick 1.0.0 → 1.2.0

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 ADDED
@@ -0,0 +1,19 @@
1
+ ### Motivation
2
+ I like react, but it always bugged me how you need to create a full fake DOM that is going to be compared to a virtual DOM to finally render the updates in the browser. Yes, it will only update the real DOM with the new changes, but the process of creating a full fake DOM and comparing it each time something is updated is incredibly resource wasting. I always thought in a way to link states to HTML elementos directly, so no more virtual DOM is needed, and like so, **N**imble **E**elements **S**uper **Quick** (`nesquick`) was born. At first it required for you to define the *subscribers*, so if you forgot to create a subscriber, no state will be linked to an HTML property. Now, with the magic of Typescript, you can link states to HTML elements without taking care of the subscribers, which makes it easier. For example, this is `nesquick`:
3
+ ```ts
4
+ import { useState } from "nesquick";
5
+
6
+ function MyComp() {
7
+ const [ getNumber, setNumber ] = useState(0);
8
+ return <div>
9
+ <button onClick={() => setNumber(Math.random())}>Update number</button>
10
+ <div>Number: {getNumber()}</div>
11
+ </div>;
12
+ }
13
+ ```
14
+ Pretty straightforward. States have getters and setters, and that's it. With this approach, no more virtual DOM is needed.
15
+
16
+ ### Documentation
17
+ Just `npm install nesquick`.
18
+
19
+ [WIP]
@@ -215,9 +215,17 @@ class NesquickComponent {
215
215
  this._renderChild(document, parent, this._pushChild(), child);
216
216
  }
217
217
  else if (typeof child === "function") {
218
- const ch = this._pushChild();
219
- (0, State_1.useRender)(child, children => {
220
- this._renderChild(document, parent, ch, children);
218
+ let ch = null;
219
+ (0, State_1.useRender)(child, (children, lastReaction) => {
220
+ if (lastReaction && ch == null && Array.isArray(children)) {
221
+ this._renderChildren(document, parent, children);
222
+ }
223
+ else {
224
+ if (ch == null) {
225
+ ch = this._pushChild();
226
+ }
227
+ this._renderChild(document, parent, ch, children);
228
+ }
221
229
  });
222
230
  }
223
231
  else {
package/lib/State.js CHANGED
@@ -79,23 +79,27 @@ var subscriptions;
79
79
  })(subscriptions || (exports.subscriptions = subscriptions = {}));
80
80
  function useState(value) {
81
81
  const reactors = new Set();
82
- const getValue = () => {
82
+ function getValue() {
83
83
  const reactor = currentReactor[currentReactor.length - 1];
84
84
  if (reactor) {
85
85
  reactors.add(reactor);
86
86
  reactor.states.set(reactors, reactor.iteration);
87
87
  }
88
88
  return value;
89
- };
90
- const setValue = (newValue) => {
89
+ }
90
+ ;
91
+ function setValue(newValue) {
91
92
  if (value !== newValue) {
92
93
  value = newValue;
93
94
  for (const reactor of reactors) {
94
95
  renderReactor(reactor, reactor.effect);
95
96
  }
96
97
  }
97
- };
98
- return [getValue, setValue];
98
+ }
99
+ ;
100
+ return [getValue, setValue, cb => {
101
+ setValue(cb(value));
102
+ }];
99
103
  }
100
104
  function useEffect(cb, reaction = null) {
101
105
  const sub = newSubscription(cb, reaction, true);
@@ -1,6 +1,7 @@
1
1
  export type Getter<T> = () => T;
2
2
  export type Setter<T> = (value: T) => void;
3
- export type State<T> = [get: Getter<T>, set: Setter<T>];
3
+ export type Updater<T> = (cb: (value: T) => T) => void;
4
+ export type State<T> = [get: Getter<T>, set: Setter<T>, update: Updater<T>];
4
5
  type Subscription<T> = {
5
6
  cb: () => T;
6
7
  iteration: number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nesquick",
3
- "version": "1.0.0",
3
+ "version": "1.2.0",
4
4
  "description": "React-like library with focus on drawing performance",
5
5
  "types": "./lib/types",
6
6
  "main": "./lib",