@oscarpalmer/mora 0.30.0 → 0.32.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.d.mts +4 -5
- package/dist/batch.mjs +4 -2
- package/dist/constants.d.mts +29 -18
- package/dist/constants.mjs +19 -2
- package/dist/effect.d.mts +4 -5
- package/dist/effect.mjs +2 -2
- package/dist/helpers/is.d.mts +21 -15
- package/dist/helpers/is.mjs +21 -12
- package/dist/helpers/proxy.d.mts +12 -10
- package/dist/helpers/proxy.mjs +32 -14
- package/dist/helpers/subscription.d.mts +8 -0
- package/dist/helpers/subscription.mjs +27 -0
- package/dist/helpers/value.d.mts +8 -6
- package/dist/helpers/value.mjs +30 -4
- package/dist/index.d.mts +4 -3
- package/dist/index.mjs +2 -2
- package/dist/models.d.mts +108 -84
- package/dist/mora.full.mjs +504 -247
- package/dist/value/array.d.mts +4 -5
- package/dist/value/array.mjs +25 -56
- package/dist/value/computed.d.mts +3 -4
- package/dist/value/computed.mjs +14 -14
- package/dist/value/reactive.d.mts +2 -3
- package/dist/value/reactive.mjs +0 -1
- package/dist/value/readonly.d.mts +5 -0
- package/dist/value/readonly.mjs +33 -0
- package/dist/value/signal.d.mts +4 -5
- package/dist/value/signal.mjs +10 -17
- package/dist/value/store.d.mts +4 -5
- package/dist/value/store.mjs +17 -41
- package/package.json +6 -6
- package/src/batch.ts +15 -5
- package/src/constants.ts +41 -3
- package/src/effect.ts +6 -2
- package/src/helpers/is.ts +37 -12
- package/src/helpers/proxy.ts +99 -49
- package/src/helpers/subscription.ts +78 -0
- package/src/helpers/value.ts +69 -4
- package/src/index.ts +18 -2
- package/src/models.ts +131 -86
- package/src/value/array.ts +67 -120
- package/src/value/computed.ts +36 -13
- package/src/value/reactive.ts +8 -5
- package/src/value/readonly.ts +72 -0
- package/src/value/signal.ts +22 -16
- package/src/value/store.ts +37 -70
- package/types/constants.d.ts +1 -1
- package/types/index.d.ts +1 -1
- package/types/value/array.d.ts +1 -1
- package/types/value/computed.d.ts +0 -3
- package/types/value/signal.d.ts +1 -1
- package/types/value/store.d.ts +1 -1
- package/dist/subscription.d.mts +0 -7
- package/dist/subscription.mjs +0 -27
- package/src/subscription.ts +0 -45
package/src/value/computed.ts
CHANGED
|
@@ -1,11 +1,21 @@
|
|
|
1
1
|
import {flushHandlers} from '../batch';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
ACTIVE,
|
|
4
|
+
BATCH,
|
|
5
|
+
NAME_COMPUTED,
|
|
6
|
+
NAME_MORA,
|
|
7
|
+
SUBSCRIPTION_TYPES_COPY,
|
|
8
|
+
SUBSCRIPTION_TYPE_FROZEN,
|
|
9
|
+
SUBSCRIPTION_TYPES,
|
|
10
|
+
} from '../constants';
|
|
3
11
|
import {internalEffect, runEffect} from '../effect';
|
|
4
|
-
import {
|
|
12
|
+
import {subscribeToSignal} from '../helpers/subscription';
|
|
13
|
+
import {handleSimpleValue, peekSimpleValue} from '../helpers/value';
|
|
5
14
|
import type {Computed, ComputedEffect, ReactiveOptions, ReactiveState} from '../models';
|
|
6
|
-
import {subscribe} from '../subscription';
|
|
7
15
|
import {reactive} from './reactive';
|
|
8
16
|
|
|
17
|
+
// #region Functions
|
|
18
|
+
|
|
9
19
|
/**
|
|
10
20
|
* Create a computed value
|
|
11
21
|
*
|
|
@@ -24,6 +34,10 @@ function getComputed<Value>(
|
|
|
24
34
|
callback: () => Value | Promise<Value>,
|
|
25
35
|
options?: ReactiveOptions<Value>,
|
|
26
36
|
): [Computed<Value>, ComputedEffect] {
|
|
37
|
+
if (typeof callback !== 'function') {
|
|
38
|
+
throw new TypeError('Computed callback must be a function');
|
|
39
|
+
}
|
|
40
|
+
|
|
27
41
|
const [rx, state] = reactive<Value>(undefined as never, options);
|
|
28
42
|
|
|
29
43
|
let fx: ComputedEffect;
|
|
@@ -31,21 +45,17 @@ function getComputed<Value>(
|
|
|
31
45
|
const instance = {
|
|
32
46
|
...rx,
|
|
33
47
|
get: () => getValue(state, fx),
|
|
34
|
-
peek: () => state.value,
|
|
35
|
-
subscribe: (callback:
|
|
36
|
-
unsubscribe: (callback: (value: Value) => void) => {
|
|
37
|
-
state.subscriptions.delete(callback);
|
|
38
|
-
},
|
|
48
|
+
peek: (copy?: never) => peekSimpleValue(state.value, copy === true),
|
|
49
|
+
subscribe: (callback: never, copy?: never) => subscribeToSignal(state, callback, copy === true),
|
|
39
50
|
};
|
|
40
51
|
|
|
41
52
|
Object.defineProperty(instance, NAME_MORA, {
|
|
42
|
-
enumerable: false,
|
|
43
53
|
value: NAME_COMPUTED,
|
|
44
54
|
});
|
|
45
55
|
|
|
46
56
|
fx = getComputedEffect(state, callback);
|
|
47
57
|
|
|
48
|
-
return [Object.freeze(instance), fx];
|
|
58
|
+
return [Object.freeze(instance) as never, fx];
|
|
49
59
|
}
|
|
50
60
|
|
|
51
61
|
function getComputedEffect<Value>(
|
|
@@ -109,12 +119,25 @@ function setAndEmit<Value>(state: ReactiveState<Value, Value>, value: Value): vo
|
|
|
109
119
|
}
|
|
110
120
|
|
|
111
121
|
for (const effect of state.effects) {
|
|
112
|
-
BATCH.handlers.
|
|
122
|
+
BATCH.handlers.set(effect, effect);
|
|
113
123
|
}
|
|
114
124
|
|
|
115
|
-
for (const
|
|
116
|
-
|
|
125
|
+
for (const type of SUBSCRIPTION_TYPES) {
|
|
126
|
+
if (type === SUBSCRIPTION_TYPE_FROZEN) {
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const copy = SUBSCRIPTION_TYPES_COPY.has(type);
|
|
131
|
+
const subscriptions = state.subscriptions?.values.to.keyed?.get(type);
|
|
132
|
+
|
|
133
|
+
if (subscriptions != null) {
|
|
134
|
+
for (const [, callback] of subscriptions) {
|
|
135
|
+
callback(peekSimpleValue(state.value, copy));
|
|
136
|
+
}
|
|
137
|
+
}
|
|
117
138
|
}
|
|
118
139
|
|
|
119
140
|
flushHandlers();
|
|
120
141
|
}
|
|
142
|
+
|
|
143
|
+
// #endregion
|
package/src/value/reactive.ts
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
import type {Reactive, ReactiveOptions, ReactiveState} from '../models';
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
// #region Functions
|
|
4
|
+
|
|
5
|
+
export function reactive<Value, Item = Value>(
|
|
4
6
|
value: Value,
|
|
5
|
-
options?: ReactiveOptions<
|
|
6
|
-
): [Reactive<Value>, ReactiveState<Value,
|
|
7
|
-
const state: ReactiveState<Value,
|
|
7
|
+
options?: ReactiveOptions<Item>,
|
|
8
|
+
): [Reactive<Value>, ReactiveState<Value, Item>] {
|
|
9
|
+
const state: ReactiveState<Value, Item> = {
|
|
8
10
|
computeds: new Set(),
|
|
9
11
|
effects: new Set(),
|
|
10
12
|
equal:
|
|
11
13
|
typeof options === 'object' && typeof options?.equal === 'function'
|
|
12
14
|
? options.equal
|
|
13
15
|
: Object.is,
|
|
14
|
-
subscriptions: new Map(),
|
|
15
16
|
value: value as Value,
|
|
16
17
|
};
|
|
17
18
|
|
|
@@ -22,3 +23,5 @@ export function reactive<Value, Equal = Value>(
|
|
|
22
23
|
|
|
23
24
|
return [instance, state];
|
|
24
25
|
}
|
|
26
|
+
|
|
27
|
+
// #endregion
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import {
|
|
2
|
+
NAME_MORA,
|
|
3
|
+
NAME_READONLY,
|
|
4
|
+
SUBSCRIPTION_TYPE_FROZEN,
|
|
5
|
+
SUBSCRIPTION_TYPE_READONLY,
|
|
6
|
+
} from '../constants';
|
|
7
|
+
import {subscribeToReactive} from '../helpers/subscription';
|
|
8
|
+
import {getFrozenValue, getSimpleValue, peekSimpleValue} from '../helpers/value';
|
|
9
|
+
import type {
|
|
10
|
+
ReactiveState,
|
|
11
|
+
ReadonlyFrozenSignal,
|
|
12
|
+
ReadonlyInstances,
|
|
13
|
+
ReadonlySignal,
|
|
14
|
+
} from '../models';
|
|
15
|
+
|
|
16
|
+
// #region Functions
|
|
17
|
+
|
|
18
|
+
export function getReadonlyInstance<Value>(
|
|
19
|
+
state: ReactiveState<Value, never>,
|
|
20
|
+
instances: ReadonlyInstances<Value>,
|
|
21
|
+
frozen: boolean,
|
|
22
|
+
): ReadonlySignal<Value> {
|
|
23
|
+
const key = frozen ? 'frozen' : 'original';
|
|
24
|
+
|
|
25
|
+
instances[key] ??= getReadonlySignal(state, frozen) as never;
|
|
26
|
+
|
|
27
|
+
return instances[key] as never;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function getReadonlySignal<Value>(
|
|
31
|
+
state: ReactiveState<Value, never>,
|
|
32
|
+
frozen: boolean,
|
|
33
|
+
): ReadonlySignal<Value> | ReadonlyFrozenSignal<Value> {
|
|
34
|
+
const type = frozen ? SUBSCRIPTION_TYPE_FROZEN : SUBSCRIPTION_TYPE_READONLY;
|
|
35
|
+
|
|
36
|
+
const instance = {
|
|
37
|
+
get: () => getReadonlyValue(state, false, frozen),
|
|
38
|
+
peek: (copy?: boolean) => getReadonlyValue(state, true, frozen, copy),
|
|
39
|
+
subscribe: (callback: never) => subscribeToReactive(type, state, callback),
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
Object.defineProperties(instance, {
|
|
43
|
+
[NAME_MORA]: {
|
|
44
|
+
value: NAME_READONLY,
|
|
45
|
+
},
|
|
46
|
+
frozen: {
|
|
47
|
+
enumerable: true,
|
|
48
|
+
value: frozen,
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
return Object.freeze(instance) as never;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function getReadonlyValue<Value>(
|
|
56
|
+
state: ReactiveState<Value, never>,
|
|
57
|
+
peek: boolean,
|
|
58
|
+
frozen: boolean,
|
|
59
|
+
copy?: boolean,
|
|
60
|
+
): unknown {
|
|
61
|
+
let value: unknown;
|
|
62
|
+
|
|
63
|
+
if (peek) {
|
|
64
|
+
value = peekSimpleValue(state.value, copy === true);
|
|
65
|
+
} else {
|
|
66
|
+
value = getSimpleValue(state);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return frozen ? getFrozenValue(value) : value;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// #endregion
|
package/src/value/signal.ts
CHANGED
|
@@ -1,8 +1,17 @@
|
|
|
1
1
|
import {NAME_MORA, NAME_SIGNAL} from '../constants';
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
|
|
2
|
+
import {subscribeToSignal} from '../helpers/subscription';
|
|
3
|
+
import {
|
|
4
|
+
emitValue,
|
|
5
|
+
getSimpleValue,
|
|
6
|
+
handleSimpleValue,
|
|
7
|
+
peekSimpleValue,
|
|
8
|
+
updateSimpleValue,
|
|
9
|
+
} from '../helpers/value';
|
|
10
|
+
import type {ReactiveOptions, ReactiveState, ReadonlyInstances, Signal} from '../models';
|
|
5
11
|
import {reactive} from './reactive';
|
|
12
|
+
import {getReadonlyInstance} from './readonly';
|
|
13
|
+
|
|
14
|
+
// #region Functions
|
|
6
15
|
|
|
7
16
|
function setAndEmit<Value>(state: ReactiveState<Value, Value>, value: Value): void {
|
|
8
17
|
if (!state.equal(state.value, value)) {
|
|
@@ -51,28 +60,25 @@ export function signal<Value>(
|
|
|
51
60
|
): Signal<Value> {
|
|
52
61
|
const [rx, state] = reactive<Value>(undefined as unknown as Value, options);
|
|
53
62
|
|
|
63
|
+
const readonlies: ReadonlyInstances<Value> = {};
|
|
64
|
+
|
|
54
65
|
const instance = {
|
|
55
66
|
...rx,
|
|
67
|
+
asReadonly: (frozen?: never) => getReadonlyInstance(state, readonlies, frozen === true),
|
|
56
68
|
get: () => getSimpleValue(state),
|
|
57
|
-
peek: () => state.value,
|
|
58
|
-
set: (value:
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
update: (callback: (value: Value) => Value) => {
|
|
62
|
-
handleSimpleValue(state, callback(state.value), setAndEmit);
|
|
63
|
-
},
|
|
64
|
-
subscribe: (callback: (value: Value) => void) => subscribe(state, callback),
|
|
65
|
-
unsubscribe: (callback: (value: Value) => void) => {
|
|
66
|
-
state.subscriptions.delete(callback);
|
|
67
|
-
},
|
|
69
|
+
peek: (copy?: boolean) => peekSimpleValue(state.value, copy === true),
|
|
70
|
+
set: (value: never) => handleSimpleValue(state, value, setAndEmit),
|
|
71
|
+
subscribe: (callback: never, copy?: never) => subscribeToSignal(state, callback, copy === true),
|
|
72
|
+
update: (callback: never) => updateSimpleValue(state, callback, setAndEmit),
|
|
68
73
|
};
|
|
69
74
|
|
|
70
75
|
Object.defineProperty(instance, NAME_MORA, {
|
|
71
|
-
enumerable: false,
|
|
72
76
|
value: NAME_SIGNAL,
|
|
73
77
|
});
|
|
74
78
|
|
|
75
79
|
handleSimpleValue(state, value, setAndEmit);
|
|
76
80
|
|
|
77
|
-
return Object.freeze(instance) as
|
|
81
|
+
return Object.freeze(instance) as never;
|
|
78
82
|
}
|
|
83
|
+
|
|
84
|
+
// #endregion
|
package/src/value/store.ts
CHANGED
|
@@ -1,30 +1,40 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type {GenericCallback, Key, PlainObject} from '@oscarpalmer/atoms/models';
|
|
1
|
+
import type {Key, PlainObject} from '@oscarpalmer/atoms/models';
|
|
3
2
|
import {startBatch, stopBatch} from '../batch';
|
|
4
3
|
import {NAME_MORA, NAME_STORE} from '../constants';
|
|
5
4
|
import {
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
emitProxyValues,
|
|
6
|
+
getValueInProxy,
|
|
7
|
+
peekValueInProxy,
|
|
8
8
|
setProxyValue,
|
|
9
9
|
setValueInProxy,
|
|
10
|
+
updateProxyValue,
|
|
10
11
|
} from '../helpers/proxy';
|
|
11
|
-
import {
|
|
12
|
+
import {subscribeToProxy} from '../helpers/subscription';
|
|
12
13
|
import type {
|
|
13
14
|
Computed,
|
|
14
15
|
ComputedEffect,
|
|
15
16
|
ReactiveOptions,
|
|
16
17
|
ReactiveState,
|
|
17
18
|
ReactiveStore,
|
|
18
|
-
|
|
19
|
+
ReadonlyInstances,
|
|
19
20
|
} from '../models';
|
|
20
|
-
import {noop, subscribe, unsubscribe} from '../subscription';
|
|
21
21
|
import {reactive} from './reactive';
|
|
22
|
+
import {getReadonlyInstance} from './readonly';
|
|
22
23
|
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
// #region Functions
|
|
25
|
+
|
|
26
|
+
function setPropertyValue<Value extends PlainObject, Item = Value>(
|
|
27
|
+
state: ReactiveState<Value, Item>,
|
|
28
|
+
key: unknown,
|
|
29
|
+
value: unknown,
|
|
30
|
+
): void {
|
|
31
|
+
(state.value as PlainObject)[key as Key] = value;
|
|
25
32
|
}
|
|
26
33
|
|
|
27
|
-
function
|
|
34
|
+
function setStoreValue<Value extends PlainObject, Item = Value>(
|
|
35
|
+
state: ReactiveState<Value, Item>,
|
|
36
|
+
value: PlainObject | undefined,
|
|
37
|
+
): void {
|
|
28
38
|
startBatch();
|
|
29
39
|
|
|
30
40
|
const actual = value ?? {};
|
|
@@ -56,14 +66,6 @@ function setObject(state: ReactiveState<PlainObject, never>, value: PlainObject
|
|
|
56
66
|
stopBatch();
|
|
57
67
|
}
|
|
58
68
|
|
|
59
|
-
function setProperty<Value extends PlainObject>(
|
|
60
|
-
state: ReactiveState<Value, Value>,
|
|
61
|
-
key: unknown,
|
|
62
|
-
value: unknown,
|
|
63
|
-
): void {
|
|
64
|
-
(state.value as PlainObject)[key as Key] = value;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
69
|
/**
|
|
68
70
|
* Create a reactive store from a function result
|
|
69
71
|
*
|
|
@@ -106,70 +108,35 @@ export function store<Value extends PlainObject>(
|
|
|
106
108
|
): ReactiveStore<Value> {
|
|
107
109
|
const [rx, state] = reactive<Value>(undefined as never, options);
|
|
108
110
|
|
|
111
|
+
const isArray = false;
|
|
109
112
|
const keyed = new Map<Key, [Computed<unknown>, ComputedEffect]>();
|
|
113
|
+
const readonlies: ReadonlyInstances<Value> = {};
|
|
110
114
|
|
|
111
115
|
state.value = new Proxy({} as Value, {
|
|
112
|
-
set: (target
|
|
113
|
-
setValueInProxy({
|
|
114
|
-
target,
|
|
115
|
-
property,
|
|
116
|
-
state,
|
|
117
|
-
value,
|
|
118
|
-
isArray: false,
|
|
119
|
-
}),
|
|
116
|
+
set: (target, property, value) => setValueInProxy(isArray, state, target, property, value),
|
|
120
117
|
});
|
|
121
118
|
|
|
122
119
|
const instance = {
|
|
123
120
|
...rx,
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
false,
|
|
135
|
-
state,
|
|
136
|
-
isStoreObject,
|
|
137
|
-
isKey,
|
|
138
|
-
setObject,
|
|
139
|
-
setProperty,
|
|
140
|
-
first,
|
|
141
|
-
second,
|
|
142
|
-
);
|
|
143
|
-
},
|
|
144
|
-
subscribe: (first: Key | GenericCallback, second?: GenericCallback): Unsubscribe => {
|
|
145
|
-
if (isKey(first) && typeof second === 'function') {
|
|
146
|
-
return getReactiveValueInProxy(instance, keyed, first, false).subscribe(second);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
return typeof first === 'function' ? subscribe(state, first) : noop;
|
|
150
|
-
},
|
|
151
|
-
unsubscribe: (first: Key | GenericCallback, second?: GenericCallback) => {
|
|
152
|
-
if (isKey(first) && typeof second === 'function') {
|
|
153
|
-
getReactiveValueInProxy(instance, keyed, first, false)?.unsubscribe(second);
|
|
154
|
-
} else if (typeof first === 'function') {
|
|
155
|
-
unsubscribe(state, first);
|
|
156
|
-
}
|
|
157
|
-
},
|
|
158
|
-
update: (callback: (value: Value) => Value) => {
|
|
159
|
-
const updated = callback(state.value);
|
|
160
|
-
|
|
161
|
-
if (updated == null || isPlainObject(updated)) {
|
|
162
|
-
setObject(state, updated);
|
|
163
|
-
}
|
|
164
|
-
},
|
|
121
|
+
asReadonly: (frozen?: never) => getReadonlyInstance(state, readonlies, frozen === true),
|
|
122
|
+
get: (value?: never) => getValueInProxy(isArray, instance as never, state, keyed, value),
|
|
123
|
+
notify: () => emitProxyValues(state, keyed),
|
|
124
|
+
peek: (first?: never, second?: never) => peekValueInProxy(isArray, state, first, second),
|
|
125
|
+
set: (first?: never, second?: never) =>
|
|
126
|
+
setProxyValue<Value>(isArray, state, setStoreValue, setPropertyValue, first, second),
|
|
127
|
+
subscribe: (first: never, second?: never, third?: never) =>
|
|
128
|
+
subscribeToProxy(isArray, instance as never, state, keyed, first, second, third),
|
|
129
|
+
update: (callback: never) =>
|
|
130
|
+
updateProxyValue(isArray, state, setStoreValue, setPropertyValue, callback),
|
|
165
131
|
};
|
|
166
132
|
|
|
167
133
|
Object.defineProperty(instance, NAME_MORA, {
|
|
168
|
-
enumerable: false,
|
|
169
134
|
value: NAME_STORE,
|
|
170
135
|
});
|
|
171
136
|
|
|
172
|
-
instance.set(value);
|
|
137
|
+
instance.set(value as never);
|
|
173
138
|
|
|
174
|
-
return Object.freeze(instance) as
|
|
139
|
+
return Object.freeze(instance) as never;
|
|
175
140
|
}
|
|
141
|
+
|
|
142
|
+
// #endregion
|
package/types/constants.d.ts
CHANGED
|
@@ -12,5 +12,5 @@ export declare const NAME_EFFECT = "effect";
|
|
|
12
12
|
export declare const NAME_MORA = "$mora";
|
|
13
13
|
export declare const NAME_SIGNAL = "signal";
|
|
14
14
|
export declare const NAME_STORE = "store";
|
|
15
|
-
export declare const
|
|
15
|
+
export declare const NAMES: Set<string>;
|
|
16
16
|
export declare const PROPERTY_LENGTH = "length";
|
package/types/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export { startBatch, stopBatch } from './batch';
|
|
2
2
|
export { effect, type Effect } from './effect';
|
|
3
|
-
export { isArray, isComputed, isEffect, isReactive, isSignal } from './helpers/is';
|
|
3
|
+
export { isArray, isComputed, isEffect, isReactive, isSignal, } from './helpers/is';
|
|
4
4
|
export type { Unsubscribe } from './models';
|
|
5
5
|
export { array, type ReactiveArray } from './value/array';
|
|
6
6
|
export { computed, type Computed } from './value/computed';
|
package/types/value/array.d.ts
CHANGED
|
@@ -131,7 +131,7 @@ export declare class ReactiveArray<Item> extends Reactive<Item[], Item> {
|
|
|
131
131
|
/**
|
|
132
132
|
* Create a reactive array
|
|
133
133
|
* @param value Initial array of items
|
|
134
|
-
* @param options
|
|
134
|
+
* @param options Optional reactivity options
|
|
135
135
|
* @returns Reactive array
|
|
136
136
|
*/
|
|
137
137
|
export declare function array<Item>(value: Item[], options?: ReactiveOptions<Item>): ReactiveArray<Item>;
|
|
@@ -10,8 +10,5 @@ export declare class Computed<Value> extends Reactive<Value> {
|
|
|
10
10
|
}
|
|
11
11
|
/**
|
|
12
12
|
* Create a computed value
|
|
13
|
-
* @param callback Callback to compute the value
|
|
14
|
-
* @param options Reactivity options
|
|
15
|
-
* @returns Computed value
|
|
16
13
|
*/
|
|
17
14
|
export declare function computed<Value>(callback: () => Value, options?: ReactiveOptions<Value>): Computed<Value>;
|
package/types/value/signal.d.ts
CHANGED
|
@@ -20,7 +20,7 @@ export declare class Signal<Value> extends Reactive<Value> {
|
|
|
20
20
|
/**
|
|
21
21
|
* Create a reactive value
|
|
22
22
|
* @param value Initial value
|
|
23
|
-
* @param options
|
|
23
|
+
* @param options Optional reactivity options
|
|
24
24
|
* @returns Reactive value
|
|
25
25
|
*/
|
|
26
26
|
export declare function signal<Value>(value: Value, options?: ReactiveOptions<Value>): Signal<Value>;
|
package/types/value/store.d.ts
CHANGED
|
@@ -88,7 +88,7 @@ export declare class Store<Value extends PlainObject> extends Reactive<Value, Va
|
|
|
88
88
|
/**
|
|
89
89
|
* Create a reactive store
|
|
90
90
|
* @param value Initial object value
|
|
91
|
-
* @param options
|
|
91
|
+
* @param options Optional reactivity options
|
|
92
92
|
* @returns Reactive store
|
|
93
93
|
*/
|
|
94
94
|
export declare function store<Value extends PlainObject>(value: Value, options?: ReactiveOptions<Value>): Store<Value>;
|
package/dist/subscription.d.mts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { ReactiveState, Unsubscribe } from "./models.mjs";
|
|
2
|
-
//#region src/subscription.d.ts
|
|
3
|
-
declare function noop(): void;
|
|
4
|
-
declare function subscribe<Value>(state: ReactiveState<Value, never>, callback: (value: Value) => void): Unsubscribe;
|
|
5
|
-
declare function unsubscribe<Value>(state: ReactiveState<Value, never>, callback: (value: Value) => void): void;
|
|
6
|
-
//#endregion
|
|
7
|
-
export { noop, subscribe, unsubscribe };
|
package/dist/subscription.mjs
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
//#region src/subscription.ts
|
|
2
|
-
function noop() {}
|
|
3
|
-
function subscribe(state, callback) {
|
|
4
|
-
if (typeof callback !== "function" || state.subscriptions.has(callback)) return noop;
|
|
5
|
-
state.subscriptions.set(callback, subscription(state, callback));
|
|
6
|
-
return () => {
|
|
7
|
-
unsubscribe(state, callback);
|
|
8
|
-
};
|
|
9
|
-
}
|
|
10
|
-
function subscription(state, callback) {
|
|
11
|
-
const instance = {
|
|
12
|
-
callback,
|
|
13
|
-
state,
|
|
14
|
-
destroy: () => {
|
|
15
|
-
instance.callback = noop;
|
|
16
|
-
instance.state = void 0;
|
|
17
|
-
}
|
|
18
|
-
};
|
|
19
|
-
callback(state.value);
|
|
20
|
-
return instance;
|
|
21
|
-
}
|
|
22
|
-
function unsubscribe(state, callback) {
|
|
23
|
-
state.subscriptions.get(callback)?.destroy();
|
|
24
|
-
state.subscriptions.delete(callback);
|
|
25
|
-
}
|
|
26
|
-
//#endregion
|
|
27
|
-
export { noop, subscribe, unsubscribe };
|
package/src/subscription.ts
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import type {ReactiveState, Subscription, Unsubscribe} from './models';
|
|
2
|
-
|
|
3
|
-
export function noop(): void {}
|
|
4
|
-
|
|
5
|
-
export function subscribe<Value>(
|
|
6
|
-
state: ReactiveState<Value, never>,
|
|
7
|
-
callback: (value: Value) => void,
|
|
8
|
-
): Unsubscribe {
|
|
9
|
-
if (typeof callback !== 'function' || state.subscriptions.has(callback)) {
|
|
10
|
-
return noop;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
state.subscriptions.set(callback, subscription(state, callback));
|
|
14
|
-
|
|
15
|
-
return () => {
|
|
16
|
-
unsubscribe(state, callback);
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function subscription<Value>(
|
|
21
|
-
state: ReactiveState<Value, never>,
|
|
22
|
-
callback: (value: Value) => void,
|
|
23
|
-
): Subscription {
|
|
24
|
-
const instance: Subscription = {
|
|
25
|
-
callback,
|
|
26
|
-
state,
|
|
27
|
-
destroy: () => {
|
|
28
|
-
instance.callback = noop;
|
|
29
|
-
instance.state = undefined as never;
|
|
30
|
-
},
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
callback(state.value);
|
|
34
|
-
|
|
35
|
-
return instance;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export function unsubscribe<Value>(
|
|
39
|
-
state: ReactiveState<Value, never>,
|
|
40
|
-
callback: (value: Value) => void,
|
|
41
|
-
): void {
|
|
42
|
-
state.subscriptions.get(callback)?.destroy();
|
|
43
|
-
|
|
44
|
-
state.subscriptions.delete(callback);
|
|
45
|
-
}
|