@esportsplus/reactivity 0.0.1 → 0.0.4

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,20 +1,9 @@
1
- declare class Reactive<T> {
2
- private effect;
3
- private fn?;
4
- private observers;
5
- private sources;
6
- private state;
7
- private value;
8
- cleanup: ((old: T) => void)[] | null;
9
- constructor(_: ((fn: VoidFunction) => T) | T, effect?: boolean);
10
- get(): T;
11
- set(value: T): void;
12
- private mark;
13
- private removeParentObservers;
14
- private sync;
15
- private update;
16
- }
17
- declare const effect: <T>(value: () => T) => Reactive<T>;
18
- declare const reactive: <T>(value: T) => {};
19
- declare const tick: () => void;
20
- export { effect, reactive, tick };
1
+ import { Queue } from './types';
2
+ declare const effect: (queue: Queue) => <T>(value: () => T) => void;
3
+ declare const reactive: <T>(value: T) => T;
4
+ declare const _default: {
5
+ effect: (queue: Queue) => <T>(value: () => T) => void;
6
+ reactive: <T_1>(value: T_1) => T_1;
7
+ };
8
+ export default _default;
9
+ export { effect, reactive };
package/build/index.js CHANGED
@@ -1,21 +1,23 @@
1
1
  import { CLEAN, CHECK, DIRTY } from './symbols';
2
2
  import obj from './obj';
3
- let index = 0, queue = [], reaction = null, running = false, stack = null;
3
+ let index = 0, reaction = null, stack = null;
4
4
  class Reactive {
5
5
  effect;
6
6
  fn;
7
7
  observers = null;
8
+ queue = null;
8
9
  sources = null;
9
10
  state;
10
11
  value;
11
12
  cleanup = null;
12
- constructor(_, effect = false) {
13
+ constructor(_, queue = null, effect = false) {
13
14
  this.effect = effect;
14
15
  if (typeof _ === 'function') {
15
16
  this.fn = _;
16
17
  this.state = DIRTY;
17
18
  this.value = undefined;
18
19
  if (effect) {
20
+ this.queue = queue;
19
21
  this.update();
20
22
  }
21
23
  }
@@ -54,7 +56,12 @@ class Reactive {
54
56
  mark(state) {
55
57
  if (this.state < state) {
56
58
  if (this.state === CLEAN && this.effect) {
57
- queue.push(this);
59
+ if (!this.queue) {
60
+ throw new Error('Effects cannot be updated without a queue');
61
+ }
62
+ this.queue.add(async () => {
63
+ await this.get();
64
+ });
58
65
  }
59
66
  this.state = state;
60
67
  if (!this.observers) {
@@ -153,8 +160,10 @@ class Reactive {
153
160
  this.state = CLEAN;
154
161
  }
155
162
  }
156
- const effect = (value) => {
157
- return new Reactive(value, true);
163
+ const effect = (queue) => {
164
+ return (value) => {
165
+ new Reactive(value, queue, true);
166
+ };
158
167
  };
159
168
  const reactive = (value) => {
160
169
  if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
@@ -162,15 +171,5 @@ const reactive = (value) => {
162
171
  }
163
172
  return new Reactive(value);
164
173
  };
165
- const tick = () => {
166
- if (running) {
167
- return;
168
- }
169
- running = true;
170
- for (let i = 0, n = queue.length; i < n; i++) {
171
- queue[i].get();
172
- }
173
- queue.length = 0;
174
- running = false;
175
- };
176
- export { effect, reactive, tick };
174
+ export default { effect, reactive };
175
+ export { effect, reactive };
package/build/obj.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- declare const factory: (values: Record<string, unknown>) => {};
1
+ declare const factory: <T = Record<string, any>>(values: T) => T;
2
2
  export default factory;
package/build/obj.js CHANGED
@@ -1,21 +1,24 @@
1
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
+ }
2
8
  const factory = (values) => {
3
9
  let lazy = {}, properties = {};
4
10
  for (let key in values) {
5
11
  properties[key] = {
6
12
  get() {
7
13
  if (!lazy[key]) {
8
- let value = values[key];
9
- if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
10
- lazy[key] = factory(value);
11
- }
12
- else {
13
- lazy[key] = reactive(value);
14
- }
14
+ lazy[key] = setup(values[key]);
15
15
  }
16
- return lazy[key]?.get() || lazy[key];
16
+ return lazy[key].get();
17
17
  },
18
18
  set(value) {
19
+ if (!lazy[key]) {
20
+ lazy[key] = setup(values[key]);
21
+ }
19
22
  lazy[key].set(value);
20
23
  }
21
24
  };
package/build/types.d.ts CHANGED
@@ -1,3 +1,6 @@
1
1
  import { CLEAN, CHECK, DIRTY } from './symbols';
2
+ type Queue = {
3
+ add: (fn: () => Promise<void> | void) => void;
4
+ };
2
5
  type State = typeof CHECK | typeof CLEAN | typeof DIRTY;
3
- export { State };
6
+ export { Queue, State };
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.1"
18
+ "version": "0.0.4"
19
19
  }
package/src/index.ts CHANGED
@@ -1,12 +1,10 @@
1
1
  import { CLEAN, CHECK, DIRTY } from './symbols';
2
- import { State } from './types';
2
+ import { Queue, State } from './types';
3
3
  import obj from './obj';
4
4
 
5
5
 
6
6
  let index = 0,
7
- queue: Reactive<any>[] = [],
8
7
  reaction: Reactive<any> | null = null,
9
- running: boolean = false,
10
8
  stack: Reactive<any>[] | null = null;
11
9
 
12
10
 
@@ -14,6 +12,7 @@ class Reactive<T> {
14
12
  private effect: boolean;
15
13
  private fn?: (onCleanup: (fn: VoidFunction) => void) => T;
16
14
  private observers: Reactive<any>[] | null = null;
15
+ private queue: Queue | null = null;
17
16
  private sources: Reactive<any>[] | null = null;
18
17
  private state: State;
19
18
  private value: T;
@@ -22,7 +21,7 @@ class Reactive<T> {
22
21
  cleanup: ((old: T) => void)[] | null = null;
23
22
 
24
23
 
25
- constructor(_: ((fn: VoidFunction) => T) | T, effect: boolean = false) {
24
+ constructor(_: ((fn: VoidFunction) => T) | T, queue: Queue | null = null, effect: boolean = false) {
26
25
  this.effect = effect;
27
26
 
28
27
  if (typeof _ === 'function') {
@@ -31,6 +30,7 @@ class Reactive<T> {
31
30
  this.value = undefined as any;
32
31
 
33
32
  if (effect) {
33
+ this.queue = queue;
34
34
  this.update();
35
35
  }
36
36
  }
@@ -63,7 +63,7 @@ class Reactive<T> {
63
63
  return this.value;
64
64
  }
65
65
 
66
- set(value: T): void {
66
+ set(value: T) {
67
67
  if (this.observers && this.value !== value) {
68
68
  for (let i = 0; i < this.observers.length; i++) {
69
69
  this.observers[i].mark(DIRTY);
@@ -74,11 +74,17 @@ class Reactive<T> {
74
74
  }
75
75
 
76
76
 
77
- private mark(state: typeof CHECK | typeof DIRTY): void {
77
+ private mark(state: typeof CHECK | typeof DIRTY) {
78
78
  if (this.state < state) {
79
79
  // If previous state was clean we need to update effects
80
80
  if (this.state === CLEAN && this.effect) {
81
- queue.push(this);
81
+ if (!this.queue) {
82
+ throw new Error('Effects cannot be updated without a queue');
83
+ }
84
+
85
+ this.queue.add(async () => {
86
+ await this.get();
87
+ });
82
88
  }
83
89
 
84
90
  this.state = state;
@@ -94,7 +100,7 @@ class Reactive<T> {
94
100
  }
95
101
 
96
102
  // We don't actually delete sources here because we're replacing the entire array soon
97
- private removeParentObservers(): void {
103
+ private removeParentObservers() {
98
104
  if (!this.sources) {
99
105
  return;
100
106
  }
@@ -108,7 +114,7 @@ class Reactive<T> {
108
114
  }
109
115
 
110
116
  // Update if dirty or if a parent is dirty
111
- private sync(): void {
117
+ private sync() {
112
118
  // If we are potentially dirty, see if we have a parent who has actually changed value
113
119
  if (this.state === CHECK && this.sources) {
114
120
  for (let i = 0, n = this.sources.length; i < n; i++) {
@@ -132,7 +138,7 @@ class Reactive<T> {
132
138
  this.state = CLEAN;
133
139
  }
134
140
 
135
- private update(): void {
141
+ private update() {
136
142
  let previous = {
137
143
  index: index,
138
144
  reaction: reaction,
@@ -217,35 +223,20 @@ class Reactive<T> {
217
223
  }
218
224
 
219
225
 
220
-
221
- const effect = <T>(value: () => T) => {
222
- return new Reactive(value, true);
226
+ const effect = (queue: Queue) => {
227
+ return <T>(value: () => T) => {
228
+ new Reactive(value, queue, true);
229
+ };
223
230
  };
224
231
 
225
- const reactive = <T>(value: T) => {
232
+ const reactive = <T>(value: T): T => {
226
233
  if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
227
- return obj(value as Record<string, unknown>);
228
- }
229
-
230
- return new Reactive(value);
231
- };
232
-
233
- const tick = () => {
234
- if (running) {
235
- return;
236
- }
237
-
238
- running = true;
239
-
240
- for (let i = 0, n = queue.length; i < n; i++) {
241
- queue[i].get();
234
+ return obj(value);
242
235
  }
243
236
 
244
- queue.length = 0;
245
- running = false;
237
+ return new Reactive(value) as T;
246
238
  };
247
239
 
248
240
 
249
- export { effect, reactive, tick };
250
-
251
-
241
+ export default { effect, reactive };
242
+ export { effect, reactive };
package/src/obj.ts CHANGED
@@ -1,7 +1,21 @@
1
1
  import { reactive } from './index';
2
2
 
3
3
 
4
- const factory = (values: Record<string, unknown>) => {
4
+ function setup(value: any) {
5
+ // if (Array.isArray(value)) {
6
+ // TODO: Need a solution
7
+ // }
8
+ // TODO: Can remove isArray once solution is found ^
9
+ if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
10
+ return factory(value);
11
+ }
12
+
13
+ return reactive(value);
14
+ }
15
+
16
+
17
+ // TODO: Typecheck on values tro get rid of lazy var
18
+ const factory = <T = Record<string, any>>(values: T) => {
5
19
  let lazy: Record<string, any> = {},
6
20
  properties: PropertyDescriptorMap = {};
7
21
 
@@ -9,29 +23,22 @@ const factory = (values: Record<string, unknown>) => {
9
23
  properties[key] = {
10
24
  get() {
11
25
  if (!lazy[key]) {
12
- let value = values[key];
13
-
14
- // if (Array.isArray(value)) {
15
- // TODO: Need a solution
16
- // }
17
- // TODO: Can remove isArray once solution is found ^
18
- if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
19
- lazy[key] = factory(value as Record<string, unknown>);
20
- }
21
- else {
22
- lazy[key] = reactive(value);
23
- }
26
+ lazy[key] = setup(values[key]);
24
27
  }
25
28
 
26
- return lazy[key]?.get() || lazy[key];
29
+ return lazy[key].get();
27
30
  },
28
31
  set(value: unknown) {
32
+ if (!lazy[key]) {
33
+ lazy[key] = setup(values[key]);
34
+ }
35
+
29
36
  lazy[key].set(value);
30
37
  }
31
38
  };
32
39
  }
33
40
 
34
- return Object.defineProperties({}, properties);
41
+ return Object.defineProperties({}, properties) as T;
35
42
  };
36
43
 
37
44
 
package/src/types.ts CHANGED
@@ -1,7 +1,11 @@
1
1
  import { CLEAN, CHECK, DIRTY } from './symbols';
2
2
 
3
3
 
4
+ type Queue = {
5
+ add: (fn: () => Promise<void> | void) => void;
6
+ };
7
+
4
8
  type State = typeof CHECK | typeof CLEAN | typeof DIRTY;
5
9
 
6
10
 
7
- export { State };
11
+ export { Queue, State };