@oscarpalmer/mora 0.17.0 → 0.18.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/helpers/value.cjs +5 -5
- package/dist/helpers/value.js +5 -5
- package/dist/mora.full.js +23 -27
- package/dist/value/array.cjs +6 -7
- package/dist/value/array.js +6 -7
- package/dist/value/computed.cjs +5 -5
- package/dist/value/computed.js +5 -5
- package/dist/value/reactive.cjs +1 -5
- package/dist/value/reactive.js +1 -5
- package/dist/value/signal.cjs +5 -5
- package/dist/value/signal.js +5 -5
- package/package.json +1 -1
- package/src/helpers/is.ts +7 -1
- package/src/helpers/proxy.ts +72 -0
- package/src/helpers/value.ts +23 -33
- package/src/index.ts +1 -0
- package/src/value/array.ts +11 -30
- package/src/value/signal.ts +2 -2
- package/src/value/store.ts +90 -0
- package/types/batch.d.cts +7 -14
- package/types/helpers/is.d.cts +10 -17
- package/types/helpers/value.d.cts +11 -18
- package/types/helpers/value.d.ts +4 -4
- package/types/index.d.cts +13 -20
- package/types/subscription.d.cts +9 -16
- package/types/subscription.d.ts +4 -4
- package/types/value/array.d.cts +10 -17
- package/types/value/array.d.ts +4 -4
- package/types/value/computed.d.cts +9 -16
- package/types/value/computed.d.ts +4 -4
- package/types/value/reactive.d.cts +7 -14
- package/types/value/reactive.d.ts +4 -11
- package/types/value/signal.d.cts +9 -16
- package/types/value/signal.d.ts +3 -3
package/dist/helpers/value.cjs
CHANGED
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
3
3
|
const batch = require("../batch.cjs");
|
|
4
4
|
const effect = require("../effect.cjs");
|
|
5
5
|
const value_computed = require("../value/computed.cjs");
|
|
6
|
-
function differentArrays(
|
|
6
|
+
function differentArrays(first, second) {
|
|
7
7
|
let { length } = first;
|
|
8
8
|
if (length !== second.length) {
|
|
9
9
|
return true;
|
|
@@ -13,21 +13,21 @@ function differentArrays(state, first, second) {
|
|
|
13
13
|
offset = Math.round(length / 10);
|
|
14
14
|
offset = offset > 25 ? 25 : offset;
|
|
15
15
|
for (let index = 0; index < offset; index += 1) {
|
|
16
|
-
if (!
|
|
16
|
+
if (!Object.is(first[index], second[index])) {
|
|
17
17
|
return true;
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
length -= offset;
|
|
22
22
|
for (let index = offset; index < length; index += 1) {
|
|
23
|
-
if (!
|
|
23
|
+
if (!Object.is(first[index], second[index])) {
|
|
24
24
|
return true;
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
27
|
return false;
|
|
28
28
|
}
|
|
29
|
-
function differentValues(
|
|
30
|
-
return Array.isArray(first) && Array.isArray(second) ? differentArrays(
|
|
29
|
+
function differentValues(first, second) {
|
|
30
|
+
return Array.isArray(first) && Array.isArray(second) ? differentArrays(first, second) : !Object.is(first, second);
|
|
31
31
|
}
|
|
32
32
|
function emitValue(state) {
|
|
33
33
|
for (const computed of state.computeds) {
|
package/dist/helpers/value.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { batchedHandlers, batchDepth, flushHandlers } from "../batch.js";
|
|
2
2
|
import { activeEffect } from "../effect.js";
|
|
3
3
|
import { activeComputed } from "../value/computed.js";
|
|
4
|
-
function differentArrays(
|
|
4
|
+
function differentArrays(first, second) {
|
|
5
5
|
let { length } = first;
|
|
6
6
|
if (length !== second.length) {
|
|
7
7
|
return true;
|
|
@@ -11,21 +11,21 @@ function differentArrays(state, first, second) {
|
|
|
11
11
|
offset = Math.round(length / 10);
|
|
12
12
|
offset = offset > 25 ? 25 : offset;
|
|
13
13
|
for (let index = 0; index < offset; index += 1) {
|
|
14
|
-
if (!
|
|
14
|
+
if (!Object.is(first[index], second[index])) {
|
|
15
15
|
return true;
|
|
16
16
|
}
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
length -= offset;
|
|
20
20
|
for (let index = offset; index < length; index += 1) {
|
|
21
|
-
if (!
|
|
21
|
+
if (!Object.is(first[index], second[index])) {
|
|
22
22
|
return true;
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
25
|
return false;
|
|
26
26
|
}
|
|
27
|
-
function differentValues(
|
|
28
|
-
return Array.isArray(first) && Array.isArray(second) ? differentArrays(
|
|
27
|
+
function differentValues(first, second) {
|
|
28
|
+
return Array.isArray(first) && Array.isArray(second) ? differentArrays(first, second) : !Object.is(first, second);
|
|
29
29
|
}
|
|
30
30
|
function emitValue(state) {
|
|
31
31
|
for (const computed of state.computeds) {
|
package/dist/mora.full.js
CHANGED
|
@@ -132,15 +132,11 @@ class Reactive {
|
|
|
132
132
|
state = {
|
|
133
133
|
computeds: new Set(),
|
|
134
134
|
effects: new Set(),
|
|
135
|
-
equal: Object.is,
|
|
136
135
|
subscriptions: new Map(),
|
|
137
136
|
value: undefined,
|
|
138
137
|
};
|
|
139
|
-
constructor(name, value
|
|
138
|
+
constructor(name, value) {
|
|
140
139
|
this.state.value = value;
|
|
141
|
-
if (typeof options === 'object' && typeof options.equal === 'function') {
|
|
142
|
-
this.state.equal = options.equal;
|
|
143
|
-
}
|
|
144
140
|
Object.defineProperty(this, '$mora', {
|
|
145
141
|
value: name,
|
|
146
142
|
});
|
|
@@ -183,15 +179,15 @@ class Computed extends Reactive {
|
|
|
183
179
|
dirty: true,
|
|
184
180
|
instance: undefined,
|
|
185
181
|
};
|
|
186
|
-
constructor(callback
|
|
187
|
-
super(computedName, undefined
|
|
182
|
+
constructor(callback) {
|
|
183
|
+
super(computedName, undefined);
|
|
188
184
|
this.effect.instance = effect(() => {
|
|
189
185
|
if (this.effect.dirty) {
|
|
190
186
|
const previousComputed = activeComputed;
|
|
191
187
|
activeComputed = this;
|
|
192
188
|
const value = callback();
|
|
193
189
|
activeComputed = previousComputed;
|
|
194
|
-
if (!
|
|
190
|
+
if (!Object.is(this.state.value, value)) {
|
|
195
191
|
this.state.value = value;
|
|
196
192
|
for (const computed of this.state.computeds) {
|
|
197
193
|
computed.effect.dirty = true;
|
|
@@ -226,11 +222,11 @@ class Computed extends Reactive {
|
|
|
226
222
|
/**
|
|
227
223
|
* Create a computed value
|
|
228
224
|
*/
|
|
229
|
-
function computed(callback
|
|
230
|
-
return new Computed(callback
|
|
225
|
+
function computed(callback) {
|
|
226
|
+
return new Computed(callback);
|
|
231
227
|
}
|
|
232
228
|
|
|
233
|
-
function differentArrays(
|
|
229
|
+
function differentArrays(first, second) {
|
|
234
230
|
let { length } = first;
|
|
235
231
|
if (length !== second.length) {
|
|
236
232
|
return true;
|
|
@@ -240,23 +236,23 @@ function differentArrays(state, first, second) {
|
|
|
240
236
|
offset = Math.round(length / 10);
|
|
241
237
|
offset = offset > 25 ? 25 : offset;
|
|
242
238
|
for (let index = 0; index < offset; index += 1) {
|
|
243
|
-
if (!
|
|
239
|
+
if (!Object.is(first[index], second[index])) {
|
|
244
240
|
return true;
|
|
245
241
|
}
|
|
246
242
|
}
|
|
247
243
|
}
|
|
248
244
|
length -= offset;
|
|
249
245
|
for (let index = offset; index < length; index += 1) {
|
|
250
|
-
if (!
|
|
246
|
+
if (!Object.is(first[index], second[index])) {
|
|
251
247
|
return true;
|
|
252
248
|
}
|
|
253
249
|
}
|
|
254
250
|
return false;
|
|
255
251
|
}
|
|
256
|
-
function differentValues(
|
|
252
|
+
function differentValues(first, second) {
|
|
257
253
|
return Array.isArray(first) && Array.isArray(second)
|
|
258
|
-
? differentArrays(
|
|
259
|
-
: !
|
|
254
|
+
? differentArrays(first, second)
|
|
255
|
+
: !Object.is(first, second);
|
|
260
256
|
}
|
|
261
257
|
function emitValue(state) {
|
|
262
258
|
for (const computed of state.computeds) {
|
|
@@ -283,8 +279,8 @@ function getValue(state) {
|
|
|
283
279
|
}
|
|
284
280
|
|
|
285
281
|
class Signal extends Reactive {
|
|
286
|
-
constructor(value
|
|
287
|
-
super(signalName, value
|
|
282
|
+
constructor(value) {
|
|
283
|
+
super(signalName, value);
|
|
288
284
|
}
|
|
289
285
|
/**
|
|
290
286
|
* @inheritdoc
|
|
@@ -296,7 +292,7 @@ class Signal extends Reactive {
|
|
|
296
292
|
* Set the value
|
|
297
293
|
*/
|
|
298
294
|
set(value) {
|
|
299
|
-
if (differentValues(this.state
|
|
295
|
+
if (differentValues(this.state.value, value)) {
|
|
300
296
|
this.state.value = value;
|
|
301
297
|
emitValue(this.state);
|
|
302
298
|
}
|
|
@@ -311,8 +307,8 @@ class Signal extends Reactive {
|
|
|
311
307
|
/**
|
|
312
308
|
* Create a reactive value
|
|
313
309
|
*/
|
|
314
|
-
function signal(value
|
|
315
|
-
return new Signal(value
|
|
310
|
+
function signal(value) {
|
|
311
|
+
return new Signal(value);
|
|
316
312
|
}
|
|
317
313
|
|
|
318
314
|
class ReactiveArray extends Reactive {
|
|
@@ -333,13 +329,13 @@ class ReactiveArray extends Reactive {
|
|
|
333
329
|
this.get().length = value;
|
|
334
330
|
}
|
|
335
331
|
}
|
|
336
|
-
constructor(value
|
|
332
|
+
constructor(value) {
|
|
337
333
|
super(arrayName, new Proxy(value, {
|
|
338
334
|
get: (target, property) => updateMethods.has(property)
|
|
339
335
|
? updateArray(property, target, this.state, this.#size)
|
|
340
336
|
: Reflect.get(target, property),
|
|
341
337
|
set: (target, property, value) => setValue(target, property, value, this.state, this.#size),
|
|
342
|
-
})
|
|
338
|
+
}));
|
|
343
339
|
this.#size.set(value.length);
|
|
344
340
|
}
|
|
345
341
|
/**
|
|
@@ -420,8 +416,8 @@ class ReactiveArray extends Reactive {
|
|
|
420
416
|
/**
|
|
421
417
|
* Create a reactive array
|
|
422
418
|
*/
|
|
423
|
-
function array(value
|
|
424
|
-
return new ReactiveArray(Array.isArray(value) ? value : []
|
|
419
|
+
function array(value) {
|
|
420
|
+
return new ReactiveArray(Array.isArray(value) ? value : []);
|
|
425
421
|
}
|
|
426
422
|
function setValue(target, property, value, state, length) {
|
|
427
423
|
const isIndex = !Number.isNaN(Number(property));
|
|
@@ -430,7 +426,7 @@ function setValue(target, property, value, state, length) {
|
|
|
430
426
|
return Reflect.set(target, property, value);
|
|
431
427
|
}
|
|
432
428
|
const previous = Reflect.get(target, property);
|
|
433
|
-
if (!
|
|
429
|
+
if (!Object.is(previous, value)) {
|
|
434
430
|
Reflect.set(target, property, value);
|
|
435
431
|
emitValue(state);
|
|
436
432
|
length.set(target.length);
|
|
@@ -445,7 +441,7 @@ function updateArray(type, array, state, length) {
|
|
|
445
441
|
const result = array[type](...args);
|
|
446
442
|
if (affectsLength
|
|
447
443
|
? array.length !== previousLength
|
|
448
|
-
: differentArrays(
|
|
444
|
+
: differentArrays(previousArray, array)) {
|
|
449
445
|
emitValue(state);
|
|
450
446
|
length.set(array.length);
|
|
451
447
|
}
|
package/dist/value/array.cjs
CHANGED
|
@@ -13,14 +13,13 @@ const value_computed = require("./computed.cjs");
|
|
|
13
13
|
const value_reactive = require("./reactive.cjs");
|
|
14
14
|
const value_signal = require("./signal.cjs");
|
|
15
15
|
class ReactiveArray extends value_reactive.Reactive {
|
|
16
|
-
constructor(value
|
|
16
|
+
constructor(value) {
|
|
17
17
|
super(
|
|
18
18
|
helpers_is.arrayName,
|
|
19
19
|
new Proxy(value, {
|
|
20
20
|
get: (target, property) => updateMethods.has(property) ? updateArray(property, target, this.state, __privateGet(this, _size)) : Reflect.get(target, property),
|
|
21
21
|
set: (target, property, value2) => setValue(target, property, value2, this.state, __privateGet(this, _size))
|
|
22
|
-
})
|
|
23
|
-
options
|
|
22
|
+
})
|
|
24
23
|
);
|
|
25
24
|
__privateAdd(this, _size, value_signal.signal(0));
|
|
26
25
|
__privateGet(this, _size).set(value.length);
|
|
@@ -117,8 +116,8 @@ class ReactiveArray extends value_reactive.Reactive {
|
|
|
117
116
|
}
|
|
118
117
|
}
|
|
119
118
|
_size = new WeakMap();
|
|
120
|
-
function array(value
|
|
121
|
-
return new ReactiveArray(Array.isArray(value) ? value : []
|
|
119
|
+
function array(value) {
|
|
120
|
+
return new ReactiveArray(Array.isArray(value) ? value : []);
|
|
122
121
|
}
|
|
123
122
|
function setValue(target, property, value, state, length) {
|
|
124
123
|
const isIndex = !Number.isNaN(Number(property));
|
|
@@ -127,7 +126,7 @@ function setValue(target, property, value, state, length) {
|
|
|
127
126
|
return Reflect.set(target, property, value);
|
|
128
127
|
}
|
|
129
128
|
const previous = Reflect.get(target, property);
|
|
130
|
-
if (!
|
|
129
|
+
if (!Object.is(previous, value)) {
|
|
131
130
|
Reflect.set(target, property, value);
|
|
132
131
|
helpers_value.emitValue(state);
|
|
133
132
|
length.set(target.length);
|
|
@@ -142,7 +141,7 @@ function updateArray(type, array2, state, length) {
|
|
|
142
141
|
const result = array2[type](
|
|
143
142
|
...args
|
|
144
143
|
);
|
|
145
|
-
if (affectsLength ? array2.length !== previousLength : helpers_value.differentArrays(
|
|
144
|
+
if (affectsLength ? array2.length !== previousLength : helpers_value.differentArrays(previousArray, array2)) {
|
|
146
145
|
helpers_value.emitValue(state);
|
|
147
146
|
length.set(array2.length);
|
|
148
147
|
}
|
package/dist/value/array.js
CHANGED
|
@@ -11,14 +11,13 @@ import { computed } from "./computed.js";
|
|
|
11
11
|
import { Reactive } from "./reactive.js";
|
|
12
12
|
import { signal } from "./signal.js";
|
|
13
13
|
class ReactiveArray extends Reactive {
|
|
14
|
-
constructor(value
|
|
14
|
+
constructor(value) {
|
|
15
15
|
super(
|
|
16
16
|
arrayName,
|
|
17
17
|
new Proxy(value, {
|
|
18
18
|
get: (target, property) => updateMethods.has(property) ? updateArray(property, target, this.state, __privateGet(this, _size)) : Reflect.get(target, property),
|
|
19
19
|
set: (target, property, value2) => setValue(target, property, value2, this.state, __privateGet(this, _size))
|
|
20
|
-
})
|
|
21
|
-
options
|
|
20
|
+
})
|
|
22
21
|
);
|
|
23
22
|
__privateAdd(this, _size, signal(0));
|
|
24
23
|
__privateGet(this, _size).set(value.length);
|
|
@@ -115,8 +114,8 @@ class ReactiveArray extends Reactive {
|
|
|
115
114
|
}
|
|
116
115
|
}
|
|
117
116
|
_size = new WeakMap();
|
|
118
|
-
function array(value
|
|
119
|
-
return new ReactiveArray(Array.isArray(value) ? value : []
|
|
117
|
+
function array(value) {
|
|
118
|
+
return new ReactiveArray(Array.isArray(value) ? value : []);
|
|
120
119
|
}
|
|
121
120
|
function setValue(target, property, value, state, length) {
|
|
122
121
|
const isIndex = !Number.isNaN(Number(property));
|
|
@@ -125,7 +124,7 @@ function setValue(target, property, value, state, length) {
|
|
|
125
124
|
return Reflect.set(target, property, value);
|
|
126
125
|
}
|
|
127
126
|
const previous = Reflect.get(target, property);
|
|
128
|
-
if (!
|
|
127
|
+
if (!Object.is(previous, value)) {
|
|
129
128
|
Reflect.set(target, property, value);
|
|
130
129
|
emitValue(state);
|
|
131
130
|
length.set(target.length);
|
|
@@ -140,7 +139,7 @@ function updateArray(type, array2, state, length) {
|
|
|
140
139
|
const result = array2[type](
|
|
141
140
|
...args
|
|
142
141
|
);
|
|
143
|
-
if (affectsLength ? array2.length !== previousLength : differentArrays(
|
|
142
|
+
if (affectsLength ? array2.length !== previousLength : differentArrays(previousArray, array2)) {
|
|
144
143
|
emitValue(state);
|
|
145
144
|
length.set(array2.length);
|
|
146
145
|
}
|
package/dist/value/computed.cjs
CHANGED
|
@@ -9,8 +9,8 @@ const helpers_is = require("../helpers/is.cjs");
|
|
|
9
9
|
const value_reactive = require("./reactive.cjs");
|
|
10
10
|
exports.activeComputed = void 0;
|
|
11
11
|
class Computed extends value_reactive.Reactive {
|
|
12
|
-
constructor(callback
|
|
13
|
-
super(helpers_is.computedName, void 0
|
|
12
|
+
constructor(callback) {
|
|
13
|
+
super(helpers_is.computedName, void 0);
|
|
14
14
|
__publicField(this, "effect", {
|
|
15
15
|
dirty: true,
|
|
16
16
|
instance: void 0
|
|
@@ -21,7 +21,7 @@ class Computed extends value_reactive.Reactive {
|
|
|
21
21
|
exports.activeComputed = this;
|
|
22
22
|
const value = callback();
|
|
23
23
|
exports.activeComputed = previousComputed;
|
|
24
|
-
if (!
|
|
24
|
+
if (!Object.is(this.state.value, value)) {
|
|
25
25
|
this.state.value = value;
|
|
26
26
|
for (const computed2 of this.state.computeds) {
|
|
27
27
|
computed2.effect.dirty = true;
|
|
@@ -53,8 +53,8 @@ class Computed extends value_reactive.Reactive {
|
|
|
53
53
|
return this.state.value;
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
|
-
function computed(callback
|
|
57
|
-
return new Computed(callback
|
|
56
|
+
function computed(callback) {
|
|
57
|
+
return new Computed(callback);
|
|
58
58
|
}
|
|
59
59
|
exports.Computed = Computed;
|
|
60
60
|
exports.computed = computed;
|
package/dist/value/computed.js
CHANGED
|
@@ -7,8 +7,8 @@ import { computedName } from "../helpers/is.js";
|
|
|
7
7
|
import { Reactive } from "./reactive.js";
|
|
8
8
|
let activeComputed;
|
|
9
9
|
class Computed extends Reactive {
|
|
10
|
-
constructor(callback
|
|
11
|
-
super(computedName, void 0
|
|
10
|
+
constructor(callback) {
|
|
11
|
+
super(computedName, void 0);
|
|
12
12
|
__publicField(this, "effect", {
|
|
13
13
|
dirty: true,
|
|
14
14
|
instance: void 0
|
|
@@ -19,7 +19,7 @@ class Computed extends Reactive {
|
|
|
19
19
|
activeComputed = this;
|
|
20
20
|
const value = callback();
|
|
21
21
|
activeComputed = previousComputed;
|
|
22
|
-
if (!
|
|
22
|
+
if (!Object.is(this.state.value, value)) {
|
|
23
23
|
this.state.value = value;
|
|
24
24
|
for (const computed2 of this.state.computeds) {
|
|
25
25
|
computed2.effect.dirty = true;
|
|
@@ -51,8 +51,8 @@ class Computed extends Reactive {
|
|
|
51
51
|
return this.state.value;
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
|
-
function computed(callback
|
|
55
|
-
return new Computed(callback
|
|
54
|
+
function computed(callback) {
|
|
55
|
+
return new Computed(callback);
|
|
56
56
|
}
|
|
57
57
|
export {
|
|
58
58
|
Computed,
|
package/dist/value/reactive.cjs
CHANGED
|
@@ -5,18 +5,14 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
5
5
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
6
6
|
const subscription = require("../subscription.cjs");
|
|
7
7
|
class Reactive {
|
|
8
|
-
constructor(name, value
|
|
8
|
+
constructor(name, value) {
|
|
9
9
|
__publicField(this, "state", {
|
|
10
10
|
computeds: /* @__PURE__ */ new Set(),
|
|
11
11
|
effects: /* @__PURE__ */ new Set(),
|
|
12
|
-
equal: Object.is,
|
|
13
12
|
subscriptions: /* @__PURE__ */ new Map(),
|
|
14
13
|
value: void 0
|
|
15
14
|
});
|
|
16
15
|
this.state.value = value;
|
|
17
|
-
if (typeof options === "object" && typeof options.equal === "function") {
|
|
18
|
-
this.state.equal = options.equal;
|
|
19
|
-
}
|
|
20
16
|
Object.defineProperty(this, "$mora", {
|
|
21
17
|
value: name
|
|
22
18
|
});
|
package/dist/value/reactive.js
CHANGED
|
@@ -3,18 +3,14 @@ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { en
|
|
|
3
3
|
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
4
|
import { subscribe, unsubscribe } from "../subscription.js";
|
|
5
5
|
class Reactive {
|
|
6
|
-
constructor(name, value
|
|
6
|
+
constructor(name, value) {
|
|
7
7
|
__publicField(this, "state", {
|
|
8
8
|
computeds: /* @__PURE__ */ new Set(),
|
|
9
9
|
effects: /* @__PURE__ */ new Set(),
|
|
10
|
-
equal: Object.is,
|
|
11
10
|
subscriptions: /* @__PURE__ */ new Map(),
|
|
12
11
|
value: void 0
|
|
13
12
|
});
|
|
14
13
|
this.state.value = value;
|
|
15
|
-
if (typeof options === "object" && typeof options.equal === "function") {
|
|
16
|
-
this.state.equal = options.equal;
|
|
17
|
-
}
|
|
18
14
|
Object.defineProperty(this, "$mora", {
|
|
19
15
|
value: name
|
|
20
16
|
});
|
package/dist/value/signal.cjs
CHANGED
|
@@ -4,8 +4,8 @@ const helpers_is = require("../helpers/is.cjs");
|
|
|
4
4
|
const helpers_value = require("../helpers/value.cjs");
|
|
5
5
|
const value_reactive = require("./reactive.cjs");
|
|
6
6
|
class Signal extends value_reactive.Reactive {
|
|
7
|
-
constructor(value
|
|
8
|
-
super(helpers_is.signalName, value
|
|
7
|
+
constructor(value) {
|
|
8
|
+
super(helpers_is.signalName, value);
|
|
9
9
|
}
|
|
10
10
|
/**
|
|
11
11
|
* @inheritdoc
|
|
@@ -17,7 +17,7 @@ class Signal extends value_reactive.Reactive {
|
|
|
17
17
|
* Set the value
|
|
18
18
|
*/
|
|
19
19
|
set(value) {
|
|
20
|
-
if (helpers_value.differentValues(this.state
|
|
20
|
+
if (helpers_value.differentValues(this.state.value, value)) {
|
|
21
21
|
this.state.value = value;
|
|
22
22
|
helpers_value.emitValue(this.state);
|
|
23
23
|
}
|
|
@@ -29,8 +29,8 @@ class Signal extends value_reactive.Reactive {
|
|
|
29
29
|
this.set(callback(this.state.value));
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
|
-
function signal(value
|
|
33
|
-
return new Signal(value
|
|
32
|
+
function signal(value) {
|
|
33
|
+
return new Signal(value);
|
|
34
34
|
}
|
|
35
35
|
exports.Signal = Signal;
|
|
36
36
|
exports.signal = signal;
|
package/dist/value/signal.js
CHANGED
|
@@ -2,8 +2,8 @@ import { signalName } from "../helpers/is.js";
|
|
|
2
2
|
import { getValue, differentValues, emitValue } from "../helpers/value.js";
|
|
3
3
|
import { Reactive } from "./reactive.js";
|
|
4
4
|
class Signal extends Reactive {
|
|
5
|
-
constructor(value
|
|
6
|
-
super(signalName, value
|
|
5
|
+
constructor(value) {
|
|
6
|
+
super(signalName, value);
|
|
7
7
|
}
|
|
8
8
|
/**
|
|
9
9
|
* @inheritdoc
|
|
@@ -15,7 +15,7 @@ class Signal extends Reactive {
|
|
|
15
15
|
* Set the value
|
|
16
16
|
*/
|
|
17
17
|
set(value) {
|
|
18
|
-
if (differentValues(this.state
|
|
18
|
+
if (differentValues(this.state.value, value)) {
|
|
19
19
|
this.state.value = value;
|
|
20
20
|
emitValue(this.state);
|
|
21
21
|
}
|
|
@@ -27,8 +27,8 @@ class Signal extends Reactive {
|
|
|
27
27
|
this.set(callback(this.state.value));
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
|
-
function signal(value
|
|
31
|
-
return new Signal(value
|
|
30
|
+
function signal(value) {
|
|
31
|
+
return new Signal(value);
|
|
32
32
|
}
|
|
33
33
|
export {
|
|
34
34
|
Signal,
|
package/package.json
CHANGED
package/src/helpers/is.ts
CHANGED
|
@@ -4,6 +4,7 @@ import type {ReactiveArray} from '../value/array';
|
|
|
4
4
|
import type {Computed} from '../value/computed';
|
|
5
5
|
import type {Reactive} from '../value/reactive';
|
|
6
6
|
import type {Signal} from '../value/signal';
|
|
7
|
+
import type {Store} from '../value/store';
|
|
7
8
|
|
|
8
9
|
/**
|
|
9
10
|
* Is the value a reactive array?
|
|
@@ -48,9 +49,14 @@ export function isSignal(value: unknown): value is Signal<unknown> {
|
|
|
48
49
|
return isMora<Signal<unknown>>(value, signalName);
|
|
49
50
|
}
|
|
50
51
|
|
|
52
|
+
export function isStore(value: unknown): value is Store<unknown> {
|
|
53
|
+
return isMora<Reactive<unknown>>(value, storeName);
|
|
54
|
+
}
|
|
55
|
+
|
|
51
56
|
export const arrayName = 'array';
|
|
52
57
|
export const computedName = 'computed';
|
|
53
58
|
export const effectName = 'effect';
|
|
54
59
|
export const signalName = 'signal';
|
|
60
|
+
export const storeName = 'store';
|
|
55
61
|
|
|
56
|
-
const reactiveNames = new Set([arrayName, computedName, signalName]);
|
|
62
|
+
const reactiveNames = new Set([arrayName, computedName, signalName, storeName]);
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import {isArrayOrPlainObject} from '@oscarpalmer/atoms/is';
|
|
2
|
+
import type {ArrayOrPlainObject, PlainObject} from '@oscarpalmer/atoms/models';
|
|
3
|
+
import {startBatch, stopBatch} from '../batch';
|
|
4
|
+
import type {ReactiveState} from '../value/reactive';
|
|
5
|
+
import type {Signal} from '../value/signal';
|
|
6
|
+
import {emitValue} from './value';
|
|
7
|
+
|
|
8
|
+
export function setProxyValue(
|
|
9
|
+
proxy: ArrayOrPlainObject,
|
|
10
|
+
value: ArrayOrPlainObject,
|
|
11
|
+
): void {
|
|
12
|
+
startBatch();
|
|
13
|
+
|
|
14
|
+
const proxyKeys = Object.keys(proxy);
|
|
15
|
+
const valueKeys = Object.keys(value);
|
|
16
|
+
|
|
17
|
+
let {length} = proxyKeys;
|
|
18
|
+
|
|
19
|
+
for (let index = 0; index < length; index += 1) {
|
|
20
|
+
const key = proxyKeys[index];
|
|
21
|
+
|
|
22
|
+
(proxy as PlainObject)[key] = valueKeys.includes(key)
|
|
23
|
+
? (value as PlainObject)[key]
|
|
24
|
+
: undefined;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
length = valueKeys.length;
|
|
28
|
+
|
|
29
|
+
for (let index = 0; index < length; index += 1) {
|
|
30
|
+
const key = valueKeys[index];
|
|
31
|
+
|
|
32
|
+
if (!proxyKeys.includes(key)) {
|
|
33
|
+
const keyedValue = (value as PlainObject)[key];
|
|
34
|
+
|
|
35
|
+
(proxy as PlainObject)[key] = keyedValue;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
stopBatch();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function setValueInProxy<Value extends ArrayOrPlainObject, Equal>(
|
|
43
|
+
target: Value,
|
|
44
|
+
property: PropertyKey,
|
|
45
|
+
value: unknown,
|
|
46
|
+
state: ReactiveState<Value, Equal>,
|
|
47
|
+
isArray: boolean,
|
|
48
|
+
length?: Signal<number>,
|
|
49
|
+
): boolean {
|
|
50
|
+
if (isArray) {
|
|
51
|
+
const isIndex = !Number.isNaN(Number(property));
|
|
52
|
+
const isLength = property === 'length';
|
|
53
|
+
|
|
54
|
+
if (!isIndex && !isLength) {
|
|
55
|
+
return Reflect.set(target, property, value);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const previous = Reflect.get(target, property);
|
|
60
|
+
|
|
61
|
+
if (!state.equal(previous as never, value as never)) {
|
|
62
|
+
Reflect.set(target, property, value);
|
|
63
|
+
|
|
64
|
+
emitValue(state);
|
|
65
|
+
|
|
66
|
+
if (isArray) {
|
|
67
|
+
length?.set((target as unknown[]).length);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return true;
|
|
72
|
+
}
|
package/src/helpers/value.ts
CHANGED
|
@@ -3,7 +3,25 @@ import {activeEffect} from '../effect';
|
|
|
3
3
|
import {type InternalComputed, activeComputed} from '../value/computed';
|
|
4
4
|
import type {ReactiveState} from '../value/reactive';
|
|
5
5
|
|
|
6
|
-
export function
|
|
6
|
+
export function emitValue<Value>(state: ReactiveState<Value, never>): void {
|
|
7
|
+
for (const computed of state.computeds) {
|
|
8
|
+
(computed as unknown as InternalComputed).effect.dirty = true;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
for (const effect of state.effects) {
|
|
12
|
+
batchedHandlers.add(effect);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
for (const [, subscription] of state.subscriptions) {
|
|
16
|
+
batchedHandlers.add(subscription as never);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (batchDepth === 0) {
|
|
20
|
+
flushHandlers();
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function equalArrays<Value>(
|
|
7
25
|
state: ReactiveState<Value[], Value>,
|
|
8
26
|
first: Value[],
|
|
9
27
|
second: Value[],
|
|
@@ -11,7 +29,7 @@ export function differentArrays<Value>(
|
|
|
11
29
|
let {length} = first;
|
|
12
30
|
|
|
13
31
|
if (length !== second.length) {
|
|
14
|
-
return
|
|
32
|
+
return false;
|
|
15
33
|
}
|
|
16
34
|
|
|
17
35
|
let offset = 0;
|
|
@@ -22,7 +40,7 @@ export function differentArrays<Value>(
|
|
|
22
40
|
|
|
23
41
|
for (let index = 0; index < offset; index += 1) {
|
|
24
42
|
if (!state.equal(first[index], second[index])) {
|
|
25
|
-
return
|
|
43
|
+
return false;
|
|
26
44
|
}
|
|
27
45
|
}
|
|
28
46
|
}
|
|
@@ -31,39 +49,11 @@ export function differentArrays<Value>(
|
|
|
31
49
|
|
|
32
50
|
for (let index = offset; index < length; index += 1) {
|
|
33
51
|
if (!state.equal(first[index], second[index])) {
|
|
34
|
-
return
|
|
52
|
+
return false;
|
|
35
53
|
}
|
|
36
54
|
}
|
|
37
55
|
|
|
38
|
-
return
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export function differentValues<Value>(
|
|
42
|
-
state: ReactiveState<Value, Value>,
|
|
43
|
-
first: Value,
|
|
44
|
-
second: Value,
|
|
45
|
-
): boolean {
|
|
46
|
-
return Array.isArray(first) && Array.isArray(second)
|
|
47
|
-
? differentArrays(state as never, first, second)
|
|
48
|
-
: !state.equal(first, second);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export function emitValue<Value>(state: ReactiveState<Value, never>): void {
|
|
52
|
-
for (const computed of state.computeds) {
|
|
53
|
-
(computed as unknown as InternalComputed).effect.dirty = true;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
for (const effect of state.effects) {
|
|
57
|
-
batchedHandlers.add(effect);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
for (const [, subscription] of state.subscriptions) {
|
|
61
|
-
batchedHandlers.add(subscription as never);
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
if (batchDepth === 0) {
|
|
65
|
-
flushHandlers();
|
|
66
|
-
}
|
|
56
|
+
return true;
|
|
67
57
|
}
|
|
68
58
|
|
|
69
59
|
export function getValue<Value>(state: ReactiveState<Value, never>): Value {
|
package/src/index.ts
CHANGED
|
@@ -11,3 +11,4 @@ export {array, type ReactiveArray} from './value/array';
|
|
|
11
11
|
export {computed, type Computed} from './value/computed';
|
|
12
12
|
export type {Reactive} from './value/reactive';
|
|
13
13
|
export {signal, type Signal} from './value/signal';
|
|
14
|
+
export {store, type Store} from './value/store';
|