@oscarpalmer/mora 0.12.0 → 0.13.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/array.cjs +130 -0
- package/dist/array.js +130 -0
- package/dist/index.cjs +2 -0
- package/dist/index.js +2 -0
- package/dist/mora.full.js +232 -85
- package/dist/reactive.cjs +12 -0
- package/dist/reactive.js +12 -0
- package/dist/signal.cjs +8 -31
- package/dist/signal.js +3 -26
- package/dist/value.cjs +38 -0
- package/dist/value.js +38 -0
- package/package.json +1 -1
- package/src/array.ts +201 -0
- package/src/index.ts +1 -1
- package/src/reactive.ts +14 -0
- package/src/signal.ts +6 -33
- package/src/value.ts +47 -0
- package/types/array.d.cts +101 -0
- package/types/array.d.ts +41 -0
- package/types/batch.d.cts +17 -9
- package/types/computed.d.cts +17 -9
- package/types/index.d.cts +60 -9
- package/types/index.d.ts +1 -0
- package/types/is.d.cts +17 -9
- package/types/reactive.d.cts +17 -9
- package/types/reactive.d.ts +8 -0
- package/types/signal.d.cts +20 -9
- package/types/signal.d.ts +3 -0
- package/types/subscription.d.cts +20 -12
- package/types/value.d.cts +64 -0
- package/types/value.d.ts +4 -0
package/dist/signal.cjs
CHANGED
|
@@ -1,45 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const batch = require("./batch.cjs");
|
|
4
|
-
const computed = require("./computed.cjs");
|
|
5
|
-
const effect = require("./effect.cjs");
|
|
6
3
|
const reactive = require("./reactive.cjs");
|
|
4
|
+
const value = require("./value.cjs");
|
|
7
5
|
class Signal extends reactive.Reactive {
|
|
8
|
-
constructor(
|
|
9
|
-
super("signal",
|
|
6
|
+
constructor(value2) {
|
|
7
|
+
super("signal", value2);
|
|
10
8
|
}
|
|
11
9
|
/**
|
|
12
10
|
* @inheritdoc
|
|
13
11
|
*/
|
|
14
12
|
get() {
|
|
15
|
-
|
|
16
|
-
this.state.computeds.add(computed.activeComputed);
|
|
17
|
-
}
|
|
18
|
-
if (effect.activeEffect != null) {
|
|
19
|
-
this.state.effects.add(effect.activeEffect);
|
|
20
|
-
}
|
|
21
|
-
return this.state.value;
|
|
13
|
+
return value.getValue(this.state);
|
|
22
14
|
}
|
|
23
15
|
/**
|
|
24
16
|
* Set the value
|
|
25
17
|
*/
|
|
26
|
-
set(value) {
|
|
27
|
-
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
this.state.value = value;
|
|
31
|
-
for (const computed2 of this.state.computeds) {
|
|
32
|
-
computed2.effect.dirty = true;
|
|
33
|
-
}
|
|
34
|
-
for (const effect2 of this.state.effects) {
|
|
35
|
-
batch.batchedHandlers.add(effect2);
|
|
36
|
-
}
|
|
37
|
-
for (const [, subscription] of this.state.subscriptions) {
|
|
38
|
-
batch.batchedHandlers.add(subscription);
|
|
39
|
-
}
|
|
40
|
-
if (batch.batchDepth === 0) {
|
|
41
|
-
batch.flushEffects();
|
|
42
|
-
}
|
|
18
|
+
set(value$1) {
|
|
19
|
+
value.setValue(this.state, value$1);
|
|
43
20
|
}
|
|
44
21
|
/**
|
|
45
22
|
* Update the value
|
|
@@ -48,8 +25,8 @@ class Signal extends reactive.Reactive {
|
|
|
48
25
|
this.set(callback(this.state.value));
|
|
49
26
|
}
|
|
50
27
|
}
|
|
51
|
-
function signal(
|
|
52
|
-
return new Signal(
|
|
28
|
+
function signal(value2) {
|
|
29
|
+
return new Signal(value2);
|
|
53
30
|
}
|
|
54
31
|
exports.Signal = Signal;
|
|
55
32
|
exports.signal = signal;
|
package/dist/signal.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import { batchedHandlers, flushEffects, batchDepth } from "./batch.js";
|
|
2
|
-
import { activeComputed } from "./computed.js";
|
|
3
|
-
import { activeEffect } from "./effect.js";
|
|
4
1
|
import { Reactive } from "./reactive.js";
|
|
2
|
+
import { getValue, setValue } from "./value.js";
|
|
5
3
|
class Signal extends Reactive {
|
|
6
4
|
constructor(value) {
|
|
7
5
|
super("signal", value);
|
|
@@ -10,34 +8,13 @@ class Signal extends Reactive {
|
|
|
10
8
|
* @inheritdoc
|
|
11
9
|
*/
|
|
12
10
|
get() {
|
|
13
|
-
|
|
14
|
-
this.state.computeds.add(activeComputed);
|
|
15
|
-
}
|
|
16
|
-
if (activeEffect != null) {
|
|
17
|
-
this.state.effects.add(activeEffect);
|
|
18
|
-
}
|
|
19
|
-
return this.state.value;
|
|
11
|
+
return getValue(this.state);
|
|
20
12
|
}
|
|
21
13
|
/**
|
|
22
14
|
* Set the value
|
|
23
15
|
*/
|
|
24
16
|
set(value) {
|
|
25
|
-
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
|
-
this.state.value = value;
|
|
29
|
-
for (const computed of this.state.computeds) {
|
|
30
|
-
computed.effect.dirty = true;
|
|
31
|
-
}
|
|
32
|
-
for (const effect of this.state.effects) {
|
|
33
|
-
batchedHandlers.add(effect);
|
|
34
|
-
}
|
|
35
|
-
for (const [, subscription] of this.state.subscriptions) {
|
|
36
|
-
batchedHandlers.add(subscription);
|
|
37
|
-
}
|
|
38
|
-
if (batchDepth === 0) {
|
|
39
|
-
flushEffects();
|
|
40
|
-
}
|
|
17
|
+
setValue(this.state, value);
|
|
41
18
|
}
|
|
42
19
|
/**
|
|
43
20
|
* Update the value
|
package/dist/value.cjs
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const batch = require("./batch.cjs");
|
|
4
|
+
const computed = require("./computed.cjs");
|
|
5
|
+
const effect = require("./effect.cjs");
|
|
6
|
+
function emitValue(state) {
|
|
7
|
+
for (const computed2 of state.computeds) {
|
|
8
|
+
computed2.effect.dirty = true;
|
|
9
|
+
}
|
|
10
|
+
for (const effect2 of state.effects) {
|
|
11
|
+
batch.batchedHandlers.add(effect2);
|
|
12
|
+
}
|
|
13
|
+
for (const [, subscription] of state.subscriptions) {
|
|
14
|
+
batch.batchedHandlers.add(subscription);
|
|
15
|
+
}
|
|
16
|
+
if (batch.batchDepth === 0) {
|
|
17
|
+
batch.flushEffects();
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function getValue(state) {
|
|
21
|
+
if (computed.activeComputed != null) {
|
|
22
|
+
state.computeds.add(computed.activeComputed);
|
|
23
|
+
}
|
|
24
|
+
if (effect.activeEffect != null) {
|
|
25
|
+
state.effects.add(effect.activeEffect);
|
|
26
|
+
}
|
|
27
|
+
return state.value;
|
|
28
|
+
}
|
|
29
|
+
function setValue(state, value) {
|
|
30
|
+
if (Object.is(state.value, value)) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
state.value = value;
|
|
34
|
+
emitValue(state);
|
|
35
|
+
}
|
|
36
|
+
exports.emitValue = emitValue;
|
|
37
|
+
exports.getValue = getValue;
|
|
38
|
+
exports.setValue = setValue;
|
package/dist/value.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { batchedHandlers, flushEffects, batchDepth } from "./batch.js";
|
|
2
|
+
import { activeComputed } from "./computed.js";
|
|
3
|
+
import { activeEffect } from "./effect.js";
|
|
4
|
+
function emitValue(state) {
|
|
5
|
+
for (const computed of state.computeds) {
|
|
6
|
+
computed.effect.dirty = true;
|
|
7
|
+
}
|
|
8
|
+
for (const effect of state.effects) {
|
|
9
|
+
batchedHandlers.add(effect);
|
|
10
|
+
}
|
|
11
|
+
for (const [, subscription] of state.subscriptions) {
|
|
12
|
+
batchedHandlers.add(subscription);
|
|
13
|
+
}
|
|
14
|
+
if (batchDepth === 0) {
|
|
15
|
+
flushEffects();
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
function getValue(state) {
|
|
19
|
+
if (activeComputed != null) {
|
|
20
|
+
state.computeds.add(activeComputed);
|
|
21
|
+
}
|
|
22
|
+
if (activeEffect != null) {
|
|
23
|
+
state.effects.add(activeEffect);
|
|
24
|
+
}
|
|
25
|
+
return state.value;
|
|
26
|
+
}
|
|
27
|
+
function setValue(state, value) {
|
|
28
|
+
if (Object.is(state.value, value)) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
state.value = value;
|
|
32
|
+
emitValue(state);
|
|
33
|
+
}
|
|
34
|
+
export {
|
|
35
|
+
emitValue,
|
|
36
|
+
getValue,
|
|
37
|
+
setValue
|
|
38
|
+
};
|
package/package.json
CHANGED
package/src/array.ts
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import {Reactive, type ReactiveState} from './reactive';
|
|
2
|
+
import {type Signal, signal} from './signal';
|
|
3
|
+
import {emitValue, getValue} from './value';
|
|
4
|
+
|
|
5
|
+
export class ReactiveArray<Item> extends Reactive<Item[]> {
|
|
6
|
+
#size = signal(0);
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The length of the array
|
|
10
|
+
*/
|
|
11
|
+
get length(): number {
|
|
12
|
+
return this.#size.get();
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Set the length of the array
|
|
17
|
+
*/
|
|
18
|
+
set length(value: number) {
|
|
19
|
+
if (
|
|
20
|
+
typeof value === 'number' &&
|
|
21
|
+
value >= 0 &&
|
|
22
|
+
value !== this.state.value.length
|
|
23
|
+
) {
|
|
24
|
+
this.get().length = value;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
constructor(value: Item[]) {
|
|
29
|
+
super(
|
|
30
|
+
'array',
|
|
31
|
+
new Proxy(value, {
|
|
32
|
+
get: (target, property) =>
|
|
33
|
+
updateMethods.has(property as string)
|
|
34
|
+
? updateArray(property as string, target, this.state, this.#size)
|
|
35
|
+
: Reflect.get(target, property),
|
|
36
|
+
set: (target, property, value) =>
|
|
37
|
+
setValue(target, property, value, this.state, this.#size),
|
|
38
|
+
}),
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
this.#size.set(value.length);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @inheritdoc
|
|
46
|
+
*/
|
|
47
|
+
get(): Item[] {
|
|
48
|
+
return getValue(this.state);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* @inheritdoc
|
|
53
|
+
*/
|
|
54
|
+
peek(): Item[];
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Get the length of the array _(without reactivity)_
|
|
58
|
+
*/
|
|
59
|
+
peek(length: true): number;
|
|
60
|
+
|
|
61
|
+
peek(length?: unknown): Item[] | number {
|
|
62
|
+
return length === true ? this.#size.peek() : [...this.state.value];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Set the value
|
|
67
|
+
*/
|
|
68
|
+
set(value: Item[]): void;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Set the value at an index
|
|
72
|
+
*/
|
|
73
|
+
set(index: number, value: Item): void;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Set the length of the array
|
|
77
|
+
*/
|
|
78
|
+
set(property: 'length', value: number): void;
|
|
79
|
+
|
|
80
|
+
set(first: number | 'length' | Item[], second?: number | Item): void {
|
|
81
|
+
if (Array.isArray(first)) {
|
|
82
|
+
this.state.value.splice(0, this.state.value.length, ...first);
|
|
83
|
+
} else if (first === 'length') {
|
|
84
|
+
this.length = second as number;
|
|
85
|
+
} else if (typeof first === 'number') {
|
|
86
|
+
this.state.value[first] = second as Item;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Create a reactive array
|
|
93
|
+
*/
|
|
94
|
+
export function array<Item>(value: Item[]): ReactiveArray<Item> {
|
|
95
|
+
return new ReactiveArray(Array.isArray(value) ? value : []);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function emit(first: unknown[], second: unknown[]): boolean {
|
|
99
|
+
let {length} = first;
|
|
100
|
+
|
|
101
|
+
if (length !== second.length) {
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
let offset = 0;
|
|
106
|
+
|
|
107
|
+
if (length >= 100) {
|
|
108
|
+
offset = Math.round(length / 10);
|
|
109
|
+
offset = offset > 25 ? 25 : offset;
|
|
110
|
+
|
|
111
|
+
for (let index = 0; index < offset; index += 1) {
|
|
112
|
+
if (!Object.is(first[index], second[index])) {
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
length -= offset;
|
|
119
|
+
|
|
120
|
+
for (let index = offset; index < length; index += 1) {
|
|
121
|
+
if (!Object.is(first[index], second[index])) {
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function setValue<Item>(
|
|
130
|
+
target: Item[],
|
|
131
|
+
property: PropertyKey,
|
|
132
|
+
value: unknown,
|
|
133
|
+
state: ReactiveState<Item[]>,
|
|
134
|
+
length: Signal<number>,
|
|
135
|
+
): boolean {
|
|
136
|
+
const isIndex = !Number.isNaN(Number(property));
|
|
137
|
+
const isLength = property === 'length';
|
|
138
|
+
|
|
139
|
+
if (!isIndex && !isLength) {
|
|
140
|
+
return Reflect.set(target, property, value);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const previous = Reflect.get(target, property);
|
|
144
|
+
|
|
145
|
+
if (Object.is(previous, value)) {
|
|
146
|
+
return true;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
Reflect.set(target, property, value);
|
|
150
|
+
|
|
151
|
+
emitValue(state);
|
|
152
|
+
|
|
153
|
+
length.set(target.length);
|
|
154
|
+
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function updateArray<Item>(
|
|
159
|
+
type: string,
|
|
160
|
+
array: Item[],
|
|
161
|
+
state: ReactiveState<Item[]>,
|
|
162
|
+
length: Signal<number>,
|
|
163
|
+
): unknown {
|
|
164
|
+
const affectsLength = lengthAffectingMethods.has(type);
|
|
165
|
+
const previousArray = affectsLength ? [] : [...array];
|
|
166
|
+
const previousLength = array.length;
|
|
167
|
+
|
|
168
|
+
return (...args: unknown[]): unknown => {
|
|
169
|
+
const result = (array[type as never] as (...args: unknown[]) => unknown)(
|
|
170
|
+
...args,
|
|
171
|
+
);
|
|
172
|
+
|
|
173
|
+
if (
|
|
174
|
+
affectsLength
|
|
175
|
+
? array.length !== previousLength
|
|
176
|
+
: emit(previousArray, array)
|
|
177
|
+
) {
|
|
178
|
+
emitValue(state);
|
|
179
|
+
|
|
180
|
+
length.set(array.length);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
return result;
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const lengthAffectingMethods = new Set<string>([
|
|
188
|
+
'pop',
|
|
189
|
+
'push',
|
|
190
|
+
'shift',
|
|
191
|
+
'unshift',
|
|
192
|
+
]);
|
|
193
|
+
|
|
194
|
+
const updateMethods = new Set<string>([
|
|
195
|
+
...lengthAffectingMethods,
|
|
196
|
+
'copyWithin',
|
|
197
|
+
'fill',
|
|
198
|
+
'reverse',
|
|
199
|
+
'sort',
|
|
200
|
+
'splice',
|
|
201
|
+
]);
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
+
export {array, type ReactiveArray} from './array';
|
|
1
2
|
export {startBatch, stopBatch} from './batch';
|
|
2
3
|
export {computed, type Computed} from './computed';
|
|
3
4
|
export {effect, type Effect} from './effect';
|
|
4
5
|
export {isComputed, isEffect, isReactive, isSignal} from './is';
|
|
5
6
|
export type {Reactive} from './reactive';
|
|
6
7
|
export {signal, type Signal} from './signal';
|
|
7
|
-
|
package/src/reactive.ts
CHANGED
|
@@ -42,6 +42,20 @@ export abstract class Reactive<Value> {
|
|
|
42
42
|
return subscribe(this.state, callback);
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
+
/**
|
|
46
|
+
* JSON representation of the value
|
|
47
|
+
*/
|
|
48
|
+
toJSON(): Value {
|
|
49
|
+
return this.get();
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* String representation of the value
|
|
54
|
+
*/
|
|
55
|
+
toString(): string {
|
|
56
|
+
return String(this.get());
|
|
57
|
+
}
|
|
58
|
+
|
|
45
59
|
/**
|
|
46
60
|
* Unsubscribe from changes
|
|
47
61
|
*/
|
package/src/signal.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import {batchDepth, batchedHandlers, flushEffects} from './batch';
|
|
2
|
-
import {type InternalComputed, activeComputed} from './computed';
|
|
3
|
-
import {activeEffect} from './effect';
|
|
4
1
|
import {Reactive} from './reactive';
|
|
2
|
+
import {getValue, setValue} from './value';
|
|
5
3
|
|
|
6
4
|
export class Signal<Value> extends Reactive<Value> {
|
|
7
5
|
constructor(value: Value) {
|
|
@@ -12,42 +10,14 @@ export class Signal<Value> extends Reactive<Value> {
|
|
|
12
10
|
* @inheritdoc
|
|
13
11
|
*/
|
|
14
12
|
get(): Value {
|
|
15
|
-
|
|
16
|
-
this.state.computeds.add(activeComputed);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
if (activeEffect != null) {
|
|
20
|
-
this.state.effects.add(activeEffect);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
return this.state.value;
|
|
13
|
+
return getValue(this.state);
|
|
24
14
|
}
|
|
25
15
|
|
|
26
16
|
/**
|
|
27
17
|
* Set the value
|
|
28
18
|
*/
|
|
29
19
|
set(value: Value): void {
|
|
30
|
-
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
this.state.value = value;
|
|
35
|
-
|
|
36
|
-
for (const computed of this.state.computeds) {
|
|
37
|
-
(computed as unknown as InternalComputed).effect.dirty = true;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
for (const effect of this.state.effects) {
|
|
41
|
-
batchedHandlers.add(effect);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
for (const [, subscription] of this.state.subscriptions) {
|
|
45
|
-
batchedHandlers.add(subscription as never);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
if (batchDepth === 0) {
|
|
49
|
-
flushEffects();
|
|
50
|
-
}
|
|
20
|
+
setValue(this.state, value);
|
|
51
21
|
}
|
|
52
22
|
|
|
53
23
|
/**
|
|
@@ -58,6 +28,9 @@ export class Signal<Value> extends Reactive<Value> {
|
|
|
58
28
|
}
|
|
59
29
|
}
|
|
60
30
|
|
|
31
|
+
/**
|
|
32
|
+
* Create a reactive value
|
|
33
|
+
*/
|
|
61
34
|
export function signal<Value>(value: Value): Signal<Value> {
|
|
62
35
|
return new Signal(value);
|
|
63
36
|
}
|
package/src/value.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import {batchDepth, batchedHandlers, flushEffects} from './batch';
|
|
2
|
+
import {type InternalComputed, activeComputed} from './computed';
|
|
3
|
+
import {activeEffect} from './effect';
|
|
4
|
+
import type {ReactiveState} from './reactive';
|
|
5
|
+
|
|
6
|
+
export function emitValue<Value>(state: ReactiveState<Value>): void {
|
|
7
|
+
for (const computed of state.computeds) {
|
|
8
|
+
(computed as unknown as InternalComputed).effect.dirty = true;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
for (const effect of state.effects) {
|
|
12
|
+
batchedHandlers.add(effect);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
for (const [, subscription] of state.subscriptions) {
|
|
16
|
+
batchedHandlers.add(subscription as never);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (batchDepth === 0) {
|
|
20
|
+
flushEffects();
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function getValue<Value>(state: ReactiveState<Value>): Value {
|
|
25
|
+
if (activeComputed != null) {
|
|
26
|
+
state.computeds.add(activeComputed);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (activeEffect != null) {
|
|
30
|
+
state.effects.add(activeEffect);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return state.value;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function setValue<Value>(
|
|
37
|
+
state: ReactiveState<Value>,
|
|
38
|
+
value: Value,
|
|
39
|
+
): void {
|
|
40
|
+
if (Object.is(state.value, value)) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
state.value = value;
|
|
45
|
+
|
|
46
|
+
emitValue(state);
|
|
47
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// Generated by dts-bundle-generator v9.5.1
|
|
2
|
+
|
|
3
|
+
export type GenericCallback = (...args: any[]) => any;
|
|
4
|
+
declare class Effect {
|
|
5
|
+
private readonly $mora;
|
|
6
|
+
private readonly state;
|
|
7
|
+
constructor(callback: GenericCallback);
|
|
8
|
+
}
|
|
9
|
+
declare class Computed<Value> extends Reactive<Value> {
|
|
10
|
+
private readonly effect;
|
|
11
|
+
constructor(callback: () => Value);
|
|
12
|
+
/**
|
|
13
|
+
* @inheritdoc
|
|
14
|
+
*/
|
|
15
|
+
get(): Value;
|
|
16
|
+
}
|
|
17
|
+
declare class Subscription<Value> {
|
|
18
|
+
state: ReactiveState<Value>;
|
|
19
|
+
callback: GenericCallback;
|
|
20
|
+
constructor(state: ReactiveState<Value>, callback: GenericCallback);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Unsubscribe from changes
|
|
24
|
+
*/
|
|
25
|
+
export type Unsubscribe = () => void;
|
|
26
|
+
declare abstract class Reactive<Value> {
|
|
27
|
+
protected readonly state: ReactiveState<Value>;
|
|
28
|
+
constructor(name: string, value: Value);
|
|
29
|
+
/**
|
|
30
|
+
* Get the value
|
|
31
|
+
*/
|
|
32
|
+
abstract get(): Value;
|
|
33
|
+
/**
|
|
34
|
+
* Get the value _(without reactivity)_
|
|
35
|
+
*/
|
|
36
|
+
peek(): Value;
|
|
37
|
+
/**
|
|
38
|
+
* Subscribe to changes
|
|
39
|
+
*/
|
|
40
|
+
subscribe(callback: (value: Value) => void): Unsubscribe;
|
|
41
|
+
/**
|
|
42
|
+
* JSON representation of the value
|
|
43
|
+
*/
|
|
44
|
+
toJSON(): Value;
|
|
45
|
+
/**
|
|
46
|
+
* String representation of the value
|
|
47
|
+
*/
|
|
48
|
+
toString(): string;
|
|
49
|
+
/**
|
|
50
|
+
* Unsubscribe from changes
|
|
51
|
+
*/
|
|
52
|
+
unsubscribe(callback: (value: Value) => void): void;
|
|
53
|
+
}
|
|
54
|
+
export type ReactiveState<Value> = {
|
|
55
|
+
computeds: Set<Computed<unknown>>;
|
|
56
|
+
effects: Set<Effect>;
|
|
57
|
+
subscriptions: Map<(value: Value) => void, Subscription<Value>>;
|
|
58
|
+
value: Value;
|
|
59
|
+
};
|
|
60
|
+
export declare class ReactiveArray<Item> extends Reactive<Item[]> {
|
|
61
|
+
#private;
|
|
62
|
+
/**
|
|
63
|
+
* The length of the array
|
|
64
|
+
*/
|
|
65
|
+
get length(): number;
|
|
66
|
+
/**
|
|
67
|
+
* Set the length of the array
|
|
68
|
+
*/
|
|
69
|
+
set length(value: number);
|
|
70
|
+
constructor(value: Item[]);
|
|
71
|
+
/**
|
|
72
|
+
* @inheritdoc
|
|
73
|
+
*/
|
|
74
|
+
get(): Item[];
|
|
75
|
+
/**
|
|
76
|
+
* @inheritdoc
|
|
77
|
+
*/
|
|
78
|
+
peek(): Item[];
|
|
79
|
+
/**
|
|
80
|
+
* Get the length of the array _(without reactivity)_
|
|
81
|
+
*/
|
|
82
|
+
peek(length: true): number;
|
|
83
|
+
/**
|
|
84
|
+
* Set the value
|
|
85
|
+
*/
|
|
86
|
+
set(value: Item[]): void;
|
|
87
|
+
/**
|
|
88
|
+
* Set the value at an index
|
|
89
|
+
*/
|
|
90
|
+
set(index: number, value: Item): void;
|
|
91
|
+
/**
|
|
92
|
+
* Set the length of the array
|
|
93
|
+
*/
|
|
94
|
+
set(property: "length", value: number): void;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Create a reactive array
|
|
98
|
+
*/
|
|
99
|
+
export declare function array<Item>(value: Item[]): ReactiveArray<Item>;
|
|
100
|
+
|
|
101
|
+
export {};
|
package/types/array.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { Reactive } from './reactive';
|
|
2
|
+
export declare class ReactiveArray<Item> extends Reactive<Item[]> {
|
|
3
|
+
#private;
|
|
4
|
+
/**
|
|
5
|
+
* The length of the array
|
|
6
|
+
*/
|
|
7
|
+
get length(): number;
|
|
8
|
+
/**
|
|
9
|
+
* Set the length of the array
|
|
10
|
+
*/
|
|
11
|
+
set length(value: number);
|
|
12
|
+
constructor(value: Item[]);
|
|
13
|
+
/**
|
|
14
|
+
* @inheritdoc
|
|
15
|
+
*/
|
|
16
|
+
get(): Item[];
|
|
17
|
+
/**
|
|
18
|
+
* @inheritdoc
|
|
19
|
+
*/
|
|
20
|
+
peek(): Item[];
|
|
21
|
+
/**
|
|
22
|
+
* Get the length of the array _(without reactivity)_
|
|
23
|
+
*/
|
|
24
|
+
peek(length: true): number;
|
|
25
|
+
/**
|
|
26
|
+
* Set the value
|
|
27
|
+
*/
|
|
28
|
+
set(value: Item[]): void;
|
|
29
|
+
/**
|
|
30
|
+
* Set the value at an index
|
|
31
|
+
*/
|
|
32
|
+
set(index: number, value: Item): void;
|
|
33
|
+
/**
|
|
34
|
+
* Set the length of the array
|
|
35
|
+
*/
|
|
36
|
+
set(property: 'length', value: number): void;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Create a reactive array
|
|
40
|
+
*/
|
|
41
|
+
export declare function array<Item>(value: Item[]): ReactiveArray<Item>;
|