@esportsplus/reactivity 0.0.4 → 0.0.6

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,9 +1,28 @@
1
1
  import { Queue } from './types';
2
+ type Infer<T> = T extends (...args: any[]) => any ? InferValue<ReturnType<T>> : InferValue<T>;
3
+ type InferValue<T> = T extends Record<string, any> ? T : Reactive<T>;
4
+ declare class Reactive<T> {
5
+ private effect;
6
+ private fn?;
7
+ private observers;
8
+ private queue;
9
+ private sources;
10
+ private state;
11
+ private value;
12
+ cleanup: ((old: T) => void)[] | null;
13
+ constructor(input: ((fn: VoidFunction) => T) | T, queue?: Queue | null, effect?: boolean);
14
+ get(): T;
15
+ set(value: T): void;
16
+ private mark;
17
+ private removeParentObservers;
18
+ private sync;
19
+ private update;
20
+ }
2
21
  declare const effect: (queue: Queue) => <T>(value: () => T) => void;
3
- declare const reactive: <T>(value: T) => T;
22
+ declare const reactive: <T>(value: T) => Infer<T>;
4
23
  declare const _default: {
5
24
  effect: (queue: Queue) => <T>(value: () => T) => void;
6
- reactive: <T_1>(value: T_1) => T_1;
25
+ reactive: <T_1>(value: T_1) => Infer<T_1>;
7
26
  };
8
27
  export default _default;
9
28
  export { effect, reactive };
package/build/index.js CHANGED
@@ -10,10 +10,10 @@ class Reactive {
10
10
  state;
11
11
  value;
12
12
  cleanup = null;
13
- constructor(_, queue = null, effect = false) {
13
+ constructor(input, queue = null, effect = false) {
14
14
  this.effect = effect;
15
- if (typeof _ === 'function') {
16
- this.fn = _;
15
+ if (typeof input === 'function') {
16
+ this.fn = input;
17
17
  this.state = DIRTY;
18
18
  this.value = undefined;
19
19
  if (effect) {
@@ -23,7 +23,7 @@ class Reactive {
23
23
  }
24
24
  else {
25
25
  this.state = CLEAN;
26
- this.value = _;
26
+ this.value = input;
27
27
  }
28
28
  }
29
29
  get() {
@@ -166,10 +166,18 @@ const effect = (queue) => {
166
166
  };
167
167
  };
168
168
  const reactive = (value) => {
169
- if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
170
- return obj(value);
169
+ let v;
170
+ if (typeof value === 'function') {
171
+ let fn = new Reactive(value);
172
+ v = (...args) => fn.get()(...args);
171
173
  }
172
- return new Reactive(value);
174
+ else if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
175
+ v = obj(value);
176
+ }
177
+ else {
178
+ v = new Reactive(value);
179
+ }
180
+ return v;
173
181
  };
174
182
  export default { effect, reactive };
175
183
  export { effect, reactive };
package/build/obj.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- declare const factory: <T = Record<string, any>>(values: T) => T;
1
+ declare const factory: <T extends Record<string, any>>(values: T) => {};
2
2
  export default factory;
package/package.json CHANGED
@@ -2,8 +2,8 @@
2
2
  "author": "ICJR",
3
3
  "description": "Reactivity",
4
4
  "devDependencies": {
5
- "tsc-alias": "^1.8.1",
6
- "typescript": "^4.9.3"
5
+ "tsc-alias": "^1.8.2",
6
+ "typescript": "^4.9.4"
7
7
  },
8
8
  "main": "./build/index.js",
9
9
  "name": "@esportsplus/reactivity",
@@ -15,5 +15,5 @@
15
15
  "prepublishOnly": "npm run build"
16
16
  },
17
17
  "types": "./build/index.d.ts",
18
- "version": "0.0.4"
18
+ "version": "0.0.6"
19
19
  }
package/src/index.ts CHANGED
@@ -3,6 +3,15 @@ import { Queue, State } from './types';
3
3
  import obj from './obj';
4
4
 
5
5
 
6
+ // We only unwrap the top level function
7
+ type Infer<T> =
8
+ T extends (...args: any[]) => any
9
+ ? InferValue<ReturnType<T>>
10
+ : InferValue<T>;
11
+
12
+ type InferValue<T> = T extends Record<string, any> ? T : Reactive<T>;
13
+
14
+
6
15
  let index = 0,
7
16
  reaction: Reactive<any> | null = null,
8
17
  stack: Reactive<any>[] | null = null;
@@ -21,11 +30,11 @@ class Reactive<T> {
21
30
  cleanup: ((old: T) => void)[] | null = null;
22
31
 
23
32
 
24
- constructor(_: ((fn: VoidFunction) => T) | T, queue: Queue | null = null, effect: boolean = false) {
33
+ constructor(input: ((fn: VoidFunction) => T) | T, queue: Queue | null = null, effect: boolean = false) {
25
34
  this.effect = effect;
26
35
 
27
- if (typeof _ === 'function') {
28
- this.fn = _ as (onCleanup: (fn: VoidFunction) => void) => T;
36
+ if (typeof input === 'function') {
37
+ this.fn = input as (onCleanup: (fn: VoidFunction) => void) => T;
29
38
  this.state = DIRTY;
30
39
  this.value = undefined as any;
31
40
 
@@ -36,7 +45,7 @@ class Reactive<T> {
36
45
  }
37
46
  else {
38
47
  this.state = CLEAN;
39
- this.value = _;
48
+ this.value = input;
40
49
  }
41
50
  }
42
51
 
@@ -229,12 +238,22 @@ const effect = (queue: Queue) => {
229
238
  };
230
239
  };
231
240
 
232
- const reactive = <T>(value: T): T => {
233
- if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
234
- return obj(value);
241
+ const reactive = <T>(value: T) => {
242
+ let v;
243
+
244
+ if (typeof value === 'function') {
245
+ let fn = new Reactive(value);
246
+
247
+ v = (...args: any[]) => fn.get()(...args);
248
+ }
249
+ else if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
250
+ v = obj(value);
251
+ }
252
+ else {
253
+ v = new Reactive(value);
235
254
  }
236
255
 
237
- return new Reactive(value) as T;
256
+ return v as Infer<T>;
238
257
  };
239
258
 
240
259
 
package/src/obj.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { reactive } from './index';
2
2
 
3
3
 
4
- function setup(value: any) {
4
+ function setup(value: unknown) {
5
5
  // if (Array.isArray(value)) {
6
6
  // TODO: Need a solution
7
7
  // }
@@ -15,7 +15,7 @@ function setup(value: any) {
15
15
 
16
16
 
17
17
  // TODO: Typecheck on values tro get rid of lazy var
18
- const factory = <T = Record<string, any>>(values: T) => {
18
+ const factory = <T extends Record<string, any>>(values: T) => {
19
19
  let lazy: Record<string, any> = {},
20
20
  properties: PropertyDescriptorMap = {};
21
21
 
@@ -38,7 +38,7 @@ const factory = <T = Record<string, any>>(values: T) => {
38
38
  };
39
39
  }
40
40
 
41
- return Object.defineProperties({}, properties) as T;
41
+ return Object.defineProperties({}, properties);
42
42
  };
43
43
 
44
44