nesquick 1.0.0 → 1.1.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 +19 -0
- package/lib/State.js +9 -5
- package/lib/types/State.d.ts +2 -1
- package/package.json +1 -1
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**element **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(Date.now())}>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]
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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);
|
package/lib/types/State.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export type Getter<T> = () => T;
|
|
2
2
|
export type Setter<T> = (value: T) => void;
|
|
3
|
-
export type
|
|
3
|
+
export type GetterSetter<T> = (cb: (value: T) => T) => void;
|
|
4
|
+
export type State<T> = [get: Getter<T>, set: Setter<T>, getSet: GetterSetter<T>];
|
|
4
5
|
type Subscription<T> = {
|
|
5
6
|
cb: () => T;
|
|
6
7
|
iteration: number;
|