@esportsplus/reactivity 0.0.15 → 0.0.17
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 -1
- package/build/methods/reactive.d.ts +2 -2
- package/build/methods/reactive.js +8 -9
- package/build/reactive.js +17 -7
- package/build/types.d.ts +3 -5
- package/package.json +1 -1
- package/src/methods/reactive.ts +14 -18
- package/src/reactive.ts +23 -8
- package/src/types.ts +3 -11
- package/build/obj.d.ts +0 -2
- package/build/obj.js +0 -28
package/build/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { scheduler } from './reactive';
|
|
|
2
2
|
import { effect, reactive } from './methods';
|
|
3
3
|
declare const _default: {
|
|
4
4
|
effect: <T>(fn: () => T) => void;
|
|
5
|
-
reactive: <T_1>(value: T_1) =>
|
|
5
|
+
reactive: <T_1>(value: T_1) => import("./types").Infer<T_1>;
|
|
6
6
|
scheduler: {
|
|
7
7
|
add: (scheduler: import("./types").Scheduler) => void;
|
|
8
8
|
delete: (scheduler: import("./types").Scheduler) => void;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import
|
|
2
|
-
declare const _default: <T>(value: T) =>
|
|
1
|
+
import { Infer } from '../types';
|
|
2
|
+
declare const _default: <T>(value: T) => Infer<T>;
|
|
3
3
|
export default _default;
|
|
@@ -1,18 +1,12 @@
|
|
|
1
1
|
import Reactive from '../reactive';
|
|
2
|
-
function factory(value
|
|
3
|
-
if (typeof value === 'function') {
|
|
4
|
-
return fn(value, wrap);
|
|
5
|
-
}
|
|
2
|
+
function factory(value) {
|
|
6
3
|
if (typeof value === 'object' && value !== null && (value.constructor === Object)) {
|
|
7
4
|
return obj(value);
|
|
8
5
|
}
|
|
9
6
|
return new Reactive(value);
|
|
10
7
|
}
|
|
11
|
-
function fn(value
|
|
8
|
+
function fn(value) {
|
|
12
9
|
let fn = new Reactive(value);
|
|
13
|
-
if (!wrap) {
|
|
14
|
-
return fn;
|
|
15
|
-
}
|
|
16
10
|
return (...args) => {
|
|
17
11
|
let value = fn.get();
|
|
18
12
|
if (args.length && typeof value === 'function') {
|
|
@@ -50,4 +44,9 @@ function obj(values) {
|
|
|
50
44
|
return Object.defineProperties({}, properties);
|
|
51
45
|
}
|
|
52
46
|
;
|
|
53
|
-
export default (value) =>
|
|
47
|
+
export default (value) => {
|
|
48
|
+
if (typeof value === 'function') {
|
|
49
|
+
return fn(value);
|
|
50
|
+
}
|
|
51
|
+
return factory(value);
|
|
52
|
+
};
|
package/build/reactive.js
CHANGED
|
@@ -10,10 +10,7 @@ function mark(instances, state) {
|
|
|
10
10
|
if (instance.effect && instance.state === CLEAN) {
|
|
11
11
|
queue.push(instance);
|
|
12
12
|
if (!scheduled) {
|
|
13
|
-
|
|
14
|
-
scheduler.schedule();
|
|
15
|
-
}
|
|
16
|
-
scheduled = true;
|
|
13
|
+
schedule();
|
|
17
14
|
}
|
|
18
15
|
}
|
|
19
16
|
instance.state = state;
|
|
@@ -23,12 +20,25 @@ function mark(instances, state) {
|
|
|
23
20
|
}
|
|
24
21
|
}
|
|
25
22
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
23
|
+
function schedule() {
|
|
24
|
+
if (scheduled) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
for (let scheduler of schedulers) {
|
|
28
|
+
scheduler.schedule();
|
|
29
29
|
}
|
|
30
|
+
scheduled = true;
|
|
31
|
+
}
|
|
32
|
+
async function task() {
|
|
33
|
+
let tasks = queue;
|
|
30
34
|
queue.length = 0;
|
|
35
|
+
for (let i = 0, n = tasks.length; i < n; i++) {
|
|
36
|
+
await tasks[i].get();
|
|
37
|
+
}
|
|
31
38
|
scheduled = false;
|
|
39
|
+
if (queue.length) {
|
|
40
|
+
schedule();
|
|
41
|
+
}
|
|
32
42
|
}
|
|
33
43
|
class Reactive {
|
|
34
44
|
fn;
|
package/build/types.d.ts
CHANGED
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
import { CLEAN, CHECK, DIRTY } from './symbols';
|
|
2
|
-
import Reactive from './reactive';
|
|
3
2
|
type Fn = () => Promise<unknown> | unknown;
|
|
4
|
-
type Infer<T> = T extends (...args: any[]) => any ?
|
|
5
|
-
|
|
6
|
-
type InferNested<T> = T extends (...args: any[]) => any ? ReturnType<T> : T extends Record<string, Primitives> ? {
|
|
7
|
-
[K in keyof T]: InferNested<T[K]>;
|
|
3
|
+
type Infer<T> = T extends (...args: any[]) => any ? ReturnType<T> : T extends Record<string, Primitives> ? {
|
|
4
|
+
[K in keyof T]: Infer<T[K]>;
|
|
8
5
|
} : T;
|
|
6
|
+
type Primitives = any[] | boolean | number | string | null | undefined | ((...args: any[]) => any);
|
|
9
7
|
type ReactiveFn<T> = (onCleanup?: (fn: VoidFunction) => void) => T;
|
|
10
8
|
type Scheduler = {
|
|
11
9
|
schedule(): void;
|
package/package.json
CHANGED
package/src/methods/reactive.ts
CHANGED
|
@@ -1,27 +1,19 @@
|
|
|
1
|
+
import { Infer } from '~/types';
|
|
1
2
|
import Reactive from '~/reactive';
|
|
2
3
|
|
|
3
4
|
|
|
4
|
-
function factory<T>(value: T
|
|
5
|
-
if (typeof value === 'function') {
|
|
6
|
-
return fn(value, wrap);
|
|
7
|
-
}
|
|
8
|
-
|
|
5
|
+
function factory<T>(value: T) {
|
|
9
6
|
if (typeof value === 'object' && value !== null && (value.constructor === Object)) {
|
|
10
7
|
return obj(value);
|
|
11
8
|
}
|
|
12
9
|
|
|
13
|
-
return new Reactive(value)
|
|
10
|
+
return new Reactive(value);
|
|
14
11
|
}
|
|
15
12
|
|
|
16
|
-
|
|
13
|
+
// Functions are wrapped to remove '.get' usage
|
|
14
|
+
function fn<T>(value: T) {
|
|
17
15
|
let fn = new Reactive(value);
|
|
18
16
|
|
|
19
|
-
// We're inside an object, it will unwrap reactive values
|
|
20
|
-
if (!wrap) {
|
|
21
|
-
return fn;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
// Factory functions are wrapped to remove '.get' usage
|
|
25
17
|
return (...args: any[]) => {
|
|
26
18
|
let value = fn.get();
|
|
27
19
|
|
|
@@ -29,9 +21,7 @@ function fn<T>(value: T, wrap: boolean) {
|
|
|
29
21
|
value = value(...args);
|
|
30
22
|
}
|
|
31
23
|
|
|
32
|
-
return value
|
|
33
|
-
? ReturnType<typeof value>
|
|
34
|
-
: typeof value;
|
|
24
|
+
return value;
|
|
35
25
|
};
|
|
36
26
|
}
|
|
37
27
|
|
|
@@ -68,8 +58,14 @@ function obj<T>(values: T) {
|
|
|
68
58
|
};
|
|
69
59
|
}
|
|
70
60
|
|
|
71
|
-
return Object.defineProperties({}, properties)
|
|
61
|
+
return Object.defineProperties({}, properties);
|
|
72
62
|
};
|
|
73
63
|
|
|
74
64
|
|
|
75
|
-
export default <T>(value: T) =>
|
|
65
|
+
export default <T>(value: T) => {
|
|
66
|
+
if (typeof value === 'function') {
|
|
67
|
+
return fn(value) as Infer<T>;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return factory(value) as Infer<T>;
|
|
71
|
+
};
|
package/src/reactive.ts
CHANGED
|
@@ -24,11 +24,7 @@ function mark(instances: Reactive<any>[] | null, state: typeof CHECK | typeof DI
|
|
|
24
24
|
queue.push(instance);
|
|
25
25
|
|
|
26
26
|
if (!scheduled) {
|
|
27
|
-
|
|
28
|
-
scheduler.schedule();
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
scheduled = true;
|
|
27
|
+
schedule();
|
|
32
28
|
}
|
|
33
29
|
}
|
|
34
30
|
|
|
@@ -41,13 +37,32 @@ function mark(instances: Reactive<any>[] | null, state: typeof CHECK | typeof DI
|
|
|
41
37
|
}
|
|
42
38
|
}
|
|
43
39
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
40
|
+
function schedule() {
|
|
41
|
+
if (scheduled) {
|
|
42
|
+
return;
|
|
47
43
|
}
|
|
48
44
|
|
|
45
|
+
for (let scheduler of schedulers) {
|
|
46
|
+
scheduler.schedule();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
scheduled = true;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async function task() {
|
|
53
|
+
let tasks = queue;
|
|
54
|
+
|
|
49
55
|
queue.length = 0;
|
|
56
|
+
|
|
57
|
+
for (let i = 0, n = tasks.length; i < n; i++) {
|
|
58
|
+
await tasks[i].get();
|
|
59
|
+
}
|
|
60
|
+
|
|
50
61
|
scheduled = false;
|
|
62
|
+
|
|
63
|
+
if (queue.length) {
|
|
64
|
+
schedule();
|
|
65
|
+
}
|
|
51
66
|
}
|
|
52
67
|
|
|
53
68
|
|
package/src/types.ts
CHANGED
|
@@ -1,25 +1,17 @@
|
|
|
1
1
|
import { CLEAN, CHECK, DIRTY } from './symbols';
|
|
2
|
-
import Reactive from './reactive';
|
|
3
2
|
|
|
4
3
|
|
|
5
4
|
type Fn = () => Promise<unknown> | unknown;
|
|
6
5
|
|
|
7
6
|
type Infer<T> =
|
|
8
|
-
T extends (...args: any[]) => any
|
|
9
|
-
? Reactive<T>
|
|
10
|
-
: T extends Record<string, any>
|
|
11
|
-
? InferNested<T>
|
|
12
|
-
: Reactive<T>;
|
|
13
|
-
|
|
14
|
-
type Primitives = any[] | boolean | number | string | null | undefined | ((...args: any[]) => any);
|
|
15
|
-
|
|
16
|
-
type InferNested<T> =
|
|
17
7
|
T extends (...args: any[]) => any
|
|
18
8
|
? ReturnType<T>
|
|
19
9
|
: T extends Record<string, Primitives>
|
|
20
|
-
? { [K in keyof T]:
|
|
10
|
+
? { [K in keyof T]: Infer<T[K]> }
|
|
21
11
|
: T;
|
|
22
12
|
|
|
13
|
+
type Primitives = any[] | boolean | number | string | null | undefined | ((...args: any[]) => any);
|
|
14
|
+
|
|
23
15
|
type ReactiveFn<T> = (onCleanup?: (fn: VoidFunction) => void) => T;
|
|
24
16
|
|
|
25
17
|
type Scheduler = {
|
package/build/obj.d.ts
DELETED
package/build/obj.js
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { reactive } from './index';
|
|
2
|
-
function setup(value) {
|
|
3
|
-
if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
|
|
4
|
-
return factory(value);
|
|
5
|
-
}
|
|
6
|
-
return reactive(value);
|
|
7
|
-
}
|
|
8
|
-
const factory = (values) => {
|
|
9
|
-
let lazy = {}, properties = {};
|
|
10
|
-
for (let key in values) {
|
|
11
|
-
properties[key] = {
|
|
12
|
-
get() {
|
|
13
|
-
if (!lazy[key]) {
|
|
14
|
-
lazy[key] = setup(values[key]);
|
|
15
|
-
}
|
|
16
|
-
return lazy[key].get();
|
|
17
|
-
},
|
|
18
|
-
set(value) {
|
|
19
|
-
if (!lazy[key]) {
|
|
20
|
-
lazy[key] = setup(values[key]);
|
|
21
|
-
}
|
|
22
|
-
lazy[key].set(value);
|
|
23
|
-
}
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
return Object.defineProperties({}, properties);
|
|
27
|
-
};
|
|
28
|
-
export default factory;
|