@oscarpalmer/mora 0.22.0 → 0.23.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/batch.js +8 -9
- package/dist/constants.js +34 -0
- package/dist/effect.js +7 -8
- package/dist/helpers/is.js +8 -18
- package/dist/helpers/proxy.js +3 -2
- package/dist/helpers/value.js +7 -8
- package/dist/index.js +1 -1
- package/dist/models.js +0 -0
- package/dist/mora.full.js +218 -130
- package/dist/subscription.js +7 -5
- package/dist/value/array.js +24 -24
- package/dist/value/computed.js +18 -21
- package/dist/value/signal.js +2 -2
- package/dist/value/store.js +8 -2
- package/package.json +9 -10
- package/src/batch.ts +11 -13
- package/src/constants.ts +49 -0
- package/src/effect.ts +11 -16
- package/src/helpers/is.ts +41 -19
- package/src/helpers/proxy.ts +34 -38
- package/src/helpers/value.ts +19 -14
- package/src/index.ts +1 -0
- package/src/models.ts +66 -0
- package/src/subscription.ts +14 -16
- package/src/value/array.ts +86 -44
- package/src/value/computed.ts +29 -39
- package/src/value/reactive.ts +9 -24
- package/src/value/signal.ts +9 -3
- package/src/value/store.ts +40 -9
- package/types/batch.d.ts +3 -5
- package/types/constants.d.ts +14 -0
- package/types/effect.d.ts +2 -1
- package/types/helpers/is.d.ts +23 -10
- package/types/helpers/proxy.d.ts +2 -3
- package/types/helpers/value.d.ts +1 -1
- package/types/index.d.ts +1 -0
- package/types/models.d.ts +56 -0
- package/types/subscription.d.ts +3 -6
- package/types/value/array.d.ts +48 -7
- package/types/value/computed.d.ts +2 -11
- package/types/value/reactive.d.ts +8 -17
- package/types/value/signal.d.ts +7 -1
- package/types/value/store.d.ts +28 -4
package/src/value/store.ts
CHANGED
|
@@ -4,26 +4,33 @@ import type {
|
|
|
4
4
|
Key,
|
|
5
5
|
PlainObject,
|
|
6
6
|
} from '@oscarpalmer/atoms/models';
|
|
7
|
-
import {
|
|
7
|
+
import {NAME_STORE} from '../constants';
|
|
8
8
|
import {
|
|
9
9
|
getReactiveValueInProxy,
|
|
10
10
|
setProxyValue,
|
|
11
11
|
setValueInProxy,
|
|
12
12
|
} from '../helpers/proxy';
|
|
13
13
|
import {getValue} from '../helpers/value';
|
|
14
|
-
import
|
|
14
|
+
import type {ReactiveOptions, Unsubscribe} from '../models';
|
|
15
|
+
import {noop, subscribe} from '../subscription';
|
|
15
16
|
import type {Computed} from './computed';
|
|
16
|
-
import {Reactive
|
|
17
|
+
import {Reactive} from './reactive';
|
|
17
18
|
|
|
18
19
|
export class Store<Value extends PlainObject> extends Reactive<Value, Value> {
|
|
19
|
-
#keyed = new Map<Key, Computed<unknown>>();
|
|
20
|
+
readonly #keyed = new Map<Key, Computed<unknown>>();
|
|
20
21
|
|
|
21
22
|
constructor(value: Value, options?: ReactiveOptions<Value>) {
|
|
22
23
|
super(
|
|
23
|
-
|
|
24
|
+
NAME_STORE,
|
|
24
25
|
new Proxy(value, {
|
|
25
|
-
set: (target, property, value) =>
|
|
26
|
-
setValueInProxy(
|
|
26
|
+
set: (target: Value, property: PropertyKey, value: unknown) =>
|
|
27
|
+
setValueInProxy({
|
|
28
|
+
target,
|
|
29
|
+
property,
|
|
30
|
+
value,
|
|
31
|
+
isArray: false,
|
|
32
|
+
state: this.state,
|
|
33
|
+
}),
|
|
27
34
|
}),
|
|
28
35
|
options,
|
|
29
36
|
);
|
|
@@ -35,12 +42,16 @@ export class Store<Value extends PlainObject> extends Reactive<Value, Value> {
|
|
|
35
42
|
get(): Value;
|
|
36
43
|
|
|
37
44
|
/**
|
|
38
|
-
* Get a value by key
|
|
45
|
+
* Get a value by key
|
|
46
|
+
* @param key Key of the value to get
|
|
47
|
+
* @returns Value for the specified key, or `undefined` if it doesn't exist
|
|
39
48
|
*/
|
|
40
49
|
get(key: keyof Value): Value[keyof Value];
|
|
41
50
|
|
|
42
51
|
/**
|
|
43
|
-
* Get a value by key
|
|
52
|
+
* Get a value by key
|
|
53
|
+
* @param key Key of the value to get
|
|
54
|
+
* @returns Value for the specified key, or `undefined` if it doesn't exist
|
|
44
55
|
*/
|
|
45
56
|
get(key: Key): unknown;
|
|
46
57
|
|
|
@@ -52,16 +63,21 @@ export class Store<Value extends PlainObject> extends Reactive<Value, Value> {
|
|
|
52
63
|
|
|
53
64
|
/**
|
|
54
65
|
* Get the value _(without reactivity)_
|
|
66
|
+
* @returns The current value
|
|
55
67
|
*/
|
|
56
68
|
peek(): Value;
|
|
57
69
|
|
|
58
70
|
/**
|
|
59
71
|
* Get a value by key _(without reactivity)_
|
|
72
|
+
* @param key Key of the value to get
|
|
73
|
+
* @returns Value for the specified key, or `undefined` if it doesn't exist
|
|
60
74
|
*/
|
|
61
75
|
peek(key: keyof Value): Value[keyof Value];
|
|
62
76
|
|
|
63
77
|
/**
|
|
64
78
|
* Get a value by key _(without reactivity)_
|
|
79
|
+
* @param key Key of the value to get
|
|
80
|
+
* @returns Value for the specified key, or `undefined` if it doesn't exist
|
|
65
81
|
*/
|
|
66
82
|
peek(key: Key): unknown;
|
|
67
83
|
|
|
@@ -71,16 +87,21 @@ export class Store<Value extends PlainObject> extends Reactive<Value, Value> {
|
|
|
71
87
|
|
|
72
88
|
/**
|
|
73
89
|
* Set the value
|
|
90
|
+
* @param value New value _(defaults to an empty object)_
|
|
74
91
|
*/
|
|
75
92
|
set(value?: Value): void;
|
|
76
93
|
|
|
77
94
|
/**
|
|
78
95
|
* Set a value by key
|
|
96
|
+
* @param key Key of the value to set
|
|
97
|
+
* @param value New value
|
|
79
98
|
*/
|
|
80
99
|
set(key: keyof Value, value: Value[keyof Value]): void;
|
|
81
100
|
|
|
82
101
|
/**
|
|
83
102
|
* Set a value by key
|
|
103
|
+
* @param key Key of the value to set
|
|
104
|
+
* @param value New value
|
|
84
105
|
*/
|
|
85
106
|
set(key: Key, value: unknown): void;
|
|
86
107
|
|
|
@@ -99,6 +120,9 @@ export class Store<Value extends PlainObject> extends Reactive<Value, Value> {
|
|
|
99
120
|
|
|
100
121
|
/**
|
|
101
122
|
* Subscribe to changes for a specific key
|
|
123
|
+
* @param key Key of the value to subscribe to
|
|
124
|
+
* @param callback Callback for changes
|
|
125
|
+
* @returns Unsubscribe callback
|
|
102
126
|
*/
|
|
103
127
|
subscribe<Key extends keyof Value>(
|
|
104
128
|
key: Key,
|
|
@@ -107,6 +131,9 @@ export class Store<Value extends PlainObject> extends Reactive<Value, Value> {
|
|
|
107
131
|
|
|
108
132
|
/**
|
|
109
133
|
* Subscribe to changes for a specific key
|
|
134
|
+
* @param key Key of the value to subscribe to
|
|
135
|
+
* @param callback Callback for changes
|
|
136
|
+
* @returns Unsubscribe callback
|
|
110
137
|
*/
|
|
111
138
|
subscribe(key: Key, callback: (value: unknown) => void): Unsubscribe;
|
|
112
139
|
|
|
@@ -125,6 +152,7 @@ export class Store<Value extends PlainObject> extends Reactive<Value, Value> {
|
|
|
125
152
|
|
|
126
153
|
/**
|
|
127
154
|
* Update the value _(based on the current value)_
|
|
155
|
+
* @param callback Callback to update the value
|
|
128
156
|
*/
|
|
129
157
|
update(callback: (value: Value) => Value): void {
|
|
130
158
|
const updated = callback({...this.state.value});
|
|
@@ -137,6 +165,9 @@ export class Store<Value extends PlainObject> extends Reactive<Value, Value> {
|
|
|
137
165
|
|
|
138
166
|
/**
|
|
139
167
|
* Create a reactive store
|
|
168
|
+
* @param value Initial object value
|
|
169
|
+
* @param options Optional reactivity options
|
|
170
|
+
* @returns Reactive store
|
|
140
171
|
*/
|
|
141
172
|
export function store<Value extends PlainObject>(
|
|
142
173
|
value: Value,
|
package/types/batch.d.ts
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
import { type Effect } from './effect';
|
|
2
|
-
import type { Subscription } from './subscription';
|
|
3
1
|
export declare function flushHandlers(): void;
|
|
4
2
|
/**
|
|
5
|
-
* Start batching effects
|
|
3
|
+
* Start batching effects
|
|
4
|
+
*
|
|
5
|
+
* _(Use {@link stopBatch} to flush and run batched effects)_
|
|
6
6
|
*/
|
|
7
7
|
export declare function startBatch(): void;
|
|
8
8
|
/**
|
|
9
9
|
* Stop batching effects and flush _(run)_ them
|
|
10
10
|
*/
|
|
11
11
|
export declare function stopBatch(): void;
|
|
12
|
-
export declare const batchedHandlers: Set<Subscription | Effect>;
|
|
13
|
-
export declare let batchDepth: number;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Active, Batch } from './models';
|
|
2
|
+
export declare const ACTIVE: Active;
|
|
3
|
+
export declare const ARRAY_THRESHOLD = 100;
|
|
4
|
+
export declare const ARRAY_OFFSET = 25;
|
|
5
|
+
export declare const ARRAY_PEEK = 10;
|
|
6
|
+
export declare const BATCH: Batch;
|
|
7
|
+
export declare const METHODS_AFFECTING_LENGTH: Set<string>;
|
|
8
|
+
export declare const METHODS_UPDATE: Set<string>;
|
|
9
|
+
export declare const NAME_ARRAY = "array";
|
|
10
|
+
export declare const NAME_COMPUTED = "computed";
|
|
11
|
+
export declare const NAME_EFFECT = "effect";
|
|
12
|
+
export declare const NAME_SIGNAL = "signal";
|
|
13
|
+
export declare const NAME_STORE = "store";
|
|
14
|
+
export declare const NAMES: Set<string>;
|
package/types/effect.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ export declare class Effect {
|
|
|
7
7
|
export declare function runEffect(effect: Effect): void;
|
|
8
8
|
/**
|
|
9
9
|
* Create an effect
|
|
10
|
+
* @param callback Callback for handling signal effects
|
|
11
|
+
* @returns Effect
|
|
10
12
|
*/
|
|
11
13
|
export declare function effect(callback: GenericCallback): Effect;
|
|
12
|
-
export declare let activeEffect: Effect | undefined;
|
package/types/helpers/is.d.ts
CHANGED
|
@@ -7,24 +7,37 @@ import type { Signal } from '../value/signal';
|
|
|
7
7
|
import type { Store } from '../value/store';
|
|
8
8
|
/**
|
|
9
9
|
* Is the value a reactive array?
|
|
10
|
+
* @param value Value to check
|
|
11
|
+
* @returns True if value is a {@link ReactiveArray}
|
|
10
12
|
*/
|
|
11
|
-
export declare function isArray(value: unknown): value is ReactiveArray<
|
|
13
|
+
export declare function isArray<Item>(value: unknown): value is ReactiveArray<Item>;
|
|
12
14
|
/**
|
|
13
15
|
* Is the value a computed signal?
|
|
16
|
+
* @param value Value to check
|
|
17
|
+
* @returns True if value is a {@link Computed}
|
|
14
18
|
*/
|
|
15
|
-
export declare function isComputed(value: unknown): value is Computed<
|
|
19
|
+
export declare function isComputed<Value>(value: unknown): value is Computed<Value>;
|
|
16
20
|
/**
|
|
17
21
|
* Is the value an effect?
|
|
22
|
+
* @param value Value to check
|
|
23
|
+
* @returns True if value is an {@link Effect}
|
|
18
24
|
*/
|
|
19
25
|
export declare function isEffect(value: unknown): value is Effect;
|
|
20
|
-
|
|
26
|
+
/**
|
|
27
|
+
* Is the value reactive?
|
|
28
|
+
* @param value Value to check
|
|
29
|
+
* @returns True if value is a {@link Reactive}
|
|
30
|
+
*/
|
|
31
|
+
export declare function isReactive<Value, Equal = Value>(value: unknown): value is Reactive<Value, Equal>;
|
|
21
32
|
/**
|
|
22
33
|
* Is the value a signal?
|
|
34
|
+
* @param value Value to check
|
|
35
|
+
* @returns True if value is a {@link Signal}
|
|
36
|
+
*/
|
|
37
|
+
export declare function isSignal<Value>(value: unknown): value is Signal<Value>;
|
|
38
|
+
/**
|
|
39
|
+
* Is the value a reactive store?
|
|
40
|
+
* @param value Value to check
|
|
41
|
+
* @returns True if value is a {@link Store}
|
|
23
42
|
*/
|
|
24
|
-
export declare function
|
|
25
|
-
export declare function isStore(value: unknown): value is Store<PlainObject>;
|
|
26
|
-
export declare const arrayName = "array";
|
|
27
|
-
export declare const computedName = "computed";
|
|
28
|
-
export declare const effectName = "effect";
|
|
29
|
-
export declare const signalName = "signal";
|
|
30
|
-
export declare const storeName = "store";
|
|
43
|
+
export declare function isStore<Value extends PlainObject>(value: unknown): value is Store<Value>;
|
package/types/helpers/proxy.d.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import type { ArrayOrPlainObject, Key, PlainObject } from '@oscarpalmer/atoms/models';
|
|
2
|
+
import type { SetValueInProxyParameters } from '../models';
|
|
2
3
|
import type { ReactiveArray } from '../value/array';
|
|
3
4
|
import { type Computed } from '../value/computed';
|
|
4
|
-
import type { ReactiveState } from '../value/reactive';
|
|
5
|
-
import type { Signal } from '../value/signal';
|
|
6
5
|
import type { Store } from '../value/store';
|
|
7
6
|
export declare function getReactiveValueInProxy<Value>(array: ReactiveArray<Value>, mapped: Map<Key, Computed<unknown>>, index: number, isArray: true): Computed<unknown>;
|
|
8
7
|
export declare function getReactiveValueInProxy<Value extends PlainObject>(store: Store<Value>, mapped: Map<Key, Computed<unknown>>, key: Key, isArray: false): Computed<unknown>;
|
|
9
8
|
export declare function setProxyValue(proxy: ArrayOrPlainObject, value: ArrayOrPlainObject): void;
|
|
10
|
-
export declare function setValueInProxy<Value extends ArrayOrPlainObject, Equal>(
|
|
9
|
+
export declare function setValueInProxy<Value extends ArrayOrPlainObject, Equal>(parameters: SetValueInProxyParameters<Value, Equal>): boolean;
|
package/types/helpers/value.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ReactiveState } from '../
|
|
1
|
+
import type { ReactiveState } from '../models';
|
|
2
2
|
export declare function emitValue<Value>(state: ReactiveState<Value, never>): void;
|
|
3
3
|
export declare function equalArrays<Value>(state: ReactiveState<Value[], Value>, first: Value[], second: Value[]): boolean;
|
|
4
4
|
export declare function getValue<Value>(state: ReactiveState<Value, never>): Value;
|
package/types/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export { startBatch, stopBatch } from './batch';
|
|
2
2
|
export { effect, type Effect } from './effect';
|
|
3
3
|
export { isArray, isComputed, isEffect, isReactive, isSignal, } from './helpers/is';
|
|
4
|
+
export type { Unsubscribe } from './models';
|
|
4
5
|
export { array, type ReactiveArray } from './value/array';
|
|
5
6
|
export { computed, type Computed } from './value/computed';
|
|
6
7
|
export type { Reactive } from './value/reactive';
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { GenericCallback } from '@oscarpalmer/atoms/models';
|
|
2
|
+
import type { Effect } from './effect';
|
|
3
|
+
import type { Subscription } from './subscription';
|
|
4
|
+
import type { Computed } from './value/computed';
|
|
5
|
+
import type { Signal } from './value/signal';
|
|
6
|
+
export type Active = {
|
|
7
|
+
computed?: Computed<unknown>;
|
|
8
|
+
effect?: Effect;
|
|
9
|
+
};
|
|
10
|
+
export type Batch = {
|
|
11
|
+
depth: number;
|
|
12
|
+
handlers: Set<Effect | Subscription>;
|
|
13
|
+
};
|
|
14
|
+
export type ComputedEffect = {
|
|
15
|
+
dirty: boolean;
|
|
16
|
+
instance: Effect;
|
|
17
|
+
};
|
|
18
|
+
export type EffectState = {
|
|
19
|
+
callback: GenericCallback;
|
|
20
|
+
};
|
|
21
|
+
export type InternalComputed = {
|
|
22
|
+
readonly effect: ComputedEffect;
|
|
23
|
+
readonly state: ReactiveState<unknown, unknown>;
|
|
24
|
+
};
|
|
25
|
+
export type InternalEffect = {
|
|
26
|
+
state: EffectState;
|
|
27
|
+
};
|
|
28
|
+
export type ReactiveOptions<Value> = {
|
|
29
|
+
/**
|
|
30
|
+
* Method for comparing values for equality
|
|
31
|
+
* @param first First value
|
|
32
|
+
* @param second Second value
|
|
33
|
+
* @returns Are the values equal?
|
|
34
|
+
* @default Object.is
|
|
35
|
+
*/
|
|
36
|
+
equal?: (first: Value, second: Value) => boolean;
|
|
37
|
+
};
|
|
38
|
+
export type ReactiveState<Value, Equal> = {
|
|
39
|
+
computeds: Set<Computed<unknown>>;
|
|
40
|
+
effects: Set<Effect>;
|
|
41
|
+
equal: (first: Equal, second: Equal) => boolean;
|
|
42
|
+
subscriptions: Map<GenericCallback, Subscription>;
|
|
43
|
+
value: Value;
|
|
44
|
+
};
|
|
45
|
+
export type SetValueInProxyParameters<Value, Equal> = {
|
|
46
|
+
target: Value;
|
|
47
|
+
property: PropertyKey;
|
|
48
|
+
value: unknown;
|
|
49
|
+
state: ReactiveState<Value, Equal>;
|
|
50
|
+
isArray: boolean;
|
|
51
|
+
length?: Signal<number>;
|
|
52
|
+
};
|
|
53
|
+
/**
|
|
54
|
+
* Unsubscribe from changes
|
|
55
|
+
*/
|
|
56
|
+
export type Unsubscribe = () => void;
|
package/types/subscription.d.ts
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
1
|
import type { GenericCallback } from '@oscarpalmer/atoms/models';
|
|
2
|
-
import type { ReactiveState } from './
|
|
2
|
+
import type { ReactiveState, Unsubscribe } from './models';
|
|
3
3
|
export declare class Subscription {
|
|
4
|
-
state: ReactiveState<unknown, never>;
|
|
5
4
|
callback: GenericCallback;
|
|
5
|
+
state: ReactiveState<unknown, never>;
|
|
6
6
|
constructor(state: ReactiveState<unknown, never>, callback: GenericCallback);
|
|
7
|
+
destroy(): void;
|
|
7
8
|
}
|
|
8
|
-
/**
|
|
9
|
-
* Unsubscribe from changes
|
|
10
|
-
*/
|
|
11
|
-
export type Unsubscribe = () => void;
|
|
12
9
|
export declare function noop(): void;
|
|
13
10
|
export declare function subscribe<Value>(state: ReactiveState<Value, never>, callback: (value: Value) => void): Unsubscribe;
|
|
14
11
|
export declare function unsubscribe<Value>(state: ReactiveState<Value, never>, callback: (value: Value) => void): void;
|
package/types/value/array.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { ReactiveOptions, Unsubscribe } from '../models';
|
|
2
2
|
import { type Computed } from './computed';
|
|
3
|
-
import { Reactive
|
|
3
|
+
import { Reactive } from './reactive';
|
|
4
4
|
export declare class ReactiveArray<Item> extends Reactive<Item[], Item> {
|
|
5
5
|
#private;
|
|
6
6
|
/**
|
|
@@ -16,61 +16,93 @@ export declare class ReactiveArray<Item> extends Reactive<Item[], Item> {
|
|
|
16
16
|
* Clear the array
|
|
17
17
|
*/
|
|
18
18
|
clear(): void;
|
|
19
|
+
/**
|
|
20
|
+
* Create a computed, filtered array
|
|
21
|
+
* @param callback Callback to evaluate each item
|
|
22
|
+
* @return Computed array of filtered items
|
|
23
|
+
*/
|
|
24
|
+
filter(callback: (item: Item, index: number, array: Item[]) => boolean): Computed<Item[]>;
|
|
19
25
|
/**
|
|
20
26
|
* @inheritdoc
|
|
21
27
|
*/
|
|
22
28
|
get(): Item[];
|
|
23
29
|
/**
|
|
24
30
|
* Get the value at an index
|
|
31
|
+
* @param index Index of item to get _(if negative, starts from the end)_
|
|
32
|
+
* @returns Item at index, or `undefined` if it doesn't exist
|
|
25
33
|
*/
|
|
26
34
|
get(index: number): Item | undefined;
|
|
27
35
|
/**
|
|
28
36
|
* Get the length of the array
|
|
37
|
+
* @returns Length of the array
|
|
29
38
|
*/
|
|
30
39
|
get(property: 'length'): number;
|
|
31
|
-
/**
|
|
32
|
-
* Create a computed, filtered array
|
|
33
|
-
*/
|
|
34
|
-
filter(callback: (item: Item, index: number, array: Item[]) => boolean): Computed<Item[]>;
|
|
35
40
|
/**
|
|
36
41
|
* Create a computed, mapped array
|
|
42
|
+
* @param callback Callback to transform each item
|
|
43
|
+
* @return Computed array of mapped items
|
|
37
44
|
*/
|
|
38
45
|
map<Mapped>(callback: (item: Item, index: number, array: Item[]) => Mapped): Computed<Mapped[]>;
|
|
46
|
+
/**
|
|
47
|
+
* Notify dependents of changes
|
|
48
|
+
*
|
|
49
|
+
* _This bypasses equality checks and will immediately notify dependents.
|
|
50
|
+
* Use this only if you're modifying nested data that would be ignored by equality checks._
|
|
51
|
+
*/
|
|
52
|
+
notify(): void;
|
|
39
53
|
/**
|
|
40
54
|
* @inheritdoc
|
|
41
55
|
*/
|
|
42
56
|
peek(): Item[];
|
|
57
|
+
/**
|
|
58
|
+
* Get the value at an index _(without reactivity)_
|
|
59
|
+
* @param index Index of item to get _(if negative, starts from the end)_
|
|
60
|
+
* @returns Item at index, or `undefined` if it doesn't exist
|
|
61
|
+
*/
|
|
43
62
|
peek(index: number): Item | undefined;
|
|
44
63
|
/**
|
|
45
64
|
* Get the length of the array _(without reactivity)_
|
|
65
|
+
* @returns Length of the array
|
|
46
66
|
*/
|
|
47
|
-
peek(
|
|
67
|
+
peek(property: 'length'): number;
|
|
48
68
|
/**
|
|
49
69
|
* Remove and return the last item of the array
|
|
70
|
+
* @returns Removed item, or `undefined` if the array is empty
|
|
50
71
|
*/
|
|
51
72
|
pop(): Item | undefined;
|
|
52
73
|
/**
|
|
53
74
|
* Add items to the end of the array
|
|
75
|
+
* @param items Items to add
|
|
76
|
+
* @returns New array length
|
|
54
77
|
*/
|
|
55
78
|
push(...items: Item[]): number;
|
|
56
79
|
/**
|
|
57
80
|
* Set the value
|
|
81
|
+
* @param value New array of items _(defaults to an empty array)_
|
|
58
82
|
*/
|
|
59
83
|
set(value?: Item[]): void;
|
|
60
84
|
/**
|
|
61
85
|
* Set the value at an index
|
|
86
|
+
* @param index Index of item to set __(if negative, starts from the end)_
|
|
87
|
+
* @param value New item
|
|
62
88
|
*/
|
|
63
89
|
set(index: number, value: Item): void;
|
|
64
90
|
/**
|
|
65
91
|
* Set the length of the array
|
|
92
|
+
* @param value New array length
|
|
66
93
|
*/
|
|
67
94
|
set(property: 'length', value: number): void;
|
|
68
95
|
/**
|
|
69
96
|
* Remove and return the first item of the array
|
|
97
|
+
* @returns Removed item, or `undefined` if the array is empty
|
|
70
98
|
*/
|
|
71
99
|
shift(): Item | undefined;
|
|
72
100
|
/**
|
|
73
101
|
* Remove and return items from the array _(and optionally add new items)_
|
|
102
|
+
* @param from Index to start removing items from
|
|
103
|
+
* @param to Index to stop removing items at _(defaults to the end of the array)_
|
|
104
|
+
* @param items Optional items to add
|
|
105
|
+
* @returns Removed items
|
|
74
106
|
*/
|
|
75
107
|
splice(from: number, to?: number, ...items: Item[]): Item[];
|
|
76
108
|
/**
|
|
@@ -79,18 +111,27 @@ export declare class ReactiveArray<Item> extends Reactive<Item[], Item> {
|
|
|
79
111
|
subscribe(callback: (value: Item[]) => void): Unsubscribe;
|
|
80
112
|
/**
|
|
81
113
|
* Subscribe to changes at a specific index
|
|
114
|
+
* @param index Index of item to subscribe to
|
|
115
|
+
* @param callback Callback for changes
|
|
116
|
+
* @returns Unsubscribe callback
|
|
82
117
|
*/
|
|
83
118
|
subscribe(index: number, callback: (value: Item | undefined) => void): Unsubscribe;
|
|
84
119
|
/**
|
|
85
120
|
* Add items to the beginning of the array
|
|
121
|
+
* @param items Items to add
|
|
122
|
+
* @returns New array length
|
|
86
123
|
*/
|
|
87
124
|
unshift(...items: Item[]): number;
|
|
88
125
|
/**
|
|
89
126
|
* Update the value _(based on the current value)_
|
|
127
|
+
* @param callback Callback to update the value
|
|
90
128
|
*/
|
|
91
129
|
update(callback: (value: Item[]) => Item[]): void;
|
|
92
130
|
}
|
|
93
131
|
/**
|
|
94
132
|
* Create a reactive array
|
|
133
|
+
* @param value Initial array of items
|
|
134
|
+
* @param options Optional reactivity options
|
|
135
|
+
* @returns Reactive array
|
|
95
136
|
*/
|
|
96
137
|
export declare function array<Item>(value: Item[], options?: ReactiveOptions<Item>): ReactiveArray<Item>;
|
|
@@ -1,14 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Reactive
|
|
3
|
-
export type ComputedEffect = {
|
|
4
|
-
dirty: boolean;
|
|
5
|
-
instance: Effect;
|
|
6
|
-
};
|
|
7
|
-
export type InternalComputed = {
|
|
8
|
-
readonly effect: ComputedEffect;
|
|
9
|
-
readonly state: ReactiveState<unknown, unknown>;
|
|
10
|
-
};
|
|
11
|
-
export declare let activeComputed: Computed<unknown> | undefined;
|
|
1
|
+
import type { ReactiveOptions } from '../models';
|
|
2
|
+
import { Reactive } from './reactive';
|
|
12
3
|
export declare class Computed<Value> extends Reactive<Value> {
|
|
13
4
|
private readonly effect;
|
|
14
5
|
constructor(callback: () => Value, options?: ReactiveOptions<Value>);
|
|
@@ -1,45 +1,36 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { Effect } from '../effect';
|
|
3
|
-
import { type Subscription, type Unsubscribe } from '../subscription';
|
|
4
|
-
import type { Computed } from './computed';
|
|
1
|
+
import type { ReactiveOptions, ReactiveState, Unsubscribe } from '../models';
|
|
5
2
|
export declare abstract class Reactive<Value, Equal = Value> {
|
|
6
3
|
protected readonly state: ReactiveState<Value, Equal>;
|
|
7
4
|
constructor(name: string, value: Value, options?: ReactiveOptions<Equal>);
|
|
8
5
|
/**
|
|
9
6
|
* Get the value
|
|
7
|
+
* @return Current value
|
|
10
8
|
*/
|
|
11
9
|
abstract get(): Value;
|
|
12
10
|
/**
|
|
13
11
|
* Get the value _(without reactivity)_
|
|
12
|
+
* @return Current value
|
|
14
13
|
*/
|
|
15
14
|
peek(): Value;
|
|
16
15
|
/**
|
|
17
16
|
* Subscribe to changes
|
|
17
|
+
* @param callback Callback for changes
|
|
18
|
+
* @return Unsubscribe callback
|
|
18
19
|
*/
|
|
19
20
|
subscribe(callback: (value: Value) => void): Unsubscribe;
|
|
20
21
|
/**
|
|
21
22
|
* JSON representation of the value
|
|
23
|
+
* @return JSON value
|
|
22
24
|
*/
|
|
23
25
|
toJSON(): Value;
|
|
24
26
|
/**
|
|
25
27
|
* String representation of the value
|
|
28
|
+
* @return Value as string
|
|
26
29
|
*/
|
|
27
30
|
toString(): string;
|
|
28
31
|
/**
|
|
29
32
|
* Unsubscribe from changes
|
|
33
|
+
* @param callback Callback to unsubscribe
|
|
30
34
|
*/
|
|
31
35
|
unsubscribe(callback: (value: Value) => void): void;
|
|
32
36
|
}
|
|
33
|
-
export type ReactiveOptions<Value> = {
|
|
34
|
-
/**
|
|
35
|
-
* Method to compare values for equality
|
|
36
|
-
*/
|
|
37
|
-
equal?: (first: Value, second: Value) => boolean;
|
|
38
|
-
};
|
|
39
|
-
export type ReactiveState<Value, Equal> = {
|
|
40
|
-
computeds: Set<Computed<unknown>>;
|
|
41
|
-
effects: Set<Effect>;
|
|
42
|
-
equal: (first: Equal, second: Equal) => boolean;
|
|
43
|
-
subscriptions: Map<GenericCallback, Subscription>;
|
|
44
|
-
value: Value;
|
|
45
|
-
};
|
package/types/value/signal.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { ReactiveOptions } from '../models';
|
|
2
|
+
import { Reactive } from './reactive';
|
|
2
3
|
export declare class Signal<Value> extends Reactive<Value> {
|
|
3
4
|
constructor(value: Value, options?: ReactiveOptions<Value>);
|
|
4
5
|
/**
|
|
@@ -7,14 +8,19 @@ export declare class Signal<Value> extends Reactive<Value> {
|
|
|
7
8
|
get(): Value;
|
|
8
9
|
/**
|
|
9
10
|
* Set the value
|
|
11
|
+
* @param value New value
|
|
10
12
|
*/
|
|
11
13
|
set(value: Value): void;
|
|
12
14
|
/**
|
|
13
15
|
* Update the value _(based on the current value)_
|
|
16
|
+
* @param callback Callback to update the value
|
|
14
17
|
*/
|
|
15
18
|
update(callback: (value: Value) => Value): void;
|
|
16
19
|
}
|
|
17
20
|
/**
|
|
18
21
|
* Create a reactive value
|
|
22
|
+
* @param value Initial value
|
|
23
|
+
* @param options Optional reactivity options
|
|
24
|
+
* @returns Reactive value
|
|
19
25
|
*/
|
|
20
26
|
export declare function signal<Value>(value: Value, options?: ReactiveOptions<Value>): Signal<Value>;
|
package/types/value/store.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Key, PlainObject } from '@oscarpalmer/atoms/models';
|
|
2
|
-
import {
|
|
3
|
-
import { Reactive
|
|
2
|
+
import type { ReactiveOptions, Unsubscribe } from '../models';
|
|
3
|
+
import { Reactive } from './reactive';
|
|
4
4
|
export declare class Store<Value extends PlainObject> extends Reactive<Value, Value> {
|
|
5
5
|
#private;
|
|
6
6
|
constructor(value: Value, options?: ReactiveOptions<Value>);
|
|
@@ -9,35 +9,49 @@ export declare class Store<Value extends PlainObject> extends Reactive<Value, Va
|
|
|
9
9
|
*/
|
|
10
10
|
get(): Value;
|
|
11
11
|
/**
|
|
12
|
-
* Get a value by key
|
|
12
|
+
* Get a value by key
|
|
13
|
+
* @param key Key of the value to get
|
|
14
|
+
* @returns Value for the specified key, or `undefined` if it doesn't exist
|
|
13
15
|
*/
|
|
14
16
|
get(key: keyof Value): Value[keyof Value];
|
|
15
17
|
/**
|
|
16
|
-
* Get a value by key
|
|
18
|
+
* Get a value by key
|
|
19
|
+
* @param key Key of the value to get
|
|
20
|
+
* @returns Value for the specified key, or `undefined` if it doesn't exist
|
|
17
21
|
*/
|
|
18
22
|
get(key: Key): unknown;
|
|
19
23
|
/**
|
|
20
24
|
* Get the value _(without reactivity)_
|
|
25
|
+
* @returns The current value
|
|
21
26
|
*/
|
|
22
27
|
peek(): Value;
|
|
23
28
|
/**
|
|
24
29
|
* Get a value by key _(without reactivity)_
|
|
30
|
+
* @param key Key of the value to get
|
|
31
|
+
* @returns Value for the specified key, or `undefined` if it doesn't exist
|
|
25
32
|
*/
|
|
26
33
|
peek(key: keyof Value): Value[keyof Value];
|
|
27
34
|
/**
|
|
28
35
|
* Get a value by key _(without reactivity)_
|
|
36
|
+
* @param key Key of the value to get
|
|
37
|
+
* @returns Value for the specified key, or `undefined` if it doesn't exist
|
|
29
38
|
*/
|
|
30
39
|
peek(key: Key): unknown;
|
|
31
40
|
/**
|
|
32
41
|
* Set the value
|
|
42
|
+
* @param value New value _(defaults to an empty object)_
|
|
33
43
|
*/
|
|
34
44
|
set(value?: Value): void;
|
|
35
45
|
/**
|
|
36
46
|
* Set a value by key
|
|
47
|
+
* @param key Key of the value to set
|
|
48
|
+
* @param value New value
|
|
37
49
|
*/
|
|
38
50
|
set(key: keyof Value, value: Value[keyof Value]): void;
|
|
39
51
|
/**
|
|
40
52
|
* Set a value by key
|
|
53
|
+
* @param key Key of the value to set
|
|
54
|
+
* @param value New value
|
|
41
55
|
*/
|
|
42
56
|
set(key: Key, value: unknown): void;
|
|
43
57
|
/**
|
|
@@ -46,18 +60,28 @@ export declare class Store<Value extends PlainObject> extends Reactive<Value, Va
|
|
|
46
60
|
subscribe(callback: (value: Value) => void): Unsubscribe;
|
|
47
61
|
/**
|
|
48
62
|
* Subscribe to changes for a specific key
|
|
63
|
+
* @param key Key of the value to subscribe to
|
|
64
|
+
* @param callback Callback for changes
|
|
65
|
+
* @returns Unsubscribe callback
|
|
49
66
|
*/
|
|
50
67
|
subscribe<Key extends keyof Value>(key: Key, callback: (value: Value[Key] | undefined) => void): Unsubscribe;
|
|
51
68
|
/**
|
|
52
69
|
* Subscribe to changes for a specific key
|
|
70
|
+
* @param key Key of the value to subscribe to
|
|
71
|
+
* @param callback Callback for changes
|
|
72
|
+
* @returns Unsubscribe callback
|
|
53
73
|
*/
|
|
54
74
|
subscribe(key: Key, callback: (value: unknown) => void): Unsubscribe;
|
|
55
75
|
/**
|
|
56
76
|
* Update the value _(based on the current value)_
|
|
77
|
+
* @param callback Callback to update the value
|
|
57
78
|
*/
|
|
58
79
|
update(callback: (value: Value) => Value): void;
|
|
59
80
|
}
|
|
60
81
|
/**
|
|
61
82
|
* Create a reactive store
|
|
83
|
+
* @param value Initial object value
|
|
84
|
+
* @param options Optional reactivity options
|
|
85
|
+
* @returns Reactive store
|
|
62
86
|
*/
|
|
63
87
|
export declare function store<Value extends PlainObject>(value: Value, options?: ReactiveOptions<Value>): Store<Value>;
|