@oscarpalmer/mora 0.20.0 → 0.22.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.js +11 -24
- package/dist/effect.js +17 -26
- package/dist/helpers/is.js +14 -21
- package/dist/helpers/proxy.js +34 -47
- package/dist/helpers/value.js +20 -46
- package/dist/index.js +4 -17
- package/dist/mora.full.js +8 -2
- package/dist/subscription.js +20 -28
- package/dist/value/array.js +94 -173
- package/dist/value/computed.js +34 -56
- package/dist/value/reactive.js +29 -53
- package/dist/value/signal.js +20 -32
- package/dist/value/store.js +30 -59
- package/package.json +12 -18
- package/src/helpers/value.ts +1 -1
- package/src/index.ts +0 -1
- package/src/value/array.ts +13 -3
- package/types/value/array.d.ts +1 -0
- package/dist/batch.cjs +0 -32
- package/dist/effect.cjs +0 -30
- package/dist/helpers/is.cjs +0 -40
- package/dist/helpers/proxy.cjs +0 -56
- package/dist/helpers/value.cjs +0 -54
- package/dist/index.cjs +0 -21
- package/dist/node_modules/@oscarpalmer/atoms/dist/internal/is.cjs +0 -17
- package/dist/node_modules/@oscarpalmer/atoms/dist/internal/is.js +0 -17
- package/dist/subscription.cjs +0 -32
- package/dist/value/array.cjs +0 -181
- package/dist/value/computed.cjs +0 -60
- package/dist/value/reactive.cjs +0 -55
- package/dist/value/signal.cjs +0 -36
- package/dist/value/store.cjs +0 -63
- package/types/batch.d.cts +0 -79
- package/types/effect.d.cts +0 -16
- package/types/helpers/is.d.cts +0 -258
- package/types/helpers/proxy.d.cts +0 -243
- package/types/helpers/value.d.cts +0 -71
- package/types/index.d.cts +0 -280
- package/types/subscription.d.cts +0 -71
- package/types/value/array.d.cts +0 -160
- package/types/value/computed.d.cts +0 -81
- package/types/value/reactive.d.cts +0 -68
- package/types/value/signal.d.cts +0 -87
- package/types/value/store.d.cts +0 -136
package/dist/value/array.js
CHANGED
|
@@ -1,181 +1,102 @@
|
|
|
1
|
-
|
|
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 _indiced, _size;
|
|
1
|
+
import { noop, subscribe } from "../subscription.js";
|
|
8
2
|
import { arrayName } from "../helpers/is.js";
|
|
9
|
-
import { setValueInProxy, getReactiveValueInProxy } from "../helpers/proxy.js";
|
|
10
|
-
import { getValue, equalArrays, emitValue } from "../helpers/value.js";
|
|
11
|
-
import { subscribe, noop } from "../subscription.js";
|
|
12
|
-
import { computed } from "./computed.js";
|
|
13
3
|
import { Reactive } from "./reactive.js";
|
|
4
|
+
import { computed } from "./computed.js";
|
|
5
|
+
import { emitValue, equalArrays, getValue } from "../helpers/value.js";
|
|
6
|
+
import { getReactiveValueInProxy, setValueInProxy } from "../helpers/proxy.js";
|
|
14
7
|
import { signal } from "./signal.js";
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* Remove and return the last item of the array
|
|
82
|
-
*/
|
|
83
|
-
pop() {
|
|
84
|
-
return this.state.value.pop();
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Add items to the end of the array
|
|
88
|
-
*/
|
|
89
|
-
push(...items) {
|
|
90
|
-
return this.state.value.push(...items);
|
|
91
|
-
}
|
|
92
|
-
set(first, second) {
|
|
93
|
-
if (first == null || Array.isArray(first)) {
|
|
94
|
-
this.state.value.splice(0, this.state.value.length, ...first ?? []);
|
|
95
|
-
} else if (first === "length") {
|
|
96
|
-
this.length = second;
|
|
97
|
-
} else if (typeof first === "number") {
|
|
98
|
-
this.state.value[first] = second;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Remove and return the first item of the array
|
|
103
|
-
*/
|
|
104
|
-
shift() {
|
|
105
|
-
return this.state.value.shift();
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Remove and return items from the array _(and optionally add new items)_
|
|
109
|
-
*/
|
|
110
|
-
splice(from, to, ...items) {
|
|
111
|
-
return this.state.value.splice(
|
|
112
|
-
from,
|
|
113
|
-
to ?? this.state.value.length,
|
|
114
|
-
...items
|
|
115
|
-
);
|
|
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
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* Add items to the beginning of the array
|
|
130
|
-
*/
|
|
131
|
-
unshift(...items) {
|
|
132
|
-
return this.state.value.unshift(...items);
|
|
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
|
-
}
|
|
143
|
-
}
|
|
144
|
-
_indiced = new WeakMap();
|
|
145
|
-
_size = new WeakMap();
|
|
8
|
+
var ReactiveArray = class extends Reactive {
|
|
9
|
+
#indiced = /* @__PURE__ */ new Map();
|
|
10
|
+
#size = signal(0);
|
|
11
|
+
get length() {
|
|
12
|
+
return this.#size.get();
|
|
13
|
+
}
|
|
14
|
+
set length(value) {
|
|
15
|
+
if (typeof value === "number" && value >= 0 && value !== this.state.value.length) this.get().length = value;
|
|
16
|
+
}
|
|
17
|
+
constructor(value, options) {
|
|
18
|
+
super(arrayName, new Proxy(value, {
|
|
19
|
+
get: (target, property) => updateMethods.has(property) ? updateArray(property, target, this.state, this.#size) : Reflect.get(target, property),
|
|
20
|
+
set: (target, property, value$1) => setValueInProxy(target, property, value$1, this.state, true, this.#size)
|
|
21
|
+
}), options);
|
|
22
|
+
this.#size.set(value.length);
|
|
23
|
+
}
|
|
24
|
+
clear() {
|
|
25
|
+
this.length = 0;
|
|
26
|
+
}
|
|
27
|
+
get(first) {
|
|
28
|
+
if (typeof first === "number") return getReactiveValueInProxy(this, this.#indiced, first, true).get();
|
|
29
|
+
if (first === "length") return this.length;
|
|
30
|
+
return getValue(this.state);
|
|
31
|
+
}
|
|
32
|
+
filter(callback) {
|
|
33
|
+
return computed(() => this.get().filter(callback));
|
|
34
|
+
}
|
|
35
|
+
map(callback) {
|
|
36
|
+
return computed(() => this.get().map(callback));
|
|
37
|
+
}
|
|
38
|
+
peek(value) {
|
|
39
|
+
if (value === true) return this.#size.peek();
|
|
40
|
+
if (typeof value === "number") return this.state.value.at(value);
|
|
41
|
+
return [...this.state.value];
|
|
42
|
+
}
|
|
43
|
+
pop() {
|
|
44
|
+
return this.state.value.pop();
|
|
45
|
+
}
|
|
46
|
+
push(...items) {
|
|
47
|
+
return this.state.value.push(...items);
|
|
48
|
+
}
|
|
49
|
+
set(first, second) {
|
|
50
|
+
if (first == null || Array.isArray(first)) this.state.value.splice(0, this.state.value.length, ...first ?? []);
|
|
51
|
+
else if (first === "length") this.length = second;
|
|
52
|
+
else if (typeof first === "number") this.state.value[first] = second;
|
|
53
|
+
}
|
|
54
|
+
shift() {
|
|
55
|
+
return this.state.value.shift();
|
|
56
|
+
}
|
|
57
|
+
splice(from, to, ...items) {
|
|
58
|
+
return this.state.value.splice(from, to ?? this.state.value.length, ...items);
|
|
59
|
+
}
|
|
60
|
+
subscribe(first, second) {
|
|
61
|
+
if (typeof first === "number" && typeof second === "function") return getReactiveValueInProxy(this, this.#indiced, first, true).subscribe(second);
|
|
62
|
+
return typeof first === "function" ? subscribe(this.state, first) : noop;
|
|
63
|
+
}
|
|
64
|
+
unshift(...items) {
|
|
65
|
+
return this.state.value.unshift(...items);
|
|
66
|
+
}
|
|
67
|
+
update(callback) {
|
|
68
|
+
const updated = callback(this.state.value);
|
|
69
|
+
if (updated == null || Array.isArray(updated)) this.set(updated);
|
|
70
|
+
}
|
|
71
|
+
};
|
|
146
72
|
function array(value, options) {
|
|
147
|
-
|
|
73
|
+
return new ReactiveArray(Array.isArray(value) ? value : [], options);
|
|
148
74
|
}
|
|
149
|
-
function updateArray(type,
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
return result;
|
|
162
|
-
};
|
|
75
|
+
function updateArray(type, array$1, state, length) {
|
|
76
|
+
const affectsLength = lengthAffectingMethods.has(type);
|
|
77
|
+
const previousArray = affectsLength ? [] : [...array$1];
|
|
78
|
+
const previousLength = array$1.length;
|
|
79
|
+
return (...args) => {
|
|
80
|
+
const result = array$1[type](...args);
|
|
81
|
+
if (affectsLength ? array$1.length !== previousLength : !equalArrays(state, previousArray, array$1)) {
|
|
82
|
+
emitValue(state);
|
|
83
|
+
length.set(array$1.length);
|
|
84
|
+
}
|
|
85
|
+
return result;
|
|
86
|
+
};
|
|
163
87
|
}
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
88
|
+
var lengthAffectingMethods = new Set([
|
|
89
|
+
"pop",
|
|
90
|
+
"push",
|
|
91
|
+
"shift",
|
|
92
|
+
"unshift"
|
|
169
93
|
]);
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
94
|
+
var updateMethods = new Set([
|
|
95
|
+
...lengthAffectingMethods,
|
|
96
|
+
"copyWithin",
|
|
97
|
+
"fill",
|
|
98
|
+
"reverse",
|
|
99
|
+
"sort",
|
|
100
|
+
"splice"
|
|
177
101
|
]);
|
|
178
|
-
export {
|
|
179
|
-
ReactiveArray,
|
|
180
|
-
array
|
|
181
|
-
};
|
|
102
|
+
export { ReactiveArray, array };
|
package/dist/value/computed.js
CHANGED
|
@@ -1,61 +1,39 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
-
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
|
-
import { batchedHandlers, batchDepth } from "../batch.js";
|
|
5
|
-
import { effect, activeEffect, runEffect } from "../effect.js";
|
|
6
1
|
import { computedName } from "../helpers/is.js";
|
|
2
|
+
import { activeEffect, effect, runEffect } from "../effect.js";
|
|
3
|
+
import { batchDepth, batchedHandlers } from "../batch.js";
|
|
7
4
|
import { Reactive } from "./reactive.js";
|
|
8
5
|
let activeComputed;
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
* @inheritdoc
|
|
40
|
-
*/
|
|
41
|
-
get() {
|
|
42
|
-
if (activeComputed != null && this !== activeComputed) {
|
|
43
|
-
this.state.computeds.add(activeComputed);
|
|
44
|
-
}
|
|
45
|
-
if (activeEffect != null && activeEffect !== this.effect.instance) {
|
|
46
|
-
this.state.effects.add(activeEffect);
|
|
47
|
-
}
|
|
48
|
-
if (this.effect.dirty && batchDepth === 0) {
|
|
49
|
-
runEffect(this.effect.instance);
|
|
50
|
-
}
|
|
51
|
-
return this.state.value;
|
|
52
|
-
}
|
|
53
|
-
}
|
|
6
|
+
var Computed = class extends Reactive {
|
|
7
|
+
effect = {
|
|
8
|
+
dirty: true,
|
|
9
|
+
instance: void 0
|
|
10
|
+
};
|
|
11
|
+
constructor(callback, options) {
|
|
12
|
+
super(computedName, void 0, options);
|
|
13
|
+
this.effect.instance = effect(() => {
|
|
14
|
+
if (this.effect.dirty) {
|
|
15
|
+
const previousComputed = activeComputed;
|
|
16
|
+
activeComputed = this;
|
|
17
|
+
const value = callback();
|
|
18
|
+
activeComputed = previousComputed;
|
|
19
|
+
if (!this.state.equal(this.state.value, value)) {
|
|
20
|
+
this.state.value = value;
|
|
21
|
+
for (const computed$1 of this.state.computeds) computed$1.effect.dirty = true;
|
|
22
|
+
for (const effect$1 of this.state.effects) batchedHandlers.add(effect$1);
|
|
23
|
+
for (const [, subscription] of this.state.subscriptions) subscription.callback(value);
|
|
24
|
+
}
|
|
25
|
+
this.effect.dirty = false;
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
get() {
|
|
30
|
+
if (activeComputed != null && this !== activeComputed) this.state.computeds.add(activeComputed);
|
|
31
|
+
if (activeEffect != null && activeEffect !== this.effect.instance) this.state.effects.add(activeEffect);
|
|
32
|
+
if (this.effect.dirty && batchDepth === 0) runEffect(this.effect.instance);
|
|
33
|
+
return this.state.value;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
54
36
|
function computed(callback, options) {
|
|
55
|
-
|
|
37
|
+
return new Computed(callback, options);
|
|
56
38
|
}
|
|
57
|
-
export {
|
|
58
|
-
Computed,
|
|
59
|
-
activeComputed,
|
|
60
|
-
computed
|
|
61
|
-
};
|
|
39
|
+
export { Computed, activeComputed, computed };
|
package/dist/value/reactive.js
CHANGED
|
@@ -1,55 +1,31 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
-
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
1
|
import { subscribe, unsubscribe } from "../subscription.js";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* JSON representation of the value
|
|
36
|
-
*/
|
|
37
|
-
toJSON() {
|
|
38
|
-
return this.get();
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* String representation of the value
|
|
42
|
-
*/
|
|
43
|
-
toString() {
|
|
44
|
-
return String(this.get());
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Unsubscribe from changes
|
|
48
|
-
*/
|
|
49
|
-
unsubscribe(callback) {
|
|
50
|
-
unsubscribe(this.state, callback);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
export {
|
|
54
|
-
Reactive
|
|
2
|
+
var Reactive = class {
|
|
3
|
+
state = {
|
|
4
|
+
computeds: /* @__PURE__ */ new Set(),
|
|
5
|
+
effects: /* @__PURE__ */ new Set(),
|
|
6
|
+
equal: Object.is,
|
|
7
|
+
subscriptions: /* @__PURE__ */ new Map(),
|
|
8
|
+
value: void 0
|
|
9
|
+
};
|
|
10
|
+
constructor(name, value, options) {
|
|
11
|
+
this.state.value = value;
|
|
12
|
+
if (typeof options === "object" && typeof options.equal === "function") this.state.equal = options.equal;
|
|
13
|
+
Object.defineProperty(this, "$mora", { value: name });
|
|
14
|
+
}
|
|
15
|
+
peek() {
|
|
16
|
+
return this.state.value;
|
|
17
|
+
}
|
|
18
|
+
subscribe(callback) {
|
|
19
|
+
return subscribe(this.state, callback);
|
|
20
|
+
}
|
|
21
|
+
toJSON() {
|
|
22
|
+
return this.get();
|
|
23
|
+
}
|
|
24
|
+
toString() {
|
|
25
|
+
return String(this.get());
|
|
26
|
+
}
|
|
27
|
+
unsubscribe(callback) {
|
|
28
|
+
unsubscribe(this.state, callback);
|
|
29
|
+
}
|
|
55
30
|
};
|
|
31
|
+
export { Reactive };
|
package/dist/value/signal.js
CHANGED
|
@@ -1,36 +1,24 @@
|
|
|
1
1
|
import { signalName } from "../helpers/is.js";
|
|
2
|
-
import { getValue, emitValue } from "../helpers/value.js";
|
|
3
2
|
import { Reactive } from "./reactive.js";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Update the value _(based on the current value)_
|
|
25
|
-
*/
|
|
26
|
-
update(callback) {
|
|
27
|
-
this.set(callback(this.state.value));
|
|
28
|
-
}
|
|
29
|
-
}
|
|
3
|
+
import { emitValue, getValue } from "../helpers/value.js";
|
|
4
|
+
var Signal = class extends Reactive {
|
|
5
|
+
constructor(value, options) {
|
|
6
|
+
super(signalName, value, options);
|
|
7
|
+
}
|
|
8
|
+
get() {
|
|
9
|
+
return getValue(this.state);
|
|
10
|
+
}
|
|
11
|
+
set(value) {
|
|
12
|
+
if (!this.state.equal(this.state.value, value)) {
|
|
13
|
+
this.state.value = value;
|
|
14
|
+
emitValue(this.state);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
update(callback) {
|
|
18
|
+
this.set(callback(this.state.value));
|
|
19
|
+
}
|
|
20
|
+
};
|
|
30
21
|
function signal(value, options) {
|
|
31
|
-
|
|
22
|
+
return new Signal(value, options);
|
|
32
23
|
}
|
|
33
|
-
export {
|
|
34
|
-
Signal,
|
|
35
|
-
signal
|
|
36
|
-
};
|
|
24
|
+
export { Signal, signal };
|
package/dist/value/store.js
CHANGED
|
@@ -1,63 +1,34 @@
|
|
|
1
|
-
|
|
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;
|
|
8
|
-
import { isPlainObject, isKey } from "../node_modules/@oscarpalmer/atoms/dist/internal/is.js";
|
|
1
|
+
import { noop, subscribe } from "../subscription.js";
|
|
9
2
|
import { storeName } from "../helpers/is.js";
|
|
10
|
-
import { setValueInProxy, getReactiveValueInProxy, setProxyValue } from "../helpers/proxy.js";
|
|
11
|
-
import { getValue } from "../helpers/value.js";
|
|
12
|
-
import { subscribe, noop } from "../subscription.js";
|
|
13
3
|
import { Reactive } from "./reactive.js";
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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
|
-
}
|
|
55
|
-
}
|
|
56
|
-
_keyed = new WeakMap();
|
|
4
|
+
import { getValue } from "../helpers/value.js";
|
|
5
|
+
import { getReactiveValueInProxy, setProxyValue, setValueInProxy } from "../helpers/proxy.js";
|
|
6
|
+
import { isKey, isPlainObject } from "@oscarpalmer/atoms/is";
|
|
7
|
+
var Store = class extends Reactive {
|
|
8
|
+
#keyed = /* @__PURE__ */ new Map();
|
|
9
|
+
constructor(value, options) {
|
|
10
|
+
super(storeName, new Proxy(value, { set: (target, property, value$1) => setValueInProxy(target, property, value$1, this.state, false) }), options);
|
|
11
|
+
}
|
|
12
|
+
get(key) {
|
|
13
|
+
return isKey(key) ? getReactiveValueInProxy(this, this.#keyed, key, false).get() : getValue(this.state);
|
|
14
|
+
}
|
|
15
|
+
peek(key) {
|
|
16
|
+
return isKey(key) ? this.state.value[key] : { ...this.state.value };
|
|
17
|
+
}
|
|
18
|
+
set(first, second) {
|
|
19
|
+
if (isKey(first)) this.state.value[first] = second;
|
|
20
|
+
else if (first == null || isPlainObject(first)) setProxyValue(this.state.value, first ?? {});
|
|
21
|
+
}
|
|
22
|
+
subscribe(first, second) {
|
|
23
|
+
if (isKey(first) && typeof second === "function") return getReactiveValueInProxy(this, this.#keyed, first, false).subscribe(second);
|
|
24
|
+
return typeof first === "function" ? subscribe(this.state, first) : noop;
|
|
25
|
+
}
|
|
26
|
+
update(callback) {
|
|
27
|
+
const updated = callback({ ...this.state.value });
|
|
28
|
+
if (updated == null || isPlainObject(updated)) setProxyValue(this.state.value, updated ?? {});
|
|
29
|
+
}
|
|
30
|
+
};
|
|
57
31
|
function store(value, options) {
|
|
58
|
-
|
|
32
|
+
return new Store(isPlainObject(value) ? value : {}, options);
|
|
59
33
|
}
|
|
60
|
-
export {
|
|
61
|
-
Store,
|
|
62
|
-
store
|
|
63
|
-
};
|
|
34
|
+
export { Store, store };
|