@oscarpalmer/mora 0.34.0 → 0.36.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/constants.d.mts +2 -2
- package/dist/helpers/proxy.d.mts +8 -8
- package/dist/helpers/proxy.mjs +32 -25
- package/dist/helpers/subscription.d.mts +3 -3
- package/dist/helpers/value.d.mts +9 -8
- package/dist/helpers/value.mjs +15 -11
- package/dist/index.d.mts +2 -2
- package/dist/models.d.mts +40 -37
- package/dist/mora.full.mjs +151 -137
- package/dist/value/array.mjs +53 -41
- package/dist/value/computed.d.mts +2 -2
- package/dist/value/computed.mjs +3 -5
- package/dist/value/readonly.d.mts +2 -2
- package/dist/value/readonly.mjs +16 -19
- package/dist/value/signal.mjs +13 -13
- package/dist/value/store.mjs +4 -6
- package/package.json +3 -3
- package/src/batch.ts +2 -2
- package/src/constants.ts +7 -2
- package/src/helpers/is.ts +1 -3
- package/src/helpers/proxy.ts +68 -57
- package/src/helpers/subscription.ts +7 -6
- package/src/helpers/value.ts +30 -20
- package/src/index.ts +1 -0
- package/src/models.ts +47 -43
- package/src/value/array.ts +81 -64
- package/src/value/computed.ts +12 -16
- package/src/value/readonly.ts +38 -29
- package/src/value/signal.ts +20 -20
- package/src/value/store.ts +4 -7
package/dist/mora.full.mjs
CHANGED
|
@@ -125,10 +125,10 @@ function emitValue(instance) {
|
|
|
125
125
|
}
|
|
126
126
|
if (BATCH.depth === 0) flushHandlers();
|
|
127
127
|
}
|
|
128
|
-
function equalArrays(
|
|
128
|
+
function equalArrays(first, second, equal) {
|
|
129
129
|
const { length } = first;
|
|
130
130
|
if (length !== second.length) return false;
|
|
131
|
-
const eq =
|
|
131
|
+
const eq = equal ?? Object.is;
|
|
132
132
|
let offset = 0;
|
|
133
133
|
if (length >= 100) {
|
|
134
134
|
offset = Math.round(length / 10);
|
|
@@ -145,21 +145,25 @@ function getFrozenValue(value) {
|
|
|
145
145
|
else if (isPlainObject(frozen)) frozen = Object.freeze({ ...frozen });
|
|
146
146
|
return frozen;
|
|
147
147
|
}
|
|
148
|
+
function getJsonValue() {
|
|
149
|
+
return this[SYMBOL_STATE].value;
|
|
150
|
+
}
|
|
148
151
|
function getSignalValue() {
|
|
152
|
+
const state = this[SYMBOL_STATE];
|
|
149
153
|
if (ACTIVE.computed != null) {
|
|
150
|
-
|
|
151
|
-
|
|
154
|
+
state.computeds ??= /* @__PURE__ */ new Set();
|
|
155
|
+
state.computeds.add(ACTIVE.computed);
|
|
152
156
|
}
|
|
153
157
|
if (ACTIVE.effect != null) {
|
|
154
|
-
|
|
155
|
-
|
|
158
|
+
state.effects ??= /* @__PURE__ */ new Set();
|
|
159
|
+
state.effects.add(ACTIVE.effect);
|
|
156
160
|
}
|
|
157
|
-
return
|
|
161
|
+
return state.value;
|
|
158
162
|
}
|
|
159
163
|
function getStringValue(json) {
|
|
160
164
|
return json === true ? JSON.stringify(this[SYMBOL_STATE].value) : String(this[SYMBOL_STATE].value);
|
|
161
165
|
}
|
|
162
|
-
function
|
|
166
|
+
function handleSignal(instance, origin, setValue, onAfter) {
|
|
163
167
|
const state = instance[SYMBOL_STATE];
|
|
164
168
|
try {
|
|
165
169
|
let actual = typeof origin === "function" ? origin() : origin;
|
|
@@ -185,9 +189,9 @@ function peekSignalValue(copy) {
|
|
|
185
189
|
else if (isPlainObject(peeked)) peeked = { ...peeked };
|
|
186
190
|
return peeked;
|
|
187
191
|
}
|
|
188
|
-
function
|
|
192
|
+
function updateSignal(instance, callback, setValue) {
|
|
189
193
|
if (typeof callback !== "function") throw new TypeError("Callback must be a function");
|
|
190
|
-
|
|
194
|
+
handleSignal(instance, callback(instance[SYMBOL_STATE].value), setValue);
|
|
191
195
|
}
|
|
192
196
|
//#endregion
|
|
193
197
|
//#region src/batch.ts
|
|
@@ -401,7 +405,6 @@ function lastIndexOf(array, ...parameters) {
|
|
|
401
405
|
return findValue(FIND_VALUE_INDEX, array, parameters, true);
|
|
402
406
|
}
|
|
403
407
|
indexOf.last = lastIndexOf;
|
|
404
|
-
Object.defineProperty(indexOf, "last", { value: lastIndexOf });
|
|
405
408
|
//#endregion
|
|
406
409
|
//#region node_modules/@oscarpalmer/atoms/dist/array/find.mjs
|
|
407
410
|
function find(array, ...parameters) {
|
|
@@ -411,7 +414,6 @@ function findLast(array, ...parameters) {
|
|
|
411
414
|
return findValue(FIND_VALUE_ITEM, array, parameters, true);
|
|
412
415
|
}
|
|
413
416
|
find.last = findLast;
|
|
414
|
-
Object.defineProperty(find, "last", { value: findLast });
|
|
415
417
|
//#endregion
|
|
416
418
|
//#region node_modules/@oscarpalmer/atoms/dist/array/select.mjs
|
|
417
419
|
function reverseSelect(array, ...parameters) {
|
|
@@ -430,7 +432,6 @@ function selectValues(array, parameters) {
|
|
|
430
432
|
}).matched;
|
|
431
433
|
}
|
|
432
434
|
select.reverse = reverseSelect;
|
|
433
|
-
Object.defineProperty(select, "reverse", { value: reverseSelect });
|
|
434
435
|
//#endregion
|
|
435
436
|
//#region node_modules/@oscarpalmer/atoms/dist/array/filter.mjs
|
|
436
437
|
function exclude(array, ...parameters) {
|
|
@@ -440,7 +441,6 @@ function filter(array, ...parameters) {
|
|
|
440
441
|
return findValues("all", array, parameters).matched;
|
|
441
442
|
}
|
|
442
443
|
filter.remove = exclude;
|
|
443
|
-
Object.defineProperty(filter, "remove", { value: exclude });
|
|
444
444
|
//#endregion
|
|
445
445
|
//#region src/helpers/misc.ts
|
|
446
446
|
function getState(value, options) {
|
|
@@ -450,7 +450,7 @@ function getState(value, options) {
|
|
|
450
450
|
};
|
|
451
451
|
}
|
|
452
452
|
//#endregion
|
|
453
|
-
//#region node_modules/@oscarpalmer/atoms/dist/internal/
|
|
453
|
+
//#region node_modules/@oscarpalmer/atoms/dist/internal/aborter.mjs
|
|
454
454
|
function createAborter(value, onAbort) {
|
|
455
455
|
if (!(value instanceof AbortSignal)) return;
|
|
456
456
|
value.addEventListener(ABORT_EVENT, onAbort, ABORT_OPTIONS);
|
|
@@ -468,40 +468,32 @@ const SUBSCRIPTION_NAME = "subscription";
|
|
|
468
468
|
const SUBSCRIPTION_PROPERTY = "$subscription";
|
|
469
469
|
const SUBSCRIPTION_STORE = "subscriptions";
|
|
470
470
|
function Subscription(state, property, parameters) {
|
|
471
|
-
|
|
471
|
+
this[SUBSCRIPTION_SYMBOL] = state;
|
|
472
|
+
this[property.key] = property.value;
|
|
472
473
|
state.aborter = createAborter(parameters.signal, () => this.unsubscribe(subscriptions, this, state));
|
|
473
|
-
Object.defineProperties(this, {
|
|
474
|
-
[property.key]: { value: property.value },
|
|
475
|
-
active: {
|
|
476
|
-
enumerable: true,
|
|
477
|
-
get: () => (state.parameters?.isActive?.() ?? true) && state.active
|
|
478
|
-
}
|
|
479
|
-
});
|
|
480
474
|
addSubscription(this);
|
|
481
475
|
}
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
476
|
+
Subscription.prototype[SUBSCRIPTION_PROPERTY] = SUBSCRIPTION_NAME;
|
|
477
|
+
Subscription.prototype.unsubscribe = removeSubscription;
|
|
478
|
+
Object.defineProperty(Subscription.prototype, "active", {
|
|
479
|
+
enumerable: true,
|
|
480
|
+
get: getSubscriptionActive
|
|
485
481
|
});
|
|
486
482
|
function Subscriptions(parameters) {
|
|
487
483
|
const { keys, property } = createSubscriptionsParameters(parameters);
|
|
488
|
-
|
|
484
|
+
this[SUBSCRIPTION_SYMBOL] = createSubscriptionsState(property, keys);
|
|
489
485
|
}
|
|
486
|
+
Subscriptions.prototype[SUBSCRIPTION_PROPERTY] = SUBSCRIPTION_STORE;
|
|
487
|
+
Subscriptions.prototype.clear = clearSubscriptions;
|
|
488
|
+
Subscriptions.prototype.create = createSubscription;
|
|
490
489
|
Object.defineProperties(Subscriptions.prototype, {
|
|
491
|
-
[SUBSCRIPTION_PROPERTY]: { value: SUBSCRIPTION_STORE },
|
|
492
|
-
clear: { value: clearSubscriptions },
|
|
493
|
-
create: { value: createSubscription },
|
|
494
490
|
items: {
|
|
495
491
|
enumerable: true,
|
|
496
|
-
get
|
|
497
|
-
return this[SUBSCRIPTION_SYMBOL].items;
|
|
498
|
-
}
|
|
492
|
+
get: getSubscriptionsItems
|
|
499
493
|
},
|
|
500
494
|
values: {
|
|
501
495
|
enumerable: true,
|
|
502
|
-
get
|
|
503
|
-
return this[SUBSCRIPTION_SYMBOL].values;
|
|
504
|
-
}
|
|
496
|
+
get: getSubscriptionsValues
|
|
505
497
|
}
|
|
506
498
|
});
|
|
507
499
|
function addSubscription(instance) {
|
|
@@ -564,6 +556,16 @@ function getExistingSubscription(subscriptions, state) {
|
|
|
564
556
|
const { parameters } = state;
|
|
565
557
|
return isKey(parameters.key) ? values.from.keyed?.get(parameters.key)?.get(parameters.value) : values.from.any.get(parameters.value);
|
|
566
558
|
}
|
|
559
|
+
function getSubscriptionActive() {
|
|
560
|
+
const state = this[SUBSCRIPTION_SYMBOL];
|
|
561
|
+
return (state.parameters?.isActive?.() ?? true) && state.active;
|
|
562
|
+
}
|
|
563
|
+
function getSubscriptionsItems() {
|
|
564
|
+
return this[SUBSCRIPTION_SYMBOL].items;
|
|
565
|
+
}
|
|
566
|
+
function getSubscriptionsValues() {
|
|
567
|
+
return this[SUBSCRIPTION_SYMBOL].values;
|
|
568
|
+
}
|
|
567
569
|
function createSubscriptionParameters(input) {
|
|
568
570
|
const values = isPlainObject(input) ? input : {};
|
|
569
571
|
if (values.value == null) throw new Error(SUBSCRIPTION_INVALID_VALUE);
|
|
@@ -689,10 +691,8 @@ Computed.prototype[NAME_MORA] = NAME_COMPUTED;
|
|
|
689
691
|
Computed.prototype.get = getComputedValue;
|
|
690
692
|
Computed.prototype.peek = peekSignalValue;
|
|
691
693
|
Computed.prototype.subscribe = subscribeToSignal;
|
|
694
|
+
Computed.prototype.toJSON = getJsonValue;
|
|
692
695
|
Computed.prototype.toString = getStringValue;
|
|
693
|
-
Computed.prototype.toJSON = function() {
|
|
694
|
-
return this[SYMBOL_STATE].value;
|
|
695
|
-
};
|
|
696
696
|
/**
|
|
697
697
|
* Create a computed value
|
|
698
698
|
*
|
|
@@ -717,7 +717,7 @@ function getComputedEffect(instance, callback) {
|
|
|
717
717
|
if (fx.dirty) {
|
|
718
718
|
const previousComputed = ACTIVE.computed;
|
|
719
719
|
ACTIVE.computed = fx;
|
|
720
|
-
|
|
720
|
+
handleSignal(instance, callback, setAndEmit$1, () => {
|
|
721
721
|
ACTIVE.computed = previousComputed;
|
|
722
722
|
});
|
|
723
723
|
fx.dirty = false;
|
|
@@ -780,8 +780,12 @@ function getReactiveValueInProxy(instance, key) {
|
|
|
780
780
|
return item[0];
|
|
781
781
|
}
|
|
782
782
|
function getValueInProxy(first) {
|
|
783
|
-
|
|
784
|
-
|
|
783
|
+
let signal;
|
|
784
|
+
if (this[SYMBOL_STATE].isArray ? typeof first === "number" : isKey(first)) signal = getReactiveValueInProxy(this, first);
|
|
785
|
+
return getSignalValue.call(signal ?? this);
|
|
786
|
+
}
|
|
787
|
+
function isProxyKey(isArray, value) {
|
|
788
|
+
return isArray ? typeof value === "number" : isKey(value);
|
|
785
789
|
}
|
|
786
790
|
function isProxyObject(isArray, value) {
|
|
787
791
|
return value == null || (isArray ? Array.isArray(value) : isPlainObject(value));
|
|
@@ -795,23 +799,24 @@ function peekValueInProxy(first, second) {
|
|
|
795
799
|
}
|
|
796
800
|
function setProxyObject(state, value) {
|
|
797
801
|
if (state.isArray) {
|
|
798
|
-
|
|
802
|
+
const values = state.value;
|
|
803
|
+
values.splice(0, values.length, ...value ?? []);
|
|
799
804
|
return;
|
|
800
805
|
}
|
|
801
806
|
startBatch();
|
|
802
|
-
const
|
|
803
|
-
const
|
|
804
|
-
const
|
|
805
|
-
const
|
|
806
|
-
let { length } =
|
|
807
|
+
const current = state.value;
|
|
808
|
+
const next = value ?? {};
|
|
809
|
+
const currentKeys = Object.keys(current);
|
|
810
|
+
const nextKeys = Object.keys(next);
|
|
811
|
+
let { length } = currentKeys;
|
|
807
812
|
for (let index = 0; index < length; index += 1) {
|
|
808
|
-
const key =
|
|
809
|
-
|
|
813
|
+
const key = currentKeys[index];
|
|
814
|
+
current[key] = nextKeys.includes(key) ? next[key] : void 0;
|
|
810
815
|
}
|
|
811
|
-
length =
|
|
816
|
+
length = nextKeys.length;
|
|
812
817
|
for (let index = 0; index < length; index += 1) {
|
|
813
|
-
const key =
|
|
814
|
-
if (!
|
|
818
|
+
const key = nextKeys[index];
|
|
819
|
+
if (!currentKeys.includes(key)) current[key] = next[key];
|
|
815
820
|
}
|
|
816
821
|
stopBatch();
|
|
817
822
|
}
|
|
@@ -820,8 +825,10 @@ function setProxyProperty(state, property, value) {
|
|
|
820
825
|
state.value[property] = value;
|
|
821
826
|
return;
|
|
822
827
|
}
|
|
823
|
-
const
|
|
824
|
-
|
|
828
|
+
const array = state.value;
|
|
829
|
+
const index = property;
|
|
830
|
+
const actual = index < 0 ? array.length + index : index;
|
|
831
|
+
if (actual > -1) array[actual] = value;
|
|
825
832
|
}
|
|
826
833
|
function setProxyValue(first, second) {
|
|
827
834
|
const state = this[SYMBOL_STATE];
|
|
@@ -833,34 +840,34 @@ function setProxyValue(first, second) {
|
|
|
833
840
|
setProxyObject(state, first);
|
|
834
841
|
return;
|
|
835
842
|
}
|
|
836
|
-
const
|
|
837
|
-
if (state.isArray &&
|
|
838
|
-
let actual =
|
|
843
|
+
const keyed = isProxyKey(state.isArray, first);
|
|
844
|
+
if (state.isArray && keyed && Number.isNaN(first)) return;
|
|
845
|
+
let actual = keyed ? second : first;
|
|
839
846
|
if (typeof actual === "function") try {
|
|
840
847
|
actual = actual();
|
|
841
848
|
} catch {
|
|
842
849
|
return;
|
|
843
850
|
}
|
|
844
851
|
if (actual instanceof Promise) {
|
|
845
|
-
if (
|
|
852
|
+
if (keyed) {
|
|
846
853
|
state.promises ??= /* @__PURE__ */ new Map();
|
|
847
854
|
state.promises.set(first, actual);
|
|
848
855
|
} else state.promise = actual;
|
|
849
856
|
actual.then((value) => {
|
|
850
|
-
if (
|
|
851
|
-
state.promises
|
|
857
|
+
if (keyed && state.promises?.get(first) === actual) {
|
|
858
|
+
state.promises?.delete(first);
|
|
852
859
|
setProxyProperty(state, first, value);
|
|
853
860
|
return;
|
|
854
861
|
}
|
|
855
|
-
if (!
|
|
862
|
+
if (!keyed && isProxyObject(state.isArray, value) && state.promise === actual) {
|
|
856
863
|
state.promise = void 0;
|
|
857
864
|
setProxyObject(state, value);
|
|
858
865
|
}
|
|
859
866
|
}).catch(() => {
|
|
860
|
-
if (
|
|
861
|
-
else if (!
|
|
867
|
+
if (keyed && state.promises?.get(first) === actual) state.promises?.delete(first);
|
|
868
|
+
else if (!keyed && state.promise === actual) state.promise = void 0;
|
|
862
869
|
});
|
|
863
|
-
} else if (
|
|
870
|
+
} else if (keyed) setProxyProperty(state, first, actual);
|
|
864
871
|
else if (isProxyObject(state.isArray, actual)) setProxyObject(state, actual);
|
|
865
872
|
}
|
|
866
873
|
function setValueInProxy(instance, target, property, value) {
|
|
@@ -883,60 +890,51 @@ function updateProxyValue(callback) {
|
|
|
883
890
|
//#endregion
|
|
884
891
|
//#region src/value/readonly.ts
|
|
885
892
|
function Readonly(state, frozen) {
|
|
893
|
+
this[SUBSCRIPTION_TYPE_FROZEN] = frozen;
|
|
886
894
|
this[SYMBOL_STATE] = state;
|
|
887
|
-
Object.defineProperty(this, SUBSCRIPTION_TYPE_FROZEN, {
|
|
888
|
-
enumerable: true,
|
|
889
|
-
value: frozen
|
|
890
|
-
});
|
|
891
895
|
}
|
|
892
896
|
Readonly.prototype[NAME_MORA] = NAME_READONLY;
|
|
897
|
+
Readonly.prototype.get = getReadonlyValue;
|
|
898
|
+
Readonly.prototype.peek = peekReadonlyValue;
|
|
893
899
|
Readonly.prototype.subscribe = subscribeToReadonly;
|
|
900
|
+
Readonly.prototype.toJSON = getJsonValue;
|
|
894
901
|
Readonly.prototype.toString = getStringValue;
|
|
895
|
-
|
|
896
|
-
return
|
|
897
|
-
}
|
|
898
|
-
|
|
899
|
-
return getReadonlyValue(this, true, this[SUBSCRIPTION_TYPE_FROZEN], copy);
|
|
900
|
-
};
|
|
901
|
-
Readonly.prototype.toJSON = function() {
|
|
902
|
-
return this[SYMBOL_STATE].value;
|
|
903
|
-
};
|
|
904
|
-
function getReadonlyInstance(frozen) {
|
|
902
|
+
function getReadonlyInstance(state, frozen) {
|
|
903
|
+
return new Readonly(state, frozen);
|
|
904
|
+
}
|
|
905
|
+
function getReadonlySignal(frozen) {
|
|
905
906
|
const key = frozen === true ? SUBSCRIPTION_TYPE_FROZEN : SUBSCRIPTION_TYPE_ORIGINAL;
|
|
906
907
|
this[SYMBOL_STATE].readonlies ??= {};
|
|
907
|
-
this[SYMBOL_STATE].readonlies[key] ??=
|
|
908
|
+
this[SYMBOL_STATE].readonlies[key] ??= getReadonlyInstance(this[SYMBOL_STATE], frozen === true);
|
|
908
909
|
return this[SYMBOL_STATE].readonlies[key];
|
|
909
910
|
}
|
|
910
|
-
function
|
|
911
|
-
return
|
|
911
|
+
function getReadonlyValue() {
|
|
912
|
+
return handleReadonlyValue(this, false, this[SUBSCRIPTION_TYPE_FROZEN]);
|
|
912
913
|
}
|
|
913
|
-
function
|
|
914
|
+
function handleReadonlyValue(instance, peek, frozen, copy) {
|
|
914
915
|
let value;
|
|
915
916
|
if (peek) value = peekSignalValue.call(instance, copy === true);
|
|
916
917
|
else value = getSignalValue.call(instance);
|
|
917
918
|
return frozen ? getFrozenValue(value) : value;
|
|
918
919
|
}
|
|
920
|
+
function peekReadonlyValue(copy) {
|
|
921
|
+
return handleReadonlyValue(this, true, this[SUBSCRIPTION_TYPE_FROZEN], copy);
|
|
922
|
+
}
|
|
919
923
|
//#endregion
|
|
920
924
|
//#region src/value/signal.ts
|
|
921
925
|
function Signal(value, options) {
|
|
922
926
|
this[SYMBOL_STATE] = getState(void 0, options);
|
|
923
|
-
|
|
927
|
+
handleSignal(this, value, setAndEmit);
|
|
924
928
|
}
|
|
925
929
|
Signal.prototype[NAME_MORA] = NAME_SIGNAL;
|
|
926
|
-
Signal.prototype.asReadonly =
|
|
930
|
+
Signal.prototype.asReadonly = getReadonlySignal;
|
|
927
931
|
Signal.prototype.get = getSignalValue;
|
|
928
932
|
Signal.prototype.peek = peekSignalValue;
|
|
933
|
+
Signal.prototype.set = setSignalValue;
|
|
929
934
|
Signal.prototype.subscribe = subscribeToSignal;
|
|
930
935
|
Signal.prototype.toString = getStringValue;
|
|
931
|
-
Signal.prototype.
|
|
932
|
-
|
|
933
|
-
};
|
|
934
|
-
Signal.prototype.toJSON = function() {
|
|
935
|
-
return this[SYMBOL_STATE].value;
|
|
936
|
-
};
|
|
937
|
-
Signal.prototype.update = function(callback) {
|
|
938
|
-
return updateSignalValue(this, callback, setAndEmit);
|
|
939
|
-
};
|
|
936
|
+
Signal.prototype.toJSON = getJsonValue;
|
|
937
|
+
Signal.prototype.update = updateSignalValue;
|
|
940
938
|
function setAndEmit(instance, value) {
|
|
941
939
|
const state = instance[SYMBOL_STATE];
|
|
942
940
|
if (!(state.equal ?? Object.is)(state.value, value)) {
|
|
@@ -944,9 +942,15 @@ function setAndEmit(instance, value) {
|
|
|
944
942
|
emitValue(instance);
|
|
945
943
|
}
|
|
946
944
|
}
|
|
945
|
+
function setSignalValue(value) {
|
|
946
|
+
handleSignal(this, value, setAndEmit);
|
|
947
|
+
}
|
|
947
948
|
function signal(value, options) {
|
|
948
949
|
return new Signal(value, options);
|
|
949
950
|
}
|
|
951
|
+
function updateSignalValue(callback) {
|
|
952
|
+
updateSignal(this, callback, setAndEmit);
|
|
953
|
+
}
|
|
950
954
|
//#endregion
|
|
951
955
|
//#region src/value/array.ts
|
|
952
956
|
function ReactiveArray(value, options) {
|
|
@@ -959,64 +963,76 @@ function ReactiveArray(value, options) {
|
|
|
959
963
|
get: (target, property) => METHODS_UPDATE.has(property) ? updateArray(this, property, target) : Reflect.get(target, property),
|
|
960
964
|
set: (target, property, value) => setValueInProxy(this, target, property, value)
|
|
961
965
|
});
|
|
962
|
-
Object.defineProperty(this, PROPERTY_LENGTH, {
|
|
963
|
-
enumerable: true,
|
|
964
|
-
get: () => this[SYMBOL_STATE].length.get(),
|
|
965
|
-
set: (value) => setArrayLength(this[SYMBOL_STATE], value)
|
|
966
|
-
});
|
|
967
966
|
setProxyValue.call(this, value);
|
|
968
967
|
}
|
|
969
968
|
ReactiveArray.prototype[NAME_MORA] = NAME_ARRAY;
|
|
970
|
-
ReactiveArray.prototype
|
|
969
|
+
Object.defineProperty(ReactiveArray.prototype, PROPERTY_LENGTH, {
|
|
970
|
+
enumerable: true,
|
|
971
|
+
get: getArrayLength,
|
|
972
|
+
set: setArrayLength
|
|
973
|
+
});
|
|
974
|
+
ReactiveArray.prototype.asReadonly = getReadonlySignal;
|
|
971
975
|
ReactiveArray.prototype.at = getArrayValues;
|
|
976
|
+
ReactiveArray.prototype.clear = clearArrayValues;
|
|
977
|
+
ReactiveArray.prototype.filter = filterArrayValues;
|
|
972
978
|
ReactiveArray.prototype.get = getArrayValues;
|
|
979
|
+
ReactiveArray.prototype.map = mapArrayValues;
|
|
973
980
|
ReactiveArray.prototype.notify = emitProxyValues;
|
|
974
981
|
ReactiveArray.prototype.peek = peekArrayValue;
|
|
982
|
+
ReactiveArray.prototype.pop = popArrayValue;
|
|
983
|
+
ReactiveArray.prototype.push = pushArrayValues;
|
|
984
|
+
ReactiveArray.prototype.select = selectArrayValues;
|
|
985
|
+
ReactiveArray.prototype.shift = shiftArrayValue;
|
|
975
986
|
ReactiveArray.prototype.set = setProxyValue;
|
|
987
|
+
ReactiveArray.prototype.splice = spliceArrayValues;
|
|
976
988
|
ReactiveArray.prototype.subscribe = subscribeToProxy;
|
|
977
989
|
ReactiveArray.prototype.toString = getStringValue;
|
|
990
|
+
ReactiveArray.prototype.toJSON = getJsonValue;
|
|
991
|
+
ReactiveArray.prototype.unshift = unshiftArrayValues;
|
|
978
992
|
ReactiveArray.prototype.update = updateProxyValue;
|
|
979
|
-
ReactiveArray.prototype.clear = function() {
|
|
980
|
-
this[SYMBOL_STATE].value.length = 0;
|
|
981
|
-
};
|
|
982
|
-
ReactiveArray.prototype.filter = function(callback) {
|
|
983
|
-
return computed(() => filter(getArrayValues.call(this), callback));
|
|
984
|
-
};
|
|
985
|
-
ReactiveArray.prototype.map = function(callback) {
|
|
986
|
-
return computed(() => getArrayValues.call(this).map(callback));
|
|
987
|
-
};
|
|
988
|
-
ReactiveArray.prototype.pop = function() {
|
|
989
|
-
return this[SYMBOL_STATE].value.pop();
|
|
990
|
-
};
|
|
991
|
-
ReactiveArray.prototype.push = function(...items) {
|
|
992
|
-
return this[SYMBOL_STATE].value.push(...items);
|
|
993
|
-
};
|
|
994
|
-
ReactiveArray.prototype.select = function(filter, map) {
|
|
995
|
-
return computed(() => select(getArrayValues.call(this), filter, map));
|
|
996
|
-
};
|
|
997
|
-
ReactiveArray.prototype.shift = function() {
|
|
998
|
-
return this[SYMBOL_STATE].value.shift();
|
|
999
|
-
};
|
|
1000
|
-
ReactiveArray.prototype.splice = function(from, to, ...items) {
|
|
1001
|
-
return this[SYMBOL_STATE].value.splice(from, to ?? this[SYMBOL_STATE].value.length, ...items);
|
|
1002
|
-
};
|
|
1003
|
-
ReactiveArray.prototype.toJSON = function() {
|
|
1004
|
-
return this[SYMBOL_STATE].value;
|
|
1005
|
-
};
|
|
1006
|
-
ReactiveArray.prototype.unshift = function(...items) {
|
|
1007
|
-
return this[SYMBOL_STATE].value.unshift(...items);
|
|
1008
|
-
};
|
|
1009
993
|
function array(value, options) {
|
|
1010
994
|
return new ReactiveArray(value, options);
|
|
1011
995
|
}
|
|
996
|
+
function clearArrayValues() {
|
|
997
|
+
this[SYMBOL_STATE].value.length = 0;
|
|
998
|
+
}
|
|
999
|
+
function filterArrayValues(callback) {
|
|
1000
|
+
return computed(() => filter(getArrayValues.call(this), callback));
|
|
1001
|
+
}
|
|
1002
|
+
function getArrayLength() {
|
|
1003
|
+
return this[SYMBOL_STATE].length.get();
|
|
1004
|
+
}
|
|
1012
1005
|
function getArrayValues(value) {
|
|
1013
1006
|
return value === "length" ? this[SYMBOL_STATE].length.get() : getValueInProxy.call(this, value);
|
|
1014
1007
|
}
|
|
1008
|
+
function mapArrayValues(callback) {
|
|
1009
|
+
return computed(() => getArrayValues.call(this).map(callback));
|
|
1010
|
+
}
|
|
1015
1011
|
function peekArrayValue(first, second) {
|
|
1016
1012
|
return first === "length" ? this[SYMBOL_STATE].length.peek() : peekValueInProxy.call(this, first, second);
|
|
1017
1013
|
}
|
|
1018
|
-
function
|
|
1019
|
-
|
|
1014
|
+
function popArrayValue() {
|
|
1015
|
+
return this[SYMBOL_STATE].value.pop();
|
|
1016
|
+
}
|
|
1017
|
+
function pushArrayValues(...items) {
|
|
1018
|
+
return this[SYMBOL_STATE].value.push(...items);
|
|
1019
|
+
}
|
|
1020
|
+
function selectArrayValues(filter, map) {
|
|
1021
|
+
return computed(() => select(getArrayValues.call(this), filter, map));
|
|
1022
|
+
}
|
|
1023
|
+
function setArrayLength(value) {
|
|
1024
|
+
const values = this[SYMBOL_STATE].value;
|
|
1025
|
+
if (typeof value === "number" && !Number.isNaN(value) && value >= 0 && value !== values.length) values.length = value;
|
|
1026
|
+
}
|
|
1027
|
+
function shiftArrayValue() {
|
|
1028
|
+
return this[SYMBOL_STATE].value.shift();
|
|
1029
|
+
}
|
|
1030
|
+
function spliceArrayValues(from, to, ...items) {
|
|
1031
|
+
const values = this[SYMBOL_STATE].value;
|
|
1032
|
+
return values.splice(from, to ?? values.length, ...items);
|
|
1033
|
+
}
|
|
1034
|
+
function unshiftArrayValues(...items) {
|
|
1035
|
+
return this[SYMBOL_STATE].value.unshift(...items);
|
|
1020
1036
|
}
|
|
1021
1037
|
function updateArray(instance, type, array) {
|
|
1022
1038
|
const state = instance[SYMBOL_STATE];
|
|
@@ -1025,7 +1041,7 @@ function updateArray(instance, type, array) {
|
|
|
1025
1041
|
const previousLength = array.length;
|
|
1026
1042
|
return (...args) => {
|
|
1027
1043
|
const result = array[type](...args);
|
|
1028
|
-
if (affectsLength ? array.length !== previousLength : !equalArrays(
|
|
1044
|
+
if (affectsLength ? array.length !== previousLength : !equalArrays(previousArray, array, state.equal)) {
|
|
1029
1045
|
emitValue(instance);
|
|
1030
1046
|
state.length.set(array.length);
|
|
1031
1047
|
}
|
|
@@ -1043,17 +1059,15 @@ function ReactiveStore(value, options) {
|
|
|
1043
1059
|
setProxyValue.call(this, value);
|
|
1044
1060
|
}
|
|
1045
1061
|
ReactiveStore.prototype[NAME_MORA] = NAME_STORE;
|
|
1046
|
-
ReactiveStore.prototype.asReadonly =
|
|
1062
|
+
ReactiveStore.prototype.asReadonly = getReadonlySignal;
|
|
1047
1063
|
ReactiveStore.prototype.get = getValueInProxy;
|
|
1048
1064
|
ReactiveStore.prototype.notify = emitProxyValues;
|
|
1049
1065
|
ReactiveStore.prototype.peek = peekValueInProxy;
|
|
1050
1066
|
ReactiveStore.prototype.set = setProxyValue;
|
|
1051
1067
|
ReactiveStore.prototype.subscribe = subscribeToProxy;
|
|
1068
|
+
ReactiveStore.prototype.toJSON = getJsonValue;
|
|
1052
1069
|
ReactiveStore.prototype.toString = getStringValue;
|
|
1053
1070
|
ReactiveStore.prototype.update = updateProxyValue;
|
|
1054
|
-
ReactiveStore.prototype.toJSON = function() {
|
|
1055
|
-
return this[SYMBOL_STATE].value;
|
|
1056
|
-
};
|
|
1057
1071
|
function store(value, options) {
|
|
1058
1072
|
return new ReactiveStore(value, options);
|
|
1059
1073
|
}
|
package/dist/value/array.mjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { METHODS_AFFECTING_LENGTH, METHODS_UPDATE, NAME_ARRAY, NAME_MORA, PROPERTY_LENGTH, SYMBOL_STATE } from "../constants.mjs";
|
|
2
|
-
import { emitValue, equalArrays, getStringValue } from "../helpers/value.mjs";
|
|
2
|
+
import { emitValue, equalArrays, getJsonValue, getStringValue } from "../helpers/value.mjs";
|
|
3
3
|
import { getState } from "../helpers/misc.mjs";
|
|
4
4
|
import { subscribeToProxy } from "../helpers/subscription.mjs";
|
|
5
5
|
import { computed } from "./computed.mjs";
|
|
6
6
|
import { emitProxyValues, getValueInProxy, peekValueInProxy, setProxyValue, setValueInProxy, updateProxyValue } from "../helpers/proxy.mjs";
|
|
7
|
-
import {
|
|
7
|
+
import { getReadonlySignal } from "./readonly.mjs";
|
|
8
8
|
import { signal } from "./signal.mjs";
|
|
9
9
|
import { select } from "@oscarpalmer/atoms/array";
|
|
10
10
|
import { filter } from "@oscarpalmer/atoms/array/filter";
|
|
@@ -19,64 +19,76 @@ function ReactiveArray(value, options) {
|
|
|
19
19
|
get: (target, property) => METHODS_UPDATE.has(property) ? updateArray(this, property, target) : Reflect.get(target, property),
|
|
20
20
|
set: (target, property, value) => setValueInProxy(this, target, property, value)
|
|
21
21
|
});
|
|
22
|
-
Object.defineProperty(this, PROPERTY_LENGTH, {
|
|
23
|
-
enumerable: true,
|
|
24
|
-
get: () => this[SYMBOL_STATE].length.get(),
|
|
25
|
-
set: (value) => setArrayLength(this[SYMBOL_STATE], value)
|
|
26
|
-
});
|
|
27
22
|
setProxyValue.call(this, value);
|
|
28
23
|
}
|
|
29
24
|
ReactiveArray.prototype[NAME_MORA] = NAME_ARRAY;
|
|
30
|
-
ReactiveArray.prototype
|
|
25
|
+
Object.defineProperty(ReactiveArray.prototype, PROPERTY_LENGTH, {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
get: getArrayLength,
|
|
28
|
+
set: setArrayLength
|
|
29
|
+
});
|
|
30
|
+
ReactiveArray.prototype.asReadonly = getReadonlySignal;
|
|
31
31
|
ReactiveArray.prototype.at = getArrayValues;
|
|
32
|
+
ReactiveArray.prototype.clear = clearArrayValues;
|
|
33
|
+
ReactiveArray.prototype.filter = filterArrayValues;
|
|
32
34
|
ReactiveArray.prototype.get = getArrayValues;
|
|
35
|
+
ReactiveArray.prototype.map = mapArrayValues;
|
|
33
36
|
ReactiveArray.prototype.notify = emitProxyValues;
|
|
34
37
|
ReactiveArray.prototype.peek = peekArrayValue;
|
|
38
|
+
ReactiveArray.prototype.pop = popArrayValue;
|
|
39
|
+
ReactiveArray.prototype.push = pushArrayValues;
|
|
40
|
+
ReactiveArray.prototype.select = selectArrayValues;
|
|
41
|
+
ReactiveArray.prototype.shift = shiftArrayValue;
|
|
35
42
|
ReactiveArray.prototype.set = setProxyValue;
|
|
43
|
+
ReactiveArray.prototype.splice = spliceArrayValues;
|
|
36
44
|
ReactiveArray.prototype.subscribe = subscribeToProxy;
|
|
37
45
|
ReactiveArray.prototype.toString = getStringValue;
|
|
46
|
+
ReactiveArray.prototype.toJSON = getJsonValue;
|
|
47
|
+
ReactiveArray.prototype.unshift = unshiftArrayValues;
|
|
38
48
|
ReactiveArray.prototype.update = updateProxyValue;
|
|
39
|
-
ReactiveArray.prototype.clear = function() {
|
|
40
|
-
this[SYMBOL_STATE].value.length = 0;
|
|
41
|
-
};
|
|
42
|
-
ReactiveArray.prototype.filter = function(callback) {
|
|
43
|
-
return computed(() => filter(getArrayValues.call(this), callback));
|
|
44
|
-
};
|
|
45
|
-
ReactiveArray.prototype.map = function(callback) {
|
|
46
|
-
return computed(() => getArrayValues.call(this).map(callback));
|
|
47
|
-
};
|
|
48
|
-
ReactiveArray.prototype.pop = function() {
|
|
49
|
-
return this[SYMBOL_STATE].value.pop();
|
|
50
|
-
};
|
|
51
|
-
ReactiveArray.prototype.push = function(...items) {
|
|
52
|
-
return this[SYMBOL_STATE].value.push(...items);
|
|
53
|
-
};
|
|
54
|
-
ReactiveArray.prototype.select = function(filter, map) {
|
|
55
|
-
return computed(() => select(getArrayValues.call(this), filter, map));
|
|
56
|
-
};
|
|
57
|
-
ReactiveArray.prototype.shift = function() {
|
|
58
|
-
return this[SYMBOL_STATE].value.shift();
|
|
59
|
-
};
|
|
60
|
-
ReactiveArray.prototype.splice = function(from, to, ...items) {
|
|
61
|
-
return this[SYMBOL_STATE].value.splice(from, to ?? this[SYMBOL_STATE].value.length, ...items);
|
|
62
|
-
};
|
|
63
|
-
ReactiveArray.prototype.toJSON = function() {
|
|
64
|
-
return this[SYMBOL_STATE].value;
|
|
65
|
-
};
|
|
66
|
-
ReactiveArray.prototype.unshift = function(...items) {
|
|
67
|
-
return this[SYMBOL_STATE].value.unshift(...items);
|
|
68
|
-
};
|
|
69
49
|
function array(value, options) {
|
|
70
50
|
return new ReactiveArray(value, options);
|
|
71
51
|
}
|
|
52
|
+
function clearArrayValues() {
|
|
53
|
+
this[SYMBOL_STATE].value.length = 0;
|
|
54
|
+
}
|
|
55
|
+
function filterArrayValues(callback) {
|
|
56
|
+
return computed(() => filter(getArrayValues.call(this), callback));
|
|
57
|
+
}
|
|
58
|
+
function getArrayLength() {
|
|
59
|
+
return this[SYMBOL_STATE].length.get();
|
|
60
|
+
}
|
|
72
61
|
function getArrayValues(value) {
|
|
73
62
|
return value === "length" ? this[SYMBOL_STATE].length.get() : getValueInProxy.call(this, value);
|
|
74
63
|
}
|
|
64
|
+
function mapArrayValues(callback) {
|
|
65
|
+
return computed(() => getArrayValues.call(this).map(callback));
|
|
66
|
+
}
|
|
75
67
|
function peekArrayValue(first, second) {
|
|
76
68
|
return first === "length" ? this[SYMBOL_STATE].length.peek() : peekValueInProxy.call(this, first, second);
|
|
77
69
|
}
|
|
78
|
-
function
|
|
79
|
-
|
|
70
|
+
function popArrayValue() {
|
|
71
|
+
return this[SYMBOL_STATE].value.pop();
|
|
72
|
+
}
|
|
73
|
+
function pushArrayValues(...items) {
|
|
74
|
+
return this[SYMBOL_STATE].value.push(...items);
|
|
75
|
+
}
|
|
76
|
+
function selectArrayValues(filter, map) {
|
|
77
|
+
return computed(() => select(getArrayValues.call(this), filter, map));
|
|
78
|
+
}
|
|
79
|
+
function setArrayLength(value) {
|
|
80
|
+
const values = this[SYMBOL_STATE].value;
|
|
81
|
+
if (typeof value === "number" && !Number.isNaN(value) && value >= 0 && value !== values.length) values.length = value;
|
|
82
|
+
}
|
|
83
|
+
function shiftArrayValue() {
|
|
84
|
+
return this[SYMBOL_STATE].value.shift();
|
|
85
|
+
}
|
|
86
|
+
function spliceArrayValues(from, to, ...items) {
|
|
87
|
+
const values = this[SYMBOL_STATE].value;
|
|
88
|
+
return values.splice(from, to ?? values.length, ...items);
|
|
89
|
+
}
|
|
90
|
+
function unshiftArrayValues(...items) {
|
|
91
|
+
return this[SYMBOL_STATE].value.unshift(...items);
|
|
80
92
|
}
|
|
81
93
|
function updateArray(instance, type, array) {
|
|
82
94
|
const state = instance[SYMBOL_STATE];
|
|
@@ -85,7 +97,7 @@ function updateArray(instance, type, array) {
|
|
|
85
97
|
const previousLength = array.length;
|
|
86
98
|
return (...args) => {
|
|
87
99
|
const result = array[type](...args);
|
|
88
|
-
if (affectsLength ? array.length !== previousLength : !equalArrays(
|
|
100
|
+
if (affectsLength ? array.length !== previousLength : !equalArrays(previousArray, array, state.equal)) {
|
|
89
101
|
emitValue(instance);
|
|
90
102
|
state.length.set(array.length);
|
|
91
103
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Computed, ComputedEffect, ReactiveOptions } from "../models.mjs";
|
|
1
|
+
import { Computed, ComputedEffect, InternalComputed, ReactiveOptions } from "../models.mjs";
|
|
2
2
|
import { GenericCallback } from "@oscarpalmer/atoms/models";
|
|
3
3
|
//#region src/value/computed.d.ts
|
|
4
4
|
declare function Computed(this: any, callback: GenericCallback, options?: ReactiveOptions<unknown>): void;
|
|
@@ -10,5 +10,5 @@ declare function Computed(this: any, callback: GenericCallback, options?: Reacti
|
|
|
10
10
|
* @returns Computed value
|
|
11
11
|
*/
|
|
12
12
|
export declare function computed<Value>(callback: () => Value | Promise<Value>, options?: ReactiveOptions<Value>): Computed<Value>;
|
|
13
|
-
export declare function internalComputed(callback: GenericCallback, options?: ReactiveOptions<unknown>): [
|
|
13
|
+
export declare function internalComputed(callback: GenericCallback, options?: ReactiveOptions<unknown>): [InternalComputed, ComputedEffect];
|
|
14
14
|
//#endregion
|