@oscarpalmer/mora 0.21.0 → 0.23.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 -25
- package/dist/constants.js +34 -0
- package/dist/effect.js +18 -28
- package/dist/helpers/is.js +9 -26
- package/dist/helpers/proxy.js +36 -48
- package/dist/helpers/value.js +21 -48
- package/dist/index.js +3 -16
- package/dist/models.js +0 -0
- package/dist/mora.full.js +218 -130
- package/dist/subscription.js +22 -28
- package/dist/value/array.js +97 -182
- package/dist/value/computed.js +33 -58
- package/dist/value/reactive.js +29 -53
- package/dist/value/signal.js +21 -33
- package/dist/value/store.js +37 -60
- package/package.json +17 -24
- package/src/batch.ts +11 -13
- package/src/constants.ts +49 -0
- package/src/effect.ts +11 -16
- package/src/helpers/is.ts +41 -19
- package/src/helpers/proxy.ts +34 -38
- package/src/helpers/value.ts +19 -14
- package/src/index.ts +1 -1
- package/src/models.ts +66 -0
- package/src/subscription.ts +14 -16
- package/src/value/array.ts +87 -45
- package/src/value/computed.ts +29 -39
- package/src/value/reactive.ts +9 -24
- package/src/value/signal.ts +9 -3
- package/src/value/store.ts +40 -9
- package/types/batch.d.ts +3 -5
- package/types/constants.d.ts +14 -0
- package/types/effect.d.ts +2 -1
- package/types/helpers/is.d.ts +23 -10
- package/types/helpers/proxy.d.ts +2 -3
- package/types/helpers/value.d.ts +1 -1
- package/types/index.d.ts +1 -0
- package/types/models.d.ts +56 -0
- package/types/subscription.d.ts +3 -6
- package/types/value/array.d.ts +48 -7
- package/types/value/computed.d.ts +2 -11
- package/types/value/reactive.d.ts +8 -17
- package/types/value/signal.d.ts +7 -1
- package/types/value/store.d.ts +28 -4
- 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 -187
- 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 -259
- package/types/helpers/proxy.d.cts +0 -244
- package/types/helpers/value.d.cts +0 -71
- package/types/index.d.cts +0 -281
- package/types/subscription.d.cts +0 -71
- package/types/value/array.d.cts +0 -161
- 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,187 +1,102 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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;
|
|
8
|
-
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";
|
|
1
|
+
import { noop, subscribe } from "../subscription.js";
|
|
2
|
+
import { METHODS_AFFECTING_LENGTH, METHODS_UPDATE, NAME_ARRAY } from "../constants.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
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
pop() {
|
|
90
|
-
return this.state.value.pop();
|
|
91
|
-
}
|
|
92
|
-
/**
|
|
93
|
-
* Add items to the end of the array
|
|
94
|
-
*/
|
|
95
|
-
push(...items) {
|
|
96
|
-
return this.state.value.push(...items);
|
|
97
|
-
}
|
|
98
|
-
set(first, second) {
|
|
99
|
-
if (first == null || Array.isArray(first)) {
|
|
100
|
-
this.state.value.splice(0, this.state.value.length, ...first ?? []);
|
|
101
|
-
} else if (first === "length") {
|
|
102
|
-
this.length = second;
|
|
103
|
-
} else if (typeof first === "number") {
|
|
104
|
-
this.state.value[first] = second;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Remove and return the first item of the array
|
|
109
|
-
*/
|
|
110
|
-
shift() {
|
|
111
|
-
return this.state.value.shift();
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* Remove and return items from the array _(and optionally add new items)_
|
|
115
|
-
*/
|
|
116
|
-
splice(from, to, ...items) {
|
|
117
|
-
return this.state.value.splice(
|
|
118
|
-
from,
|
|
119
|
-
to ?? this.state.value.length,
|
|
120
|
-
...items
|
|
121
|
-
);
|
|
122
|
-
}
|
|
123
|
-
subscribe(first, second) {
|
|
124
|
-
if (typeof first === "number" && typeof second === "function") {
|
|
125
|
-
return getReactiveValueInProxy(
|
|
126
|
-
this,
|
|
127
|
-
__privateGet(this, _indiced),
|
|
128
|
-
first,
|
|
129
|
-
true
|
|
130
|
-
).subscribe(second);
|
|
131
|
-
}
|
|
132
|
-
return typeof first === "function" ? subscribe(this.state, first) : noop;
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* Add items to the beginning of the array
|
|
136
|
-
*/
|
|
137
|
-
unshift(...items) {
|
|
138
|
-
return this.state.value.unshift(...items);
|
|
139
|
-
}
|
|
140
|
-
/**
|
|
141
|
-
* Update the value _(based on the current value)_
|
|
142
|
-
*/
|
|
143
|
-
update(callback) {
|
|
144
|
-
const updated = callback(this.state.value);
|
|
145
|
-
if (updated == null || Array.isArray(updated)) {
|
|
146
|
-
this.set(updated);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
_indiced = new WeakMap();
|
|
151
|
-
_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(NAME_ARRAY, new Proxy(value, {
|
|
19
|
+
get: (target, property) => METHODS_UPDATE.has(property) ? updateArray(property, target, this.state, this.#size) : Reflect.get(target, property),
|
|
20
|
+
set: (target, property, value$1) => setValueInProxy({
|
|
21
|
+
property,
|
|
22
|
+
target,
|
|
23
|
+
value: value$1,
|
|
24
|
+
isArray: true,
|
|
25
|
+
state: this.state,
|
|
26
|
+
length: this.#size
|
|
27
|
+
})
|
|
28
|
+
}), options);
|
|
29
|
+
this.#size.set(value.length);
|
|
30
|
+
}
|
|
31
|
+
clear() {
|
|
32
|
+
this.length = 0;
|
|
33
|
+
}
|
|
34
|
+
filter(callback) {
|
|
35
|
+
return computed(() => this.get().filter(callback));
|
|
36
|
+
}
|
|
37
|
+
get(first) {
|
|
38
|
+
if (typeof first === "number") return getReactiveValueInProxy(this, this.#indiced, first, true).get();
|
|
39
|
+
if (first === "length") return this.length;
|
|
40
|
+
return getValue(this.state);
|
|
41
|
+
}
|
|
42
|
+
map(callback) {
|
|
43
|
+
return computed(() => this.get().map(callback));
|
|
44
|
+
}
|
|
45
|
+
notify() {
|
|
46
|
+
emitValue(this.state);
|
|
47
|
+
}
|
|
48
|
+
peek(value) {
|
|
49
|
+
if (value === "length") return this.#size.peek();
|
|
50
|
+
if (typeof value === "number") return this.state.value.at(value);
|
|
51
|
+
return [...this.state.value];
|
|
52
|
+
}
|
|
53
|
+
pop() {
|
|
54
|
+
return this.state.value.pop();
|
|
55
|
+
}
|
|
56
|
+
push(...items) {
|
|
57
|
+
return this.state.value.push(...items);
|
|
58
|
+
}
|
|
59
|
+
set(first, second) {
|
|
60
|
+
if (first == null || Array.isArray(first)) this.state.value.splice(0, this.state.value.length, ...first ?? []);
|
|
61
|
+
else if (first === "length") this.length = second;
|
|
62
|
+
else if (typeof first === "number" && !Number.isNaN(first)) setAtIndex(this.state.value, first, second);
|
|
63
|
+
}
|
|
64
|
+
shift() {
|
|
65
|
+
return this.state.value.shift();
|
|
66
|
+
}
|
|
67
|
+
splice(from, to, ...items) {
|
|
68
|
+
return this.state.value.splice(from, to ?? this.state.value.length, ...items);
|
|
69
|
+
}
|
|
70
|
+
subscribe(first, second) {
|
|
71
|
+
if (typeof first === "number" && typeof second === "function") return getReactiveValueInProxy(this, this.#indiced, first, true).subscribe(second);
|
|
72
|
+
return typeof first === "function" ? subscribe(this.state, first) : noop;
|
|
73
|
+
}
|
|
74
|
+
unshift(...items) {
|
|
75
|
+
return this.state.value.unshift(...items);
|
|
76
|
+
}
|
|
77
|
+
update(callback) {
|
|
78
|
+
const updated = callback(this.state.value);
|
|
79
|
+
if (updated == null || Array.isArray(updated)) this.set(updated);
|
|
80
|
+
}
|
|
81
|
+
};
|
|
152
82
|
function array(value, options) {
|
|
153
|
-
|
|
83
|
+
return new ReactiveArray(Array.isArray(value) ? value : [], options);
|
|
154
84
|
}
|
|
155
|
-
function updateArray(type,
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
return result;
|
|
168
|
-
};
|
|
85
|
+
function updateArray(type, array$1, state, length) {
|
|
86
|
+
const affectsLength = METHODS_AFFECTING_LENGTH.has(type);
|
|
87
|
+
const previousArray = affectsLength ? [] : [...array$1];
|
|
88
|
+
const previousLength = array$1.length;
|
|
89
|
+
return (...args) => {
|
|
90
|
+
const result = array$1[type](...args);
|
|
91
|
+
if (affectsLength ? array$1.length !== previousLength : !equalArrays(state, previousArray, array$1)) {
|
|
92
|
+
emitValue(state);
|
|
93
|
+
length.set(array$1.length);
|
|
94
|
+
}
|
|
95
|
+
return result;
|
|
96
|
+
};
|
|
169
97
|
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
]);
|
|
176
|
-
const updateMethods = /* @__PURE__ */ new Set([
|
|
177
|
-
...lengthAffectingMethods,
|
|
178
|
-
"copyWithin",
|
|
179
|
-
"fill",
|
|
180
|
-
"reverse",
|
|
181
|
-
"sort",
|
|
182
|
-
"splice"
|
|
183
|
-
]);
|
|
184
|
-
export {
|
|
185
|
-
ReactiveArray,
|
|
186
|
-
array
|
|
187
|
-
};
|
|
98
|
+
function setAtIndex(array$1, index, value) {
|
|
99
|
+
const actual = index < 0 ? array$1.length + index : index;
|
|
100
|
+
if (actual > -1) array$1[actual] = value;
|
|
101
|
+
}
|
|
102
|
+
export { ReactiveArray, array };
|
package/dist/value/computed.js
CHANGED
|
@@ -1,61 +1,36 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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
|
-
import { computedName } from "../helpers/is.js";
|
|
1
|
+
import { ACTIVE, BATCH, NAME_COMPUTED } from "../constants.js";
|
|
2
|
+
import { effect, runEffect } from "../effect.js";
|
|
7
3
|
import { Reactive } from "./reactive.js";
|
|
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
|
-
|
|
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
|
-
}
|
|
4
|
+
var Computed = class extends Reactive {
|
|
5
|
+
effect = {
|
|
6
|
+
dirty: true,
|
|
7
|
+
instance: void 0
|
|
8
|
+
};
|
|
9
|
+
constructor(callback, options) {
|
|
10
|
+
super(NAME_COMPUTED, void 0, options);
|
|
11
|
+
this.effect.instance = effect(() => {
|
|
12
|
+
if (!this.effect.dirty) return;
|
|
13
|
+
const previousComputed = ACTIVE.computed;
|
|
14
|
+
ACTIVE.computed = this;
|
|
15
|
+
const value = callback();
|
|
16
|
+
ACTIVE.computed = previousComputed;
|
|
17
|
+
if (!this.state.equal(this.state.value, value)) {
|
|
18
|
+
this.state.value = value;
|
|
19
|
+
for (const computed$1 of this.state.computeds) computed$1.effect.dirty = true;
|
|
20
|
+
for (const effect$1 of this.state.effects) BATCH.handlers.add(effect$1);
|
|
21
|
+
for (const [, subscription] of this.state.subscriptions) subscription.callback(value);
|
|
22
|
+
}
|
|
23
|
+
this.effect.dirty = false;
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
get() {
|
|
27
|
+
if (ACTIVE.computed != null && this !== ACTIVE.computed) this.state.computeds.add(ACTIVE.computed);
|
|
28
|
+
if (ACTIVE.effect != null && ACTIVE.effect !== this.effect.instance) this.state.effects.add(ACTIVE.effect);
|
|
29
|
+
if (this.effect.dirty && BATCH.depth === 0) runEffect(this.effect.instance);
|
|
30
|
+
return this.state.value;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
54
33
|
function computed(callback, options) {
|
|
55
|
-
|
|
34
|
+
return new Computed(callback, options);
|
|
56
35
|
}
|
|
57
|
-
export {
|
|
58
|
-
Computed,
|
|
59
|
-
activeComputed,
|
|
60
|
-
computed
|
|
61
|
-
};
|
|
36
|
+
export { Computed, 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
|
-
import {
|
|
2
|
-
import { getValue, emitValue } from "../helpers/value.js";
|
|
1
|
+
import { NAME_SIGNAL } from "../constants.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(NAME_SIGNAL, 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,40 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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";
|
|
9
|
-
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";
|
|
1
|
+
import { noop, subscribe } from "../subscription.js";
|
|
2
|
+
import { NAME_STORE } from "../constants.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
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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(NAME_STORE, new Proxy(value, { set: (target, property, value$1) => setValueInProxy({
|
|
11
|
+
target,
|
|
12
|
+
property,
|
|
13
|
+
value: value$1,
|
|
14
|
+
isArray: false,
|
|
15
|
+
state: this.state
|
|
16
|
+
}) }), options);
|
|
17
|
+
}
|
|
18
|
+
get(key) {
|
|
19
|
+
return isKey(key) ? getReactiveValueInProxy(this, this.#keyed, key, false).get() : getValue(this.state);
|
|
20
|
+
}
|
|
21
|
+
peek(key) {
|
|
22
|
+
return isKey(key) ? this.state.value[key] : { ...this.state.value };
|
|
23
|
+
}
|
|
24
|
+
set(first, second) {
|
|
25
|
+
if (isKey(first)) this.state.value[first] = second;
|
|
26
|
+
else if (first == null || isPlainObject(first)) setProxyValue(this.state.value, first ?? {});
|
|
27
|
+
}
|
|
28
|
+
subscribe(first, second) {
|
|
29
|
+
if (isKey(first) && typeof second === "function") return getReactiveValueInProxy(this, this.#keyed, first, false).subscribe(second);
|
|
30
|
+
return typeof first === "function" ? subscribe(this.state, first) : noop;
|
|
31
|
+
}
|
|
32
|
+
update(callback) {
|
|
33
|
+
const updated = callback({ ...this.state.value });
|
|
34
|
+
if (updated == null || isPlainObject(updated)) setProxyValue(this.state.value, updated ?? {});
|
|
35
|
+
}
|
|
36
|
+
};
|
|
57
37
|
function store(value, options) {
|
|
58
|
-
|
|
38
|
+
return new Store(isPlainObject(value) ? value : {}, options);
|
|
59
39
|
}
|
|
60
|
-
export {
|
|
61
|
-
Store,
|
|
62
|
-
store
|
|
63
|
-
};
|
|
40
|
+
export { Store, store };
|