@esportsplus/reactivity 0.0.4 → 0.0.5

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,29 @@
1
1
  import { Queue } from './types';
2
+ type Infer<T> = T extends (...args: any[]) => any ? Infer<ReturnType<T>> : T extends Record<string, any> ? {
3
+ [K in keyof T]: Infer<T[K]>;
4
+ } : Reactive<T>;
5
+ declare class Reactive<T> {
6
+ private effect;
7
+ private fn?;
8
+ private observers;
9
+ private queue;
10
+ private sources;
11
+ private state;
12
+ private value;
13
+ cleanup: ((old: T) => void)[] | null;
14
+ constructor(input: ((fn: VoidFunction) => T) | T, queue?: Queue | null, effect?: boolean);
15
+ get(): T;
16
+ set(value: T): void;
17
+ private mark;
18
+ private removeParentObservers;
19
+ private sync;
20
+ private update;
21
+ }
2
22
  declare const effect: (queue: Queue) => <T>(value: () => T) => void;
3
- declare const reactive: <T>(value: T) => T;
23
+ declare const reactive: <T>(value: T) => Infer<T>;
4
24
  declare const _default: {
5
25
  effect: (queue: Queue) => <T>(value: () => T) => void;
6
- reactive: <T_1>(value: T_1) => T_1;
26
+ reactive: <T_1>(value: T_1) => Infer<T_1>;
7
27
  };
8
28
  export default _default;
9
29
  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,14 @@ const effect = (queue) => {
166
166
  };
167
167
  };
168
168
  const reactive = (value) => {
169
+ let v;
169
170
  if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
170
- return obj(value);
171
+ v = obj(value);
171
172
  }
172
- return new Reactive(value);
173
+ else {
174
+ v = new Reactive(value);
175
+ }
176
+ return v;
173
177
  };
174
178
  export default { effect, reactive };
175
179
  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.5"
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
+ type Infer<T> =
7
+ T extends (...args: any[]) => any
8
+ ? Infer<ReturnType<T>>
9
+ : T extends Record<string, any>
10
+ ? { [K in keyof T]: Infer<T[K]> }
11
+ // Requires class type
12
+ : 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,17 @@ const effect = (queue: Queue) => {
229
238
  };
230
239
  };
231
240
 
232
- const reactive = <T>(value: T): T => {
241
+ const reactive = <T>(value: T) => {
242
+ let v;
243
+
233
244
  if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
234
- return obj(value);
245
+ v = obj(value);
246
+ }
247
+ else {
248
+ v = new Reactive(value);
235
249
  }
236
250
 
237
- return new Reactive(value) as T;
251
+ return v as Infer<T>;
238
252
  };
239
253
 
240
254
 
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