@oscarpalmer/mora 0.26.0 → 0.28.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.d.mts +14 -0
- package/dist/{batch.js → batch.mjs} +13 -3
- package/dist/constants.d.mts +20 -0
- package/dist/{constants.js → constants.mjs} +2 -0
- package/dist/effect.d.mts +17 -0
- package/dist/{effect.js → effect.mjs} +11 -4
- package/dist/helpers/is.d.mts +47 -0
- package/dist/helpers/is.mjs +55 -0
- package/dist/helpers/proxy.d.mts +15 -0
- package/dist/helpers/proxy.mjs +72 -0
- package/dist/helpers/value.d.mts +8 -0
- package/dist/helpers/{value.js → value.mjs} +4 -2
- package/dist/index.d.mts +10 -0
- package/dist/index.mjs +8 -0
- package/dist/models.d.mts +62 -0
- package/dist/models.mjs +1 -0
- package/dist/{mora.full.js → mora.full.mjs} +206 -106
- package/dist/subscription.d.mts +15 -0
- package/dist/{subscription.js → subscription.mjs} +2 -0
- package/dist/value/array.d.mts +156 -0
- package/dist/value/array.mjs +165 -0
- package/dist/value/computed.d.mts +21 -0
- package/dist/value/computed.mjs +68 -0
- package/dist/value/reactive.d.mts +40 -0
- package/dist/value/{reactive.js → reactive.mjs} +25 -2
- package/dist/value/signal.d.mts +44 -0
- package/dist/value/signal.mjs +59 -0
- package/dist/value/store.d.mts +112 -0
- package/dist/value/store.mjs +81 -0
- package/package.json +41 -35
- package/src/constants.ts +1 -6
- package/src/helpers/is.ts +1 -1
- package/src/helpers/proxy.ts +64 -17
- package/src/models.ts +3 -1
- package/src/value/array.ts +79 -23
- package/src/value/computed.ts +65 -30
- package/src/value/signal.ts +77 -9
- package/src/value/store.ts +101 -12
- package/dist/helpers/is.js +0 -23
- package/dist/helpers/proxy.js +0 -48
- package/dist/index.js +0 -8
- package/dist/models.js +0 -0
- package/dist/value/array.js +0 -100
- package/dist/value/computed.js +0 -36
- package/dist/value/signal.js +0 -24
- package/dist/value/store.js +0 -43
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { Reactive } from "./reactive.mjs";
|
|
2
|
+
import { Computed } from "./computed.mjs";
|
|
3
|
+
import { ReactiveOptions, Unsubscribe } from "../models.mjs";
|
|
4
|
+
import { PROPERTY_LENGTH } from "../constants.mjs";
|
|
5
|
+
|
|
6
|
+
//#region src/value/array.d.ts
|
|
7
|
+
declare class ReactiveArray<Item> extends Reactive<Item[], Item> {
|
|
8
|
+
#private;
|
|
9
|
+
/**
|
|
10
|
+
* The length of the array
|
|
11
|
+
*/
|
|
12
|
+
get length(): number;
|
|
13
|
+
/**
|
|
14
|
+
* Set the length of the array
|
|
15
|
+
*/
|
|
16
|
+
set length(value: number);
|
|
17
|
+
constructor(value: Item[], options?: ReactiveOptions<Item>);
|
|
18
|
+
/**
|
|
19
|
+
* Clear the array
|
|
20
|
+
*/
|
|
21
|
+
clear(): void;
|
|
22
|
+
/**
|
|
23
|
+
* Create a computed, filtered array
|
|
24
|
+
* @param callback Callback to evaluate each item
|
|
25
|
+
* @return Computed array of filtered items
|
|
26
|
+
*/
|
|
27
|
+
filter(callback: (item: Item, index: number, array: Item[]) => boolean): Computed<Item[]>;
|
|
28
|
+
/**
|
|
29
|
+
* @inheritdoc
|
|
30
|
+
*/
|
|
31
|
+
get(): Item[];
|
|
32
|
+
/**
|
|
33
|
+
* Get the value at an index
|
|
34
|
+
* @param index Index of item to get _(if negative, starts from the end)_
|
|
35
|
+
* @returns Item at index, or `undefined` if it doesn't exist
|
|
36
|
+
*/
|
|
37
|
+
get(index: number): Item | undefined;
|
|
38
|
+
/**
|
|
39
|
+
* Get the length of the array
|
|
40
|
+
* @returns Length of the array
|
|
41
|
+
*/
|
|
42
|
+
get(property: typeof PROPERTY_LENGTH): number;
|
|
43
|
+
/**
|
|
44
|
+
* Create a computed, mapped array
|
|
45
|
+
* @param callback Callback to transform each item
|
|
46
|
+
* @return Computed array of mapped items
|
|
47
|
+
*/
|
|
48
|
+
map<Mapped>(callback: (item: Item, index: number, array: Item[]) => Mapped): Computed<Mapped[]>;
|
|
49
|
+
/**
|
|
50
|
+
* Notify dependents of changes
|
|
51
|
+
*
|
|
52
|
+
* _This bypasses equality checks and will immediately notify dependents.
|
|
53
|
+
* Use this only if you're modifying nested data that would be ignored by equality checks._
|
|
54
|
+
*/
|
|
55
|
+
notify(): void;
|
|
56
|
+
/**
|
|
57
|
+
* @inheritdoc
|
|
58
|
+
*/
|
|
59
|
+
peek(): Item[];
|
|
60
|
+
/**
|
|
61
|
+
* Get the value at an index _(without reactivity)_
|
|
62
|
+
* @param index Index of item to get _(if negative, starts from the end)_
|
|
63
|
+
* @returns Item at index, or `undefined` if it doesn't exist
|
|
64
|
+
*/
|
|
65
|
+
peek(index: number): Item | undefined;
|
|
66
|
+
/**
|
|
67
|
+
* Get the length of the array _(without reactivity)_
|
|
68
|
+
* @returns Length of the array
|
|
69
|
+
*/
|
|
70
|
+
peek(property: typeof PROPERTY_LENGTH): number;
|
|
71
|
+
/**
|
|
72
|
+
* Remove and return the last item of the array
|
|
73
|
+
* @returns Removed item, or `undefined` if the array is empty
|
|
74
|
+
*/
|
|
75
|
+
pop(): Item | undefined;
|
|
76
|
+
/**
|
|
77
|
+
* Add items to the end of the array
|
|
78
|
+
* @param items Items to add
|
|
79
|
+
* @returns New array length
|
|
80
|
+
*/
|
|
81
|
+
push(...items: Item[]): number;
|
|
82
|
+
/**
|
|
83
|
+
* Set the value
|
|
84
|
+
* @param value New array of items _(defaults to an empty array)_
|
|
85
|
+
*/
|
|
86
|
+
set(value?: Item[] | (() => null | undefined | Item[] | Promise<null | undefined | Item[]>) | Promise<null | undefined | Item[]>): void;
|
|
87
|
+
/**
|
|
88
|
+
* Set the value at an index
|
|
89
|
+
* @param index Index of item to set __(if negative, starts from the end)_
|
|
90
|
+
* @param value New item
|
|
91
|
+
*/
|
|
92
|
+
set(index: number, value: Item | (() => Item | Promise<Item>) | Promise<Item>): void;
|
|
93
|
+
/**
|
|
94
|
+
* Set the length of the array
|
|
95
|
+
* @param value New array length
|
|
96
|
+
*/
|
|
97
|
+
set(property: typeof PROPERTY_LENGTH, value: number): void;
|
|
98
|
+
/**
|
|
99
|
+
* Remove and return the first item of the array
|
|
100
|
+
* @returns Removed item, or `undefined` if the array is empty
|
|
101
|
+
*/
|
|
102
|
+
shift(): Item | undefined;
|
|
103
|
+
/**
|
|
104
|
+
* Remove and return items from the array _(and optionally add new items)_
|
|
105
|
+
* @param from Index to start removing items from
|
|
106
|
+
* @param to Index to stop removing items at _(defaults to the end of the array)_
|
|
107
|
+
* @param items Optional items to add
|
|
108
|
+
* @returns Removed items
|
|
109
|
+
*/
|
|
110
|
+
splice(from: number, to?: number, ...items: Item[]): Item[];
|
|
111
|
+
/**
|
|
112
|
+
* @inheritdoc
|
|
113
|
+
*/
|
|
114
|
+
subscribe(callback: (value: Item[]) => void): Unsubscribe;
|
|
115
|
+
/**
|
|
116
|
+
* Subscribe to changes at a specific index
|
|
117
|
+
* @param index Index of item to subscribe to
|
|
118
|
+
* @param callback Callback for changes
|
|
119
|
+
* @returns Unsubscribe callback
|
|
120
|
+
*/
|
|
121
|
+
subscribe(index: number, callback: (value: Item | undefined) => void): Unsubscribe;
|
|
122
|
+
/**
|
|
123
|
+
* Add items to the beginning of the array
|
|
124
|
+
* @param items Items to add
|
|
125
|
+
* @returns New array length
|
|
126
|
+
*/
|
|
127
|
+
unshift(...items: Item[]): number;
|
|
128
|
+
/**
|
|
129
|
+
* Update the value _(based on the current value)_
|
|
130
|
+
* @param callback Callback to update the value
|
|
131
|
+
*/
|
|
132
|
+
update(callback: (value: Item[]) => Item[]): void;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Create a reactive array from a function result
|
|
136
|
+
* @param value Initial array of items
|
|
137
|
+
* @param options Reactivity options
|
|
138
|
+
* @returns Reactive array
|
|
139
|
+
*/
|
|
140
|
+
declare function array<Item>(value: () => Item[] | Promise<Item[]>, options?: ReactiveOptions<Item>): ReactiveArray<Item>;
|
|
141
|
+
/**
|
|
142
|
+
* Create a reactive array from a promise
|
|
143
|
+
* @param value Initial array of items
|
|
144
|
+
* @param options Reactivity options
|
|
145
|
+
* @returns Reactive array
|
|
146
|
+
*/
|
|
147
|
+
declare function array<Item>(value: Promise<Item[]>, options?: ReactiveOptions<Item>): ReactiveArray<Item>;
|
|
148
|
+
/**
|
|
149
|
+
* Create a reactive array
|
|
150
|
+
* @param value Initial array of items
|
|
151
|
+
* @param options Reactivity options
|
|
152
|
+
* @returns Reactive array
|
|
153
|
+
*/
|
|
154
|
+
declare function array<Item>(value: Item[], options?: ReactiveOptions<Item>): ReactiveArray<Item>;
|
|
155
|
+
//#endregion
|
|
156
|
+
export { ReactiveArray, array };
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { METHODS_AFFECTING_LENGTH, METHODS_UPDATE, NAME_ARRAY } from "../constants.mjs";
|
|
2
|
+
import { noop, subscribe } from "../subscription.mjs";
|
|
3
|
+
import { Reactive } from "./reactive.mjs";
|
|
4
|
+
import { computed } from "./computed.mjs";
|
|
5
|
+
import { emitValue, equalArrays, getValue } from "../helpers/value.mjs";
|
|
6
|
+
import { emityProxyValues, getReactiveValueInProxy, setProxyValue, setValueInProxy } from "../helpers/proxy.mjs";
|
|
7
|
+
import { signal } from "./signal.mjs";
|
|
8
|
+
//#region src/value/array.ts
|
|
9
|
+
var ReactiveArray = class extends Reactive {
|
|
10
|
+
#indiced = /* @__PURE__ */ new Map();
|
|
11
|
+
#size = signal(0);
|
|
12
|
+
/**
|
|
13
|
+
* The length of the array
|
|
14
|
+
*/
|
|
15
|
+
get length() {
|
|
16
|
+
return this.#size.get();
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Set the length of the array
|
|
20
|
+
*/
|
|
21
|
+
set length(value) {
|
|
22
|
+
if (typeof value === "number" && value >= 0 && value !== this.state.value.length) this.get().length = value;
|
|
23
|
+
}
|
|
24
|
+
constructor(value, options) {
|
|
25
|
+
super(NAME_ARRAY, new Proxy(value, {
|
|
26
|
+
get: (target, property) => METHODS_UPDATE.has(property) ? updateArray(property, target, this.state, this.#size) : Reflect.get(target, property),
|
|
27
|
+
set: (target, property, value) => setValueInProxy({
|
|
28
|
+
property,
|
|
29
|
+
target,
|
|
30
|
+
value,
|
|
31
|
+
isArray: true,
|
|
32
|
+
state: this.state,
|
|
33
|
+
length: this.#size
|
|
34
|
+
})
|
|
35
|
+
}), options);
|
|
36
|
+
this.#size.set(value.length);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Clear the array
|
|
40
|
+
*/
|
|
41
|
+
clear() {
|
|
42
|
+
this.length = 0;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Create a computed, filtered array
|
|
46
|
+
* @param callback Callback to evaluate each item
|
|
47
|
+
* @return Computed array of filtered items
|
|
48
|
+
*/
|
|
49
|
+
filter(callback) {
|
|
50
|
+
return computed(() => this.get().filter(callback));
|
|
51
|
+
}
|
|
52
|
+
get(first) {
|
|
53
|
+
if (typeof first === "number") return getReactiveValueInProxy(this, this.#indiced, first, true).get();
|
|
54
|
+
return first === "length" ? this.length : getValue(this.state);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Create a computed, mapped array
|
|
58
|
+
* @param callback Callback to transform each item
|
|
59
|
+
* @return Computed array of mapped items
|
|
60
|
+
*/
|
|
61
|
+
map(callback) {
|
|
62
|
+
return computed(() => this.get().map(callback));
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Notify dependents of changes
|
|
66
|
+
*
|
|
67
|
+
* _This bypasses equality checks and will immediately notify dependents.
|
|
68
|
+
* Use this only if you're modifying nested data that would be ignored by equality checks._
|
|
69
|
+
*/
|
|
70
|
+
notify() {
|
|
71
|
+
emityProxyValues(this.state, this.#indiced);
|
|
72
|
+
}
|
|
73
|
+
peek(value) {
|
|
74
|
+
if (value === "length") return this.#size.peek();
|
|
75
|
+
return typeof value === "number" ? this.state.value.at(value) : this.state.value.slice();
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Remove and return the last item of the array
|
|
79
|
+
* @returns Removed item, or `undefined` if the array is empty
|
|
80
|
+
*/
|
|
81
|
+
pop() {
|
|
82
|
+
return this.state.value.pop();
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Add items to the end of the array
|
|
86
|
+
* @param items Items to add
|
|
87
|
+
* @returns New array length
|
|
88
|
+
*/
|
|
89
|
+
push(...items) {
|
|
90
|
+
return this.state.value.push(...items);
|
|
91
|
+
}
|
|
92
|
+
set(first, second) {
|
|
93
|
+
setProxyValue(true, this.state, isArrayValue, isArrayIndex, setArray, setAtIndex, first, second);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Remove and return the first item of the array
|
|
97
|
+
* @returns Removed item, or `undefined` if the array is empty
|
|
98
|
+
*/
|
|
99
|
+
shift() {
|
|
100
|
+
return this.state.value.shift();
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Remove and return items from the array _(and optionally add new items)_
|
|
104
|
+
* @param from Index to start removing items from
|
|
105
|
+
* @param to Index to stop removing items at _(defaults to the end of the array)_
|
|
106
|
+
* @param items Optional items to add
|
|
107
|
+
* @returns Removed items
|
|
108
|
+
*/
|
|
109
|
+
splice(from, to, ...items) {
|
|
110
|
+
return this.state.value.splice(from, to ?? this.state.value.length, ...items);
|
|
111
|
+
}
|
|
112
|
+
subscribe(first, second) {
|
|
113
|
+
if (typeof first === "number" && typeof second === "function") return getReactiveValueInProxy(this, this.#indiced, first, true).subscribe(second);
|
|
114
|
+
return typeof first === "function" ? subscribe(this.state, first) : noop;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Add items to the beginning of the array
|
|
118
|
+
* @param items Items to add
|
|
119
|
+
* @returns New array length
|
|
120
|
+
*/
|
|
121
|
+
unshift(...items) {
|
|
122
|
+
return this.state.value.unshift(...items);
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Update the value _(based on the current value)_
|
|
126
|
+
* @param callback Callback to update the value
|
|
127
|
+
*/
|
|
128
|
+
update(callback) {
|
|
129
|
+
const updated = callback(this.state.value);
|
|
130
|
+
if (updated == null || Array.isArray(updated)) this.set(updated);
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
function array(value, options) {
|
|
134
|
+
const instance = new ReactiveArray([], options);
|
|
135
|
+
instance.set(value);
|
|
136
|
+
return instance;
|
|
137
|
+
}
|
|
138
|
+
function isArrayIndex(value) {
|
|
139
|
+
return typeof value === "number";
|
|
140
|
+
}
|
|
141
|
+
function isArrayValue(value) {
|
|
142
|
+
return value == null || Array.isArray(value);
|
|
143
|
+
}
|
|
144
|
+
function updateArray(type, array, state, length) {
|
|
145
|
+
const affectsLength = METHODS_AFFECTING_LENGTH.has(type);
|
|
146
|
+
const previousArray = affectsLength ? [] : array.slice();
|
|
147
|
+
const previousLength = array.length;
|
|
148
|
+
return (...args) => {
|
|
149
|
+
const result = array[type](...args);
|
|
150
|
+
if (affectsLength ? array.length !== previousLength : !equalArrays(state, previousArray, array)) {
|
|
151
|
+
emitValue(state);
|
|
152
|
+
length.set(array.length);
|
|
153
|
+
}
|
|
154
|
+
return result;
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
function setArray(state, value) {
|
|
158
|
+
state.value.splice(0, state.value.length, ...value ?? []);
|
|
159
|
+
}
|
|
160
|
+
function setAtIndex(state, index, value) {
|
|
161
|
+
const actual = index < 0 ? state.value.length + index : index;
|
|
162
|
+
if (actual > -1) state.value[actual] = value;
|
|
163
|
+
}
|
|
164
|
+
//#endregion
|
|
165
|
+
export { ReactiveArray, array };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Reactive } from "./reactive.mjs";
|
|
2
|
+
import { ReactiveOptions } from "../models.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/value/computed.d.ts
|
|
5
|
+
declare class Computed<Value> extends Reactive<Value> {
|
|
6
|
+
private readonly effect;
|
|
7
|
+
constructor(callback: () => Value | Promise<Value>, options?: ReactiveOptions<Value>);
|
|
8
|
+
/**
|
|
9
|
+
* @inheritdoc
|
|
10
|
+
*/
|
|
11
|
+
get(): Value;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Create a computed value
|
|
15
|
+
* @param callback Callback to compute the value
|
|
16
|
+
* @param options Reactivity options
|
|
17
|
+
* @returns Computed value
|
|
18
|
+
*/
|
|
19
|
+
declare function computed<Value>(callback: () => Value | Promise<Value>, options?: ReactiveOptions<Value>): Computed<Value>;
|
|
20
|
+
//#endregion
|
|
21
|
+
export { Computed, computed };
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { ACTIVE, BATCH, NAME_COMPUTED } from "../constants.mjs";
|
|
2
|
+
import { effect, runEffect } from "../effect.mjs";
|
|
3
|
+
import { flushHandlers } from "../batch.mjs";
|
|
4
|
+
import { Reactive } from "./reactive.mjs";
|
|
5
|
+
//#region src/value/computed.ts
|
|
6
|
+
var Computed = class extends Reactive {
|
|
7
|
+
effect = {
|
|
8
|
+
dirty: true,
|
|
9
|
+
instance: void 0
|
|
10
|
+
};
|
|
11
|
+
constructor(callback, options) {
|
|
12
|
+
super(NAME_COMPUTED, void 0, options);
|
|
13
|
+
this.effect.instance = effect(() => {
|
|
14
|
+
if (this.effect.dirty) {
|
|
15
|
+
setValue(this, this.state, callback);
|
|
16
|
+
this.effect.dirty = false;
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* @inheritdoc
|
|
22
|
+
*/
|
|
23
|
+
get() {
|
|
24
|
+
if (ACTIVE.computed != null && this !== ACTIVE.computed) this.state.computeds.add(ACTIVE.computed);
|
|
25
|
+
if (ACTIVE.effect != null && ACTIVE.effect !== this.effect.instance) this.state.effects.add(ACTIVE.effect);
|
|
26
|
+
if (this.effect.dirty && BATCH.depth === 0) runEffect(this.effect.instance);
|
|
27
|
+
return this.state.value;
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* Create a computed value
|
|
32
|
+
* @param callback Callback to compute the value
|
|
33
|
+
* @param options Reactivity options
|
|
34
|
+
* @returns Computed value
|
|
35
|
+
*/
|
|
36
|
+
function computed(callback, options) {
|
|
37
|
+
return new Computed(callback, options);
|
|
38
|
+
}
|
|
39
|
+
function setAndEmit(state, value) {
|
|
40
|
+
if (state.equal(state.value, value)) return;
|
|
41
|
+
state.value = value;
|
|
42
|
+
for (const computed of state.computeds) computed.effect.dirty = true;
|
|
43
|
+
for (const effect of state.effects) BATCH.handlers.add(effect);
|
|
44
|
+
for (const [, subscription] of state.subscriptions) subscription.callback(value);
|
|
45
|
+
flushHandlers();
|
|
46
|
+
}
|
|
47
|
+
function setValue(instance, state, callback) {
|
|
48
|
+
const previousComputed = ACTIVE.computed;
|
|
49
|
+
ACTIVE.computed = instance;
|
|
50
|
+
try {
|
|
51
|
+
const value = callback();
|
|
52
|
+
if (value instanceof Promise) {
|
|
53
|
+
state.promise = value;
|
|
54
|
+
value.then((resolvedValue) => {
|
|
55
|
+
if (state.promise === value) {
|
|
56
|
+
state.promise = void 0;
|
|
57
|
+
setAndEmit(state, resolvedValue);
|
|
58
|
+
}
|
|
59
|
+
}).catch(() => {
|
|
60
|
+
if (state.promise === value) state.promise = void 0;
|
|
61
|
+
});
|
|
62
|
+
} else setAndEmit(state, value);
|
|
63
|
+
} finally {
|
|
64
|
+
ACTIVE.computed = previousComputed;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
//#endregion
|
|
68
|
+
export { Computed, computed };
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { ReactiveOptions, ReactiveState, Unsubscribe } from "../models.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/value/reactive.d.ts
|
|
4
|
+
declare abstract class Reactive<Value, Equal = Value> {
|
|
5
|
+
protected readonly state: ReactiveState<Value, Equal>;
|
|
6
|
+
constructor(name: string, value: Value, options?: ReactiveOptions<Equal>);
|
|
7
|
+
/**
|
|
8
|
+
* Get the value
|
|
9
|
+
* @return Current value
|
|
10
|
+
*/
|
|
11
|
+
abstract get(): Value;
|
|
12
|
+
/**
|
|
13
|
+
* Get the value _(without reactivity)_
|
|
14
|
+
* @return Current value
|
|
15
|
+
*/
|
|
16
|
+
peek(): Value;
|
|
17
|
+
/**
|
|
18
|
+
* Subscribe to changes
|
|
19
|
+
* @param callback Callback for changes
|
|
20
|
+
* @return Unsubscribe callback
|
|
21
|
+
*/
|
|
22
|
+
subscribe(callback: (value: Value) => void): Unsubscribe;
|
|
23
|
+
/**
|
|
24
|
+
* JSON representation of the value
|
|
25
|
+
* @return JSON value
|
|
26
|
+
*/
|
|
27
|
+
toJSON(): Value;
|
|
28
|
+
/**
|
|
29
|
+
* String representation of the value
|
|
30
|
+
* @return Value as string
|
|
31
|
+
*/
|
|
32
|
+
toString(): string;
|
|
33
|
+
/**
|
|
34
|
+
* Unsubscribe from changes
|
|
35
|
+
* @param callback Callback to unsubscribe
|
|
36
|
+
*/
|
|
37
|
+
unsubscribe(callback: (value: Value) => void): void;
|
|
38
|
+
}
|
|
39
|
+
//#endregion
|
|
40
|
+
export { Reactive };
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { NAME_MORA } from "../constants.
|
|
2
|
-
import { subscribe, unsubscribe } from "../subscription.
|
|
1
|
+
import { NAME_MORA } from "../constants.mjs";
|
|
2
|
+
import { subscribe, unsubscribe } from "../subscription.mjs";
|
|
3
|
+
//#region src/value/reactive.ts
|
|
3
4
|
var Reactive = class {
|
|
4
5
|
state = {
|
|
5
6
|
computeds: /* @__PURE__ */ new Set(),
|
|
@@ -13,20 +14,42 @@ var Reactive = class {
|
|
|
13
14
|
if (typeof options === "object" && typeof options.equal === "function") this.state.equal = options.equal;
|
|
14
15
|
Object.defineProperty(this, NAME_MORA, { value: name });
|
|
15
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* Get the value _(without reactivity)_
|
|
19
|
+
* @return Current value
|
|
20
|
+
*/
|
|
16
21
|
peek() {
|
|
17
22
|
return this.state.value;
|
|
18
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Subscribe to changes
|
|
26
|
+
* @param callback Callback for changes
|
|
27
|
+
* @return Unsubscribe callback
|
|
28
|
+
*/
|
|
19
29
|
subscribe(callback) {
|
|
20
30
|
return subscribe(this.state, callback);
|
|
21
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* JSON representation of the value
|
|
34
|
+
* @return JSON value
|
|
35
|
+
*/
|
|
22
36
|
toJSON() {
|
|
23
37
|
return this.get();
|
|
24
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* String representation of the value
|
|
41
|
+
* @return Value as string
|
|
42
|
+
*/
|
|
25
43
|
toString() {
|
|
26
44
|
return String(this.get());
|
|
27
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* Unsubscribe from changes
|
|
48
|
+
* @param callback Callback to unsubscribe
|
|
49
|
+
*/
|
|
28
50
|
unsubscribe(callback) {
|
|
29
51
|
unsubscribe(this.state, callback);
|
|
30
52
|
}
|
|
31
53
|
};
|
|
54
|
+
//#endregion
|
|
32
55
|
export { Reactive };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Reactive } from "./reactive.mjs";
|
|
2
|
+
import { ReactiveOptions } from "../models.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/value/signal.d.ts
|
|
5
|
+
declare class Signal<Value> extends Reactive<Value> {
|
|
6
|
+
constructor(value: Value, options?: ReactiveOptions<Value>);
|
|
7
|
+
/**
|
|
8
|
+
* @inheritdoc
|
|
9
|
+
*/
|
|
10
|
+
get(): Value;
|
|
11
|
+
/**
|
|
12
|
+
* Set the value
|
|
13
|
+
* @param value New value
|
|
14
|
+
*/
|
|
15
|
+
set(value: Value | (() => Value | Promise<Value>) | Promise<Value>): void;
|
|
16
|
+
/**
|
|
17
|
+
* Update the value _(based on the current value)_
|
|
18
|
+
* @param callback Callback to update the value
|
|
19
|
+
*/
|
|
20
|
+
update(callback: (value: Value) => Value): void;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Create a reactive value from a function result
|
|
24
|
+
* @param value Initial value
|
|
25
|
+
* @param options Reactivity options
|
|
26
|
+
* @returns Reactive value
|
|
27
|
+
*/
|
|
28
|
+
declare function signal<Value>(value: () => Value | Promise<Value>, options?: ReactiveOptions<Value>): Signal<Value>;
|
|
29
|
+
/**
|
|
30
|
+
* Create a reactive value from a promise
|
|
31
|
+
* @param value Initial value
|
|
32
|
+
* @param options Reactivity options
|
|
33
|
+
* @returns Reactive value
|
|
34
|
+
*/
|
|
35
|
+
declare function signal<Value>(value: Promise<Value>, options?: ReactiveOptions<Value>): Signal<Value>;
|
|
36
|
+
/**
|
|
37
|
+
* Create a reactive value
|
|
38
|
+
* @param value Initial value
|
|
39
|
+
* @param options Reactivity options
|
|
40
|
+
* @returns Reactive value
|
|
41
|
+
*/
|
|
42
|
+
declare function signal<Value>(value: Value, options?: ReactiveOptions<Value>): Signal<Value>;
|
|
43
|
+
//#endregion
|
|
44
|
+
export { Signal, signal };
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { NAME_SIGNAL } from "../constants.mjs";
|
|
2
|
+
import { Reactive } from "./reactive.mjs";
|
|
3
|
+
import { emitValue, getValue } from "../helpers/value.mjs";
|
|
4
|
+
//#region src/value/signal.ts
|
|
5
|
+
var Signal = class extends Reactive {
|
|
6
|
+
constructor(value, options) {
|
|
7
|
+
super(NAME_SIGNAL, value, options);
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* @inheritdoc
|
|
11
|
+
*/
|
|
12
|
+
get() {
|
|
13
|
+
return getValue(this.state);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Set the value
|
|
17
|
+
* @param value New value
|
|
18
|
+
*/
|
|
19
|
+
set(value) {
|
|
20
|
+
setValue(this.state, value);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Update the value _(based on the current value)_
|
|
24
|
+
* @param callback Callback to update the value
|
|
25
|
+
*/
|
|
26
|
+
update(callback) {
|
|
27
|
+
this.set(callback(this.state.value));
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
function setAndEmit(state, value) {
|
|
31
|
+
if (!state.equal(state.value, value)) {
|
|
32
|
+
state.value = value;
|
|
33
|
+
emitValue(state);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
function setValue(state, value) {
|
|
37
|
+
try {
|
|
38
|
+
let actual = value;
|
|
39
|
+
if (typeof value === "function") actual = value();
|
|
40
|
+
if (actual instanceof Promise) {
|
|
41
|
+
state.promise = actual;
|
|
42
|
+
actual.then((value) => {
|
|
43
|
+
if (actual === state.promise) {
|
|
44
|
+
state.promise = void 0;
|
|
45
|
+
setAndEmit(state, value);
|
|
46
|
+
}
|
|
47
|
+
}).catch(() => {
|
|
48
|
+
if (actual === state.promise) state.promise = void 0;
|
|
49
|
+
});
|
|
50
|
+
} else setAndEmit(state, actual);
|
|
51
|
+
} catch {}
|
|
52
|
+
}
|
|
53
|
+
function signal(value, options) {
|
|
54
|
+
const instance = new Signal(void 0, options);
|
|
55
|
+
instance.set(value);
|
|
56
|
+
return instance;
|
|
57
|
+
}
|
|
58
|
+
//#endregion
|
|
59
|
+
export { Signal, signal };
|