@esportsplus/reactivity 0.1.15 → 0.1.18
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/build/index.d.ts +1 -0
- package/build/index.js +1 -0
- package/build/macro.d.ts +1 -1
- package/build/reactive/object.d.ts +1 -1
- package/build/signal.d.ts +2 -2
- package/build/signal.js +10 -5
- package/build/types.d.ts +2 -2
- package/package.json +2 -2
- package/src/index.ts +1 -0
- package/src/reactive/object.ts +1 -1
- package/src/signal.ts +13 -8
- package/src/types.ts +2 -2
package/build/index.d.ts
CHANGED
|
@@ -2,4 +2,5 @@ export { default as macro } from './macro';
|
|
|
2
2
|
export { default as resource } from './resource';
|
|
3
3
|
export { default as reactive } from './reactive';
|
|
4
4
|
export { computed, dispose, effect, root, signal } from './signal';
|
|
5
|
+
export * from './constants';
|
|
5
6
|
export * from './types';
|
package/build/index.js
CHANGED
|
@@ -2,4 +2,5 @@ export { default as macro } from './macro';
|
|
|
2
2
|
export { default as resource } from './resource';
|
|
3
3
|
export { default as reactive } from './reactive';
|
|
4
4
|
export { computed, dispose, effect, root, signal } from './signal';
|
|
5
|
+
export * from './constants';
|
|
5
6
|
export * from './types';
|
package/build/macro.d.ts
CHANGED
|
@@ -6,5 +6,5 @@ declare class Macro<A extends unknown[], R> extends CustomFunction {
|
|
|
6
6
|
constructor(fn: Function<A, R>, options?: Options);
|
|
7
7
|
dispose(): void;
|
|
8
8
|
}
|
|
9
|
-
declare const _default: <A extends unknown[], R>(fn: () => (...args: A) => R, options?: Options) => Macro<A, R>;
|
|
9
|
+
declare const _default: <A extends unknown[], R>(fn: (instance: Computed<(...args: A) => R>) => (...args: A) => R, options?: Options) => Macro<A, R>;
|
|
10
10
|
export default _default;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Computed, Object, Options, Signal } from '../types';
|
|
2
2
|
import { ReactiveArray, ReactiveObjectArray } from './array';
|
|
3
|
-
type Node = Computed<
|
|
3
|
+
type Node = Computed<any> | ReactiveArray<any> | ReactiveObjectArray<Object> | Signal<any>;
|
|
4
4
|
declare class ReactiveObject<T extends Object> {
|
|
5
5
|
nodes: Record<PropertyKey, Node>;
|
|
6
6
|
constructor(data: T, options?: Options);
|
package/build/signal.d.ts
CHANGED
|
@@ -20,12 +20,12 @@ declare class Reactive<T> {
|
|
|
20
20
|
once<T>(event: Event, listener: Listener<T>): void;
|
|
21
21
|
set(value: T): T;
|
|
22
22
|
}
|
|
23
|
-
declare const computed: <T>(fn: () => T
|
|
23
|
+
declare const computed: <T>(fn: (instance: Computed<T>) => T, options?: Options) => Computed<T>;
|
|
24
24
|
declare const dispose: <T extends {
|
|
25
25
|
dispose: VoidFunction;
|
|
26
26
|
}>(dispose?: T | T[] | null | undefined) => T | T[] | null | undefined;
|
|
27
27
|
declare const effect: (fn: Effect['fn']) => Effect;
|
|
28
|
-
declare const root: <T>(fn: (
|
|
28
|
+
declare const root: <T>(fn: (instance: Root) => T, scheduler?: Scheduler) => T;
|
|
29
29
|
declare const signal: <T>(value: T, options?: Options) => Signal<T>;
|
|
30
30
|
export { computed, dispose, effect, root, signal };
|
|
31
31
|
export { Reactive };
|
package/build/signal.js
CHANGED
|
@@ -65,6 +65,7 @@ class Reactive {
|
|
|
65
65
|
if (this.state === DISPOSED) {
|
|
66
66
|
return;
|
|
67
67
|
}
|
|
68
|
+
this.dispatch('cleanup', this);
|
|
68
69
|
this.dispatch('dispose', this);
|
|
69
70
|
removeSourceObservers(this, 0);
|
|
70
71
|
this.listeners = null;
|
|
@@ -96,7 +97,10 @@ class Reactive {
|
|
|
96
97
|
}
|
|
97
98
|
on(event, listener) {
|
|
98
99
|
if (this.state === DIRTY) {
|
|
99
|
-
|
|
100
|
+
if (event !== 'cleanup') {
|
|
101
|
+
throw new Error(`Reactivity: events set within computed or effects must use the 'cleanup' event name`);
|
|
102
|
+
}
|
|
103
|
+
listener.once = true;
|
|
100
104
|
}
|
|
101
105
|
if (this.listeners === null) {
|
|
102
106
|
this.listeners = { [event]: [listener] };
|
|
@@ -185,8 +189,9 @@ function update(node) {
|
|
|
185
189
|
observer = node;
|
|
186
190
|
observers = null;
|
|
187
191
|
try {
|
|
192
|
+
node.dispatch('cleanup');
|
|
188
193
|
node.dispatch('update');
|
|
189
|
-
let value = node.fn.call(node);
|
|
194
|
+
let value = node.fn.call(null, node);
|
|
190
195
|
if (observers) {
|
|
191
196
|
removeSourceObservers(node, index);
|
|
192
197
|
if (node.sources !== null && index > 0) {
|
|
@@ -249,9 +254,9 @@ const effect = (fn) => {
|
|
|
249
254
|
update(instance);
|
|
250
255
|
return instance;
|
|
251
256
|
};
|
|
252
|
-
const root = (fn, scheduler
|
|
257
|
+
const root = (fn, scheduler) => {
|
|
253
258
|
let o = observer, s = scope;
|
|
254
|
-
if (scheduler ===
|
|
259
|
+
if (scheduler === undefined) {
|
|
255
260
|
if (scope === null) {
|
|
256
261
|
throw new Error('Reactivity: `root` cannot be created without a task scheduler');
|
|
257
262
|
}
|
|
@@ -261,7 +266,7 @@ const root = (fn, scheduler = null) => {
|
|
|
261
266
|
scope = new Reactive(CLEAN, ROOT, null);
|
|
262
267
|
scope.scheduler = scheduler;
|
|
263
268
|
scope.tracking = fn.length > 0;
|
|
264
|
-
let result = fn(scope);
|
|
269
|
+
let result = fn.call(null, scope);
|
|
265
270
|
observer = o;
|
|
266
271
|
scope = s;
|
|
267
272
|
return result;
|
package/build/types.d.ts
CHANGED
|
@@ -5,11 +5,11 @@ type Base<T> = Omit<Reactive<T>, 'changed' | 'fn' | 'get' | 'scheduler' | 'set'
|
|
|
5
5
|
type Changed = (a: unknown, b: unknown) => boolean;
|
|
6
6
|
type Computed<T> = {
|
|
7
7
|
changed: Changed;
|
|
8
|
-
fn: NeverAsync<() => T>;
|
|
8
|
+
fn: NeverAsync<(instance: Computed<T>) => T>;
|
|
9
9
|
get(): T;
|
|
10
10
|
} & Base<T>;
|
|
11
11
|
type Effect = {
|
|
12
|
-
fn: NeverAsync<(
|
|
12
|
+
fn: NeverAsync<(instance: Effect) => void>;
|
|
13
13
|
root: Root;
|
|
14
14
|
task: Function;
|
|
15
15
|
} & Omit<Base<void>, 'value'>;
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"@esportsplus/custom-function": "^0.0.1"
|
|
5
5
|
},
|
|
6
6
|
"devDependencies": {
|
|
7
|
-
"@esportsplus/typescript": "^0.0.
|
|
7
|
+
"@esportsplus/typescript": "^0.0.15"
|
|
8
8
|
},
|
|
9
9
|
"main": "build/index.js",
|
|
10
10
|
"name": "@esportsplus/reactivity",
|
|
@@ -16,5 +16,5 @@
|
|
|
16
16
|
"prepublishOnly": "npm run build"
|
|
17
17
|
},
|
|
18
18
|
"types": "build/index.d.ts",
|
|
19
|
-
"version": "0.1.
|
|
19
|
+
"version": "0.1.18"
|
|
20
20
|
}
|
package/src/index.ts
CHANGED
|
@@ -2,4 +2,5 @@ export { default as macro } from './macro';
|
|
|
2
2
|
export { default as resource } from './resource';
|
|
3
3
|
export { default as reactive } from './reactive';
|
|
4
4
|
export { computed, dispose, effect, root, signal } from './signal';
|
|
5
|
+
export * from './constants';
|
|
5
6
|
export * from './types';
|
package/src/reactive/object.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { defineProperty, isArray } from '~/utilities';
|
|
|
4
4
|
import { ReactiveArray, ReactiveObjectArray } from './array';
|
|
5
5
|
|
|
6
6
|
|
|
7
|
-
type Node = Computed<
|
|
7
|
+
type Node = Computed<any> | ReactiveArray<any> | ReactiveObjectArray<Object> | Signal<any>;
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
class ReactiveObject<T extends Object> {
|
package/src/signal.ts
CHANGED
|
@@ -88,6 +88,7 @@ class Reactive<T> {
|
|
|
88
88
|
return;
|
|
89
89
|
}
|
|
90
90
|
|
|
91
|
+
this.dispatch('cleanup', this);
|
|
91
92
|
this.dispatch('dispose', this);
|
|
92
93
|
|
|
93
94
|
removeSourceObservers(this, 0);
|
|
@@ -125,9 +126,12 @@ class Reactive<T> {
|
|
|
125
126
|
}
|
|
126
127
|
|
|
127
128
|
on<T>(event: Event, listener: Listener<T>) {
|
|
128
|
-
// Events cannot be set within effects or computed's
|
|
129
129
|
if (this.state === DIRTY) {
|
|
130
|
-
|
|
130
|
+
if (event !== 'cleanup') {
|
|
131
|
+
throw new Error(`Reactivity: events set within computed or effects must use the 'cleanup' event name`);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
listener.once = true;
|
|
131
135
|
}
|
|
132
136
|
|
|
133
137
|
if (this.listeners === null) {
|
|
@@ -186,7 +190,7 @@ function notify<T>(nodes: Reactive<T>[] | null, state: typeof CHECK | typeof DIR
|
|
|
186
190
|
|
|
187
191
|
if (node.state < state) {
|
|
188
192
|
if (node.type === EFFECT && node.state === CLEAN) {
|
|
189
|
-
node.root
|
|
193
|
+
(node as Effect).root.scheduler((node as Effect).task);
|
|
190
194
|
}
|
|
191
195
|
|
|
192
196
|
node.state = state;
|
|
@@ -244,10 +248,11 @@ function update<T>(node: Reactive<T>) {
|
|
|
244
248
|
observers = null as typeof observers;
|
|
245
249
|
|
|
246
250
|
try {
|
|
251
|
+
node.dispatch('cleanup');
|
|
247
252
|
node.dispatch('update');
|
|
248
253
|
|
|
249
254
|
// @ts-ignore
|
|
250
|
-
let value = node.fn.call(node);
|
|
255
|
+
let value = node.fn.call(null, node);
|
|
251
256
|
|
|
252
257
|
if (observers) {
|
|
253
258
|
removeSourceObservers(node, index);
|
|
@@ -328,11 +333,11 @@ const effect = (fn: Effect['fn']) => {
|
|
|
328
333
|
return instance as Effect;
|
|
329
334
|
};
|
|
330
335
|
|
|
331
|
-
const root = <T>(fn: NeverAsync<(
|
|
336
|
+
const root = <T>(fn: NeverAsync<(instance: Root) => T>, scheduler?: Scheduler) => {
|
|
332
337
|
let o = observer,
|
|
333
338
|
s = scope;
|
|
334
339
|
|
|
335
|
-
if (scheduler ===
|
|
340
|
+
if (scheduler === undefined) {
|
|
336
341
|
if (scope === null) {
|
|
337
342
|
throw new Error('Reactivity: `root` cannot be created without a task scheduler');
|
|
338
343
|
}
|
|
@@ -343,10 +348,10 @@ const root = <T>(fn: NeverAsync<(root: Root) => T>, scheduler: Scheduler | null
|
|
|
343
348
|
observer = null;
|
|
344
349
|
|
|
345
350
|
scope = new Reactive(CLEAN, ROOT, null) as any as Root;
|
|
346
|
-
scope.scheduler = scheduler
|
|
351
|
+
scope.scheduler = scheduler;
|
|
347
352
|
scope.tracking = fn.length > 0;
|
|
348
353
|
|
|
349
|
-
let result = fn(scope);
|
|
354
|
+
let result = fn.call(null, scope);
|
|
350
355
|
|
|
351
356
|
observer = o;
|
|
352
357
|
scope = s;
|
package/src/types.ts
CHANGED
|
@@ -9,12 +9,12 @@ type Changed = (a: unknown, b: unknown) => boolean;
|
|
|
9
9
|
|
|
10
10
|
type Computed<T> = {
|
|
11
11
|
changed: Changed;
|
|
12
|
-
fn: NeverAsync<() => T>;
|
|
12
|
+
fn: NeverAsync<(instance: Computed<T>) => T>;
|
|
13
13
|
get(): T;
|
|
14
14
|
} & Base<T>;
|
|
15
15
|
|
|
16
16
|
type Effect = {
|
|
17
|
-
fn: NeverAsync<(
|
|
17
|
+
fn: NeverAsync<(instance: Effect) => void>;
|
|
18
18
|
root: Root;
|
|
19
19
|
task: Function;
|
|
20
20
|
} & Omit<Base<void>, 'value'>;
|