@oscarpalmer/mora 0.15.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/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 +75 -64
- package/dist/value/array.cjs +15 -9
- package/dist/value/array.js +14 -8
- package/dist/value/computed.cjs +6 -6
- package/dist/value/computed.js +6 -6
- package/dist/value/reactive.cjs +5 -1
- package/dist/value/reactive.js +5 -1
- package/dist/value/signal.cjs +8 -5
- package/dist/value/signal.js +9 -6
- package/package.json +3 -3
- package/src/batch.ts +2 -2
- package/src/helpers/is.ts +1 -1
- package/src/helpers/value.ts +66 -14
- package/src/subscription.ts +3 -3
- package/src/value/array.ts +22 -12
- package/src/value/computed.ts +12 -9
- package/src/value/reactive.ts +17 -4
- package/src/value/signal.ts +14 -7
- package/types/batch.d.cts +15 -8
- package/types/batch.d.ts +1 -1
- package/types/helpers/is.d.cts +21 -10
- package/types/helpers/is.d.ts +1 -1
- package/types/helpers/value.d.cts +18 -9
- package/types/helpers/value.d.ts +4 -2
- package/types/index.d.cts +24 -13
- package/types/subscription.d.cts +16 -9
- package/types/subscription.d.ts +4 -4
- package/types/value/array.d.cts +21 -10
- package/types/value/array.d.ts +8 -4
- package/types/value/computed.d.cts +16 -9
- package/types/value/computed.d.ts +5 -6
- 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/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(state, 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 (!state.equal(first[index], second[index])) {
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
length -= offset;
|
|
22
|
+
for (let index = offset; index < length; index += 1) {
|
|
23
|
+
if (!state.equal(first[index], second[index])) {
|
|
24
|
+
return true;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
function differentValues(state, first, second) {
|
|
30
|
+
return Array.isArray(first) && Array.isArray(second) ? differentArrays(state, first, second) : !state.equal(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(state, 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 (!state.equal(first[index], second[index])) {
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
length -= offset;
|
|
20
|
+
for (let index = offset; index < length; index += 1) {
|
|
21
|
+
if (!state.equal(first[index], second[index])) {
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
function differentValues(state, first, second) {
|
|
28
|
+
return Array.isArray(first) && Array.isArray(second) ? differentArrays(state, first, second) : !state.equal(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;
|
|
@@ -170,11 +132,15 @@ class Reactive {
|
|
|
170
132
|
state = {
|
|
171
133
|
computeds: new Set(),
|
|
172
134
|
effects: new Set(),
|
|
135
|
+
equal: Object.is,
|
|
173
136
|
subscriptions: new Map(),
|
|
174
137
|
value: undefined,
|
|
175
138
|
};
|
|
176
|
-
constructor(name, value) {
|
|
139
|
+
constructor(name, value, options) {
|
|
177
140
|
this.state.value = value;
|
|
141
|
+
if (typeof options === 'object' && typeof options.equal === 'function') {
|
|
142
|
+
this.state.equal = options.equal;
|
|
143
|
+
}
|
|
178
144
|
Object.defineProperty(this, '$mora', {
|
|
179
145
|
value: name,
|
|
180
146
|
});
|
|
@@ -217,15 +183,15 @@ class Computed extends Reactive {
|
|
|
217
183
|
dirty: true,
|
|
218
184
|
instance: undefined,
|
|
219
185
|
};
|
|
220
|
-
constructor(callback) {
|
|
221
|
-
super(computedName, undefined);
|
|
186
|
+
constructor(callback, options) {
|
|
187
|
+
super(computedName, undefined, options);
|
|
222
188
|
this.effect.instance = effect(() => {
|
|
223
189
|
if (this.effect.dirty) {
|
|
224
190
|
const previousComputed = activeComputed;
|
|
225
191
|
activeComputed = this;
|
|
226
192
|
const value = callback();
|
|
227
193
|
activeComputed = previousComputed;
|
|
228
|
-
if (!
|
|
194
|
+
if (!this.state.equal(this.state.value, value)) {
|
|
229
195
|
this.state.value = value;
|
|
230
196
|
for (const computed of this.state.computeds) {
|
|
231
197
|
computed.effect.dirty = true;
|
|
@@ -245,7 +211,7 @@ class Computed extends Reactive {
|
|
|
245
211
|
* @inheritdoc
|
|
246
212
|
*/
|
|
247
213
|
get() {
|
|
248
|
-
if (activeComputed != null &&
|
|
214
|
+
if (activeComputed != null && this !== activeComputed) {
|
|
249
215
|
this.state.computeds.add(activeComputed);
|
|
250
216
|
}
|
|
251
217
|
if (activeEffect != null && activeEffect !== this.effect.instance) {
|
|
@@ -260,10 +226,52 @@ class Computed extends Reactive {
|
|
|
260
226
|
/**
|
|
261
227
|
* Create a computed value
|
|
262
228
|
*/
|
|
263
|
-
function computed(callback) {
|
|
264
|
-
return new Computed(callback);
|
|
229
|
+
function computed(callback, options) {
|
|
230
|
+
return new Computed(callback, options);
|
|
265
231
|
}
|
|
266
232
|
|
|
233
|
+
function differentArrays(state, first, second) {
|
|
234
|
+
let { length } = first;
|
|
235
|
+
if (length !== second.length) {
|
|
236
|
+
return true;
|
|
237
|
+
}
|
|
238
|
+
let offset = 0;
|
|
239
|
+
if (length >= 100) {
|
|
240
|
+
offset = Math.round(length / 10);
|
|
241
|
+
offset = offset > 25 ? 25 : offset;
|
|
242
|
+
for (let index = 0; index < offset; index += 1) {
|
|
243
|
+
if (!state.equal(first[index], second[index])) {
|
|
244
|
+
return true;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
length -= offset;
|
|
249
|
+
for (let index = offset; index < length; index += 1) {
|
|
250
|
+
if (!state.equal(first[index], second[index])) {
|
|
251
|
+
return true;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return false;
|
|
255
|
+
}
|
|
256
|
+
function differentValues(state, first, second) {
|
|
257
|
+
return Array.isArray(first) && Array.isArray(second)
|
|
258
|
+
? differentArrays(state, first, second)
|
|
259
|
+
: !state.equal(first, second);
|
|
260
|
+
}
|
|
261
|
+
function emitValue(state) {
|
|
262
|
+
for (const computed of state.computeds) {
|
|
263
|
+
computed.effect.dirty = true;
|
|
264
|
+
}
|
|
265
|
+
for (const effect of state.effects) {
|
|
266
|
+
batchedHandlers.add(effect);
|
|
267
|
+
}
|
|
268
|
+
for (const [, subscription] of state.subscriptions) {
|
|
269
|
+
batchedHandlers.add(subscription);
|
|
270
|
+
}
|
|
271
|
+
if (batchDepth === 0) {
|
|
272
|
+
flushHandlers();
|
|
273
|
+
}
|
|
274
|
+
}
|
|
267
275
|
function getValue(state) {
|
|
268
276
|
if (activeComputed != null) {
|
|
269
277
|
state.computeds.add(activeComputed);
|
|
@@ -273,16 +281,10 @@ function getValue(state) {
|
|
|
273
281
|
}
|
|
274
282
|
return state.value;
|
|
275
283
|
}
|
|
276
|
-
function setValue$1(state, value) {
|
|
277
|
-
if (!Object.is(state.value, value)) {
|
|
278
|
-
state.value = value;
|
|
279
|
-
emitValue(state);
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
284
|
|
|
283
285
|
class Signal extends Reactive {
|
|
284
|
-
constructor(value) {
|
|
285
|
-
super(signalName, value);
|
|
286
|
+
constructor(value, options) {
|
|
287
|
+
super(signalName, value, options);
|
|
286
288
|
}
|
|
287
289
|
/**
|
|
288
290
|
* @inheritdoc
|
|
@@ -294,7 +296,10 @@ class Signal extends Reactive {
|
|
|
294
296
|
* Set the value
|
|
295
297
|
*/
|
|
296
298
|
set(value) {
|
|
297
|
-
|
|
299
|
+
if (differentValues(this.state, this.state.value, value)) {
|
|
300
|
+
this.state.value = value;
|
|
301
|
+
emitValue(this.state);
|
|
302
|
+
}
|
|
298
303
|
}
|
|
299
304
|
/**
|
|
300
305
|
* Update the value
|
|
@@ -306,8 +311,8 @@ class Signal extends Reactive {
|
|
|
306
311
|
/**
|
|
307
312
|
* Create a reactive value
|
|
308
313
|
*/
|
|
309
|
-
function signal(value) {
|
|
310
|
-
return new Signal(value);
|
|
314
|
+
function signal(value, options) {
|
|
315
|
+
return new Signal(value, options);
|
|
311
316
|
}
|
|
312
317
|
|
|
313
318
|
class ReactiveArray extends Reactive {
|
|
@@ -328,13 +333,13 @@ class ReactiveArray extends Reactive {
|
|
|
328
333
|
this.get().length = value;
|
|
329
334
|
}
|
|
330
335
|
}
|
|
331
|
-
constructor(value) {
|
|
336
|
+
constructor(value, options) {
|
|
332
337
|
super(arrayName, new Proxy(value, {
|
|
333
338
|
get: (target, property) => updateMethods.has(property)
|
|
334
339
|
? updateArray(property, target, this.state, this.#size)
|
|
335
340
|
: Reflect.get(target, property),
|
|
336
341
|
set: (target, property, value) => setValue(target, property, value, this.state, this.#size),
|
|
337
|
-
}));
|
|
342
|
+
}), options);
|
|
338
343
|
this.#size.set(value.length);
|
|
339
344
|
}
|
|
340
345
|
/**
|
|
@@ -343,6 +348,12 @@ class ReactiveArray extends Reactive {
|
|
|
343
348
|
at(index) {
|
|
344
349
|
return this.get().at(index);
|
|
345
350
|
}
|
|
351
|
+
/**
|
|
352
|
+
* Clear the array
|
|
353
|
+
*/
|
|
354
|
+
clear() {
|
|
355
|
+
this.length = 0;
|
|
356
|
+
}
|
|
346
357
|
/**
|
|
347
358
|
* @inheritdoc
|
|
348
359
|
*/
|
|
@@ -409,8 +420,8 @@ class ReactiveArray extends Reactive {
|
|
|
409
420
|
/**
|
|
410
421
|
* Create a reactive array
|
|
411
422
|
*/
|
|
412
|
-
function array(value) {
|
|
413
|
-
return new ReactiveArray(Array.isArray(value) ? value : []);
|
|
423
|
+
function array(value, options) {
|
|
424
|
+
return new ReactiveArray(Array.isArray(value) ? value : [], options);
|
|
414
425
|
}
|
|
415
426
|
function setValue(target, property, value, state, length) {
|
|
416
427
|
const isIndex = !Number.isNaN(Number(property));
|
|
@@ -419,7 +430,7 @@ function setValue(target, property, value, state, length) {
|
|
|
419
430
|
return Reflect.set(target, property, value);
|
|
420
431
|
}
|
|
421
432
|
const previous = Reflect.get(target, property);
|
|
422
|
-
if (!
|
|
433
|
+
if (!state.equal(previous, value)) {
|
|
423
434
|
Reflect.set(target, property, value);
|
|
424
435
|
emitValue(state);
|
|
425
436
|
length.set(target.length);
|
|
@@ -434,7 +445,7 @@ function updateArray(type, array, state, length) {
|
|
|
434
445
|
const result = array[type](...args);
|
|
435
446
|
if (affectsLength
|
|
436
447
|
? array.length !== previousLength
|
|
437
|
-
:
|
|
448
|
+
: differentArrays(state, previousArray, array)) {
|
|
438
449
|
emitValue(state);
|
|
439
450
|
length.set(array.length);
|
|
440
451
|
}
|
package/dist/value/array.cjs
CHANGED
|
@@ -7,20 +7,20 @@ 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");
|
|
13
12
|
const value_computed = require("./computed.cjs");
|
|
14
13
|
const value_reactive = require("./reactive.cjs");
|
|
15
14
|
const value_signal = require("./signal.cjs");
|
|
16
15
|
class ReactiveArray extends value_reactive.Reactive {
|
|
17
|
-
constructor(value) {
|
|
16
|
+
constructor(value, options) {
|
|
18
17
|
super(
|
|
19
18
|
helpers_is.arrayName,
|
|
20
19
|
new Proxy(value, {
|
|
21
20
|
get: (target, property) => updateMethods.has(property) ? updateArray(property, target, this.state, __privateGet(this, _size)) : Reflect.get(target, property),
|
|
22
21
|
set: (target, property, value2) => setValue(target, property, value2, this.state, __privateGet(this, _size))
|
|
23
|
-
})
|
|
22
|
+
}),
|
|
23
|
+
options
|
|
24
24
|
);
|
|
25
25
|
__privateAdd(this, _size, value_signal.signal(0));
|
|
26
26
|
__privateGet(this, _size).set(value.length);
|
|
@@ -45,6 +45,12 @@ class ReactiveArray extends value_reactive.Reactive {
|
|
|
45
45
|
at(index) {
|
|
46
46
|
return this.get().at(index);
|
|
47
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* Clear the array
|
|
50
|
+
*/
|
|
51
|
+
clear() {
|
|
52
|
+
this.length = 0;
|
|
53
|
+
}
|
|
48
54
|
/**
|
|
49
55
|
* @inheritdoc
|
|
50
56
|
*/
|
|
@@ -111,8 +117,8 @@ class ReactiveArray extends value_reactive.Reactive {
|
|
|
111
117
|
}
|
|
112
118
|
}
|
|
113
119
|
_size = new WeakMap();
|
|
114
|
-
function array(value) {
|
|
115
|
-
return new ReactiveArray(Array.isArray(value) ? value : []);
|
|
120
|
+
function array(value, options) {
|
|
121
|
+
return new ReactiveArray(Array.isArray(value) ? value : [], options);
|
|
116
122
|
}
|
|
117
123
|
function setValue(target, property, value, state, length) {
|
|
118
124
|
const isIndex = !Number.isNaN(Number(property));
|
|
@@ -121,9 +127,9 @@ function setValue(target, property, value, state, length) {
|
|
|
121
127
|
return Reflect.set(target, property, value);
|
|
122
128
|
}
|
|
123
129
|
const previous = Reflect.get(target, property);
|
|
124
|
-
if (!
|
|
130
|
+
if (!state.equal(previous, value)) {
|
|
125
131
|
Reflect.set(target, property, value);
|
|
126
|
-
|
|
132
|
+
helpers_value.emitValue(state);
|
|
127
133
|
length.set(target.length);
|
|
128
134
|
}
|
|
129
135
|
return true;
|
|
@@ -136,8 +142,8 @@ function updateArray(type, array2, state, length) {
|
|
|
136
142
|
const result = array2[type](
|
|
137
143
|
...args
|
|
138
144
|
);
|
|
139
|
-
if (affectsLength ? array2.length !== previousLength :
|
|
140
|
-
|
|
145
|
+
if (affectsLength ? array2.length !== previousLength : helpers_value.differentArrays(state, previousArray, array2)) {
|
|
146
|
+
helpers_value.emitValue(state);
|
|
141
147
|
length.set(array2.length);
|
|
142
148
|
}
|
|
143
149
|
return result;
|
package/dist/value/array.js
CHANGED
|
@@ -5,20 +5,20 @@ 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";
|
|
11
10
|
import { computed } from "./computed.js";
|
|
12
11
|
import { Reactive } from "./reactive.js";
|
|
13
12
|
import { signal } from "./signal.js";
|
|
14
13
|
class ReactiveArray extends Reactive {
|
|
15
|
-
constructor(value) {
|
|
14
|
+
constructor(value, options) {
|
|
16
15
|
super(
|
|
17
16
|
arrayName,
|
|
18
17
|
new Proxy(value, {
|
|
19
18
|
get: (target, property) => updateMethods.has(property) ? updateArray(property, target, this.state, __privateGet(this, _size)) : Reflect.get(target, property),
|
|
20
19
|
set: (target, property, value2) => setValue(target, property, value2, this.state, __privateGet(this, _size))
|
|
21
|
-
})
|
|
20
|
+
}),
|
|
21
|
+
options
|
|
22
22
|
);
|
|
23
23
|
__privateAdd(this, _size, signal(0));
|
|
24
24
|
__privateGet(this, _size).set(value.length);
|
|
@@ -43,6 +43,12 @@ class ReactiveArray extends Reactive {
|
|
|
43
43
|
at(index) {
|
|
44
44
|
return this.get().at(index);
|
|
45
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* Clear the array
|
|
48
|
+
*/
|
|
49
|
+
clear() {
|
|
50
|
+
this.length = 0;
|
|
51
|
+
}
|
|
46
52
|
/**
|
|
47
53
|
* @inheritdoc
|
|
48
54
|
*/
|
|
@@ -109,8 +115,8 @@ class ReactiveArray extends Reactive {
|
|
|
109
115
|
}
|
|
110
116
|
}
|
|
111
117
|
_size = new WeakMap();
|
|
112
|
-
function array(value) {
|
|
113
|
-
return new ReactiveArray(Array.isArray(value) ? value : []);
|
|
118
|
+
function array(value, options) {
|
|
119
|
+
return new ReactiveArray(Array.isArray(value) ? value : [], options);
|
|
114
120
|
}
|
|
115
121
|
function setValue(target, property, value, state, length) {
|
|
116
122
|
const isIndex = !Number.isNaN(Number(property));
|
|
@@ -119,7 +125,7 @@ function setValue(target, property, value, state, length) {
|
|
|
119
125
|
return Reflect.set(target, property, value);
|
|
120
126
|
}
|
|
121
127
|
const previous = Reflect.get(target, property);
|
|
122
|
-
if (!
|
|
128
|
+
if (!state.equal(previous, value)) {
|
|
123
129
|
Reflect.set(target, property, value);
|
|
124
130
|
emitValue(state);
|
|
125
131
|
length.set(target.length);
|
|
@@ -134,7 +140,7 @@ function updateArray(type, array2, state, length) {
|
|
|
134
140
|
const result = array2[type](
|
|
135
141
|
...args
|
|
136
142
|
);
|
|
137
|
-
if (affectsLength ? array2.length !== previousLength :
|
|
143
|
+
if (affectsLength ? array2.length !== previousLength : differentArrays(state, previousArray, array2)) {
|
|
138
144
|
emitValue(state);
|
|
139
145
|
length.set(array2.length);
|
|
140
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;
|
|
@@ -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) {
|
|
@@ -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;
|
|
@@ -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) {
|
|
@@ -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
|
});
|