@oscarpalmer/mora 0.12.0 → 0.14.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.
Files changed (59) hide show
  1. package/dist/batch.cjs +2 -2
  2. package/dist/batch.js +1 -1
  3. package/dist/effect.cjs +2 -1
  4. package/dist/effect.js +2 -1
  5. package/dist/helpers/emit.cjs +42 -0
  6. package/dist/helpers/emit.js +42 -0
  7. package/dist/helpers/is.cjs +34 -0
  8. package/dist/helpers/is.js +34 -0
  9. package/dist/helpers/value.cjs +22 -0
  10. package/dist/helpers/value.js +22 -0
  11. package/dist/index.cjs +12 -9
  12. package/dist/index.js +6 -3
  13. package/dist/mora.full.js +214 -55
  14. package/dist/value/array.cjs +108 -0
  15. package/dist/value/array.js +108 -0
  16. package/dist/{computed.cjs → value/computed.cjs} +6 -5
  17. package/dist/{computed.js → value/computed.js} +4 -3
  18. package/dist/{reactive.cjs → value/reactive.cjs} +13 -1
  19. package/dist/{reactive.js → value/reactive.js} +13 -1
  20. package/dist/value/signal.cjs +33 -0
  21. package/dist/value/signal.js +33 -0
  22. package/package.json +1 -1
  23. package/src/batch.ts +1 -1
  24. package/src/effect.ts +12 -11
  25. package/src/helpers/emit.ts +52 -0
  26. package/src/helpers/is.ts +56 -0
  27. package/src/helpers/value.ts +27 -0
  28. package/src/index.ts +11 -5
  29. package/src/subscription.ts +1 -1
  30. package/src/value/array.ts +170 -0
  31. package/src/{computed.ts → value/computed.ts} +4 -3
  32. package/src/{reactive.ts → value/reactive.ts} +17 -3
  33. package/src/value/signal.ts +37 -0
  34. package/types/batch.d.cts +8 -0
  35. package/types/{is.d.cts → helpers/emit.d.cts} +10 -28
  36. package/types/helpers/emit.d.ts +3 -0
  37. package/types/helpers/is.d.cts +133 -0
  38. package/types/helpers/is.d.ts +26 -0
  39. package/types/helpers/value.d.cts +63 -0
  40. package/types/helpers/value.d.ts +3 -0
  41. package/types/index.d.cts +63 -8
  42. package/types/index.d.ts +5 -4
  43. package/types/subscription.d.cts +8 -0
  44. package/types/subscription.d.ts +1 -1
  45. package/types/value/array.d.cts +101 -0
  46. package/types/value/array.d.ts +41 -0
  47. package/types/{computed.d.cts → value/computed.d.cts} +8 -0
  48. package/types/{computed.d.ts → value/computed.d.ts} +1 -1
  49. package/types/{reactive.d.cts → value/reactive.d.cts} +8 -0
  50. package/types/{reactive.d.ts → value/reactive.d.ts} +10 -2
  51. package/types/{signal.d.cts → value/signal.d.cts} +11 -0
  52. package/types/{signal.d.ts → value/signal.d.ts} +3 -0
  53. package/dist/is.cjs +0 -21
  54. package/dist/is.js +0 -21
  55. package/dist/signal.cjs +0 -55
  56. package/dist/signal.js +0 -55
  57. package/src/is.ts +0 -39
  58. package/src/signal.ts +0 -63
  59. package/types/is.d.ts +0 -17
@@ -0,0 +1,101 @@
1
+ // Generated by dts-bundle-generator v9.5.1
2
+
3
+ export type GenericCallback = (...args: any[]) => any;
4
+ declare class Effect {
5
+ private readonly $mora;
6
+ private readonly state;
7
+ constructor(callback: GenericCallback);
8
+ }
9
+ declare class Computed<Value> extends Reactive<Value> {
10
+ private readonly effect;
11
+ constructor(callback: () => Value);
12
+ /**
13
+ * @inheritdoc
14
+ */
15
+ get(): Value;
16
+ }
17
+ declare abstract class Reactive<Value> {
18
+ protected readonly state: ReactiveState<Value>;
19
+ constructor(name: string, value: Value);
20
+ /**
21
+ * Get the value
22
+ */
23
+ abstract get(): Value;
24
+ /**
25
+ * Get the value _(without reactivity)_
26
+ */
27
+ peek(): Value;
28
+ /**
29
+ * Subscribe to changes
30
+ */
31
+ subscribe(callback: (value: Value) => void): Unsubscribe;
32
+ /**
33
+ * JSON representation of the value
34
+ */
35
+ toJSON(): Value;
36
+ /**
37
+ * String representation of the value
38
+ */
39
+ toString(): string;
40
+ /**
41
+ * Unsubscribe from changes
42
+ */
43
+ unsubscribe(callback: (value: Value) => void): void;
44
+ }
45
+ export type ReactiveState<Value> = {
46
+ computeds: Set<Computed<unknown>>;
47
+ effects: Set<Effect>;
48
+ subscriptions: Map<(value: Value) => void, Subscription<Value>>;
49
+ value: Value;
50
+ };
51
+ declare class Subscription<Value> {
52
+ state: ReactiveState<Value>;
53
+ callback: GenericCallback;
54
+ constructor(state: ReactiveState<Value>, callback: GenericCallback);
55
+ }
56
+ /**
57
+ * Unsubscribe from changes
58
+ */
59
+ export type Unsubscribe = () => void;
60
+ export declare class ReactiveArray<Item> extends Reactive<Item[]> {
61
+ #private;
62
+ /**
63
+ * The length of the array
64
+ */
65
+ get length(): number;
66
+ /**
67
+ * Set the length of the array
68
+ */
69
+ set length(value: number);
70
+ constructor(value: Item[]);
71
+ /**
72
+ * @inheritdoc
73
+ */
74
+ get(): Item[];
75
+ /**
76
+ * @inheritdoc
77
+ */
78
+ peek(): Item[];
79
+ /**
80
+ * Get the length of the array _(without reactivity)_
81
+ */
82
+ peek(length: true): number;
83
+ /**
84
+ * Set the value
85
+ */
86
+ set(value: Item[]): void;
87
+ /**
88
+ * Set the value at an index
89
+ */
90
+ set(index: number, value: Item): void;
91
+ /**
92
+ * Set the length of the array
93
+ */
94
+ set(property: "length", value: number): void;
95
+ }
96
+ /**
97
+ * Create a reactive array
98
+ */
99
+ export declare function array<Item>(value: Item[]): ReactiveArray<Item>;
100
+
101
+ export {};
@@ -0,0 +1,41 @@
1
+ import { Reactive } from './reactive';
2
+ export declare class ReactiveArray<Item> extends Reactive<Item[]> {
3
+ #private;
4
+ /**
5
+ * The length of the array
6
+ */
7
+ get length(): number;
8
+ /**
9
+ * Set the length of the array
10
+ */
11
+ set length(value: number);
12
+ constructor(value: Item[]);
13
+ /**
14
+ * @inheritdoc
15
+ */
16
+ get(): Item[];
17
+ /**
18
+ * @inheritdoc
19
+ */
20
+ peek(): Item[];
21
+ /**
22
+ * Get the length of the array _(without reactivity)_
23
+ */
24
+ peek(length: true): number;
25
+ /**
26
+ * Set the value
27
+ */
28
+ set(value: Item[]): void;
29
+ /**
30
+ * Set the value at an index
31
+ */
32
+ set(index: number, value: Item): void;
33
+ /**
34
+ * Set the length of the array
35
+ */
36
+ set(property: 'length', value: number): void;
37
+ }
38
+ /**
39
+ * Create a reactive array
40
+ */
41
+ export declare function array<Item>(value: Item[]): ReactiveArray<Item>;
@@ -42,6 +42,14 @@ declare abstract class Reactive<Value> {
42
42
  * Subscribe to changes
43
43
  */
44
44
  subscribe(callback: (value: Value) => void): Unsubscribe;
45
+ /**
46
+ * JSON representation of the value
47
+ */
48
+ toJSON(): Value;
49
+ /**
50
+ * String representation of the value
51
+ */
52
+ toString(): string;
45
53
  /**
46
54
  * Unsubscribe from changes
47
55
  */
@@ -1,4 +1,4 @@
1
- import { type Effect } from './effect';
1
+ import { type Effect } from '../effect';
2
2
  import { Reactive, type ReactiveState } from './reactive';
3
3
  type ComputedEffect = {
4
4
  dirty: boolean;
@@ -29,6 +29,14 @@ export declare abstract class Reactive<Value> {
29
29
  * Subscribe to changes
30
30
  */
31
31
  subscribe(callback: (value: Value) => void): Unsubscribe;
32
+ /**
33
+ * JSON representation of the value
34
+ */
35
+ toJSON(): Value;
36
+ /**
37
+ * String representation of the value
38
+ */
39
+ toString(): string;
32
40
  /**
33
41
  * Unsubscribe from changes
34
42
  */
@@ -1,6 +1,6 @@
1
+ import type { Effect } from '../effect';
2
+ import { type Subscription, type Unsubscribe } from '../subscription';
1
3
  import type { Computed } from './computed';
2
- import type { Effect } from './effect';
3
- import { type Subscription, type Unsubscribe } from './subscription';
4
4
  export declare abstract class Reactive<Value> {
5
5
  protected readonly state: ReactiveState<Value>;
6
6
  constructor(name: string, value: Value);
@@ -16,6 +16,14 @@ export declare abstract class Reactive<Value> {
16
16
  * Subscribe to changes
17
17
  */
18
18
  subscribe(callback: (value: Value) => void): Unsubscribe;
19
+ /**
20
+ * JSON representation of the value
21
+ */
22
+ toJSON(): Value;
23
+ /**
24
+ * String representation of the value
25
+ */
26
+ toString(): string;
19
27
  /**
20
28
  * Unsubscribe from changes
21
29
  */
@@ -29,6 +29,14 @@ declare abstract class Reactive<Value> {
29
29
  * Subscribe to changes
30
30
  */
31
31
  subscribe(callback: (value: Value) => void): Unsubscribe;
32
+ /**
33
+ * JSON representation of the value
34
+ */
35
+ toJSON(): Value;
36
+ /**
37
+ * String representation of the value
38
+ */
39
+ toString(): string;
32
40
  /**
33
41
  * Unsubscribe from changes
34
42
  */
@@ -64,6 +72,9 @@ export declare class Signal<Value> extends Reactive<Value> {
64
72
  */
65
73
  update(callback: (value: Value) => Value): void;
66
74
  }
75
+ /**
76
+ * Create a reactive value
77
+ */
67
78
  export declare function signal<Value>(value: Value): Signal<Value>;
68
79
 
69
80
  export {};
@@ -14,4 +14,7 @@ export declare class Signal<Value> extends Reactive<Value> {
14
14
  */
15
15
  update(callback: (value: Value) => Value): void;
16
16
  }
17
+ /**
18
+ * Create a reactive value
19
+ */
17
20
  export declare function signal<Value>(value: Value): Signal<Value>;
package/dist/is.cjs DELETED
@@ -1,21 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- function isComputed(value) {
4
- return isMora(value, "computed");
5
- }
6
- function isEffect(value) {
7
- return isMora(value, "effect");
8
- }
9
- function isMora(value, name) {
10
- return typeof value === "object" && value != null && "$mora" in value && value.$mora === name;
11
- }
12
- function isReactive(value) {
13
- return isComputed(value) || isSignal(value);
14
- }
15
- function isSignal(value) {
16
- return isMora(value, "signal");
17
- }
18
- exports.isComputed = isComputed;
19
- exports.isEffect = isEffect;
20
- exports.isReactive = isReactive;
21
- exports.isSignal = isSignal;
package/dist/is.js DELETED
@@ -1,21 +0,0 @@
1
- function isComputed(value) {
2
- return isMora(value, "computed");
3
- }
4
- function isEffect(value) {
5
- return isMora(value, "effect");
6
- }
7
- function isMora(value, name) {
8
- return typeof value === "object" && value != null && "$mora" in value && value.$mora === name;
9
- }
10
- function isReactive(value) {
11
- return isComputed(value) || isSignal(value);
12
- }
13
- function isSignal(value) {
14
- return isMora(value, "signal");
15
- }
16
- export {
17
- isComputed,
18
- isEffect,
19
- isReactive,
20
- isSignal
21
- };
package/dist/signal.cjs DELETED
@@ -1,55 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const batch = require("./batch.cjs");
4
- const computed = require("./computed.cjs");
5
- const effect = require("./effect.cjs");
6
- const reactive = require("./reactive.cjs");
7
- class Signal extends reactive.Reactive {
8
- constructor(value) {
9
- super("signal", value);
10
- }
11
- /**
12
- * @inheritdoc
13
- */
14
- get() {
15
- if (computed.activeComputed != null) {
16
- this.state.computeds.add(computed.activeComputed);
17
- }
18
- if (effect.activeEffect != null) {
19
- this.state.effects.add(effect.activeEffect);
20
- }
21
- return this.state.value;
22
- }
23
- /**
24
- * Set the value
25
- */
26
- set(value) {
27
- if (Object.is(this.state.value, value)) {
28
- return;
29
- }
30
- this.state.value = value;
31
- for (const computed2 of this.state.computeds) {
32
- computed2.effect.dirty = true;
33
- }
34
- for (const effect2 of this.state.effects) {
35
- batch.batchedHandlers.add(effect2);
36
- }
37
- for (const [, subscription] of this.state.subscriptions) {
38
- batch.batchedHandlers.add(subscription);
39
- }
40
- if (batch.batchDepth === 0) {
41
- batch.flushEffects();
42
- }
43
- }
44
- /**
45
- * Update the value
46
- */
47
- update(callback) {
48
- this.set(callback(this.state.value));
49
- }
50
- }
51
- function signal(value) {
52
- return new Signal(value);
53
- }
54
- exports.Signal = Signal;
55
- exports.signal = signal;
package/dist/signal.js DELETED
@@ -1,55 +0,0 @@
1
- import { batchedHandlers, flushEffects, batchDepth } from "./batch.js";
2
- import { activeComputed } from "./computed.js";
3
- import { activeEffect } from "./effect.js";
4
- import { Reactive } from "./reactive.js";
5
- class Signal extends Reactive {
6
- constructor(value) {
7
- super("signal", value);
8
- }
9
- /**
10
- * @inheritdoc
11
- */
12
- get() {
13
- if (activeComputed != null) {
14
- this.state.computeds.add(activeComputed);
15
- }
16
- if (activeEffect != null) {
17
- this.state.effects.add(activeEffect);
18
- }
19
- return this.state.value;
20
- }
21
- /**
22
- * Set the value
23
- */
24
- set(value) {
25
- if (Object.is(this.state.value, value)) {
26
- return;
27
- }
28
- this.state.value = value;
29
- for (const computed of this.state.computeds) {
30
- computed.effect.dirty = true;
31
- }
32
- for (const effect of this.state.effects) {
33
- batchedHandlers.add(effect);
34
- }
35
- for (const [, subscription] of this.state.subscriptions) {
36
- batchedHandlers.add(subscription);
37
- }
38
- if (batchDepth === 0) {
39
- flushEffects();
40
- }
41
- }
42
- /**
43
- * Update the value
44
- */
45
- update(callback) {
46
- this.set(callback(this.state.value));
47
- }
48
- }
49
- function signal(value) {
50
- return new Signal(value);
51
- }
52
- export {
53
- Signal,
54
- signal
55
- };
package/src/is.ts DELETED
@@ -1,39 +0,0 @@
1
- import type {PlainObject} from '@oscarpalmer/atoms/models';
2
- import type {Computed} from './computed';
3
- import type {Effect} from './effect';
4
- import type {Reactive} from './reactive';
5
- import type {Signal} from './signal';
6
-
7
- /**
8
- * Is the value a computed signal?
9
- */
10
- export function isComputed(value: unknown): value is Computed<unknown> {
11
- return isMora<Computed<unknown>>(value, 'computed');
12
- }
13
-
14
- /**
15
- * Is the value an effect?
16
- */
17
- export function isEffect(value: unknown): value is Effect {
18
- return isMora<Effect>(value, 'effect');
19
- }
20
-
21
- function isMora<T>(value: unknown, name: string): value is T {
22
- return (
23
- typeof value === 'object' &&
24
- value != null &&
25
- '$mora' in value &&
26
- (value as PlainObject).$mora === name
27
- );
28
- }
29
-
30
- export function isReactive(value: unknown): value is Reactive<unknown> {
31
- return isComputed(value) || isSignal(value);
32
- }
33
-
34
- /**
35
- * Is the value a signal?
36
- */
37
- export function isSignal(value: unknown): value is Signal<unknown> {
38
- return isMora<Signal<unknown>>(value, 'signal');
39
- }
package/src/signal.ts DELETED
@@ -1,63 +0,0 @@
1
- import {batchDepth, batchedHandlers, flushEffects} from './batch';
2
- import {type InternalComputed, activeComputed} from './computed';
3
- import {activeEffect} from './effect';
4
- import {Reactive} from './reactive';
5
-
6
- export class Signal<Value> extends Reactive<Value> {
7
- constructor(value: Value) {
8
- super('signal', value);
9
- }
10
-
11
- /**
12
- * @inheritdoc
13
- */
14
- get(): Value {
15
- if (activeComputed != null) {
16
- this.state.computeds.add(activeComputed);
17
- }
18
-
19
- if (activeEffect != null) {
20
- this.state.effects.add(activeEffect);
21
- }
22
-
23
- return this.state.value;
24
- }
25
-
26
- /**
27
- * Set the value
28
- */
29
- set(value: Value): void {
30
- if (Object.is(this.state.value, value)) {
31
- return;
32
- }
33
-
34
- this.state.value = value;
35
-
36
- for (const computed of this.state.computeds) {
37
- (computed as unknown as InternalComputed).effect.dirty = true;
38
- }
39
-
40
- for (const effect of this.state.effects) {
41
- batchedHandlers.add(effect);
42
- }
43
-
44
- for (const [, subscription] of this.state.subscriptions) {
45
- batchedHandlers.add(subscription as never);
46
- }
47
-
48
- if (batchDepth === 0) {
49
- flushEffects();
50
- }
51
- }
52
-
53
- /**
54
- * Update the value
55
- */
56
- update(callback: (value: Value) => Value): void {
57
- this.set(callback(this.state.value));
58
- }
59
- }
60
-
61
- export function signal<Value>(value: Value): Signal<Value> {
62
- return new Signal(value);
63
- }
package/types/is.d.ts DELETED
@@ -1,17 +0,0 @@
1
- import type { Computed } from './computed';
2
- import type { Effect } from './effect';
3
- import type { Reactive } from './reactive';
4
- import type { Signal } from './signal';
5
- /**
6
- * Is the value a computed signal?
7
- */
8
- export declare function isComputed(value: unknown): value is Computed<unknown>;
9
- /**
10
- * Is the value an effect?
11
- */
12
- export declare function isEffect(value: unknown): value is Effect;
13
- export declare function isReactive(value: unknown): value is Reactive<unknown>;
14
- /**
15
- * Is the value a signal?
16
- */
17
- export declare function isSignal(value: unknown): value is Signal<unknown>;