nesquick 0.0.27 → 1.0.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/lib/NesquickComponent.js +5 -5
- package/lib/State.js +69 -17
- package/lib/types/State.d.ts +8 -4
- package/package.json +1 -1
package/lib/NesquickComponent.js
CHANGED
|
@@ -161,7 +161,7 @@ class NesquickComponent {
|
|
|
161
161
|
_renderStyle(element, style) {
|
|
162
162
|
switch (typeof style) {
|
|
163
163
|
case "function": {
|
|
164
|
-
(0, State_1.useRender)(style, (style,
|
|
164
|
+
(0, State_1.useRender)(style, (style, lastReaction) => {
|
|
165
165
|
if (this._styleSubscriptions != null) {
|
|
166
166
|
this._styleSubscriptions.dispose();
|
|
167
167
|
this._styleSubscriptions = null;
|
|
@@ -169,16 +169,16 @@ class NesquickComponent {
|
|
|
169
169
|
switch (typeof style) {
|
|
170
170
|
case "object": {
|
|
171
171
|
if (style) {
|
|
172
|
-
if (
|
|
172
|
+
if (lastReaction) {
|
|
173
|
+
this._renderStyles(element, style);
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
173
176
|
element.removeAttribute("style");
|
|
174
177
|
this._styleSubscriptions = new State_1.Subscriptions();
|
|
175
178
|
State_1.subscriptions.set(this._styleSubscriptions);
|
|
176
179
|
this._renderStyles(element, style);
|
|
177
180
|
State_1.subscriptions.reset();
|
|
178
181
|
}
|
|
179
|
-
else {
|
|
180
|
-
this._renderStyles(element, style);
|
|
181
|
-
}
|
|
182
182
|
}
|
|
183
183
|
break;
|
|
184
184
|
}
|
package/lib/State.js
CHANGED
|
@@ -4,6 +4,7 @@ exports.subscriptions = exports.Subscriptions = void 0;
|
|
|
4
4
|
exports.useState = useState;
|
|
5
5
|
exports.useEffect = useEffect;
|
|
6
6
|
exports.useRender = useRender;
|
|
7
|
+
exports.useMemo = useMemo;
|
|
7
8
|
exports.useDispose = useDispose;
|
|
8
9
|
let currentReactor = [];
|
|
9
10
|
var reactor;
|
|
@@ -87,18 +88,57 @@ function useState(value) {
|
|
|
87
88
|
return value;
|
|
88
89
|
};
|
|
89
90
|
const setValue = (newValue) => {
|
|
90
|
-
value
|
|
91
|
-
|
|
92
|
-
|
|
91
|
+
if (value !== newValue) {
|
|
92
|
+
value = newValue;
|
|
93
|
+
for (const reactor of reactors) {
|
|
94
|
+
renderReactor(reactor, reactor.effect);
|
|
95
|
+
}
|
|
93
96
|
}
|
|
94
97
|
};
|
|
95
98
|
return [getValue, setValue];
|
|
96
99
|
}
|
|
97
100
|
function useEffect(cb, reaction = null) {
|
|
98
|
-
newSubscription(cb, reaction, true);
|
|
101
|
+
const sub = newSubscription(cb, reaction, true);
|
|
102
|
+
runSubscription(sub);
|
|
99
103
|
}
|
|
100
104
|
function useRender(cb, reaction = null) {
|
|
101
|
-
newSubscription(cb, reaction, false);
|
|
105
|
+
const sub = newSubscription(cb, reaction, false);
|
|
106
|
+
runSubscription(sub);
|
|
107
|
+
}
|
|
108
|
+
function useMemo(cb) {
|
|
109
|
+
const reactors = new Set();
|
|
110
|
+
let state = 0;
|
|
111
|
+
let value;
|
|
112
|
+
const sub = newSubscription(() => {
|
|
113
|
+
if (state === 0) {
|
|
114
|
+
sub.iteration--;
|
|
115
|
+
}
|
|
116
|
+
else if (state === 1) {
|
|
117
|
+
state = 2;
|
|
118
|
+
value = cb();
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
sub.iteration--;
|
|
122
|
+
state = 0;
|
|
123
|
+
for (const reactor of reactors) {
|
|
124
|
+
renderReactor(reactor, reactor.effect);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}, null, true);
|
|
128
|
+
return () => {
|
|
129
|
+
if (state === 0) {
|
|
130
|
+
state = 1;
|
|
131
|
+
runSubscription(sub);
|
|
132
|
+
}
|
|
133
|
+
if (!sub.cancelled) {
|
|
134
|
+
const reactor = currentReactor[currentReactor.length - 1];
|
|
135
|
+
if (reactor) {
|
|
136
|
+
reactors.add(reactor);
|
|
137
|
+
reactor.states.set(reactors, reactor.iteration);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return value;
|
|
141
|
+
};
|
|
102
142
|
}
|
|
103
143
|
function useDispose(cb) {
|
|
104
144
|
const container = currentSubscriptions[currentSubscriptions.length - 1];
|
|
@@ -107,8 +147,10 @@ function useDispose(cb) {
|
|
|
107
147
|
}
|
|
108
148
|
}
|
|
109
149
|
function newSubscription(cb, reaction, effect) {
|
|
110
|
-
|
|
150
|
+
return {
|
|
111
151
|
cb: cb,
|
|
152
|
+
firstRun: true,
|
|
153
|
+
lastValue: null,
|
|
112
154
|
reaction: reaction,
|
|
113
155
|
iteration: 0,
|
|
114
156
|
states: new Map(),
|
|
@@ -117,16 +159,6 @@ function newSubscription(cb, reaction, effect) {
|
|
|
117
159
|
cancelled: false,
|
|
118
160
|
pending: false
|
|
119
161
|
};
|
|
120
|
-
renderReactor(sub, true);
|
|
121
|
-
if (sub.states.size === 0) {
|
|
122
|
-
cancelSubscription(sub);
|
|
123
|
-
}
|
|
124
|
-
else {
|
|
125
|
-
const container = currentSubscriptions[currentSubscriptions.length - 1];
|
|
126
|
-
if (container) {
|
|
127
|
-
container.list.push(sub);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
162
|
}
|
|
131
163
|
function cancelSubscription(sub) {
|
|
132
164
|
sub.cancelled = true;
|
|
@@ -136,6 +168,7 @@ function cancelSubscription(sub) {
|
|
|
136
168
|
}
|
|
137
169
|
function runSubscription(sub) {
|
|
138
170
|
sub.pending = false;
|
|
171
|
+
sub.iteration++;
|
|
139
172
|
reactor.set(sub);
|
|
140
173
|
const res = sub.cb();
|
|
141
174
|
reactor.reset();
|
|
@@ -144,5 +177,24 @@ function runSubscription(sub) {
|
|
|
144
177
|
sub.states.delete(state);
|
|
145
178
|
}
|
|
146
179
|
}
|
|
147
|
-
|
|
180
|
+
if (sub.states.size === 0) {
|
|
181
|
+
cancelSubscription(sub);
|
|
182
|
+
}
|
|
183
|
+
if (sub.firstRun) {
|
|
184
|
+
sub.firstRun = false;
|
|
185
|
+
if (sub.states.size > 0) {
|
|
186
|
+
const container = currentSubscriptions[currentSubscriptions.length - 1];
|
|
187
|
+
if (container) {
|
|
188
|
+
container.list.push(sub);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
if (sub.reaction) {
|
|
192
|
+
sub.lastValue = res;
|
|
193
|
+
sub.reaction(res, sub.cancelled);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
else if (sub.reaction && sub.lastValue !== res) {
|
|
197
|
+
sub.lastValue = res;
|
|
198
|
+
sub.reaction(res, sub.cancelled);
|
|
199
|
+
}
|
|
148
200
|
}
|
package/lib/types/State.d.ts
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
export type Getter<T> = () => T;
|
|
2
2
|
export type Setter<T> = (value: T) => void;
|
|
3
3
|
export type State<T> = [get: Getter<T>, set: Setter<T>];
|
|
4
|
-
|
|
4
|
+
type Subscription<T> = {
|
|
5
5
|
cb: () => T;
|
|
6
|
-
reaction: ((data: T, isState: boolean) => void) | null;
|
|
7
6
|
iteration: number;
|
|
7
|
+
lastValue: T | null;
|
|
8
|
+
firstRun: boolean;
|
|
9
|
+
reaction: ((data: T, lastReaction: boolean) => void) | null;
|
|
8
10
|
states: Map<Set<Subscription<any>>, number>;
|
|
9
11
|
next: Subscription<T> | null;
|
|
10
12
|
effect: boolean;
|
|
@@ -21,6 +23,8 @@ export declare namespace subscriptions {
|
|
|
21
23
|
function reset(): void;
|
|
22
24
|
}
|
|
23
25
|
export declare function useState<T>(value: T): State<T>;
|
|
24
|
-
export declare function useEffect<T>(cb: () => T, reaction?: ((data: T,
|
|
25
|
-
export declare function useRender<T>(cb: () => T, reaction?: ((data: T,
|
|
26
|
+
export declare function useEffect<T>(cb: () => T, reaction?: ((data: T, lastReaction: boolean) => void) | null): void;
|
|
27
|
+
export declare function useRender<T>(cb: () => T, reaction?: ((data: T, lastReaction: boolean) => void) | null): void;
|
|
28
|
+
export declare function useMemo<T>(cb: () => T): Getter<T>;
|
|
26
29
|
export declare function useDispose(cb: () => void): void;
|
|
30
|
+
export {};
|