@oscarpalmer/mora 0.34.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 +127 -111
- 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 +1 -1
- 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/dist/value/signal.mjs
CHANGED
|
@@ -1,28 +1,22 @@
|
|
|
1
1
|
import { NAME_MORA, NAME_SIGNAL, SYMBOL_STATE } from "../constants.mjs";
|
|
2
|
-
import { emitValue, getSignalValue, getStringValue,
|
|
2
|
+
import { emitValue, getJsonValue, getSignalValue, getStringValue, handleSignal, peekSignalValue, updateSignal } from "../helpers/value.mjs";
|
|
3
3
|
import { getState } from "../helpers/misc.mjs";
|
|
4
4
|
import { subscribeToSignal } from "../helpers/subscription.mjs";
|
|
5
|
-
import {
|
|
5
|
+
import { getReadonlySignal } from "./readonly.mjs";
|
|
6
6
|
//#region src/value/signal.ts
|
|
7
7
|
function Signal(value, options) {
|
|
8
8
|
this[SYMBOL_STATE] = getState(void 0, options);
|
|
9
|
-
|
|
9
|
+
handleSignal(this, value, setAndEmit);
|
|
10
10
|
}
|
|
11
11
|
Signal.prototype[NAME_MORA] = NAME_SIGNAL;
|
|
12
|
-
Signal.prototype.asReadonly =
|
|
12
|
+
Signal.prototype.asReadonly = getReadonlySignal;
|
|
13
13
|
Signal.prototype.get = getSignalValue;
|
|
14
14
|
Signal.prototype.peek = peekSignalValue;
|
|
15
|
+
Signal.prototype.set = setSignalValue;
|
|
15
16
|
Signal.prototype.subscribe = subscribeToSignal;
|
|
16
17
|
Signal.prototype.toString = getStringValue;
|
|
17
|
-
Signal.prototype.
|
|
18
|
-
|
|
19
|
-
};
|
|
20
|
-
Signal.prototype.toJSON = function() {
|
|
21
|
-
return this[SYMBOL_STATE].value;
|
|
22
|
-
};
|
|
23
|
-
Signal.prototype.update = function(callback) {
|
|
24
|
-
return updateSignalValue(this, callback, setAndEmit);
|
|
25
|
-
};
|
|
18
|
+
Signal.prototype.toJSON = getJsonValue;
|
|
19
|
+
Signal.prototype.update = updateSignalValue;
|
|
26
20
|
function setAndEmit(instance, value) {
|
|
27
21
|
const state = instance[SYMBOL_STATE];
|
|
28
22
|
if (!(state.equal ?? Object.is)(state.value, value)) {
|
|
@@ -30,8 +24,14 @@ function setAndEmit(instance, value) {
|
|
|
30
24
|
emitValue(instance);
|
|
31
25
|
}
|
|
32
26
|
}
|
|
27
|
+
function setSignalValue(value) {
|
|
28
|
+
handleSignal(this, value, setAndEmit);
|
|
29
|
+
}
|
|
33
30
|
function signal(value, options) {
|
|
34
31
|
return new Signal(value, options);
|
|
35
32
|
}
|
|
33
|
+
function updateSignalValue(callback) {
|
|
34
|
+
updateSignal(this, callback, setAndEmit);
|
|
35
|
+
}
|
|
36
36
|
//#endregion
|
|
37
37
|
export { signal };
|
package/dist/value/store.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { NAME_MORA, NAME_STORE, SYMBOL_STATE } from "../constants.mjs";
|
|
2
|
-
import { getStringValue } from "../helpers/value.mjs";
|
|
2
|
+
import { getJsonValue, getStringValue } from "../helpers/value.mjs";
|
|
3
3
|
import { getState } from "../helpers/misc.mjs";
|
|
4
4
|
import { subscribeToProxy } from "../helpers/subscription.mjs";
|
|
5
5
|
import { emitProxyValues, getValueInProxy, peekValueInProxy, setProxyValue, setValueInProxy, updateProxyValue } from "../helpers/proxy.mjs";
|
|
6
|
-
import {
|
|
6
|
+
import { getReadonlySignal } from "./readonly.mjs";
|
|
7
7
|
//#region src/value/store.ts
|
|
8
8
|
function ReactiveStore(value, options) {
|
|
9
9
|
this[SYMBOL_STATE] = {
|
|
@@ -14,17 +14,15 @@ function ReactiveStore(value, options) {
|
|
|
14
14
|
setProxyValue.call(this, value);
|
|
15
15
|
}
|
|
16
16
|
ReactiveStore.prototype[NAME_MORA] = NAME_STORE;
|
|
17
|
-
ReactiveStore.prototype.asReadonly =
|
|
17
|
+
ReactiveStore.prototype.asReadonly = getReadonlySignal;
|
|
18
18
|
ReactiveStore.prototype.get = getValueInProxy;
|
|
19
19
|
ReactiveStore.prototype.notify = emitProxyValues;
|
|
20
20
|
ReactiveStore.prototype.peek = peekValueInProxy;
|
|
21
21
|
ReactiveStore.prototype.set = setProxyValue;
|
|
22
22
|
ReactiveStore.prototype.subscribe = subscribeToProxy;
|
|
23
|
+
ReactiveStore.prototype.toJSON = getJsonValue;
|
|
23
24
|
ReactiveStore.prototype.toString = getStringValue;
|
|
24
25
|
ReactiveStore.prototype.update = updateProxyValue;
|
|
25
|
-
ReactiveStore.prototype.toJSON = function() {
|
|
26
|
-
return this[SYMBOL_STATE].value;
|
|
27
|
-
};
|
|
28
26
|
function store(value, options) {
|
|
29
27
|
return new ReactiveStore(value, options);
|
|
30
28
|
}
|
package/package.json
CHANGED
package/src/batch.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {BATCH} from './constants';
|
|
2
2
|
import {runEffect} from './effect';
|
|
3
3
|
import {getFrozenValue, peekSignalValue} from './helpers/value';
|
|
4
|
-
import type {
|
|
4
|
+
import type {StoredSubscription} from './models';
|
|
5
5
|
|
|
6
6
|
// #region Functions
|
|
7
7
|
|
|
@@ -30,7 +30,7 @@ export function flushHandlers(): void {
|
|
|
30
30
|
: peekSignalValue.call(subscription.instance, subscription.copy),
|
|
31
31
|
);
|
|
32
32
|
} else {
|
|
33
|
-
runEffect(handler
|
|
33
|
+
runEffect(handler);
|
|
34
34
|
}
|
|
35
35
|
}
|
|
36
36
|
}
|
package/src/constants.ts
CHANGED
|
@@ -16,9 +16,14 @@ export const BATCH: Batch = {
|
|
|
16
16
|
handlers: new Map(),
|
|
17
17
|
};
|
|
18
18
|
|
|
19
|
-
export const METHODS_AFFECTING_LENGTH = new Set<string>([
|
|
19
|
+
export const METHODS_AFFECTING_LENGTH = new Set<string | symbol>([
|
|
20
|
+
'pop',
|
|
21
|
+
'push',
|
|
22
|
+
'shift',
|
|
23
|
+
'unshift',
|
|
24
|
+
]);
|
|
20
25
|
|
|
21
|
-
export const METHODS_UPDATE = new Set<string>([
|
|
26
|
+
export const METHODS_UPDATE = new Set<string | symbol>([
|
|
22
27
|
...METHODS_AFFECTING_LENGTH,
|
|
23
28
|
'copyWithin',
|
|
24
29
|
'fill',
|
package/src/helpers/is.ts
CHANGED
|
@@ -46,9 +46,7 @@ function isMora<Instance>(value: unknown, name: string | Set<string>): value is
|
|
|
46
46
|
typeof value === 'object' &&
|
|
47
47
|
value != null &&
|
|
48
48
|
NAME_MORA in value &&
|
|
49
|
-
(typeof name === 'string'
|
|
50
|
-
? (value as PlainObject)[NAME_MORA] === name
|
|
51
|
-
: name.has((value as PlainObject)[NAME_MORA] as never))
|
|
49
|
+
(typeof name === 'string' ? value[NAME_MORA] === name : name.has(value[NAME_MORA] as never))
|
|
52
50
|
);
|
|
53
51
|
}
|
|
54
52
|
|
package/src/helpers/proxy.ts
CHANGED
|
@@ -1,20 +1,19 @@
|
|
|
1
1
|
import {isKey, isPlainObject} from '@oscarpalmer/atoms/is';
|
|
2
|
-
import type {
|
|
2
|
+
import type {
|
|
3
|
+
ArrayOrPlainObject,
|
|
4
|
+
GenericCallback,
|
|
5
|
+
Key,
|
|
6
|
+
PlainObject,
|
|
7
|
+
} from '@oscarpalmer/atoms/models';
|
|
3
8
|
import {startBatch, stopBatch} from '../batch';
|
|
4
9
|
import {PROPERTY_LENGTH, SYMBOL_STATE} from '../constants';
|
|
5
|
-
import type {
|
|
6
|
-
Computed,
|
|
7
|
-
ReactiveArray,
|
|
8
|
-
ReactiveProxyState,
|
|
9
|
-
ReactiveStore,
|
|
10
|
-
InternalProxy,
|
|
11
|
-
} from '../models';
|
|
10
|
+
import type {InternalArray, InternalComputed, InternalStore, ReactiveProxyState} from '../models';
|
|
12
11
|
import {internalComputed} from '../value/computed';
|
|
13
12
|
import {emitValue, getSignalValue, peekSignalValue} from './value';
|
|
14
13
|
|
|
15
14
|
// #region Functions
|
|
16
15
|
|
|
17
|
-
export function emitProxyValues(this:
|
|
16
|
+
export function emitProxyValues(this: InternalArray | InternalStore): void {
|
|
18
17
|
const state = this[SYMBOL_STATE];
|
|
19
18
|
|
|
20
19
|
state.mapped ??= new Map();
|
|
@@ -30,10 +29,10 @@ export function emitProxyValues(this: InternalProxy): void {
|
|
|
30
29
|
}
|
|
31
30
|
|
|
32
31
|
export function getReactiveValueInProxy(
|
|
33
|
-
instance:
|
|
32
|
+
instance: InternalArray | InternalStore,
|
|
34
33
|
key: Key,
|
|
35
|
-
):
|
|
36
|
-
const state =
|
|
34
|
+
): InternalComputed {
|
|
35
|
+
const state = instance[SYMBOL_STATE];
|
|
37
36
|
|
|
38
37
|
state.mapped ??= new Map();
|
|
39
38
|
|
|
@@ -54,19 +53,29 @@ export function getReactiveValueInProxy(
|
|
|
54
53
|
return item[0];
|
|
55
54
|
}
|
|
56
55
|
|
|
57
|
-
export function getValueInProxy(this:
|
|
56
|
+
export function getValueInProxy(this: InternalArray | InternalStore, first?: unknown): unknown {
|
|
57
|
+
let signal: InternalComputed | undefined;
|
|
58
|
+
|
|
58
59
|
if (this[SYMBOL_STATE].isArray ? typeof first === 'number' : isKey(first)) {
|
|
59
|
-
|
|
60
|
+
signal = getReactiveValueInProxy(this, first as Key);
|
|
60
61
|
}
|
|
61
62
|
|
|
62
|
-
return getSignalValue.call(this);
|
|
63
|
+
return getSignalValue.call(signal ?? this);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function isProxyKey(isArray: boolean, value: unknown): value is Key {
|
|
67
|
+
return isArray ? typeof value === 'number' : isKey(value);
|
|
63
68
|
}
|
|
64
69
|
|
|
65
70
|
function isProxyObject(isArray: boolean, value: unknown): value is ArrayOrPlainObject {
|
|
66
71
|
return value == null || (isArray ? Array.isArray(value) : isPlainObject(value));
|
|
67
72
|
}
|
|
68
73
|
|
|
69
|
-
export function peekValueInProxy(
|
|
74
|
+
export function peekValueInProxy(
|
|
75
|
+
this: InternalArray | InternalStore,
|
|
76
|
+
first?: unknown,
|
|
77
|
+
second?: boolean,
|
|
78
|
+
): unknown {
|
|
70
79
|
const state = this[SYMBOL_STATE];
|
|
71
80
|
|
|
72
81
|
let value: unknown;
|
|
@@ -84,40 +93,38 @@ export function peekValueInProxy(this: InternalProxy, first?: unknown, second?:
|
|
|
84
93
|
|
|
85
94
|
function setProxyObject(state: ReactiveProxyState, value: unknown): void {
|
|
86
95
|
if (state.isArray) {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
...((value ?? []) as unknown[]),
|
|
91
|
-
);
|
|
96
|
+
const values = state.value as unknown[];
|
|
97
|
+
|
|
98
|
+
values.splice(0, values.length, ...((value ?? []) as unknown[]));
|
|
92
99
|
|
|
93
100
|
return;
|
|
94
101
|
}
|
|
95
102
|
|
|
96
103
|
startBatch();
|
|
97
104
|
|
|
98
|
-
const
|
|
99
|
-
const
|
|
105
|
+
const current = state.value as PlainObject;
|
|
106
|
+
const next = (value as Record<string, unknown>) ?? {};
|
|
100
107
|
|
|
101
|
-
const
|
|
102
|
-
const
|
|
108
|
+
const currentKeys = Object.keys(current);
|
|
109
|
+
const nextKeys = Object.keys(next);
|
|
103
110
|
|
|
104
|
-
let {length} =
|
|
111
|
+
let {length} = currentKeys;
|
|
105
112
|
|
|
106
113
|
for (let index = 0; index < length; index += 1) {
|
|
107
|
-
const key =
|
|
114
|
+
const key = currentKeys[index];
|
|
108
115
|
|
|
109
|
-
|
|
116
|
+
current[key] = nextKeys.includes(key) ? next[key] : undefined;
|
|
110
117
|
}
|
|
111
118
|
|
|
112
|
-
length =
|
|
119
|
+
length = nextKeys.length;
|
|
113
120
|
|
|
114
121
|
for (let index = 0; index < length; index += 1) {
|
|
115
|
-
const key =
|
|
122
|
+
const key = nextKeys[index];
|
|
116
123
|
|
|
117
|
-
if (!
|
|
118
|
-
const keyedValue =
|
|
124
|
+
if (!currentKeys.includes(key)) {
|
|
125
|
+
const keyedValue = next[key];
|
|
119
126
|
|
|
120
|
-
|
|
127
|
+
current[key] = keyedValue;
|
|
121
128
|
}
|
|
122
129
|
}
|
|
123
130
|
|
|
@@ -131,18 +138,22 @@ function setProxyProperty(state: ReactiveProxyState, property: unknown, value: u
|
|
|
131
138
|
return;
|
|
132
139
|
}
|
|
133
140
|
|
|
134
|
-
const
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
141
|
+
const array = state.value as unknown[];
|
|
142
|
+
const index = property as number;
|
|
143
|
+
|
|
144
|
+
const actual = index < 0 ? array.length + index : index;
|
|
138
145
|
|
|
139
146
|
if (actual > -1) {
|
|
140
|
-
|
|
147
|
+
array[actual] = value;
|
|
141
148
|
}
|
|
142
149
|
}
|
|
143
150
|
|
|
144
|
-
export function setProxyValue(
|
|
145
|
-
|
|
151
|
+
export function setProxyValue(
|
|
152
|
+
this: InternalArray | InternalStore,
|
|
153
|
+
first?: unknown,
|
|
154
|
+
second?: unknown,
|
|
155
|
+
): void {
|
|
156
|
+
const state = this[SYMBOL_STATE];
|
|
146
157
|
|
|
147
158
|
if (state.isArray && first === PROPERTY_LENGTH) {
|
|
148
159
|
(state.value as unknown[]).length = second as number;
|
|
@@ -156,55 +167,55 @@ export function setProxyValue(this: InternalProxy, first?: unknown, second?: unk
|
|
|
156
167
|
return;
|
|
157
168
|
}
|
|
158
169
|
|
|
159
|
-
const
|
|
170
|
+
const keyed = isProxyKey(state.isArray, first);
|
|
160
171
|
|
|
161
|
-
if (state.isArray &&
|
|
172
|
+
if (state.isArray && keyed && Number.isNaN(first)) {
|
|
162
173
|
return;
|
|
163
174
|
}
|
|
164
175
|
|
|
165
|
-
let actual =
|
|
176
|
+
let actual = keyed ? second : first;
|
|
166
177
|
|
|
167
178
|
if (typeof actual === 'function') {
|
|
168
179
|
try {
|
|
169
|
-
actual = (actual as
|
|
180
|
+
actual = (actual as GenericCallback)();
|
|
170
181
|
} catch {
|
|
171
182
|
return;
|
|
172
183
|
}
|
|
173
184
|
}
|
|
174
185
|
|
|
175
186
|
if (actual instanceof Promise) {
|
|
176
|
-
if (
|
|
187
|
+
if (keyed) {
|
|
177
188
|
state.promises ??= new Map();
|
|
178
189
|
|
|
179
|
-
state.promises.set(first
|
|
190
|
+
state.promises.set(first, actual as Promise<never>);
|
|
180
191
|
} else {
|
|
181
192
|
state.promise = actual as Promise<never>;
|
|
182
193
|
}
|
|
183
194
|
|
|
184
195
|
void actual
|
|
185
196
|
.then(value => {
|
|
186
|
-
if (
|
|
187
|
-
state.promises
|
|
197
|
+
if (keyed && state.promises?.get(first) === actual) {
|
|
198
|
+
state.promises?.delete(first);
|
|
188
199
|
|
|
189
200
|
setProxyProperty(state, first, value);
|
|
190
201
|
|
|
191
202
|
return;
|
|
192
203
|
}
|
|
193
204
|
|
|
194
|
-
if (!
|
|
205
|
+
if (!keyed && isProxyObject(state.isArray, value) && state.promise === actual) {
|
|
195
206
|
state.promise = undefined;
|
|
196
207
|
|
|
197
208
|
setProxyObject(state, value);
|
|
198
209
|
}
|
|
199
210
|
})
|
|
200
211
|
.catch(() => {
|
|
201
|
-
if (
|
|
202
|
-
state.promises
|
|
203
|
-
} else if (!
|
|
212
|
+
if (keyed && state.promises?.get(first) === actual) {
|
|
213
|
+
state.promises?.delete(first);
|
|
214
|
+
} else if (!keyed && state.promise === actual) {
|
|
204
215
|
state.promise = undefined;
|
|
205
216
|
}
|
|
206
217
|
});
|
|
207
|
-
} else if (
|
|
218
|
+
} else if (keyed) {
|
|
208
219
|
setProxyProperty(state, first, actual);
|
|
209
220
|
} else if (isProxyObject(state.isArray, actual)) {
|
|
210
221
|
setProxyObject(state, actual);
|
|
@@ -212,7 +223,7 @@ export function setProxyValue(this: InternalProxy, first?: unknown, second?: unk
|
|
|
212
223
|
}
|
|
213
224
|
|
|
214
225
|
export function setValueInProxy(
|
|
215
|
-
instance:
|
|
226
|
+
instance: InternalArray | InternalStore,
|
|
216
227
|
target: object,
|
|
217
228
|
property: PropertyKey,
|
|
218
229
|
value: unknown,
|
|
@@ -228,9 +239,9 @@ export function setValueInProxy(
|
|
|
228
239
|
}
|
|
229
240
|
}
|
|
230
241
|
|
|
231
|
-
const previous = Reflect.get(target
|
|
242
|
+
const previous = Reflect.get(target, property);
|
|
232
243
|
|
|
233
|
-
if (!(state.equal ?? Object.is)(previous
|
|
244
|
+
if (!(state.equal ?? Object.is)(previous, value)) {
|
|
234
245
|
Reflect.set(target, property, value);
|
|
235
246
|
|
|
236
247
|
emitValue(instance);
|
|
@@ -243,7 +254,7 @@ export function setValueInProxy(
|
|
|
243
254
|
return true;
|
|
244
255
|
}
|
|
245
256
|
|
|
246
|
-
export function updateProxyValue(this:
|
|
257
|
+
export function updateProxyValue(this: InternalArray | InternalStore, callback: unknown): void {
|
|
247
258
|
if (typeof callback !== 'function') {
|
|
248
259
|
throw new TypeError('Callback must be a function');
|
|
249
260
|
}
|
|
@@ -12,9 +12,10 @@ import {
|
|
|
12
12
|
SYMBOL_STATE,
|
|
13
13
|
} from '../constants';
|
|
14
14
|
import type {
|
|
15
|
-
|
|
15
|
+
InternalArray,
|
|
16
16
|
InternalReadonly,
|
|
17
|
-
|
|
17
|
+
InternalStateful,
|
|
18
|
+
InternalStore,
|
|
18
19
|
Subscriber,
|
|
19
20
|
SubscriptionType,
|
|
20
21
|
} from '../models';
|
|
@@ -24,20 +25,20 @@ import {getFrozenValue, peekSignalValue} from './value';
|
|
|
24
25
|
// #region Functions
|
|
25
26
|
|
|
26
27
|
export function subscribeToProxy(
|
|
27
|
-
this:
|
|
28
|
+
this: InternalArray | InternalStore,
|
|
28
29
|
first: Key | Subscriber<unknown>,
|
|
29
30
|
second?: Subscriber<unknown> | boolean,
|
|
30
31
|
third?: boolean,
|
|
31
32
|
): Subscription {
|
|
32
33
|
if (isKey(first)) {
|
|
33
|
-
return getReactiveValueInProxy(this
|
|
34
|
+
return getReactiveValueInProxy(this, first).subscribe(second as Subscriber<unknown>, third);
|
|
34
35
|
}
|
|
35
36
|
|
|
36
37
|
return subscribeToSignal.call(this, first, second);
|
|
37
38
|
}
|
|
38
39
|
|
|
39
40
|
function subscribeToReactive(
|
|
40
|
-
this:
|
|
41
|
+
this: InternalStateful,
|
|
41
42
|
type: SubscriptionType,
|
|
42
43
|
subscriber: Subscriber<unknown>,
|
|
43
44
|
): Subscription {
|
|
@@ -75,7 +76,7 @@ export function subscribeToReadonly(
|
|
|
75
76
|
}
|
|
76
77
|
|
|
77
78
|
export function subscribeToSignal(
|
|
78
|
-
this:
|
|
79
|
+
this: InternalStateful,
|
|
79
80
|
subscriber: Subscriber<unknown>,
|
|
80
81
|
copy?: unknown,
|
|
81
82
|
): Subscription {
|
package/src/helpers/value.ts
CHANGED
|
@@ -6,16 +6,16 @@ import {
|
|
|
6
6
|
ARRAY_PEEK,
|
|
7
7
|
ARRAY_THRESHOLD,
|
|
8
8
|
BATCH,
|
|
9
|
-
SUBSCRIPTION_TYPES_COPY,
|
|
10
9
|
SUBSCRIPTION_TYPE_FROZEN,
|
|
11
10
|
SUBSCRIPTION_TYPES,
|
|
11
|
+
SUBSCRIPTION_TYPES_COPY,
|
|
12
12
|
SYMBOL_STATE,
|
|
13
13
|
} from '../constants';
|
|
14
|
-
import type {
|
|
14
|
+
import type {InternalStateful} from '../models';
|
|
15
15
|
|
|
16
16
|
// #region Functions
|
|
17
17
|
|
|
18
|
-
export function emitValue(instance:
|
|
18
|
+
export function emitValue(instance: InternalStateful): void {
|
|
19
19
|
const state = instance[SYMBOL_STATE];
|
|
20
20
|
|
|
21
21
|
if (state.computeds != null && state.computeds.size > 0) {
|
|
@@ -51,14 +51,18 @@ export function emitValue(instance: InternalSignal): void {
|
|
|
51
51
|
}
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
export function equalArrays(
|
|
54
|
+
export function equalArrays(
|
|
55
|
+
first: unknown[],
|
|
56
|
+
second: unknown[],
|
|
57
|
+
equal?: (first: unknown, second: unknown) => boolean,
|
|
58
|
+
): boolean {
|
|
55
59
|
const {length} = first;
|
|
56
60
|
|
|
57
61
|
if (length !== second.length) {
|
|
58
62
|
return false;
|
|
59
63
|
}
|
|
60
64
|
|
|
61
|
-
const eq =
|
|
65
|
+
const eq = equal ?? Object.is;
|
|
62
66
|
|
|
63
67
|
let offset = 0;
|
|
64
68
|
|
|
@@ -101,32 +105,38 @@ export function getFrozenValue(value: unknown): unknown {
|
|
|
101
105
|
return frozen;
|
|
102
106
|
}
|
|
103
107
|
|
|
104
|
-
export function
|
|
108
|
+
export function getJsonValue(this: InternalStateful): unknown {
|
|
109
|
+
return this[SYMBOL_STATE].value;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function getSignalValue(this: InternalStateful): unknown {
|
|
113
|
+
const state = this[SYMBOL_STATE];
|
|
114
|
+
|
|
105
115
|
if (ACTIVE.computed != null) {
|
|
106
|
-
|
|
116
|
+
state.computeds ??= new Set();
|
|
107
117
|
|
|
108
|
-
|
|
118
|
+
state.computeds.add(ACTIVE.computed);
|
|
109
119
|
}
|
|
110
120
|
|
|
111
121
|
if (ACTIVE.effect != null) {
|
|
112
|
-
|
|
122
|
+
state.effects ??= new Set();
|
|
113
123
|
|
|
114
|
-
|
|
124
|
+
state.effects.add(ACTIVE.effect);
|
|
115
125
|
}
|
|
116
126
|
|
|
117
|
-
return
|
|
127
|
+
return state.value;
|
|
118
128
|
}
|
|
119
129
|
|
|
120
|
-
export function getStringValue(this:
|
|
130
|
+
export function getStringValue(this: InternalStateful, json?: boolean): string {
|
|
121
131
|
return json === true
|
|
122
132
|
? JSON.stringify(this[SYMBOL_STATE].value)
|
|
123
133
|
: String(this[SYMBOL_STATE].value);
|
|
124
134
|
}
|
|
125
135
|
|
|
126
|
-
export function
|
|
127
|
-
instance:
|
|
136
|
+
export function handleSignal(
|
|
137
|
+
instance: InternalStateful,
|
|
128
138
|
origin: unknown,
|
|
129
|
-
setValue: (instance:
|
|
139
|
+
setValue: (instance: InternalStateful, value: unknown) => void,
|
|
130
140
|
onAfter?: () => void,
|
|
131
141
|
): void {
|
|
132
142
|
const state = instance[SYMBOL_STATE];
|
|
@@ -161,7 +171,7 @@ export function handleSignalValue(
|
|
|
161
171
|
}
|
|
162
172
|
|
|
163
173
|
export function peekSignalValue(
|
|
164
|
-
this:
|
|
174
|
+
this: InternalStateful | [InternalStateful, unknown],
|
|
165
175
|
copy?: boolean,
|
|
166
176
|
): unknown {
|
|
167
177
|
let peeked: unknown = Array.isArray(this) ? this[1] : this[SYMBOL_STATE].value;
|
|
@@ -179,16 +189,16 @@ export function peekSignalValue(
|
|
|
179
189
|
return peeked;
|
|
180
190
|
}
|
|
181
191
|
|
|
182
|
-
export function
|
|
183
|
-
instance:
|
|
192
|
+
export function updateSignal(
|
|
193
|
+
instance: InternalStateful,
|
|
184
194
|
callback: (value: unknown) => unknown,
|
|
185
|
-
setValue: (instance:
|
|
195
|
+
setValue: (instance: InternalStateful, value: unknown) => void,
|
|
186
196
|
): void {
|
|
187
197
|
if (typeof callback !== 'function') {
|
|
188
198
|
throw new TypeError('Callback must be a function');
|
|
189
199
|
}
|
|
190
200
|
|
|
191
|
-
|
|
201
|
+
handleSignal(instance, callback(instance[SYMBOL_STATE].value), setValue);
|
|
192
202
|
}
|
|
193
203
|
|
|
194
204
|
// #endregion
|