@esportsplus/reactivity 0.0.8 → 0.0.10

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  import { scheduler } from './reactive';
2
2
  import { effect, reactive } from './methods';
3
3
  declare const _default: {
4
- effect: <T>(value: () => T) => void;
4
+ effect: <T>(fn: () => T) => void;
5
5
  reactive: <T_1>(value: T_1) => import("./types").Infer<T_1>;
6
6
  scheduler: {
7
7
  add: (scheduler: import("./types").Scheduler) => void;
@@ -1,2 +1,2 @@
1
- declare const _default: <T>(value: () => T) => void;
1
+ declare const _default: <T>(fn: () => T) => void;
2
2
  export default _default;
@@ -1,4 +1,4 @@
1
1
  import Reactive from '../reactive';
2
- export default (value) => {
3
- new Reactive(value, true);
2
+ export default (fn) => {
3
+ new Reactive(fn, true);
4
4
  };
@@ -1,4 +1,5 @@
1
1
  import { Scheduler } from './types';
2
+ type Fn<T> = (onCleanup?: (fn: VoidFunction) => void) => T;
2
3
  declare class Reactive<T> {
3
4
  private effect;
4
5
  private fn?;
@@ -7,8 +8,8 @@ declare class Reactive<T> {
7
8
  private state;
8
9
  private value;
9
10
  cleanup: ((old: T) => void)[] | null;
10
- constructor(input: ((fn: VoidFunction) => T) | T, effect?: boolean);
11
- get(): T;
11
+ constructor(data: Fn<T> | T, effect?: boolean);
12
+ get(): T extends (...args: any[]) => any ? ReturnType<T> : T;
12
13
  set(value: T): void;
13
14
  private mark;
14
15
  private removeParentObservers;
package/build/reactive.js CHANGED
@@ -14,10 +14,10 @@ class Reactive {
14
14
  state;
15
15
  value;
16
16
  cleanup = null;
17
- constructor(input, effect = false) {
17
+ constructor(data, effect = false) {
18
18
  this.effect = effect;
19
- if (typeof input === 'function') {
20
- this.fn = input;
19
+ if (typeof data === 'function') {
20
+ this.fn = data;
21
21
  this.state = DIRTY;
22
22
  this.value = undefined;
23
23
  if (effect) {
@@ -26,7 +26,7 @@ class Reactive {
26
26
  }
27
27
  else {
28
28
  this.state = CLEAN;
29
- this.value = input;
29
+ this.value = data;
30
30
  }
31
31
  }
32
32
  get() {
package/build/types.d.ts CHANGED
@@ -2,7 +2,8 @@ import { CLEAN, CHECK, DIRTY } from './symbols';
2
2
  import Reactive from './reactive';
3
3
  type Fn = () => Promise<unknown> | unknown;
4
4
  type Infer<T> = T extends (...args: any[]) => any ? Reactive<T> : T extends Record<string, any> ? InferNested<T> : Reactive<T>;
5
- type InferNested<T> = T extends (...args: any[]) => any ? ReturnType<T> : T extends Record<string, any> ? {
5
+ type Primitives = any[] | boolean | number | string | null | undefined | ((...args: any[]) => any);
6
+ type InferNested<T> = T extends (...args: any[]) => any ? ReturnType<T> : T extends Record<string, Primitives> ? {
6
7
  [K in keyof T]: InferNested<T[K]>;
7
8
  } : T;
8
9
  type Scheduler = {
package/package.json CHANGED
@@ -15,5 +15,5 @@
15
15
  "prepublishOnly": "npm run build"
16
16
  },
17
17
  "types": "./build/index.d.ts",
18
- "version": "0.0.8"
18
+ "version": "0.0.10"
19
19
  }
@@ -1,6 +1,6 @@
1
1
  import Reactive from '~/reactive';
2
2
 
3
3
 
4
- export default <T>(value: () => T): void => {
5
- new Reactive(value, true);
4
+ export default <T>(fn: () => T): void => {
5
+ new Reactive(fn, true);
6
6
  };
package/src/reactive.ts CHANGED
@@ -18,9 +18,12 @@ async function task() {
18
18
  }
19
19
 
20
20
 
21
+ type Fn<T> = (onCleanup?: (fn: VoidFunction) => void) => T;
22
+
23
+
21
24
  class Reactive<T> {
22
25
  private effect: boolean;
23
- private fn?: (onCleanup: (fn: VoidFunction) => void) => T;
26
+ private fn?: Fn<T>;
24
27
  private observers: Reactive<any>[] | null = null;
25
28
  private sources: Reactive<any>[] | null = null;
26
29
  private state: State;
@@ -30,11 +33,11 @@ class Reactive<T> {
30
33
  cleanup: ((old: T) => void)[] | null = null;
31
34
 
32
35
 
33
- constructor(input: ((fn: VoidFunction) => T) | T, effect: boolean = false) {
36
+ constructor(data: Fn<T> | T, effect: boolean = false) {
34
37
  this.effect = effect;
35
38
 
36
- if (typeof input === 'function') {
37
- this.fn = input as (onCleanup: (fn: VoidFunction) => void) => T;
39
+ if (typeof data === 'function') {
40
+ this.fn = data as Fn<T>;
38
41
  this.state = DIRTY;
39
42
  this.value = undefined as any;
40
43
 
@@ -44,12 +47,12 @@ class Reactive<T> {
44
47
  }
45
48
  else {
46
49
  this.state = CLEAN;
47
- this.value = input;
50
+ this.value = data;
48
51
  }
49
52
  }
50
53
 
51
54
 
52
- get(): T {
55
+ get() {
53
56
  if (reaction) {
54
57
  if (!stack && reaction.sources && reaction.sources[index] == this) {
55
58
  index++;
@@ -68,7 +71,7 @@ class Reactive<T> {
68
71
  this.sync();
69
72
  }
70
73
 
71
- return this.value;
74
+ return this.value as T extends (...args: any[]) => any ? ReturnType<T> : T;
72
75
  }
73
76
 
74
77
  set(value: T) {
package/src/types.ts CHANGED
@@ -11,10 +11,12 @@ type Infer<T> =
11
11
  ? InferNested<T>
12
12
  : Reactive<T>;
13
13
 
14
+ type Primitives = any[] | boolean | number | string | null | undefined | ((...args: any[]) => any);
15
+
14
16
  type InferNested<T> =
15
17
  T extends (...args: any[]) => any
16
18
  ? ReturnType<T>
17
- : T extends Record<string, any>
19
+ : T extends Record<string, Primitives>
18
20
  ? { [K in keyof T]: InferNested<T[K]> }
19
21
  : T;
20
22