@oscarpalmer/mora 0.10.0 → 0.11.1

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/dist/computed.cjs CHANGED
@@ -5,24 +5,17 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
5
5
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
6
6
  const batch = require("./batch.cjs");
7
7
  const effect = require("./effect.cjs");
8
- const subscription = require("./subscription.cjs");
8
+ const reactive = require("./reactive.cjs");
9
9
  exports.activeComputed = void 0;
10
- class Computed {
10
+ class Computed extends reactive.Reactive {
11
11
  constructor(callback) {
12
- __publicField(this, "state");
13
- Object.defineProperty(this, "$mora", {
14
- value: "computed"
15
- });
16
- this.state = {
17
- computeds: /* @__PURE__ */ new Set(),
12
+ super("computed", void 0);
13
+ __publicField(this, "effect", {
18
14
  dirty: true,
19
- effect: void 0,
20
- effects: /* @__PURE__ */ new Set(),
21
- subscriptions: /* @__PURE__ */ new Map(),
22
- value: void 0
23
- };
24
- this.state.effect = effect.effect(() => {
25
- if (this.state.dirty) {
15
+ instance: void 0
16
+ });
17
+ this.effect.instance = effect.effect(() => {
18
+ if (this.effect.dirty) {
26
19
  const previousComputed = exports.activeComputed;
27
20
  exports.activeComputed = this;
28
21
  const value = callback();
@@ -30,16 +23,16 @@ class Computed {
30
23
  if (!Object.is(this.state.value, value)) {
31
24
  this.state.value = value;
32
25
  for (const computed2 of this.state.computeds) {
33
- computed2.state.dirty = true;
26
+ computed2.effect.dirty = true;
34
27
  }
35
28
  for (const effect2 of this.state.effects) {
36
29
  batch.batchedHandlers.add(effect2);
37
30
  }
38
- for (const [, subscription2] of this.state.subscriptions) {
39
- subscription2.callback(value);
31
+ for (const [, subscription] of this.state.subscriptions) {
32
+ subscription.callback(value);
40
33
  }
41
34
  }
42
- this.state.dirty = false;
35
+ this.effect.dirty = false;
43
36
  }
44
37
  });
45
38
  }
@@ -50,32 +43,14 @@ class Computed {
50
43
  if (exports.activeComputed != null && exports.activeComputed !== this) {
51
44
  this.state.computeds.add(exports.activeComputed);
52
45
  }
53
- if (effect.activeEffect != null && effect.activeEffect !== this.state.effect) {
46
+ if (effect.activeEffect != null && effect.activeEffect !== this.effect.instance) {
54
47
  this.state.effects.add(effect.activeEffect);
55
48
  }
56
- if (this.state.dirty && batch.batchDepth === 0) {
57
- effect.runEffect(this.state.effect);
49
+ if (this.effect.dirty && batch.batchDepth === 0) {
50
+ effect.runEffect(this.effect.instance);
58
51
  }
59
52
  return this.state.value;
60
53
  }
61
- /**
62
- * Get the value _(without reactivity)_
63
- */
64
- peek() {
65
- return this.state.value;
66
- }
67
- /**
68
- * Subscribe to changes
69
- */
70
- subscribe(callback) {
71
- return subscription.subscribe(this.state, callback);
72
- }
73
- /**
74
- * Unsubscribe from changes
75
- */
76
- unsubscribe(callback) {
77
- subscription.unsubscribe(this.state, callback);
78
- }
79
54
  }
80
55
  function computed(callback) {
81
56
  return new Computed(callback);
package/dist/computed.js CHANGED
@@ -3,24 +3,17 @@ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { en
3
3
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
4
  import { batchedHandlers, batchDepth } from "./batch.js";
5
5
  import { effect, activeEffect, runEffect } from "./effect.js";
6
- import { subscribe, unsubscribe } from "./subscription.js";
6
+ import { Reactive } from "./reactive.js";
7
7
  let activeComputed;
8
- class Computed {
8
+ class Computed extends Reactive {
9
9
  constructor(callback) {
10
- __publicField(this, "state");
11
- Object.defineProperty(this, "$mora", {
12
- value: "computed"
13
- });
14
- this.state = {
15
- computeds: /* @__PURE__ */ new Set(),
10
+ super("computed", void 0);
11
+ __publicField(this, "effect", {
16
12
  dirty: true,
17
- effect: void 0,
18
- effects: /* @__PURE__ */ new Set(),
19
- subscriptions: /* @__PURE__ */ new Map(),
20
- value: void 0
21
- };
22
- this.state.effect = effect(() => {
23
- if (this.state.dirty) {
13
+ instance: void 0
14
+ });
15
+ this.effect.instance = effect(() => {
16
+ if (this.effect.dirty) {
24
17
  const previousComputed = activeComputed;
25
18
  activeComputed = this;
26
19
  const value = callback();
@@ -28,7 +21,7 @@ class Computed {
28
21
  if (!Object.is(this.state.value, value)) {
29
22
  this.state.value = value;
30
23
  for (const computed2 of this.state.computeds) {
31
- computed2.state.dirty = true;
24
+ computed2.effect.dirty = true;
32
25
  }
33
26
  for (const effect2 of this.state.effects) {
34
27
  batchedHandlers.add(effect2);
@@ -37,7 +30,7 @@ class Computed {
37
30
  subscription.callback(value);
38
31
  }
39
32
  }
40
- this.state.dirty = false;
33
+ this.effect.dirty = false;
41
34
  }
42
35
  });
43
36
  }
@@ -48,32 +41,14 @@ class Computed {
48
41
  if (activeComputed != null && activeComputed !== this) {
49
42
  this.state.computeds.add(activeComputed);
50
43
  }
51
- if (activeEffect != null && activeEffect !== this.state.effect) {
44
+ if (activeEffect != null && activeEffect !== this.effect.instance) {
52
45
  this.state.effects.add(activeEffect);
53
46
  }
54
- if (this.state.dirty && batchDepth === 0) {
55
- runEffect(this.state.effect);
47
+ if (this.effect.dirty && batchDepth === 0) {
48
+ runEffect(this.effect.instance);
56
49
  }
57
50
  return this.state.value;
58
51
  }
59
- /**
60
- * Get the value _(without reactivity)_
61
- */
62
- peek() {
63
- return this.state.value;
64
- }
65
- /**
66
- * Subscribe to changes
67
- */
68
- subscribe(callback) {
69
- return subscribe(this.state, callback);
70
- }
71
- /**
72
- * Unsubscribe from changes
73
- */
74
- unsubscribe(callback) {
75
- unsubscribe(this.state, callback);
76
- }
77
52
  }
78
53
  function computed(callback) {
79
54
  return new Computed(callback);
package/dist/index.cjs CHANGED
@@ -11,5 +11,6 @@ exports.computed = computed.computed;
11
11
  exports.effect = effect.effect;
12
12
  exports.isComputed = is.isComputed;
13
13
  exports.isEffect = is.isEffect;
14
+ exports.isReactive = is.isReactive;
14
15
  exports.isSignal = is.isSignal;
15
16
  exports.signal = signal.signal;
package/dist/index.js CHANGED
@@ -1,13 +1,14 @@
1
1
  import { startBatch, stopBatch } from "./batch.js";
2
2
  import { computed } from "./computed.js";
3
3
  import { effect } from "./effect.js";
4
- import { isComputed, isEffect, isSignal } from "./is.js";
4
+ import { isComputed, isEffect, isReactive, isSignal } from "./is.js";
5
5
  import { signal } from "./signal.js";
6
6
  export {
7
7
  computed,
8
8
  effect,
9
9
  isComputed,
10
10
  isEffect,
11
+ isReactive,
11
12
  isSignal,
12
13
  signal,
13
14
  startBatch,
package/dist/is.cjs CHANGED
@@ -9,9 +9,13 @@ function isEffect(value) {
9
9
  function isMora(value, name) {
10
10
  return typeof value === "object" && value != null && "$mora" in value && value.$mora === name;
11
11
  }
12
+ function isReactive(value) {
13
+ return isComputed(value) || isSignal(value);
14
+ }
12
15
  function isSignal(value) {
13
16
  return isMora(value, "signal");
14
17
  }
15
18
  exports.isComputed = isComputed;
16
19
  exports.isEffect = isEffect;
20
+ exports.isReactive = isReactive;
17
21
  exports.isSignal = isSignal;
package/dist/is.js CHANGED
@@ -7,11 +7,15 @@ function isEffect(value) {
7
7
  function isMora(value, name) {
8
8
  return typeof value === "object" && value != null && "$mora" in value && value.$mora === name;
9
9
  }
10
+ function isReactive(value) {
11
+ return isComputed(value) || isSignal(value);
12
+ }
10
13
  function isSignal(value) {
11
14
  return isMora(value, "signal");
12
15
  }
13
16
  export {
14
17
  isComputed,
15
18
  isEffect,
19
+ isReactive,
16
20
  isSignal
17
21
  };
package/dist/mora.full.js CHANGED
@@ -45,6 +45,9 @@ function isMora(value, name) {
45
45
  '$mora' in value &&
46
46
  value.$mora === name);
47
47
  }
48
+ function isReactive(value) {
49
+ return isComputed(value) || isSignal(value);
50
+ }
48
51
  /**
49
52
  * Is the value a signal?
50
53
  */
@@ -112,23 +115,49 @@ function unsubscribe(state, callback) {
112
115
  }
113
116
  }
114
117
 
115
- let activeComputed;
116
- class Computed {
117
- state;
118
- constructor(callback) {
118
+ class Reactive {
119
+ state = {
120
+ computeds: new Set(),
121
+ effects: new Set(),
122
+ subscriptions: new Map(),
123
+ value: undefined,
124
+ };
125
+ constructor(name, value) {
126
+ this.state.value = value;
119
127
  Object.defineProperty(this, '$mora', {
120
- value: 'computed',
128
+ value: name,
121
129
  });
122
- this.state = {
123
- computeds: new Set(),
124
- dirty: true,
125
- effect: undefined,
126
- effects: new Set(),
127
- subscriptions: new Map(),
128
- value: undefined,
129
- };
130
- this.state.effect = effect(() => {
131
- if (this.state.dirty) {
130
+ }
131
+ /**
132
+ * Get the value _(without reactivity)_
133
+ */
134
+ peek() {
135
+ return this.state.value;
136
+ }
137
+ /**
138
+ * Subscribe to changes
139
+ */
140
+ subscribe(callback) {
141
+ return subscribe(this.state, callback);
142
+ }
143
+ /**
144
+ * Unsubscribe from changes
145
+ */
146
+ unsubscribe(callback) {
147
+ unsubscribe(this.state, callback);
148
+ }
149
+ }
150
+
151
+ let activeComputed;
152
+ class Computed extends Reactive {
153
+ effect = {
154
+ dirty: true,
155
+ instance: undefined,
156
+ };
157
+ constructor(callback) {
158
+ super('computed', undefined);
159
+ this.effect.instance = effect(() => {
160
+ if (this.effect.dirty) {
132
161
  const previousComputed = activeComputed;
133
162
  activeComputed = this;
134
163
  const value = callback();
@@ -136,7 +165,7 @@ class Computed {
136
165
  if (!Object.is(this.state.value, value)) {
137
166
  this.state.value = value;
138
167
  for (const computed of this.state.computeds) {
139
- computed.state.dirty = true;
168
+ computed.effect.dirty = true;
140
169
  }
141
170
  for (const effect of this.state.effects) {
142
171
  batchedHandlers.add(effect);
@@ -145,7 +174,7 @@ class Computed {
145
174
  subscription.callback(value);
146
175
  }
147
176
  }
148
- this.state.dirty = false;
177
+ this.effect.dirty = false;
149
178
  }
150
179
  });
151
180
  }
@@ -156,32 +185,14 @@ class Computed {
156
185
  if (activeComputed != null && activeComputed !== this) {
157
186
  this.state.computeds.add(activeComputed);
158
187
  }
159
- if (activeEffect != null && activeEffect !== this.state.effect) {
188
+ if (activeEffect != null && activeEffect !== this.effect.instance) {
160
189
  this.state.effects.add(activeEffect);
161
190
  }
162
- if (this.state.dirty && batchDepth === 0) {
163
- runEffect(this.state.effect);
191
+ if (this.effect.dirty && batchDepth === 0) {
192
+ runEffect(this.effect.instance);
164
193
  }
165
194
  return this.state.value;
166
195
  }
167
- /**
168
- * Get the value _(without reactivity)_
169
- */
170
- peek() {
171
- return this.state.value;
172
- }
173
- /**
174
- * Subscribe to changes
175
- */
176
- subscribe(callback) {
177
- return subscribe(this.state, callback);
178
- }
179
- /**
180
- * Unsubscribe from changes
181
- */
182
- unsubscribe(callback) {
183
- unsubscribe(this.state, callback);
184
- }
185
196
  }
186
197
  /**
187
198
  * Create a computed value
@@ -190,18 +201,9 @@ function computed(callback) {
190
201
  return new Computed(callback);
191
202
  }
192
203
 
193
- class Signal {
194
- state;
204
+ class Signal extends Reactive {
195
205
  constructor(value) {
196
- Object.defineProperty(this, '$mora', {
197
- value: 'signal',
198
- });
199
- this.state = {
200
- value,
201
- computeds: new Set(),
202
- effects: new Set(),
203
- subscriptions: new Map(),
204
- };
206
+ super('signal', value);
205
207
  }
206
208
  /**
207
209
  * Get the value
@@ -215,12 +217,6 @@ class Signal {
215
217
  }
216
218
  return this.state.value;
217
219
  }
218
- /**
219
- * Get the value _(without reactivity)_
220
- */
221
- peek() {
222
- return this.state.value;
223
- }
224
220
  /**
225
221
  * Set the value
226
222
  */
@@ -230,7 +226,7 @@ class Signal {
230
226
  }
231
227
  this.state.value = value;
232
228
  for (const computed of this.state.computeds) {
233
- computed.state.dirty = true;
229
+ computed.effect.dirty = true;
234
230
  }
235
231
  for (const effect of this.state.effects) {
236
232
  batchedHandlers.add(effect);
@@ -242,18 +238,6 @@ class Signal {
242
238
  flushEffects();
243
239
  }
244
240
  }
245
- /**
246
- * Subscribe to changes
247
- */
248
- subscribe(callback) {
249
- return subscribe(this.state, callback);
250
- }
251
- /**
252
- * Unsubscribe from changes
253
- */
254
- unsubscribe(callback) {
255
- unsubscribe(this.state, callback);
256
- }
257
241
  /**
258
242
  * Update the value
259
243
  */
@@ -265,4 +249,4 @@ function signal(value) {
265
249
  return new Signal(value);
266
250
  }
267
251
 
268
- export { computed, effect, isComputed, isEffect, isSignal, signal, startBatch, stopBatch };
252
+ export { computed, effect, isComputed, isEffect, isReactive, isSignal, signal, startBatch, stopBatch };
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
4
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
6
+ const subscription = require("./subscription.cjs");
7
+ class Reactive {
8
+ constructor(name, value) {
9
+ __publicField(this, "state", {
10
+ computeds: /* @__PURE__ */ new Set(),
11
+ effects: /* @__PURE__ */ new Set(),
12
+ subscriptions: /* @__PURE__ */ new Map(),
13
+ value: void 0
14
+ });
15
+ this.state.value = value;
16
+ Object.defineProperty(this, "$mora", {
17
+ value: name
18
+ });
19
+ }
20
+ /**
21
+ * Get the value _(without reactivity)_
22
+ */
23
+ peek() {
24
+ return this.state.value;
25
+ }
26
+ /**
27
+ * Subscribe to changes
28
+ */
29
+ subscribe(callback) {
30
+ return subscription.subscribe(this.state, callback);
31
+ }
32
+ /**
33
+ * Unsubscribe from changes
34
+ */
35
+ unsubscribe(callback) {
36
+ subscription.unsubscribe(this.state, callback);
37
+ }
38
+ }
39
+ exports.Reactive = Reactive;
@@ -0,0 +1,39 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
+ import { subscribe, unsubscribe } from "./subscription.js";
5
+ class Reactive {
6
+ constructor(name, value) {
7
+ __publicField(this, "state", {
8
+ computeds: /* @__PURE__ */ new Set(),
9
+ effects: /* @__PURE__ */ new Set(),
10
+ subscriptions: /* @__PURE__ */ new Map(),
11
+ value: void 0
12
+ });
13
+ this.state.value = value;
14
+ Object.defineProperty(this, "$mora", {
15
+ value: name
16
+ });
17
+ }
18
+ /**
19
+ * Get the value _(without reactivity)_
20
+ */
21
+ peek() {
22
+ return this.state.value;
23
+ }
24
+ /**
25
+ * Subscribe to changes
26
+ */
27
+ subscribe(callback) {
28
+ return subscribe(this.state, callback);
29
+ }
30
+ /**
31
+ * Unsubscribe from changes
32
+ */
33
+ unsubscribe(callback) {
34
+ unsubscribe(this.state, callback);
35
+ }
36
+ }
37
+ export {
38
+ Reactive
39
+ };
package/dist/signal.cjs CHANGED
@@ -1,24 +1,12 @@
1
1
  "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
4
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
6
3
  const batch = require("./batch.cjs");
7
4
  const computed = require("./computed.cjs");
8
5
  const effect = require("./effect.cjs");
9
- const subscription = require("./subscription.cjs");
10
- class Signal {
6
+ const reactive = require("./reactive.cjs");
7
+ class Signal extends reactive.Reactive {
11
8
  constructor(value) {
12
- __publicField(this, "state");
13
- Object.defineProperty(this, "$mora", {
14
- value: "signal"
15
- });
16
- this.state = {
17
- value,
18
- computeds: /* @__PURE__ */ new Set(),
19
- effects: /* @__PURE__ */ new Set(),
20
- subscriptions: /* @__PURE__ */ new Map()
21
- };
9
+ super("signal", value);
22
10
  }
23
11
  /**
24
12
  * Get the value
@@ -32,12 +20,6 @@ class Signal {
32
20
  }
33
21
  return this.state.value;
34
22
  }
35
- /**
36
- * Get the value _(without reactivity)_
37
- */
38
- peek() {
39
- return this.state.value;
40
- }
41
23
  /**
42
24
  * Set the value
43
25
  */
@@ -47,30 +29,18 @@ class Signal {
47
29
  }
48
30
  this.state.value = value;
49
31
  for (const computed2 of this.state.computeds) {
50
- computed2.state.dirty = true;
32
+ computed2.effect.dirty = true;
51
33
  }
52
34
  for (const effect2 of this.state.effects) {
53
35
  batch.batchedHandlers.add(effect2);
54
36
  }
55
- for (const [, subscription2] of this.state.subscriptions) {
56
- batch.batchedHandlers.add(subscription2);
37
+ for (const [, subscription] of this.state.subscriptions) {
38
+ batch.batchedHandlers.add(subscription);
57
39
  }
58
40
  if (batch.batchDepth === 0) {
59
41
  batch.flushEffects();
60
42
  }
61
43
  }
62
- /**
63
- * Subscribe to changes
64
- */
65
- subscribe(callback) {
66
- return subscription.subscribe(this.state, callback);
67
- }
68
- /**
69
- * Unsubscribe from changes
70
- */
71
- unsubscribe(callback) {
72
- subscription.unsubscribe(this.state, callback);
73
- }
74
44
  /**
75
45
  * Update the value
76
46
  */
package/dist/signal.js CHANGED
@@ -1,22 +1,10 @@
1
- var __defProp = Object.defineProperty;
2
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
- var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
1
  import { batchedHandlers, flushEffects, batchDepth } from "./batch.js";
5
2
  import { activeComputed } from "./computed.js";
6
3
  import { activeEffect } from "./effect.js";
7
- import { subscribe, unsubscribe } from "./subscription.js";
8
- class Signal {
4
+ import { Reactive } from "./reactive.js";
5
+ class Signal extends Reactive {
9
6
  constructor(value) {
10
- __publicField(this, "state");
11
- Object.defineProperty(this, "$mora", {
12
- value: "signal"
13
- });
14
- this.state = {
15
- value,
16
- computeds: /* @__PURE__ */ new Set(),
17
- effects: /* @__PURE__ */ new Set(),
18
- subscriptions: /* @__PURE__ */ new Map()
19
- };
7
+ super("signal", value);
20
8
  }
21
9
  /**
22
10
  * Get the value
@@ -30,12 +18,6 @@ class Signal {
30
18
  }
31
19
  return this.state.value;
32
20
  }
33
- /**
34
- * Get the value _(without reactivity)_
35
- */
36
- peek() {
37
- return this.state.value;
38
- }
39
21
  /**
40
22
  * Set the value
41
23
  */
@@ -45,7 +27,7 @@ class Signal {
45
27
  }
46
28
  this.state.value = value;
47
29
  for (const computed of this.state.computeds) {
48
- computed.state.dirty = true;
30
+ computed.effect.dirty = true;
49
31
  }
50
32
  for (const effect of this.state.effects) {
51
33
  batchedHandlers.add(effect);
@@ -57,18 +39,6 @@ class Signal {
57
39
  flushEffects();
58
40
  }
59
41
  }
60
- /**
61
- * Subscribe to changes
62
- */
63
- subscribe(callback) {
64
- return subscribe(this.state, callback);
65
- }
66
- /**
67
- * Unsubscribe from changes
68
- */
69
- unsubscribe(callback) {
70
- unsubscribe(this.state, callback);
71
- }
72
42
  /**
73
43
  * Update the value
74
44
  */
package/package.json CHANGED
@@ -54,5 +54,5 @@
54
54
  },
55
55
  "type": "module",
56
56
  "types": "./types/index.d.ts",
57
- "version": "0.10.0"
57
+ "version": "0.11.1"
58
58
  }
package/src/batch.ts CHANGED
@@ -36,6 +36,6 @@ export function stopBatch(): void {
36
36
  flushEffects();
37
37
  }
38
38
 
39
- export const batchedHandlers = new Set<Effect | Subscription>();
39
+ export const batchedHandlers = new Set<Effect | Subscription<unknown>>();
40
40
 
41
41
  export let batchDepth = 0;