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