@oscarpalmer/mora 0.33.0 → 0.35.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/constants.d.mts +2 -2
- package/dist/helpers/proxy.d.mts +8 -8
- package/dist/helpers/proxy.mjs +32 -25
- package/dist/helpers/subscription.d.mts +3 -3
- package/dist/helpers/value.d.mts +9 -8
- package/dist/helpers/value.mjs +15 -11
- package/dist/index.d.mts +2 -2
- package/dist/models.d.mts +40 -37
- package/dist/mora.full.mjs +195 -167
- package/dist/value/array.mjs +53 -41
- package/dist/value/computed.d.mts +2 -2
- package/dist/value/computed.mjs +3 -5
- package/dist/value/readonly.d.mts +2 -2
- package/dist/value/readonly.mjs +16 -19
- package/dist/value/signal.mjs +13 -13
- package/dist/value/store.mjs +4 -6
- package/package.json +2 -2
- package/src/batch.ts +2 -2
- package/src/constants.ts +7 -2
- package/src/helpers/is.ts +1 -3
- package/src/helpers/proxy.ts +68 -57
- package/src/helpers/subscription.ts +7 -6
- package/src/helpers/value.ts +30 -20
- package/src/index.ts +1 -0
- package/src/models.ts +47 -43
- package/src/value/array.ts +81 -64
- package/src/value/computed.ts +12 -16
- package/src/value/readonly.ts +38 -29
- package/src/value/signal.ts +20 -20
- package/src/value/store.ts +4 -7
package/src/value/readonly.ts
CHANGED
|
@@ -6,42 +6,51 @@ import {
|
|
|
6
6
|
SYMBOL_STATE,
|
|
7
7
|
} from '../constants';
|
|
8
8
|
import {subscribeToReadonly} from '../helpers/subscription';
|
|
9
|
-
import {
|
|
10
|
-
|
|
9
|
+
import {
|
|
10
|
+
getFrozenValue,
|
|
11
|
+
getJsonValue,
|
|
12
|
+
getSignalValue,
|
|
13
|
+
getStringValue,
|
|
14
|
+
peekSignalValue,
|
|
15
|
+
} from '../helpers/value';
|
|
16
|
+
import type {InternalSignal, ReadonlyFrozenSignal, ReadonlySignal, SignalState} from '../models';
|
|
17
|
+
|
|
18
|
+
// #region Types
|
|
19
|
+
|
|
20
|
+
type InternalReadonly = {
|
|
21
|
+
[SUBSCRIPTION_TYPE_FROZEN]: boolean;
|
|
22
|
+
} & InternalSignal;
|
|
23
|
+
|
|
24
|
+
// #endregion
|
|
11
25
|
|
|
12
26
|
// #region Instance
|
|
13
27
|
|
|
14
28
|
function Readonly(this: any, state: SignalState, frozen: boolean) {
|
|
29
|
+
this[SUBSCRIPTION_TYPE_FROZEN] = frozen;
|
|
15
30
|
this[SYMBOL_STATE] = state;
|
|
16
|
-
|
|
17
|
-
Object.defineProperty(this, SUBSCRIPTION_TYPE_FROZEN, {
|
|
18
|
-
enumerable: true,
|
|
19
|
-
value: frozen,
|
|
20
|
-
});
|
|
21
31
|
}
|
|
22
32
|
|
|
23
33
|
Readonly.prototype[NAME_MORA] = NAME_READONLY;
|
|
24
34
|
|
|
35
|
+
Readonly.prototype.get = getReadonlyValue;
|
|
36
|
+
Readonly.prototype.peek = peekReadonlyValue;
|
|
25
37
|
Readonly.prototype.subscribe = subscribeToReadonly;
|
|
38
|
+
Readonly.prototype.toJSON = getJsonValue;
|
|
26
39
|
Readonly.prototype.toString = getStringValue;
|
|
27
40
|
|
|
28
|
-
Readonly.prototype.get = function () {
|
|
29
|
-
return getReadonlyValue(this, false, this[SUBSCRIPTION_TYPE_FROZEN]);
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
Readonly.prototype.peek = function (copy?: boolean) {
|
|
33
|
-
return getReadonlyValue(this, true, this[SUBSCRIPTION_TYPE_FROZEN], copy);
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
Readonly.prototype.toJSON = function () {
|
|
37
|
-
return this[SYMBOL_STATE].value;
|
|
38
|
-
};
|
|
39
|
-
|
|
40
41
|
// #endregion
|
|
41
42
|
|
|
42
43
|
// #region Functions
|
|
43
44
|
|
|
44
|
-
export function getReadonlyInstance
|
|
45
|
+
export function getReadonlyInstance(
|
|
46
|
+
state: SignalState,
|
|
47
|
+
frozen: boolean,
|
|
48
|
+
): ReadonlySignal<unknown> | ReadonlyFrozenSignal<unknown> {
|
|
49
|
+
// @ts-expect-error All good, no worries :-)
|
|
50
|
+
return new Readonly(state, frozen);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function getReadonlySignal<Value>(
|
|
45
54
|
this: InternalSignal,
|
|
46
55
|
frozen?: never,
|
|
47
56
|
): ReadonlySignal<Value> {
|
|
@@ -49,7 +58,7 @@ export function getReadonlyInstance<Value>(
|
|
|
49
58
|
|
|
50
59
|
this[SYMBOL_STATE].readonlies ??= {};
|
|
51
60
|
|
|
52
|
-
this[SYMBOL_STATE].readonlies[key] ??=
|
|
61
|
+
this[SYMBOL_STATE].readonlies[key] ??= getReadonlyInstance(
|
|
53
62
|
this[SYMBOL_STATE],
|
|
54
63
|
frozen === true,
|
|
55
64
|
) as never;
|
|
@@ -57,16 +66,12 @@ export function getReadonlyInstance<Value>(
|
|
|
57
66
|
return this[SYMBOL_STATE].readonlies[key] as never;
|
|
58
67
|
}
|
|
59
68
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
frozen: boolean,
|
|
63
|
-
): ReadonlySignal<unknown> | ReadonlyFrozenSignal<unknown> {
|
|
64
|
-
// @ts-expect-error All good, no worries :-)
|
|
65
|
-
return new Readonly(state, frozen);
|
|
69
|
+
function getReadonlyValue(this: InternalReadonly) {
|
|
70
|
+
return handleReadonlyValue(this, false, this[SUBSCRIPTION_TYPE_FROZEN]);
|
|
66
71
|
}
|
|
67
72
|
|
|
68
|
-
function
|
|
69
|
-
instance:
|
|
73
|
+
function handleReadonlyValue(
|
|
74
|
+
instance: InternalReadonly,
|
|
70
75
|
peek: boolean,
|
|
71
76
|
frozen: boolean,
|
|
72
77
|
copy?: boolean,
|
|
@@ -82,4 +87,8 @@ function getReadonlyValue(
|
|
|
82
87
|
return frozen ? getFrozenValue(value) : value;
|
|
83
88
|
}
|
|
84
89
|
|
|
90
|
+
function peekReadonlyValue(this: InternalReadonly, copy?: boolean) {
|
|
91
|
+
return handleReadonlyValue(this, true, this[SUBSCRIPTION_TYPE_FROZEN], copy);
|
|
92
|
+
}
|
|
93
|
+
|
|
85
94
|
// #endregion
|
package/src/value/signal.ts
CHANGED
|
@@ -3,57 +3,53 @@ import {getState} from '../helpers/misc';
|
|
|
3
3
|
import {subscribeToSignal} from '../helpers/subscription';
|
|
4
4
|
import {
|
|
5
5
|
emitValue,
|
|
6
|
+
getJsonValue,
|
|
6
7
|
getSignalValue,
|
|
7
8
|
getStringValue,
|
|
8
|
-
|
|
9
|
+
handleSignal,
|
|
9
10
|
peekSignalValue,
|
|
10
|
-
|
|
11
|
+
updateSignal,
|
|
11
12
|
} from '../helpers/value';
|
|
12
|
-
import type {ReactiveOptions, Signal
|
|
13
|
-
import {
|
|
13
|
+
import type {InternalSignal, InternalStateful, ReactiveOptions, Signal} from '../models';
|
|
14
|
+
import {getReadonlySignal} from './readonly';
|
|
14
15
|
|
|
15
16
|
// #region Instance
|
|
16
17
|
|
|
17
18
|
function Signal(this: any, value: unknown, options?: ReactiveOptions<unknown>) {
|
|
18
19
|
this[SYMBOL_STATE] = getState(undefined, options);
|
|
19
20
|
|
|
20
|
-
|
|
21
|
+
handleSignal(this, value, setAndEmit);
|
|
21
22
|
}
|
|
22
23
|
|
|
23
24
|
Signal.prototype[NAME_MORA] = NAME_SIGNAL;
|
|
24
25
|
|
|
25
|
-
Signal.prototype.asReadonly =
|
|
26
|
+
Signal.prototype.asReadonly = getReadonlySignal;
|
|
26
27
|
Signal.prototype.get = getSignalValue;
|
|
27
28
|
Signal.prototype.peek = peekSignalValue;
|
|
29
|
+
Signal.prototype.set = setSignalValue;
|
|
28
30
|
Signal.prototype.subscribe = subscribeToSignal;
|
|
29
31
|
Signal.prototype.toString = getStringValue;
|
|
30
|
-
|
|
31
|
-
Signal.prototype.
|
|
32
|
-
return handleSignalValue(this, value, setAndEmit);
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
Signal.prototype.toJSON = function () {
|
|
36
|
-
return this[SYMBOL_STATE].value;
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
Signal.prototype.update = function (callback: never) {
|
|
40
|
-
return updateSignalValue(this, callback, setAndEmit);
|
|
41
|
-
};
|
|
32
|
+
Signal.prototype.toJSON = getJsonValue;
|
|
33
|
+
Signal.prototype.update = updateSignalValue;
|
|
42
34
|
|
|
43
35
|
// #endregion
|
|
44
36
|
|
|
45
37
|
// #region Functions
|
|
46
38
|
|
|
47
|
-
function setAndEmit(instance:
|
|
39
|
+
function setAndEmit(instance: InternalStateful, value: unknown): void {
|
|
48
40
|
const state = instance[SYMBOL_STATE];
|
|
49
41
|
|
|
50
|
-
if (!(state.equal ?? Object.is)(state.value
|
|
42
|
+
if (!(state.equal ?? Object.is)(state.value, value)) {
|
|
51
43
|
state.value = value;
|
|
52
44
|
|
|
53
45
|
emitValue(instance);
|
|
54
46
|
}
|
|
55
47
|
}
|
|
56
48
|
|
|
49
|
+
function setSignalValue(this: InternalSignal, value: unknown): void {
|
|
50
|
+
handleSignal(this, value, setAndEmit);
|
|
51
|
+
}
|
|
52
|
+
|
|
57
53
|
/**
|
|
58
54
|
* Create a reactive value from a function result
|
|
59
55
|
*
|
|
@@ -92,4 +88,8 @@ export function signal(value: unknown, options?: ReactiveOptions<unknown>): Sign
|
|
|
92
88
|
return new Signal(value, options);
|
|
93
89
|
}
|
|
94
90
|
|
|
91
|
+
function updateSignalValue(this: InternalSignal, callback: never): void {
|
|
92
|
+
updateSignal(this, callback, setAndEmit);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
95
|
// #endregion
|
package/src/value/store.ts
CHANGED
|
@@ -10,9 +10,9 @@ import {
|
|
|
10
10
|
updateProxyValue,
|
|
11
11
|
} from '../helpers/proxy';
|
|
12
12
|
import {subscribeToProxy} from '../helpers/subscription';
|
|
13
|
-
import {getStringValue} from '../helpers/value';
|
|
13
|
+
import {getJsonValue, getStringValue} from '../helpers/value';
|
|
14
14
|
import type {ReactiveOptions, ReactiveStore} from '../models';
|
|
15
|
-
import {
|
|
15
|
+
import {getReadonlySignal} from './readonly';
|
|
16
16
|
|
|
17
17
|
// #region Instance
|
|
18
18
|
|
|
@@ -34,19 +34,16 @@ function ReactiveStore(this: any, value: never, options?: ReactiveOptions<unknow
|
|
|
34
34
|
|
|
35
35
|
ReactiveStore.prototype[NAME_MORA] = NAME_STORE;
|
|
36
36
|
|
|
37
|
-
ReactiveStore.prototype.asReadonly =
|
|
37
|
+
ReactiveStore.prototype.asReadonly = getReadonlySignal;
|
|
38
38
|
ReactiveStore.prototype.get = getValueInProxy;
|
|
39
39
|
ReactiveStore.prototype.notify = emitProxyValues;
|
|
40
40
|
ReactiveStore.prototype.peek = peekValueInProxy;
|
|
41
41
|
ReactiveStore.prototype.set = setProxyValue;
|
|
42
42
|
ReactiveStore.prototype.subscribe = subscribeToProxy;
|
|
43
|
+
ReactiveStore.prototype.toJSON = getJsonValue;
|
|
43
44
|
ReactiveStore.prototype.toString = getStringValue;
|
|
44
45
|
ReactiveStore.prototype.update = updateProxyValue;
|
|
45
46
|
|
|
46
|
-
ReactiveStore.prototype.toJSON = function () {
|
|
47
|
-
return this[SYMBOL_STATE].value;
|
|
48
|
-
};
|
|
49
|
-
|
|
50
47
|
// #endregion
|
|
51
48
|
|
|
52
49
|
// #region Functions
|