@oscarpalmer/mora 0.14.0 → 0.16.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.cjs +3 -3
- package/dist/batch.js +3 -3
- package/dist/helpers/value.cjs +44 -8
- package/dist/helpers/value.js +45 -9
- package/dist/mora.full.js +104 -49
- package/dist/value/array.cjs +62 -4
- package/dist/value/array.js +61 -3
- package/dist/value/computed.cjs +1 -1
- package/dist/value/computed.js +1 -1
- package/dist/value/signal.cjs +4 -1
- package/dist/value/signal.js +5 -2
- package/package.json +3 -3
- package/src/batch.ts +2 -2
- package/src/helpers/is.ts +1 -1
- package/src/helpers/value.ts +57 -13
- package/src/value/array.ts +74 -3
- package/src/value/computed.ts +2 -2
- package/src/value/signal.ts +6 -2
- package/types/batch.d.cts +1 -1
- package/types/batch.d.ts +1 -1
- package/types/helpers/is.d.cts +36 -0
- package/types/helpers/is.d.ts +1 -1
- package/types/helpers/value.d.cts +3 -1
- package/types/helpers/value.d.ts +3 -1
- package/types/index.d.cts +36 -0
- package/types/value/array.d.cts +36 -0
- package/types/value/array.d.ts +37 -0
- package/types/value/computed.d.ts +1 -2
- package/dist/helpers/emit.cjs +0 -42
- package/dist/helpers/emit.js +0 -42
- package/src/helpers/emit.ts +0 -52
- package/types/helpers/emit.d.cts +0 -63
- package/types/helpers/emit.d.ts +0 -3
package/dist/batch.cjs
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const effect = require("./effect.cjs");
|
|
4
4
|
const helpers_is = require("./helpers/is.cjs");
|
|
5
|
-
function
|
|
5
|
+
function flushHandlers() {
|
|
6
6
|
while (exports.batchDepth === 0 && batchedHandlers.size > 0) {
|
|
7
7
|
const handlers = [...batchedHandlers];
|
|
8
8
|
batchedHandlers.clear();
|
|
@@ -22,11 +22,11 @@ function stopBatch() {
|
|
|
22
22
|
if (exports.batchDepth > 0) {
|
|
23
23
|
exports.batchDepth -= 1;
|
|
24
24
|
}
|
|
25
|
-
|
|
25
|
+
flushHandlers();
|
|
26
26
|
}
|
|
27
27
|
const batchedHandlers = /* @__PURE__ */ new Set();
|
|
28
28
|
exports.batchDepth = 0;
|
|
29
29
|
exports.batchedHandlers = batchedHandlers;
|
|
30
|
-
exports.
|
|
30
|
+
exports.flushHandlers = flushHandlers;
|
|
31
31
|
exports.startBatch = startBatch;
|
|
32
32
|
exports.stopBatch = stopBatch;
|
package/dist/batch.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { runEffect } from "./effect.js";
|
|
2
2
|
import { isEffect } from "./helpers/is.js";
|
|
3
|
-
function
|
|
3
|
+
function flushHandlers() {
|
|
4
4
|
while (batchDepth === 0 && batchedHandlers.size > 0) {
|
|
5
5
|
const handlers = [...batchedHandlers];
|
|
6
6
|
batchedHandlers.clear();
|
|
@@ -20,14 +20,14 @@ function stopBatch() {
|
|
|
20
20
|
if (batchDepth > 0) {
|
|
21
21
|
batchDepth -= 1;
|
|
22
22
|
}
|
|
23
|
-
|
|
23
|
+
flushHandlers();
|
|
24
24
|
}
|
|
25
25
|
const batchedHandlers = /* @__PURE__ */ new Set();
|
|
26
26
|
let batchDepth = 0;
|
|
27
27
|
export {
|
|
28
28
|
batchDepth,
|
|
29
29
|
batchedHandlers,
|
|
30
|
-
|
|
30
|
+
flushHandlers,
|
|
31
31
|
startBatch,
|
|
32
32
|
stopBatch
|
|
33
33
|
};
|
package/dist/helpers/value.cjs
CHANGED
|
@@ -1,8 +1,48 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const batch = require("../batch.cjs");
|
|
3
4
|
const effect = require("../effect.cjs");
|
|
4
5
|
const value_computed = require("../value/computed.cjs");
|
|
5
|
-
|
|
6
|
+
function differentArrays(first, second) {
|
|
7
|
+
let { length } = first;
|
|
8
|
+
if (length !== second.length) {
|
|
9
|
+
return true;
|
|
10
|
+
}
|
|
11
|
+
let offset = 0;
|
|
12
|
+
if (length >= 100) {
|
|
13
|
+
offset = Math.round(length / 10);
|
|
14
|
+
offset = offset > 25 ? 25 : offset;
|
|
15
|
+
for (let index = 0; index < offset; index += 1) {
|
|
16
|
+
if (!Object.is(first[index], second[index])) {
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
length -= offset;
|
|
22
|
+
for (let index = offset; index < length; index += 1) {
|
|
23
|
+
if (!Object.is(first[index], second[index])) {
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
function differentValues(first, second) {
|
|
30
|
+
return Array.isArray(first) && Array.isArray(second) ? differentArrays(first, second) : !Object.is(first, second);
|
|
31
|
+
}
|
|
32
|
+
function emitValue(state) {
|
|
33
|
+
for (const computed of state.computeds) {
|
|
34
|
+
computed.effect.dirty = true;
|
|
35
|
+
}
|
|
36
|
+
for (const effect2 of state.effects) {
|
|
37
|
+
batch.batchedHandlers.add(effect2);
|
|
38
|
+
}
|
|
39
|
+
for (const [, subscription] of state.subscriptions) {
|
|
40
|
+
batch.batchedHandlers.add(subscription);
|
|
41
|
+
}
|
|
42
|
+
if (batch.batchDepth === 0) {
|
|
43
|
+
batch.flushHandlers();
|
|
44
|
+
}
|
|
45
|
+
}
|
|
6
46
|
function getValue(state) {
|
|
7
47
|
if (value_computed.activeComputed != null) {
|
|
8
48
|
state.computeds.add(value_computed.activeComputed);
|
|
@@ -12,11 +52,7 @@ function getValue(state) {
|
|
|
12
52
|
}
|
|
13
53
|
return state.value;
|
|
14
54
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
helpers_emit.emitValue(state);
|
|
19
|
-
}
|
|
20
|
-
}
|
|
55
|
+
exports.differentArrays = differentArrays;
|
|
56
|
+
exports.differentValues = differentValues;
|
|
57
|
+
exports.emitValue = emitValue;
|
|
21
58
|
exports.getValue = getValue;
|
|
22
|
-
exports.setValue = setValue;
|
package/dist/helpers/value.js
CHANGED
|
@@ -1,6 +1,46 @@
|
|
|
1
|
+
import { batchedHandlers, batchDepth, flushHandlers } from "../batch.js";
|
|
1
2
|
import { activeEffect } from "../effect.js";
|
|
2
3
|
import { activeComputed } from "../value/computed.js";
|
|
3
|
-
|
|
4
|
+
function differentArrays(first, second) {
|
|
5
|
+
let { length } = first;
|
|
6
|
+
if (length !== second.length) {
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
9
|
+
let offset = 0;
|
|
10
|
+
if (length >= 100) {
|
|
11
|
+
offset = Math.round(length / 10);
|
|
12
|
+
offset = offset > 25 ? 25 : offset;
|
|
13
|
+
for (let index = 0; index < offset; index += 1) {
|
|
14
|
+
if (!Object.is(first[index], second[index])) {
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
length -= offset;
|
|
20
|
+
for (let index = offset; index < length; index += 1) {
|
|
21
|
+
if (!Object.is(first[index], second[index])) {
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
function differentValues(first, second) {
|
|
28
|
+
return Array.isArray(first) && Array.isArray(second) ? differentArrays(first, second) : !Object.is(first, second);
|
|
29
|
+
}
|
|
30
|
+
function emitValue(state) {
|
|
31
|
+
for (const computed of state.computeds) {
|
|
32
|
+
computed.effect.dirty = true;
|
|
33
|
+
}
|
|
34
|
+
for (const effect of state.effects) {
|
|
35
|
+
batchedHandlers.add(effect);
|
|
36
|
+
}
|
|
37
|
+
for (const [, subscription] of state.subscriptions) {
|
|
38
|
+
batchedHandlers.add(subscription);
|
|
39
|
+
}
|
|
40
|
+
if (batchDepth === 0) {
|
|
41
|
+
flushHandlers();
|
|
42
|
+
}
|
|
43
|
+
}
|
|
4
44
|
function getValue(state) {
|
|
5
45
|
if (activeComputed != null) {
|
|
6
46
|
state.computeds.add(activeComputed);
|
|
@@ -10,13 +50,9 @@ function getValue(state) {
|
|
|
10
50
|
}
|
|
11
51
|
return state.value;
|
|
12
52
|
}
|
|
13
|
-
function setValue(state, value) {
|
|
14
|
-
if (!Object.is(state.value, value)) {
|
|
15
|
-
state.value = value;
|
|
16
|
-
emitValue(state);
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
53
|
export {
|
|
20
|
-
|
|
21
|
-
|
|
54
|
+
differentArrays,
|
|
55
|
+
differentValues,
|
|
56
|
+
emitValue,
|
|
57
|
+
getValue
|
|
22
58
|
};
|
package/dist/mora.full.js
CHANGED
|
@@ -68,7 +68,7 @@ function effect(callback) {
|
|
|
68
68
|
}
|
|
69
69
|
let activeEffect;
|
|
70
70
|
|
|
71
|
-
function
|
|
71
|
+
function flushHandlers() {
|
|
72
72
|
while (batchDepth === 0 && batchedHandlers.size > 0) {
|
|
73
73
|
const handlers = [...batchedHandlers];
|
|
74
74
|
batchedHandlers.clear();
|
|
@@ -95,49 +95,11 @@ function stopBatch() {
|
|
|
95
95
|
if (batchDepth > 0) {
|
|
96
96
|
batchDepth -= 1;
|
|
97
97
|
}
|
|
98
|
-
|
|
98
|
+
flushHandlers();
|
|
99
99
|
}
|
|
100
100
|
const batchedHandlers = new Set();
|
|
101
101
|
let batchDepth = 0;
|
|
102
102
|
|
|
103
|
-
function emitArrayChanges(first, second) {
|
|
104
|
-
let { length } = first;
|
|
105
|
-
if (length !== second.length) {
|
|
106
|
-
return true;
|
|
107
|
-
}
|
|
108
|
-
let offset = 0;
|
|
109
|
-
if (length >= 100) {
|
|
110
|
-
offset = Math.round(length / 10);
|
|
111
|
-
offset = offset > 25 ? 25 : offset;
|
|
112
|
-
for (let index = 0; index < offset; index += 1) {
|
|
113
|
-
if (!Object.is(first[index], second[index])) {
|
|
114
|
-
return true;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
length -= offset;
|
|
119
|
-
for (let index = offset; index < length; index += 1) {
|
|
120
|
-
if (!Object.is(first[index], second[index])) {
|
|
121
|
-
return true;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
return false;
|
|
125
|
-
}
|
|
126
|
-
function emitValue(state) {
|
|
127
|
-
for (const computed of state.computeds) {
|
|
128
|
-
computed.effect.dirty = true;
|
|
129
|
-
}
|
|
130
|
-
for (const effect of state.effects) {
|
|
131
|
-
batchedHandlers.add(effect);
|
|
132
|
-
}
|
|
133
|
-
for (const [, subscription] of state.subscriptions) {
|
|
134
|
-
batchedHandlers.add(subscription);
|
|
135
|
-
}
|
|
136
|
-
if (batchDepth === 0) {
|
|
137
|
-
flushEffects();
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
103
|
class Subscription {
|
|
142
104
|
state;
|
|
143
105
|
callback;
|
|
@@ -245,7 +207,7 @@ class Computed extends Reactive {
|
|
|
245
207
|
* @inheritdoc
|
|
246
208
|
*/
|
|
247
209
|
get() {
|
|
248
|
-
if (activeComputed != null &&
|
|
210
|
+
if (activeComputed != null && this !== activeComputed) {
|
|
249
211
|
this.state.computeds.add(activeComputed);
|
|
250
212
|
}
|
|
251
213
|
if (activeEffect != null && activeEffect !== this.effect.instance) {
|
|
@@ -264,6 +226,48 @@ function computed(callback) {
|
|
|
264
226
|
return new Computed(callback);
|
|
265
227
|
}
|
|
266
228
|
|
|
229
|
+
function differentArrays(first, second) {
|
|
230
|
+
let { length } = first;
|
|
231
|
+
if (length !== second.length) {
|
|
232
|
+
return true;
|
|
233
|
+
}
|
|
234
|
+
let offset = 0;
|
|
235
|
+
if (length >= 100) {
|
|
236
|
+
offset = Math.round(length / 10);
|
|
237
|
+
offset = offset > 25 ? 25 : offset;
|
|
238
|
+
for (let index = 0; index < offset; index += 1) {
|
|
239
|
+
if (!Object.is(first[index], second[index])) {
|
|
240
|
+
return true;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
length -= offset;
|
|
245
|
+
for (let index = offset; index < length; index += 1) {
|
|
246
|
+
if (!Object.is(first[index], second[index])) {
|
|
247
|
+
return true;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
return false;
|
|
251
|
+
}
|
|
252
|
+
function differentValues(first, second) {
|
|
253
|
+
return Array.isArray(first) && Array.isArray(second)
|
|
254
|
+
? differentArrays(first, second)
|
|
255
|
+
: !Object.is(first, second);
|
|
256
|
+
}
|
|
257
|
+
function emitValue(state) {
|
|
258
|
+
for (const computed of state.computeds) {
|
|
259
|
+
computed.effect.dirty = true;
|
|
260
|
+
}
|
|
261
|
+
for (const effect of state.effects) {
|
|
262
|
+
batchedHandlers.add(effect);
|
|
263
|
+
}
|
|
264
|
+
for (const [, subscription] of state.subscriptions) {
|
|
265
|
+
batchedHandlers.add(subscription);
|
|
266
|
+
}
|
|
267
|
+
if (batchDepth === 0) {
|
|
268
|
+
flushHandlers();
|
|
269
|
+
}
|
|
270
|
+
}
|
|
267
271
|
function getValue(state) {
|
|
268
272
|
if (activeComputed != null) {
|
|
269
273
|
state.computeds.add(activeComputed);
|
|
@@ -273,12 +277,6 @@ function getValue(state) {
|
|
|
273
277
|
}
|
|
274
278
|
return state.value;
|
|
275
279
|
}
|
|
276
|
-
function setValue$1(state, value) {
|
|
277
|
-
if (!Object.is(state.value, value)) {
|
|
278
|
-
state.value = value;
|
|
279
|
-
emitValue(state);
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
280
|
|
|
283
281
|
class Signal extends Reactive {
|
|
284
282
|
constructor(value) {
|
|
@@ -294,7 +292,10 @@ class Signal extends Reactive {
|
|
|
294
292
|
* Set the value
|
|
295
293
|
*/
|
|
296
294
|
set(value) {
|
|
297
|
-
|
|
295
|
+
if (differentValues(this.state.value, value)) {
|
|
296
|
+
this.state.value = value;
|
|
297
|
+
emitValue(this.state);
|
|
298
|
+
}
|
|
298
299
|
}
|
|
299
300
|
/**
|
|
300
301
|
* Update the value
|
|
@@ -337,15 +338,51 @@ class ReactiveArray extends Reactive {
|
|
|
337
338
|
}));
|
|
338
339
|
this.#size.set(value.length);
|
|
339
340
|
}
|
|
341
|
+
/**
|
|
342
|
+
* Get an indexed item
|
|
343
|
+
*/
|
|
344
|
+
at(index) {
|
|
345
|
+
return this.get().at(index);
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Clear the array
|
|
349
|
+
*/
|
|
350
|
+
clear() {
|
|
351
|
+
this.length = 0;
|
|
352
|
+
}
|
|
340
353
|
/**
|
|
341
354
|
* @inheritdoc
|
|
342
355
|
*/
|
|
343
356
|
get() {
|
|
344
357
|
return getValue(this.state);
|
|
345
358
|
}
|
|
359
|
+
/**
|
|
360
|
+
* Create a computed, filtered array
|
|
361
|
+
*/
|
|
362
|
+
filter(callback) {
|
|
363
|
+
return computed(() => this.get().filter(callback));
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Create a computed, mapped array
|
|
367
|
+
*/
|
|
368
|
+
map(callback) {
|
|
369
|
+
return computed(() => this.get().map(callback));
|
|
370
|
+
}
|
|
346
371
|
peek(length) {
|
|
347
372
|
return length === true ? this.#size.peek() : [...this.state.value];
|
|
348
373
|
}
|
|
374
|
+
/**
|
|
375
|
+
* Remove and return the last item of the array
|
|
376
|
+
*/
|
|
377
|
+
pop() {
|
|
378
|
+
return this.state.value.pop();
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* Add items to the end of the array
|
|
382
|
+
*/
|
|
383
|
+
push(...items) {
|
|
384
|
+
return this.state.value.push(...items);
|
|
385
|
+
}
|
|
349
386
|
set(first, second) {
|
|
350
387
|
if (Array.isArray(first)) {
|
|
351
388
|
this.state.value.splice(0, this.state.value.length, ...first);
|
|
@@ -357,6 +394,24 @@ class ReactiveArray extends Reactive {
|
|
|
357
394
|
this.state.value[first] = second;
|
|
358
395
|
}
|
|
359
396
|
}
|
|
397
|
+
/**
|
|
398
|
+
* Remove and return the first item of the array
|
|
399
|
+
*/
|
|
400
|
+
shift() {
|
|
401
|
+
return this.state.value.shift();
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* Remove and return items from the array _(and optionally add new items)_
|
|
405
|
+
*/
|
|
406
|
+
splice(from, to, ...items) {
|
|
407
|
+
return this.state.value.splice(from, to ?? this.state.value.length, ...items);
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* Add items to the beginning of the array
|
|
411
|
+
*/
|
|
412
|
+
unshift(...items) {
|
|
413
|
+
return this.state.value.unshift(...items);
|
|
414
|
+
}
|
|
360
415
|
}
|
|
361
416
|
/**
|
|
362
417
|
* Create a reactive array
|
|
@@ -386,7 +441,7 @@ function updateArray(type, array, state, length) {
|
|
|
386
441
|
const result = array[type](...args);
|
|
387
442
|
if (affectsLength
|
|
388
443
|
? array.length !== previousLength
|
|
389
|
-
:
|
|
444
|
+
: differentArrays(previousArray, array)) {
|
|
390
445
|
emitValue(state);
|
|
391
446
|
length.set(array.length);
|
|
392
447
|
}
|
package/dist/value/array.cjs
CHANGED
|
@@ -7,9 +7,9 @@ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read fr
|
|
|
7
7
|
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
8
8
|
var _size;
|
|
9
9
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
10
|
-
const helpers_emit = require("../helpers/emit.cjs");
|
|
11
10
|
const helpers_is = require("../helpers/is.cjs");
|
|
12
11
|
const helpers_value = require("../helpers/value.cjs");
|
|
12
|
+
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 {
|
|
@@ -38,15 +38,51 @@ class ReactiveArray extends value_reactive.Reactive {
|
|
|
38
38
|
this.get().length = value;
|
|
39
39
|
}
|
|
40
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* Get an indexed item
|
|
43
|
+
*/
|
|
44
|
+
at(index) {
|
|
45
|
+
return this.get().at(index);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Clear the array
|
|
49
|
+
*/
|
|
50
|
+
clear() {
|
|
51
|
+
this.length = 0;
|
|
52
|
+
}
|
|
41
53
|
/**
|
|
42
54
|
* @inheritdoc
|
|
43
55
|
*/
|
|
44
56
|
get() {
|
|
45
57
|
return helpers_value.getValue(this.state);
|
|
46
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* Create a computed, filtered array
|
|
61
|
+
*/
|
|
62
|
+
filter(callback) {
|
|
63
|
+
return value_computed.computed(() => this.get().filter(callback));
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Create a computed, mapped array
|
|
67
|
+
*/
|
|
68
|
+
map(callback) {
|
|
69
|
+
return value_computed.computed(() => this.get().map(callback));
|
|
70
|
+
}
|
|
47
71
|
peek(length) {
|
|
48
72
|
return length === true ? __privateGet(this, _size).peek() : [...this.state.value];
|
|
49
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* Remove and return the last item of the array
|
|
76
|
+
*/
|
|
77
|
+
pop() {
|
|
78
|
+
return this.state.value.pop();
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Add items to the end of the array
|
|
82
|
+
*/
|
|
83
|
+
push(...items) {
|
|
84
|
+
return this.state.value.push(...items);
|
|
85
|
+
}
|
|
50
86
|
set(first, second) {
|
|
51
87
|
if (Array.isArray(first)) {
|
|
52
88
|
this.state.value.splice(0, this.state.value.length, ...first);
|
|
@@ -56,6 +92,28 @@ class ReactiveArray extends value_reactive.Reactive {
|
|
|
56
92
|
this.state.value[first] = second;
|
|
57
93
|
}
|
|
58
94
|
}
|
|
95
|
+
/**
|
|
96
|
+
* Remove and return the first item of the array
|
|
97
|
+
*/
|
|
98
|
+
shift() {
|
|
99
|
+
return this.state.value.shift();
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Remove and return items from the array _(and optionally add new items)_
|
|
103
|
+
*/
|
|
104
|
+
splice(from, to, ...items) {
|
|
105
|
+
return this.state.value.splice(
|
|
106
|
+
from,
|
|
107
|
+
to ?? this.state.value.length,
|
|
108
|
+
...items
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Add items to the beginning of the array
|
|
113
|
+
*/
|
|
114
|
+
unshift(...items) {
|
|
115
|
+
return this.state.value.unshift(...items);
|
|
116
|
+
}
|
|
59
117
|
}
|
|
60
118
|
_size = new WeakMap();
|
|
61
119
|
function array(value) {
|
|
@@ -70,7 +128,7 @@ function setValue(target, property, value, state, length) {
|
|
|
70
128
|
const previous = Reflect.get(target, property);
|
|
71
129
|
if (!Object.is(previous, value)) {
|
|
72
130
|
Reflect.set(target, property, value);
|
|
73
|
-
|
|
131
|
+
helpers_value.emitValue(state);
|
|
74
132
|
length.set(target.length);
|
|
75
133
|
}
|
|
76
134
|
return true;
|
|
@@ -83,8 +141,8 @@ function updateArray(type, array2, state, length) {
|
|
|
83
141
|
const result = array2[type](
|
|
84
142
|
...args
|
|
85
143
|
);
|
|
86
|
-
if (affectsLength ? array2.length !== previousLength :
|
|
87
|
-
|
|
144
|
+
if (affectsLength ? array2.length !== previousLength : helpers_value.differentArrays(previousArray, array2)) {
|
|
145
|
+
helpers_value.emitValue(state);
|
|
88
146
|
length.set(array2.length);
|
|
89
147
|
}
|
|
90
148
|
return result;
|
package/dist/value/array.js
CHANGED
|
@@ -5,9 +5,9 @@ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot
|
|
|
5
5
|
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
6
6
|
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
7
7
|
var _size;
|
|
8
|
-
import { emitValue, emitArrayChanges } from "../helpers/emit.js";
|
|
9
8
|
import { arrayName } from "../helpers/is.js";
|
|
10
|
-
import { getValue } from "../helpers/value.js";
|
|
9
|
+
import { getValue, emitValue, differentArrays } from "../helpers/value.js";
|
|
10
|
+
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 {
|
|
@@ -36,15 +36,51 @@ class ReactiveArray extends Reactive {
|
|
|
36
36
|
this.get().length = value;
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* Get an indexed item
|
|
41
|
+
*/
|
|
42
|
+
at(index) {
|
|
43
|
+
return this.get().at(index);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Clear the array
|
|
47
|
+
*/
|
|
48
|
+
clear() {
|
|
49
|
+
this.length = 0;
|
|
50
|
+
}
|
|
39
51
|
/**
|
|
40
52
|
* @inheritdoc
|
|
41
53
|
*/
|
|
42
54
|
get() {
|
|
43
55
|
return getValue(this.state);
|
|
44
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Create a computed, filtered array
|
|
59
|
+
*/
|
|
60
|
+
filter(callback) {
|
|
61
|
+
return computed(() => this.get().filter(callback));
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Create a computed, mapped array
|
|
65
|
+
*/
|
|
66
|
+
map(callback) {
|
|
67
|
+
return computed(() => this.get().map(callback));
|
|
68
|
+
}
|
|
45
69
|
peek(length) {
|
|
46
70
|
return length === true ? __privateGet(this, _size).peek() : [...this.state.value];
|
|
47
71
|
}
|
|
72
|
+
/**
|
|
73
|
+
* Remove and return the last item of the array
|
|
74
|
+
*/
|
|
75
|
+
pop() {
|
|
76
|
+
return this.state.value.pop();
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Add items to the end of the array
|
|
80
|
+
*/
|
|
81
|
+
push(...items) {
|
|
82
|
+
return this.state.value.push(...items);
|
|
83
|
+
}
|
|
48
84
|
set(first, second) {
|
|
49
85
|
if (Array.isArray(first)) {
|
|
50
86
|
this.state.value.splice(0, this.state.value.length, ...first);
|
|
@@ -54,6 +90,28 @@ class ReactiveArray extends Reactive {
|
|
|
54
90
|
this.state.value[first] = second;
|
|
55
91
|
}
|
|
56
92
|
}
|
|
93
|
+
/**
|
|
94
|
+
* Remove and return the first item of the array
|
|
95
|
+
*/
|
|
96
|
+
shift() {
|
|
97
|
+
return this.state.value.shift();
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Remove and return items from the array _(and optionally add new items)_
|
|
101
|
+
*/
|
|
102
|
+
splice(from, to, ...items) {
|
|
103
|
+
return this.state.value.splice(
|
|
104
|
+
from,
|
|
105
|
+
to ?? this.state.value.length,
|
|
106
|
+
...items
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Add items to the beginning of the array
|
|
111
|
+
*/
|
|
112
|
+
unshift(...items) {
|
|
113
|
+
return this.state.value.unshift(...items);
|
|
114
|
+
}
|
|
57
115
|
}
|
|
58
116
|
_size = new WeakMap();
|
|
59
117
|
function array(value) {
|
|
@@ -81,7 +139,7 @@ function updateArray(type, array2, state, length) {
|
|
|
81
139
|
const result = array2[type](
|
|
82
140
|
...args
|
|
83
141
|
);
|
|
84
|
-
if (affectsLength ? array2.length !== previousLength :
|
|
142
|
+
if (affectsLength ? array2.length !== previousLength : differentArrays(previousArray, array2)) {
|
|
85
143
|
emitValue(state);
|
|
86
144
|
length.set(array2.length);
|
|
87
145
|
}
|
package/dist/value/computed.cjs
CHANGED
|
@@ -41,7 +41,7 @@ class Computed extends value_reactive.Reactive {
|
|
|
41
41
|
* @inheritdoc
|
|
42
42
|
*/
|
|
43
43
|
get() {
|
|
44
|
-
if (exports.activeComputed != null && exports.activeComputed
|
|
44
|
+
if (exports.activeComputed != null && this !== exports.activeComputed) {
|
|
45
45
|
this.state.computeds.add(exports.activeComputed);
|
|
46
46
|
}
|
|
47
47
|
if (effect.activeEffect != null && effect.activeEffect !== this.effect.instance) {
|
package/dist/value/computed.js
CHANGED
|
@@ -39,7 +39,7 @@ class Computed extends Reactive {
|
|
|
39
39
|
* @inheritdoc
|
|
40
40
|
*/
|
|
41
41
|
get() {
|
|
42
|
-
if (activeComputed != null &&
|
|
42
|
+
if (activeComputed != null && this !== activeComputed) {
|
|
43
43
|
this.state.computeds.add(activeComputed);
|
|
44
44
|
}
|
|
45
45
|
if (activeEffect != null && activeEffect !== this.effect.instance) {
|
package/dist/value/signal.cjs
CHANGED
|
@@ -17,7 +17,10 @@ class Signal extends value_reactive.Reactive {
|
|
|
17
17
|
* Set the value
|
|
18
18
|
*/
|
|
19
19
|
set(value) {
|
|
20
|
-
helpers_value.
|
|
20
|
+
if (helpers_value.differentValues(this.state.value, value)) {
|
|
21
|
+
this.state.value = value;
|
|
22
|
+
helpers_value.emitValue(this.state);
|
|
23
|
+
}
|
|
21
24
|
}
|
|
22
25
|
/**
|
|
23
26
|
* Update the value
|
package/dist/value/signal.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { signalName } from "../helpers/is.js";
|
|
2
|
-
import { getValue,
|
|
2
|
+
import { getValue, differentValues, emitValue } from "../helpers/value.js";
|
|
3
3
|
import { Reactive } from "./reactive.js";
|
|
4
4
|
class Signal extends Reactive {
|
|
5
5
|
constructor(value) {
|
|
@@ -15,7 +15,10 @@ class Signal extends Reactive {
|
|
|
15
15
|
* Set the value
|
|
16
16
|
*/
|
|
17
17
|
set(value) {
|
|
18
|
-
|
|
18
|
+
if (differentValues(this.state.value, value)) {
|
|
19
|
+
this.state.value = value;
|
|
20
|
+
emitValue(this.state);
|
|
21
|
+
}
|
|
19
22
|
}
|
|
20
23
|
/**
|
|
21
24
|
* Update the value
|
package/package.json
CHANGED
|
@@ -12,14 +12,14 @@
|
|
|
12
12
|
"@rollup/plugin-node-resolve": "^16",
|
|
13
13
|
"@rollup/plugin-typescript": "^12.1",
|
|
14
14
|
"@types/node": "^22.15",
|
|
15
|
-
"@vitest/coverage-istanbul": "^3.
|
|
15
|
+
"@vitest/coverage-istanbul": "^3.2",
|
|
16
16
|
"dts-bundle-generator": "^9.5",
|
|
17
17
|
"glob": "^11",
|
|
18
18
|
"jsdom": "^26.1",
|
|
19
19
|
"tslib": "^2.8",
|
|
20
20
|
"typescript": "^5.8",
|
|
21
21
|
"vite": "^6.3",
|
|
22
|
-
"vitest": "^3.
|
|
22
|
+
"vitest": "^3.2"
|
|
23
23
|
},
|
|
24
24
|
"exports": {
|
|
25
25
|
".": {
|
|
@@ -54,5 +54,5 @@
|
|
|
54
54
|
},
|
|
55
55
|
"type": "module",
|
|
56
56
|
"types": "./types/index.d.ts",
|
|
57
|
-
"version": "0.
|
|
57
|
+
"version": "0.16.0"
|
|
58
58
|
}
|