@oscarpalmer/mora 0.18.2 → 0.20.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/proxy.cjs +13 -0
- package/dist/helpers/proxy.js +13 -0
- package/dist/mora.full.js +58 -14
- package/dist/value/array.cjs +33 -13
- package/dist/value/array.js +34 -14
- package/dist/value/signal.cjs +1 -1
- package/dist/value/signal.js +1 -1
- package/dist/value/store.cjs +28 -1
- package/dist/value/store.js +29 -2
- package/package.json +3 -3
- package/src/batch.ts +1 -1
- package/src/helpers/proxy.ts +45 -1
- package/src/index.ts +1 -0
- package/src/subscription.ts +2 -2
- package/src/value/array.ts +69 -13
- package/src/value/reactive.ts +2 -1
- package/src/value/signal.ts +1 -1
- package/src/value/store.ts +59 -3
- package/types/batch.d.cts +5 -5
- package/types/batch.d.ts +1 -1
- package/types/helpers/is.d.cts +43 -10
- package/types/helpers/proxy.d.cts +159 -5
- package/types/helpers/proxy.d.ts +6 -1
- package/types/helpers/value.d.cts +4 -4
- package/types/index.d.cts +43 -10
- package/types/subscription.d.cts +4 -4
- package/types/subscription.d.ts +3 -3
- package/types/value/array.d.cts +25 -9
- package/types/value/array.d.ts +22 -5
- package/types/value/computed.d.cts +4 -4
- package/types/value/reactive.d.cts +4 -4
- package/types/value/reactive.d.ts +2 -1
- package/types/value/signal.d.cts +5 -5
- package/types/value/signal.d.ts +1 -1
- package/types/value/store.d.cts +21 -4
- package/types/value/store.d.ts +18 -0
package/dist/helpers/proxy.cjs
CHANGED
|
@@ -1,7 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const batch = require("../batch.cjs");
|
|
4
|
+
const value_computed = require("../value/computed.cjs");
|
|
4
5
|
const helpers_value = require("./value.cjs");
|
|
6
|
+
function getReactiveValueInProxy(reactive, mapped, key, isArray) {
|
|
7
|
+
let item = mapped.get(key);
|
|
8
|
+
if (item == null) {
|
|
9
|
+
item = value_computed.computed(() => {
|
|
10
|
+
const value = reactive.get();
|
|
11
|
+
return isArray ? value.at(key) : value[key];
|
|
12
|
+
});
|
|
13
|
+
mapped.set(key, item);
|
|
14
|
+
}
|
|
15
|
+
return item;
|
|
16
|
+
}
|
|
5
17
|
function setProxyValue(proxy, value) {
|
|
6
18
|
batch.startBatch();
|
|
7
19
|
const proxyKeys = Object.keys(proxy);
|
|
@@ -39,5 +51,6 @@ function setValueInProxy(target, property, value, state, isArray, length) {
|
|
|
39
51
|
}
|
|
40
52
|
return true;
|
|
41
53
|
}
|
|
54
|
+
exports.getReactiveValueInProxy = getReactiveValueInProxy;
|
|
42
55
|
exports.setProxyValue = setProxyValue;
|
|
43
56
|
exports.setValueInProxy = setValueInProxy;
|
package/dist/helpers/proxy.js
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
import { startBatch, stopBatch } from "../batch.js";
|
|
2
|
+
import { computed } from "../value/computed.js";
|
|
2
3
|
import { emitValue } from "./value.js";
|
|
4
|
+
function getReactiveValueInProxy(reactive, mapped, key, isArray) {
|
|
5
|
+
let item = mapped.get(key);
|
|
6
|
+
if (item == null) {
|
|
7
|
+
item = computed(() => {
|
|
8
|
+
const value = reactive.get();
|
|
9
|
+
return isArray ? value.at(key) : value[key];
|
|
10
|
+
});
|
|
11
|
+
mapped.set(key, item);
|
|
12
|
+
}
|
|
13
|
+
return item;
|
|
14
|
+
}
|
|
3
15
|
function setProxyValue(proxy, value) {
|
|
4
16
|
startBatch();
|
|
5
17
|
const proxyKeys = Object.keys(proxy);
|
|
@@ -38,6 +50,7 @@ function setValueInProxy(target, property, value, state, isArray, length) {
|
|
|
38
50
|
return true;
|
|
39
51
|
}
|
|
40
52
|
export {
|
|
53
|
+
getReactiveValueInProxy,
|
|
41
54
|
setProxyValue,
|
|
42
55
|
setValueInProxy
|
|
43
56
|
};
|
package/dist/mora.full.js
CHANGED
|
@@ -278,6 +278,19 @@ function getValue(state) {
|
|
|
278
278
|
return state.value;
|
|
279
279
|
}
|
|
280
280
|
|
|
281
|
+
function getReactiveValueInProxy(reactive, mapped, key, isArray) {
|
|
282
|
+
let item = mapped.get(key);
|
|
283
|
+
if (item == null) {
|
|
284
|
+
item = computed(() => {
|
|
285
|
+
const value = reactive.get();
|
|
286
|
+
return isArray
|
|
287
|
+
? value.at(key)
|
|
288
|
+
: value[key];
|
|
289
|
+
});
|
|
290
|
+
mapped.set(key, item);
|
|
291
|
+
}
|
|
292
|
+
return item;
|
|
293
|
+
}
|
|
281
294
|
function setProxyValue(proxy, value) {
|
|
282
295
|
startBatch();
|
|
283
296
|
const proxyKeys = Object.keys(proxy);
|
|
@@ -338,7 +351,7 @@ class Signal extends Reactive {
|
|
|
338
351
|
}
|
|
339
352
|
}
|
|
340
353
|
/**
|
|
341
|
-
* Update the value
|
|
354
|
+
* Update the value _(based on the current value)_
|
|
342
355
|
*/
|
|
343
356
|
update(callback) {
|
|
344
357
|
this.set(callback(this.state.value));
|
|
@@ -352,6 +365,7 @@ function signal(value, options) {
|
|
|
352
365
|
}
|
|
353
366
|
|
|
354
367
|
class ReactiveArray extends Reactive {
|
|
368
|
+
#indiced = new Map();
|
|
355
369
|
#size = signal(0);
|
|
356
370
|
/**
|
|
357
371
|
* The length of the array
|
|
@@ -378,22 +392,19 @@ class ReactiveArray extends Reactive {
|
|
|
378
392
|
}), options);
|
|
379
393
|
this.#size.set(value.length);
|
|
380
394
|
}
|
|
381
|
-
/**
|
|
382
|
-
* Get an indexed item
|
|
383
|
-
*/
|
|
384
|
-
at(index) {
|
|
385
|
-
return this.get().at(index);
|
|
386
|
-
}
|
|
387
395
|
/**
|
|
388
396
|
* Clear the array
|
|
389
397
|
*/
|
|
390
398
|
clear() {
|
|
391
399
|
this.length = 0;
|
|
392
400
|
}
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
401
|
+
get(first) {
|
|
402
|
+
if (typeof first === 'number') {
|
|
403
|
+
return getReactiveValueInProxy(this, this.#indiced, first, true).get();
|
|
404
|
+
}
|
|
405
|
+
if (first === 'length') {
|
|
406
|
+
return this.length;
|
|
407
|
+
}
|
|
397
408
|
return getValue(this.state);
|
|
398
409
|
}
|
|
399
410
|
/**
|
|
@@ -424,8 +435,8 @@ class ReactiveArray extends Reactive {
|
|
|
424
435
|
return this.state.value.push(...items);
|
|
425
436
|
}
|
|
426
437
|
set(first, second) {
|
|
427
|
-
if (Array.isArray(first)) {
|
|
428
|
-
this.state.value.splice(0, this.state.value.length, ...first);
|
|
438
|
+
if (first == null || Array.isArray(first)) {
|
|
439
|
+
this.state.value.splice(0, this.state.value.length, ...(first ?? []));
|
|
429
440
|
}
|
|
430
441
|
else if (first === 'length') {
|
|
431
442
|
this.length = second;
|
|
@@ -446,12 +457,27 @@ class ReactiveArray extends Reactive {
|
|
|
446
457
|
splice(from, to, ...items) {
|
|
447
458
|
return this.state.value.splice(from, to ?? this.state.value.length, ...items);
|
|
448
459
|
}
|
|
460
|
+
subscribe(first, second) {
|
|
461
|
+
if (typeof first === 'number' && typeof second === 'function') {
|
|
462
|
+
return getReactiveValueInProxy(this, this.#indiced, first, true).subscribe(second);
|
|
463
|
+
}
|
|
464
|
+
return typeof first === 'function' ? subscribe(this.state, first) : noop;
|
|
465
|
+
}
|
|
449
466
|
/**
|
|
450
467
|
* Add items to the beginning of the array
|
|
451
468
|
*/
|
|
452
469
|
unshift(...items) {
|
|
453
470
|
return this.state.value.unshift(...items);
|
|
454
471
|
}
|
|
472
|
+
/**
|
|
473
|
+
* Update the value _(based on the current value)_
|
|
474
|
+
*/
|
|
475
|
+
update(callback) {
|
|
476
|
+
const updated = callback(this.state.value);
|
|
477
|
+
if (updated == null || Array.isArray(updated)) {
|
|
478
|
+
this.set(updated);
|
|
479
|
+
}
|
|
480
|
+
}
|
|
455
481
|
}
|
|
456
482
|
/**
|
|
457
483
|
* Create a reactive array
|
|
@@ -504,13 +530,16 @@ function isPlainObject(value) {
|
|
|
504
530
|
}
|
|
505
531
|
|
|
506
532
|
class Store extends Reactive {
|
|
533
|
+
#keyed = new Map();
|
|
507
534
|
constructor(value, options) {
|
|
508
535
|
super(storeName, new Proxy(value, {
|
|
509
536
|
set: (target, property, value) => setValueInProxy(target, property, value, this.state, false),
|
|
510
537
|
}), options);
|
|
511
538
|
}
|
|
512
539
|
get(key) {
|
|
513
|
-
return isKey(key)
|
|
540
|
+
return isKey(key)
|
|
541
|
+
? getReactiveValueInProxy(this, this.#keyed, key, false).get()
|
|
542
|
+
: getValue(this.state);
|
|
514
543
|
}
|
|
515
544
|
peek(key) {
|
|
516
545
|
return isKey(key) ? this.state.value[key] : { ...this.state.value };
|
|
@@ -523,6 +552,21 @@ class Store extends Reactive {
|
|
|
523
552
|
setProxyValue(this.state.value, first ?? {});
|
|
524
553
|
}
|
|
525
554
|
}
|
|
555
|
+
subscribe(first, second) {
|
|
556
|
+
if (isKey(first) && typeof second === 'function') {
|
|
557
|
+
return getReactiveValueInProxy(this, this.#keyed, first, false).subscribe(second);
|
|
558
|
+
}
|
|
559
|
+
return typeof first === 'function' ? subscribe(this.state, first) : noop;
|
|
560
|
+
}
|
|
561
|
+
/**
|
|
562
|
+
* Update the value _(based on the current value)_
|
|
563
|
+
*/
|
|
564
|
+
update(callback) {
|
|
565
|
+
const updated = callback({ ...this.state.value });
|
|
566
|
+
if (updated == null || isPlainObject(updated)) {
|
|
567
|
+
setProxyValue(this.state.value, updated ?? {});
|
|
568
|
+
}
|
|
569
|
+
}
|
|
526
570
|
}
|
|
527
571
|
/**
|
|
528
572
|
* Create a reactive store
|
package/dist/value/array.cjs
CHANGED
|
@@ -5,11 +5,12 @@ var __typeError = (msg) => {
|
|
|
5
5
|
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
6
6
|
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
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
|
-
var _size;
|
|
8
|
+
var _indiced, _size;
|
|
9
9
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
10
10
|
const helpers_is = require("../helpers/is.cjs");
|
|
11
11
|
const helpers_proxy = require("../helpers/proxy.cjs");
|
|
12
12
|
const helpers_value = require("../helpers/value.cjs");
|
|
13
|
+
const subscription = require("../subscription.cjs");
|
|
13
14
|
const value_computed = require("./computed.cjs");
|
|
14
15
|
const value_reactive = require("./reactive.cjs");
|
|
15
16
|
const value_signal = require("./signal.cjs");
|
|
@@ -30,6 +31,7 @@ class ReactiveArray extends value_reactive.Reactive {
|
|
|
30
31
|
}),
|
|
31
32
|
options
|
|
32
33
|
);
|
|
34
|
+
__privateAdd(this, _indiced, /* @__PURE__ */ new Map());
|
|
33
35
|
__privateAdd(this, _size, value_signal.signal(0));
|
|
34
36
|
__privateGet(this, _size).set(value.length);
|
|
35
37
|
}
|
|
@@ -47,22 +49,19 @@ class ReactiveArray extends value_reactive.Reactive {
|
|
|
47
49
|
this.get().length = value;
|
|
48
50
|
}
|
|
49
51
|
}
|
|
50
|
-
/**
|
|
51
|
-
* Get an indexed item
|
|
52
|
-
*/
|
|
53
|
-
at(index) {
|
|
54
|
-
return this.get().at(index);
|
|
55
|
-
}
|
|
56
52
|
/**
|
|
57
53
|
* Clear the array
|
|
58
54
|
*/
|
|
59
55
|
clear() {
|
|
60
56
|
this.length = 0;
|
|
61
57
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
58
|
+
get(first) {
|
|
59
|
+
if (typeof first === "number") {
|
|
60
|
+
return helpers_proxy.getReactiveValueInProxy(this, __privateGet(this, _indiced), first, true).get();
|
|
61
|
+
}
|
|
62
|
+
if (first === "length") {
|
|
63
|
+
return this.length;
|
|
64
|
+
}
|
|
66
65
|
return helpers_value.getValue(this.state);
|
|
67
66
|
}
|
|
68
67
|
/**
|
|
@@ -93,8 +92,8 @@ class ReactiveArray extends value_reactive.Reactive {
|
|
|
93
92
|
return this.state.value.push(...items);
|
|
94
93
|
}
|
|
95
94
|
set(first, second) {
|
|
96
|
-
if (Array.isArray(first)) {
|
|
97
|
-
this.state.value.splice(0, this.state.value.length, ...first);
|
|
95
|
+
if (first == null || Array.isArray(first)) {
|
|
96
|
+
this.state.value.splice(0, this.state.value.length, ...first ?? []);
|
|
98
97
|
} else if (first === "length") {
|
|
99
98
|
this.length = second;
|
|
100
99
|
} else if (typeof first === "number") {
|
|
@@ -117,13 +116,34 @@ class ReactiveArray extends value_reactive.Reactive {
|
|
|
117
116
|
...items
|
|
118
117
|
);
|
|
119
118
|
}
|
|
119
|
+
subscribe(first, second) {
|
|
120
|
+
if (typeof first === "number" && typeof second === "function") {
|
|
121
|
+
return helpers_proxy.getReactiveValueInProxy(
|
|
122
|
+
this,
|
|
123
|
+
__privateGet(this, _indiced),
|
|
124
|
+
first,
|
|
125
|
+
true
|
|
126
|
+
).subscribe(second);
|
|
127
|
+
}
|
|
128
|
+
return typeof first === "function" ? subscription.subscribe(this.state, first) : subscription.noop;
|
|
129
|
+
}
|
|
120
130
|
/**
|
|
121
131
|
* Add items to the beginning of the array
|
|
122
132
|
*/
|
|
123
133
|
unshift(...items) {
|
|
124
134
|
return this.state.value.unshift(...items);
|
|
125
135
|
}
|
|
136
|
+
/**
|
|
137
|
+
* Update the value _(based on the current value)_
|
|
138
|
+
*/
|
|
139
|
+
update(callback) {
|
|
140
|
+
const updated = callback(this.state.value);
|
|
141
|
+
if (updated == null || Array.isArray(updated)) {
|
|
142
|
+
this.set(updated);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
126
145
|
}
|
|
146
|
+
_indiced = new WeakMap();
|
|
127
147
|
_size = new WeakMap();
|
|
128
148
|
function array(value, options) {
|
|
129
149
|
return new ReactiveArray(Array.isArray(value) ? value : [], options);
|
package/dist/value/array.js
CHANGED
|
@@ -4,10 +4,11 @@ var __typeError = (msg) => {
|
|
|
4
4
|
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
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
|
-
var _size;
|
|
7
|
+
var _indiced, _size;
|
|
8
8
|
import { arrayName } from "../helpers/is.js";
|
|
9
|
-
import { setValueInProxy } from "../helpers/proxy.js";
|
|
9
|
+
import { setValueInProxy, getReactiveValueInProxy } from "../helpers/proxy.js";
|
|
10
10
|
import { getValue, equalArrays, emitValue } from "../helpers/value.js";
|
|
11
|
+
import { subscribe, noop } from "../subscription.js";
|
|
11
12
|
import { computed } from "./computed.js";
|
|
12
13
|
import { Reactive } from "./reactive.js";
|
|
13
14
|
import { signal } from "./signal.js";
|
|
@@ -28,6 +29,7 @@ class ReactiveArray extends Reactive {
|
|
|
28
29
|
}),
|
|
29
30
|
options
|
|
30
31
|
);
|
|
32
|
+
__privateAdd(this, _indiced, /* @__PURE__ */ new Map());
|
|
31
33
|
__privateAdd(this, _size, signal(0));
|
|
32
34
|
__privateGet(this, _size).set(value.length);
|
|
33
35
|
}
|
|
@@ -45,22 +47,19 @@ class ReactiveArray extends Reactive {
|
|
|
45
47
|
this.get().length = value;
|
|
46
48
|
}
|
|
47
49
|
}
|
|
48
|
-
/**
|
|
49
|
-
* Get an indexed item
|
|
50
|
-
*/
|
|
51
|
-
at(index) {
|
|
52
|
-
return this.get().at(index);
|
|
53
|
-
}
|
|
54
50
|
/**
|
|
55
51
|
* Clear the array
|
|
56
52
|
*/
|
|
57
53
|
clear() {
|
|
58
54
|
this.length = 0;
|
|
59
55
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
56
|
+
get(first) {
|
|
57
|
+
if (typeof first === "number") {
|
|
58
|
+
return getReactiveValueInProxy(this, __privateGet(this, _indiced), first, true).get();
|
|
59
|
+
}
|
|
60
|
+
if (first === "length") {
|
|
61
|
+
return this.length;
|
|
62
|
+
}
|
|
64
63
|
return getValue(this.state);
|
|
65
64
|
}
|
|
66
65
|
/**
|
|
@@ -91,8 +90,8 @@ class ReactiveArray extends Reactive {
|
|
|
91
90
|
return this.state.value.push(...items);
|
|
92
91
|
}
|
|
93
92
|
set(first, second) {
|
|
94
|
-
if (Array.isArray(first)) {
|
|
95
|
-
this.state.value.splice(0, this.state.value.length, ...first);
|
|
93
|
+
if (first == null || Array.isArray(first)) {
|
|
94
|
+
this.state.value.splice(0, this.state.value.length, ...first ?? []);
|
|
96
95
|
} else if (first === "length") {
|
|
97
96
|
this.length = second;
|
|
98
97
|
} else if (typeof first === "number") {
|
|
@@ -115,13 +114,34 @@ class ReactiveArray extends Reactive {
|
|
|
115
114
|
...items
|
|
116
115
|
);
|
|
117
116
|
}
|
|
117
|
+
subscribe(first, second) {
|
|
118
|
+
if (typeof first === "number" && typeof second === "function") {
|
|
119
|
+
return getReactiveValueInProxy(
|
|
120
|
+
this,
|
|
121
|
+
__privateGet(this, _indiced),
|
|
122
|
+
first,
|
|
123
|
+
true
|
|
124
|
+
).subscribe(second);
|
|
125
|
+
}
|
|
126
|
+
return typeof first === "function" ? subscribe(this.state, first) : noop;
|
|
127
|
+
}
|
|
118
128
|
/**
|
|
119
129
|
* Add items to the beginning of the array
|
|
120
130
|
*/
|
|
121
131
|
unshift(...items) {
|
|
122
132
|
return this.state.value.unshift(...items);
|
|
123
133
|
}
|
|
134
|
+
/**
|
|
135
|
+
* Update the value _(based on the current value)_
|
|
136
|
+
*/
|
|
137
|
+
update(callback) {
|
|
138
|
+
const updated = callback(this.state.value);
|
|
139
|
+
if (updated == null || Array.isArray(updated)) {
|
|
140
|
+
this.set(updated);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
124
143
|
}
|
|
144
|
+
_indiced = new WeakMap();
|
|
125
145
|
_size = new WeakMap();
|
|
126
146
|
function array(value, options) {
|
|
127
147
|
return new ReactiveArray(Array.isArray(value) ? value : [], options);
|
package/dist/value/signal.cjs
CHANGED
package/dist/value/signal.js
CHANGED
package/dist/value/store.cjs
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __typeError = (msg) => {
|
|
3
|
+
throw TypeError(msg);
|
|
4
|
+
};
|
|
5
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
6
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
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
|
+
var _keyed;
|
|
2
9
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
10
|
const is = require("../node_modules/@oscarpalmer/atoms/dist/internal/is.cjs");
|
|
4
11
|
const helpers_is = require("../helpers/is.cjs");
|
|
5
12
|
const helpers_proxy = require("../helpers/proxy.cjs");
|
|
6
13
|
const helpers_value = require("../helpers/value.cjs");
|
|
14
|
+
const subscription = require("../subscription.cjs");
|
|
7
15
|
const value_reactive = require("./reactive.cjs");
|
|
8
16
|
class Store extends value_reactive.Reactive {
|
|
9
17
|
constructor(value, options) {
|
|
@@ -14,9 +22,10 @@ class Store extends value_reactive.Reactive {
|
|
|
14
22
|
}),
|
|
15
23
|
options
|
|
16
24
|
);
|
|
25
|
+
__privateAdd(this, _keyed, /* @__PURE__ */ new Map());
|
|
17
26
|
}
|
|
18
27
|
get(key) {
|
|
19
|
-
return is.isKey(key) ? this
|
|
28
|
+
return is.isKey(key) ? helpers_proxy.getReactiveValueInProxy(this, __privateGet(this, _keyed), key, false).get() : helpers_value.getValue(this.state);
|
|
20
29
|
}
|
|
21
30
|
peek(key) {
|
|
22
31
|
return is.isKey(key) ? this.state.value[key] : { ...this.state.value };
|
|
@@ -28,7 +37,25 @@ class Store extends value_reactive.Reactive {
|
|
|
28
37
|
helpers_proxy.setProxyValue(this.state.value, first ?? {});
|
|
29
38
|
}
|
|
30
39
|
}
|
|
40
|
+
subscribe(first, second) {
|
|
41
|
+
if (is.isKey(first) && typeof second === "function") {
|
|
42
|
+
return helpers_proxy.getReactiveValueInProxy(this, __privateGet(this, _keyed), first, false).subscribe(
|
|
43
|
+
second
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
return typeof first === "function" ? subscription.subscribe(this.state, first) : subscription.noop;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Update the value _(based on the current value)_
|
|
50
|
+
*/
|
|
51
|
+
update(callback) {
|
|
52
|
+
const updated = callback({ ...this.state.value });
|
|
53
|
+
if (updated == null || is.isPlainObject(updated)) {
|
|
54
|
+
helpers_proxy.setProxyValue(this.state.value, updated ?? {});
|
|
55
|
+
}
|
|
56
|
+
}
|
|
31
57
|
}
|
|
58
|
+
_keyed = new WeakMap();
|
|
32
59
|
function store(value, options) {
|
|
33
60
|
return new Store(is.isPlainObject(value) ? value : {}, options);
|
|
34
61
|
}
|
package/dist/value/store.js
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
|
+
var __typeError = (msg) => {
|
|
2
|
+
throw TypeError(msg);
|
|
3
|
+
};
|
|
4
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
5
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
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
|
+
var _keyed;
|
|
1
8
|
import { isPlainObject, isKey } from "../node_modules/@oscarpalmer/atoms/dist/internal/is.js";
|
|
2
9
|
import { storeName } from "../helpers/is.js";
|
|
3
|
-
import { setValueInProxy, setProxyValue } from "../helpers/proxy.js";
|
|
10
|
+
import { setValueInProxy, getReactiveValueInProxy, setProxyValue } from "../helpers/proxy.js";
|
|
4
11
|
import { getValue } from "../helpers/value.js";
|
|
12
|
+
import { subscribe, noop } from "../subscription.js";
|
|
5
13
|
import { Reactive } from "./reactive.js";
|
|
6
14
|
class Store extends Reactive {
|
|
7
15
|
constructor(value, options) {
|
|
@@ -12,9 +20,10 @@ class Store extends Reactive {
|
|
|
12
20
|
}),
|
|
13
21
|
options
|
|
14
22
|
);
|
|
23
|
+
__privateAdd(this, _keyed, /* @__PURE__ */ new Map());
|
|
15
24
|
}
|
|
16
25
|
get(key) {
|
|
17
|
-
return isKey(key) ? this
|
|
26
|
+
return isKey(key) ? getReactiveValueInProxy(this, __privateGet(this, _keyed), key, false).get() : getValue(this.state);
|
|
18
27
|
}
|
|
19
28
|
peek(key) {
|
|
20
29
|
return isKey(key) ? this.state.value[key] : { ...this.state.value };
|
|
@@ -26,7 +35,25 @@ class Store extends Reactive {
|
|
|
26
35
|
setProxyValue(this.state.value, first ?? {});
|
|
27
36
|
}
|
|
28
37
|
}
|
|
38
|
+
subscribe(first, second) {
|
|
39
|
+
if (isKey(first) && typeof second === "function") {
|
|
40
|
+
return getReactiveValueInProxy(this, __privateGet(this, _keyed), first, false).subscribe(
|
|
41
|
+
second
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
return typeof first === "function" ? subscribe(this.state, first) : noop;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Update the value _(based on the current value)_
|
|
48
|
+
*/
|
|
49
|
+
update(callback) {
|
|
50
|
+
const updated = callback({ ...this.state.value });
|
|
51
|
+
if (updated == null || isPlainObject(updated)) {
|
|
52
|
+
setProxyValue(this.state.value, updated ?? {});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
29
55
|
}
|
|
56
|
+
_keyed = new WeakMap();
|
|
30
57
|
function store(value, options) {
|
|
31
58
|
return new Store(isPlainObject(value) ? value : {}, options);
|
|
32
59
|
}
|
package/package.json
CHANGED
|
@@ -4,14 +4,14 @@
|
|
|
4
4
|
"url": "https://oscarpalmer.se"
|
|
5
5
|
},
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"@oscarpalmer/atoms": "^0.
|
|
7
|
+
"@oscarpalmer/atoms": "^0.102"
|
|
8
8
|
},
|
|
9
9
|
"description": "Signals and stuff…",
|
|
10
10
|
"devDependencies": {
|
|
11
11
|
"@biomejs/biome": "^1.9",
|
|
12
12
|
"@rollup/plugin-node-resolve": "^16",
|
|
13
13
|
"@rollup/plugin-typescript": "^12.1",
|
|
14
|
-
"@types/node": "^
|
|
14
|
+
"@types/node": "^24",
|
|
15
15
|
"@vitest/coverage-istanbul": "^3.2",
|
|
16
16
|
"dts-bundle-generator": "^9.5",
|
|
17
17
|
"glob": "^11",
|
|
@@ -54,5 +54,5 @@
|
|
|
54
54
|
},
|
|
55
55
|
"type": "module",
|
|
56
56
|
"types": "./types/index.d.ts",
|
|
57
|
-
"version": "0.
|
|
57
|
+
"version": "0.20.0"
|
|
58
58
|
}
|
package/src/batch.ts
CHANGED
package/src/helpers/proxy.ts
CHANGED
|
@@ -1,9 +1,53 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
ArrayOrPlainObject,
|
|
3
|
+
Key,
|
|
4
|
+
PlainObject,
|
|
5
|
+
} from '@oscarpalmer/atoms/models';
|
|
2
6
|
import {startBatch, stopBatch} from '../batch';
|
|
7
|
+
import type {ReactiveArray} from '../value/array';
|
|
8
|
+
import {type Computed, computed} from '../value/computed';
|
|
3
9
|
import type {ReactiveState} from '../value/reactive';
|
|
4
10
|
import type {Signal} from '../value/signal';
|
|
11
|
+
import type {Store} from '../value/store';
|
|
5
12
|
import {emitValue} from './value';
|
|
6
13
|
|
|
14
|
+
export function getReactiveValueInProxy<Value>(
|
|
15
|
+
array: ReactiveArray<Value>,
|
|
16
|
+
mapped: Map<Key, Computed<unknown>>,
|
|
17
|
+
index: number,
|
|
18
|
+
isArray: true,
|
|
19
|
+
): Computed<unknown>;
|
|
20
|
+
|
|
21
|
+
export function getReactiveValueInProxy<Value extends PlainObject>(
|
|
22
|
+
store: Store<Value>,
|
|
23
|
+
mapped: Map<Key, Computed<unknown>>,
|
|
24
|
+
key: Key,
|
|
25
|
+
isArray: false,
|
|
26
|
+
): Computed<unknown>;
|
|
27
|
+
|
|
28
|
+
export function getReactiveValueInProxy(
|
|
29
|
+
reactive: ReactiveArray<unknown> | Store<PlainObject>,
|
|
30
|
+
mapped: Map<Key, Computed<unknown>>,
|
|
31
|
+
key: Key,
|
|
32
|
+
isArray: boolean,
|
|
33
|
+
): Computed<unknown> {
|
|
34
|
+
let item = mapped.get(key);
|
|
35
|
+
|
|
36
|
+
if (item == null) {
|
|
37
|
+
item = computed(() => {
|
|
38
|
+
const value = reactive.get();
|
|
39
|
+
|
|
40
|
+
return isArray
|
|
41
|
+
? (value as unknown[]).at(key as number)
|
|
42
|
+
: (value as PlainObject)[key];
|
|
43
|
+
}) as Computed<unknown>;
|
|
44
|
+
|
|
45
|
+
mapped.set(key, item);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return item;
|
|
49
|
+
}
|
|
50
|
+
|
|
7
51
|
export function setProxyValue(
|
|
8
52
|
proxy: ArrayOrPlainObject,
|
|
9
53
|
value: ArrayOrPlainObject,
|
package/src/index.ts
CHANGED
package/src/subscription.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type {GenericCallback} from '@oscarpalmer/atoms/models';
|
|
2
2
|
import type {ReactiveState} from './value/reactive';
|
|
3
3
|
|
|
4
|
-
export class Subscription
|
|
4
|
+
export class Subscription {
|
|
5
5
|
constructor(
|
|
6
|
-
public state: ReactiveState<
|
|
6
|
+
public state: ReactiveState<unknown, never>,
|
|
7
7
|
public callback: GenericCallback,
|
|
8
8
|
) {
|
|
9
9
|
callback(state.value);
|