@oscarpalmer/mora 0.10.0 → 0.11.0

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/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
@@ -112,23 +112,49 @@ function unsubscribe(state, callback) {
112
112
  }
113
113
  }
114
114
 
115
- let activeComputed;
116
- class Computed {
117
- state;
118
- constructor(callback) {
115
+ class Reactive {
116
+ state = {
117
+ computeds: new Set(),
118
+ effects: new Set(),
119
+ subscriptions: new Map(),
120
+ value: undefined,
121
+ };
122
+ constructor(name, value) {
123
+ this.state.value = value;
119
124
  Object.defineProperty(this, '$mora', {
120
- value: 'computed',
125
+ value: name,
121
126
  });
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) {
127
+ }
128
+ /**
129
+ * Get the value _(without reactivity)_
130
+ */
131
+ peek() {
132
+ return this.state.value;
133
+ }
134
+ /**
135
+ * Subscribe to changes
136
+ */
137
+ subscribe(callback) {
138
+ return subscribe(this.state, callback);
139
+ }
140
+ /**
141
+ * Unsubscribe from changes
142
+ */
143
+ unsubscribe(callback) {
144
+ unsubscribe(this.state, callback);
145
+ }
146
+ }
147
+
148
+ let activeComputed;
149
+ class Computed extends Reactive {
150
+ effect = {
151
+ dirty: true,
152
+ instance: undefined,
153
+ };
154
+ constructor(callback) {
155
+ super('computed', undefined);
156
+ this.effect.instance = effect(() => {
157
+ if (this.effect.dirty) {
132
158
  const previousComputed = activeComputed;
133
159
  activeComputed = this;
134
160
  const value = callback();
@@ -136,7 +162,7 @@ class Computed {
136
162
  if (!Object.is(this.state.value, value)) {
137
163
  this.state.value = value;
138
164
  for (const computed of this.state.computeds) {
139
- computed.state.dirty = true;
165
+ computed.effect.dirty = true;
140
166
  }
141
167
  for (const effect of this.state.effects) {
142
168
  batchedHandlers.add(effect);
@@ -145,7 +171,7 @@ class Computed {
145
171
  subscription.callback(value);
146
172
  }
147
173
  }
148
- this.state.dirty = false;
174
+ this.effect.dirty = false;
149
175
  }
150
176
  });
151
177
  }
@@ -156,32 +182,14 @@ class Computed {
156
182
  if (activeComputed != null && activeComputed !== this) {
157
183
  this.state.computeds.add(activeComputed);
158
184
  }
159
- if (activeEffect != null && activeEffect !== this.state.effect) {
185
+ if (activeEffect != null && activeEffect !== this.effect.instance) {
160
186
  this.state.effects.add(activeEffect);
161
187
  }
162
- if (this.state.dirty && batchDepth === 0) {
163
- runEffect(this.state.effect);
188
+ if (this.effect.dirty && batchDepth === 0) {
189
+ runEffect(this.effect.instance);
164
190
  }
165
191
  return this.state.value;
166
192
  }
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
193
  }
186
194
  /**
187
195
  * Create a computed value
@@ -190,18 +198,9 @@ function computed(callback) {
190
198
  return new Computed(callback);
191
199
  }
192
200
 
193
- class Signal {
194
- state;
201
+ class Signal extends Reactive {
195
202
  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
- };
203
+ super('signal', value);
205
204
  }
206
205
  /**
207
206
  * Get the value
@@ -215,12 +214,6 @@ class Signal {
215
214
  }
216
215
  return this.state.value;
217
216
  }
218
- /**
219
- * Get the value _(without reactivity)_
220
- */
221
- peek() {
222
- return this.state.value;
223
- }
224
217
  /**
225
218
  * Set the value
226
219
  */
@@ -230,7 +223,7 @@ class Signal {
230
223
  }
231
224
  this.state.value = value;
232
225
  for (const computed of this.state.computeds) {
233
- computed.state.dirty = true;
226
+ computed.effect.dirty = true;
234
227
  }
235
228
  for (const effect of this.state.effects) {
236
229
  batchedHandlers.add(effect);
@@ -242,18 +235,6 @@ class Signal {
242
235
  flushEffects();
243
236
  }
244
237
  }
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
238
  /**
258
239
  * Update the value
259
240
  */
@@ -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.0"
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;