@oscarpalmer/mora 0.23.0 → 0.25.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/constants.js +4 -2
- package/dist/effect.js +2 -2
- package/dist/helpers/is.js +3 -3
- package/dist/helpers/proxy.js +9 -5
- package/dist/mora.full.js +496 -583
- package/dist/value/array.js +5 -7
- package/dist/value/reactive.js +2 -1
- package/dist/value/store.js +5 -2
- package/package.json +11 -13
- package/src/batch.ts +1 -1
- package/src/constants.ts +7 -8
- package/src/effect.ts +5 -7
- package/src/helpers/is.ts +8 -11
- package/src/helpers/proxy.ts +35 -21
- package/src/helpers/value.ts +1 -7
- package/src/index.ts +1 -7
- package/src/value/array.ts +22 -64
- package/src/value/computed.ts +3 -0
- package/src/value/reactive.ts +2 -1
- package/src/value/signal.ts +2 -5
- package/src/value/store.ts +24 -22
- package/types/constants.d.ts +3 -1
- package/types/helpers/proxy.d.ts +3 -1
- package/types/index.d.ts +1 -1
- package/types/value/array.d.ts +1 -1
- package/types/value/computed.d.ts +3 -0
- package/types/value/signal.d.ts +1 -1
- package/types/value/store.d.ts +11 -4
package/dist/value/array.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
+
import { METHODS_AFFECTING_LENGTH, METHODS_UPDATE, NAME_ARRAY, PROPERTY_LENGTH } from "../constants.js";
|
|
1
2
|
import { noop, subscribe } from "../subscription.js";
|
|
2
|
-
import { METHODS_AFFECTING_LENGTH, METHODS_UPDATE, NAME_ARRAY } from "../constants.js";
|
|
3
3
|
import { Reactive } from "./reactive.js";
|
|
4
4
|
import { computed } from "./computed.js";
|
|
5
5
|
import { emitValue, equalArrays, getValue } from "../helpers/value.js";
|
|
6
|
-
import { getReactiveValueInProxy, setValueInProxy } from "../helpers/proxy.js";
|
|
6
|
+
import { emityProxyValues, getReactiveValueInProxy, setValueInProxy } from "../helpers/proxy.js";
|
|
7
7
|
import { signal } from "./signal.js";
|
|
8
8
|
var ReactiveArray = class extends Reactive {
|
|
9
9
|
#indiced = /* @__PURE__ */ new Map();
|
|
@@ -36,19 +36,17 @@ var ReactiveArray = class extends Reactive {
|
|
|
36
36
|
}
|
|
37
37
|
get(first) {
|
|
38
38
|
if (typeof first === "number") return getReactiveValueInProxy(this, this.#indiced, first, true).get();
|
|
39
|
-
|
|
40
|
-
return getValue(this.state);
|
|
39
|
+
return first === "length" ? this.length : getValue(this.state);
|
|
41
40
|
}
|
|
42
41
|
map(callback) {
|
|
43
42
|
return computed(() => this.get().map(callback));
|
|
44
43
|
}
|
|
45
44
|
notify() {
|
|
46
|
-
|
|
45
|
+
emityProxyValues(this.state, this.#indiced);
|
|
47
46
|
}
|
|
48
47
|
peek(value) {
|
|
49
48
|
if (value === "length") return this.#size.peek();
|
|
50
|
-
|
|
51
|
-
return [...this.state.value];
|
|
49
|
+
return typeof value === "number" ? this.state.value.at(value) : [...this.state.value];
|
|
52
50
|
}
|
|
53
51
|
pop() {
|
|
54
52
|
return this.state.value.pop();
|
package/dist/value/reactive.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { NAME_MORA } from "../constants.js";
|
|
1
2
|
import { subscribe, unsubscribe } from "../subscription.js";
|
|
2
3
|
var Reactive = class {
|
|
3
4
|
state = {
|
|
@@ -10,7 +11,7 @@ var Reactive = class {
|
|
|
10
11
|
constructor(name, value, options) {
|
|
11
12
|
this.state.value = value;
|
|
12
13
|
if (typeof options === "object" && typeof options.equal === "function") this.state.equal = options.equal;
|
|
13
|
-
Object.defineProperty(this,
|
|
14
|
+
Object.defineProperty(this, NAME_MORA, { value: name });
|
|
14
15
|
}
|
|
15
16
|
peek() {
|
|
16
17
|
return this.state.value;
|
package/dist/value/store.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { noop, subscribe } from "../subscription.js";
|
|
2
1
|
import { NAME_STORE } from "../constants.js";
|
|
2
|
+
import { noop, subscribe } from "../subscription.js";
|
|
3
3
|
import { Reactive } from "./reactive.js";
|
|
4
4
|
import { getValue } from "../helpers/value.js";
|
|
5
|
-
import { getReactiveValueInProxy, setProxyValue, setValueInProxy } from "../helpers/proxy.js";
|
|
5
|
+
import { emityProxyValues, getReactiveValueInProxy, setProxyValue, setValueInProxy } from "../helpers/proxy.js";
|
|
6
6
|
import { isKey, isPlainObject } from "@oscarpalmer/atoms/is";
|
|
7
7
|
var Store = class extends Reactive {
|
|
8
8
|
#keyed = /* @__PURE__ */ new Map();
|
|
@@ -18,6 +18,9 @@ var Store = class extends Reactive {
|
|
|
18
18
|
get(key) {
|
|
19
19
|
return isKey(key) ? getReactiveValueInProxy(this, this.#keyed, key, false).get() : getValue(this.state);
|
|
20
20
|
}
|
|
21
|
+
notify() {
|
|
22
|
+
emityProxyValues(this.state, this.#keyed);
|
|
23
|
+
}
|
|
21
24
|
peek(key) {
|
|
22
25
|
return isKey(key) ? this.state.value[key] : { ...this.state.value };
|
|
23
26
|
}
|
package/package.json
CHANGED
|
@@ -4,21 +4,19 @@
|
|
|
4
4
|
"url": "https://oscarpalmer.se"
|
|
5
5
|
},
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"@oscarpalmer/atoms": "^0.
|
|
7
|
+
"@oscarpalmer/atoms": "^0.115"
|
|
8
8
|
},
|
|
9
9
|
"description": "Signals and stuff…",
|
|
10
10
|
"devDependencies": {
|
|
11
|
-
"@
|
|
12
|
-
"@rollup/plugin-node-resolve": "^16",
|
|
13
|
-
"@rollup/plugin-typescript": "^12.3",
|
|
14
|
-
"@types/node": "^24.9",
|
|
11
|
+
"@types/node": "^25",
|
|
15
12
|
"@vitest/coverage-istanbul": "^4",
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
"
|
|
13
|
+
"jsdom": "^27.3",
|
|
14
|
+
"oxfmt": "^0.18",
|
|
15
|
+
"oxlint": "^1.33",
|
|
16
|
+
"rolldown": "1.0.0-beta.55",
|
|
19
17
|
"tslib": "^2.8",
|
|
20
18
|
"typescript": "^5.9",
|
|
21
|
-
"vite": "
|
|
19
|
+
"vite": "8.0.0-beta.2",
|
|
22
20
|
"vitest": "^4"
|
|
23
21
|
},
|
|
24
22
|
"exports": {
|
|
@@ -38,14 +36,14 @@
|
|
|
38
36
|
"url": "git+https://github.com/oscarpalmer/mora.git"
|
|
39
37
|
},
|
|
40
38
|
"scripts": {
|
|
41
|
-
"build": "npm run clean && npx vite build && npm run
|
|
39
|
+
"build": "npm run clean && npx vite build && npm run rolldown:build && npx tsc",
|
|
42
40
|
"clean": "rm -rf ./dist && rm -rf ./types && rm -f ./tsconfig.tsbuildinfo",
|
|
43
|
-
"
|
|
44
|
-
"
|
|
41
|
+
"rolldown:build": "npx rolldown -c",
|
|
42
|
+
"rolldown:watch": "npx rolldown -c --watch",
|
|
45
43
|
"test": "npx vitest --coverage",
|
|
46
44
|
"watch": "npx vite build --watch"
|
|
47
45
|
},
|
|
48
46
|
"type": "module",
|
|
49
47
|
"types": "./types/index.d.ts",
|
|
50
|
-
"version": "0.
|
|
48
|
+
"version": "0.25.0"
|
|
51
49
|
}
|
package/src/batch.ts
CHANGED
package/src/constants.ts
CHANGED
|
@@ -15,14 +15,14 @@ export const BATCH: Batch = {
|
|
|
15
15
|
handlers: new Set<Effect | Subscription>(),
|
|
16
16
|
};
|
|
17
17
|
|
|
18
|
-
export const METHODS_AFFECTING_LENGTH
|
|
18
|
+
export const METHODS_AFFECTING_LENGTH = new Set<string>([
|
|
19
19
|
'pop',
|
|
20
20
|
'push',
|
|
21
21
|
'shift',
|
|
22
22
|
'unshift',
|
|
23
23
|
]);
|
|
24
24
|
|
|
25
|
-
export const METHODS_UPDATE
|
|
25
|
+
export const METHODS_UPDATE = new Set<string>([
|
|
26
26
|
...METHODS_AFFECTING_LENGTH,
|
|
27
27
|
'copyWithin',
|
|
28
28
|
'fill',
|
|
@@ -37,13 +37,12 @@ export const NAME_COMPUTED = 'computed';
|
|
|
37
37
|
|
|
38
38
|
export const NAME_EFFECT = 'effect';
|
|
39
39
|
|
|
40
|
+
export const NAME_MORA = '$mora';
|
|
41
|
+
|
|
40
42
|
export const NAME_SIGNAL = 'signal';
|
|
41
43
|
|
|
42
44
|
export const NAME_STORE = 'store';
|
|
43
45
|
|
|
44
|
-
export const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
NAME_SIGNAL,
|
|
48
|
-
NAME_STORE,
|
|
49
|
-
]);
|
|
46
|
+
export const NAME_ALL = new Set([NAME_ARRAY, NAME_COMPUTED, NAME_SIGNAL, NAME_STORE]);
|
|
47
|
+
|
|
48
|
+
export const PROPERTY_LENGTH = 'length';
|
package/src/effect.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
import type {GenericCallback} from '@oscarpalmer/atoms/models';
|
|
2
|
-
import {ACTIVE, NAME_EFFECT} from './constants';
|
|
2
|
+
import {ACTIVE, NAME_EFFECT, NAME_MORA} from './constants';
|
|
3
3
|
import type {EffectState, InternalEffect} from './models';
|
|
4
4
|
|
|
5
5
|
export class Effect {
|
|
6
|
-
private
|
|
6
|
+
declare private readonly $mora: string;
|
|
7
7
|
|
|
8
|
-
private
|
|
8
|
+
declare private readonly state: EffectState;
|
|
9
9
|
|
|
10
10
|
constructor(callback: GenericCallback) {
|
|
11
|
-
Object.defineProperty(this,
|
|
11
|
+
Object.defineProperty(this, NAME_MORA, {
|
|
12
12
|
value: NAME_EFFECT,
|
|
13
13
|
});
|
|
14
14
|
|
|
@@ -38,7 +38,5 @@ export function runEffect(effect: Effect): void {
|
|
|
38
38
|
* @returns Effect
|
|
39
39
|
*/
|
|
40
40
|
export function effect(callback: GenericCallback): Effect {
|
|
41
|
-
return typeof callback === 'function'
|
|
42
|
-
? new Effect(callback)
|
|
43
|
-
: (undefined as never);
|
|
41
|
+
return typeof callback === 'function' ? new Effect(callback) : (undefined as never);
|
|
44
42
|
}
|
package/src/helpers/is.ts
CHANGED
|
@@ -3,9 +3,10 @@ import {
|
|
|
3
3
|
NAME_ARRAY,
|
|
4
4
|
NAME_COMPUTED,
|
|
5
5
|
NAME_EFFECT,
|
|
6
|
+
NAME_MORA,
|
|
6
7
|
NAME_SIGNAL,
|
|
7
8
|
NAME_STORE,
|
|
8
|
-
|
|
9
|
+
NAME_ALL,
|
|
9
10
|
} from '../constants';
|
|
10
11
|
import type {Effect} from '../effect';
|
|
11
12
|
import type {ReactiveArray} from '../value/array';
|
|
@@ -45,10 +46,10 @@ function isMora<T>(value: unknown, name: string | Set<string>): value is T {
|
|
|
45
46
|
return (
|
|
46
47
|
typeof value === 'object' &&
|
|
47
48
|
value != null &&
|
|
48
|
-
|
|
49
|
+
NAME_MORA in value &&
|
|
49
50
|
(typeof name === 'string'
|
|
50
|
-
? (value as PlainObject)
|
|
51
|
-
: name.has((value as PlainObject)
|
|
51
|
+
? (value as PlainObject)[NAME_MORA] === name
|
|
52
|
+
: name.has((value as PlainObject)[NAME_MORA] as never))
|
|
52
53
|
);
|
|
53
54
|
}
|
|
54
55
|
|
|
@@ -57,10 +58,8 @@ function isMora<T>(value: unknown, name: string | Set<string>): value is T {
|
|
|
57
58
|
* @param value Value to check
|
|
58
59
|
* @returns True if value is a {@link Reactive}
|
|
59
60
|
*/
|
|
60
|
-
export function isReactive<Value, Equal = Value>(
|
|
61
|
-
value
|
|
62
|
-
): value is Reactive<Value, Equal> {
|
|
63
|
-
return isMora<Reactive<Value, Equal>>(value, NAMES);
|
|
61
|
+
export function isReactive<Value, Equal = Value>(value: unknown): value is Reactive<Value, Equal> {
|
|
62
|
+
return isMora<Reactive<Value, Equal>>(value, NAME_ALL);
|
|
64
63
|
}
|
|
65
64
|
|
|
66
65
|
/**
|
|
@@ -77,8 +76,6 @@ export function isSignal<Value>(value: unknown): value is Signal<Value> {
|
|
|
77
76
|
* @param value Value to check
|
|
78
77
|
* @returns True if value is a {@link Store}
|
|
79
78
|
*/
|
|
80
|
-
export function isStore<Value extends PlainObject>(
|
|
81
|
-
value: unknown,
|
|
82
|
-
): value is Store<Value> {
|
|
79
|
+
export function isStore<Value extends PlainObject>(value: unknown): value is Store<Value> {
|
|
83
80
|
return isMora<Store<Value>>(value, NAME_STORE);
|
|
84
81
|
}
|
package/src/helpers/proxy.ts
CHANGED
|
@@ -1,15 +1,36 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
ArrayOrPlainObject,
|
|
3
|
-
Key,
|
|
4
|
-
PlainObject,
|
|
5
|
-
} from '@oscarpalmer/atoms/models';
|
|
1
|
+
import type {ArrayOrPlainObject, Key, PlainObject} from '@oscarpalmer/atoms/models';
|
|
6
2
|
import {startBatch, stopBatch} from '../batch';
|
|
7
|
-
import
|
|
3
|
+
import {PROPERTY_LENGTH} from '../constants';
|
|
4
|
+
import type {InternalComputed, ReactiveState, SetValueInProxyParameters} from '../models';
|
|
8
5
|
import type {ReactiveArray} from '../value/array';
|
|
9
6
|
import {type Computed, computed} from '../value/computed';
|
|
10
7
|
import type {Store} from '../value/store';
|
|
11
8
|
import {emitValue} from './value';
|
|
12
9
|
|
|
10
|
+
export function emityProxyValues<Value>(
|
|
11
|
+
state: ReactiveState<Value, Value>,
|
|
12
|
+
mapped: Map<Key, Computed<unknown>>,
|
|
13
|
+
): void;
|
|
14
|
+
|
|
15
|
+
export function emityProxyValues<Value>(
|
|
16
|
+
state: ReactiveState<Value[], Value>,
|
|
17
|
+
mapped: Map<Key, Computed<unknown>>,
|
|
18
|
+
): void;
|
|
19
|
+
|
|
20
|
+
export function emityProxyValues(
|
|
21
|
+
state: ReactiveState<unknown, unknown>,
|
|
22
|
+
mapped: Map<Key, Computed<unknown>>,
|
|
23
|
+
): void {
|
|
24
|
+
const values = [...mapped.values()];
|
|
25
|
+
const {length} = values;
|
|
26
|
+
|
|
27
|
+
for (let index = 0; index < length; index += 1) {
|
|
28
|
+
(values[index] as unknown as InternalComputed).effect.dirty = true;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
emitValue(state);
|
|
32
|
+
}
|
|
33
|
+
|
|
13
34
|
export function getReactiveValueInProxy<Value>(
|
|
14
35
|
array: ReactiveArray<Value>,
|
|
15
36
|
mapped: Map<Key, Computed<unknown>>,
|
|
@@ -33,13 +54,11 @@ export function getReactiveValueInProxy(
|
|
|
33
54
|
let item = mapped.get(key);
|
|
34
55
|
|
|
35
56
|
if (item == null) {
|
|
36
|
-
item = computed(() =>
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
: (value as PlainObject)[key];
|
|
42
|
-
}) as Computed<unknown>;
|
|
57
|
+
item = computed(() =>
|
|
58
|
+
isArray
|
|
59
|
+
? (reactive.get() as unknown[]).at(key as number)
|
|
60
|
+
: (reactive.get() as PlainObject)[key],
|
|
61
|
+
) as Computed<unknown>;
|
|
43
62
|
|
|
44
63
|
mapped.set(key, item);
|
|
45
64
|
}
|
|
@@ -47,10 +66,7 @@ export function getReactiveValueInProxy(
|
|
|
47
66
|
return item;
|
|
48
67
|
}
|
|
49
68
|
|
|
50
|
-
export function setProxyValue(
|
|
51
|
-
proxy: ArrayOrPlainObject,
|
|
52
|
-
value: ArrayOrPlainObject,
|
|
53
|
-
): void {
|
|
69
|
+
export function setProxyValue(proxy: ArrayOrPlainObject, value: ArrayOrPlainObject): void {
|
|
54
70
|
startBatch();
|
|
55
71
|
|
|
56
72
|
const proxyKeys = Object.keys(proxy);
|
|
@@ -61,9 +77,7 @@ export function setProxyValue(
|
|
|
61
77
|
for (let index = 0; index < length; index += 1) {
|
|
62
78
|
const key = proxyKeys[index];
|
|
63
79
|
|
|
64
|
-
(proxy as PlainObject)[key] = valueKeys.includes(key)
|
|
65
|
-
? (value as PlainObject)[key]
|
|
66
|
-
: undefined;
|
|
80
|
+
(proxy as PlainObject)[key] = valueKeys.includes(key) ? (value as PlainObject)[key] : undefined;
|
|
67
81
|
}
|
|
68
82
|
|
|
69
83
|
length = valueKeys.length;
|
|
@@ -88,7 +102,7 @@ export function setValueInProxy<Value extends ArrayOrPlainObject, Equal>(
|
|
|
88
102
|
|
|
89
103
|
if (isArray) {
|
|
90
104
|
const isIndex = !Number.isNaN(Number(property));
|
|
91
|
-
const isLength = property ===
|
|
105
|
+
const isLength = property === PROPERTY_LENGTH;
|
|
92
106
|
|
|
93
107
|
if (!(isIndex || isLength)) {
|
|
94
108
|
return Reflect.set(target, property, value);
|
package/src/helpers/value.ts
CHANGED
|
@@ -1,11 +1,5 @@
|
|
|
1
1
|
import {flushHandlers} from '../batch';
|
|
2
|
-
import {
|
|
3
|
-
ACTIVE,
|
|
4
|
-
ARRAY_OFFSET,
|
|
5
|
-
ARRAY_PEEK,
|
|
6
|
-
ARRAY_THRESHOLD,
|
|
7
|
-
BATCH,
|
|
8
|
-
} from '../constants';
|
|
2
|
+
import {ACTIVE, ARRAY_OFFSET, ARRAY_PEEK, ARRAY_THRESHOLD, BATCH} from '../constants';
|
|
9
3
|
import type {InternalComputed, ReactiveState} from '../models';
|
|
10
4
|
|
|
11
5
|
export function emitValue<Value>(state: ReactiveState<Value, never>): void {
|
package/src/index.ts
CHANGED
|
@@ -1,12 +1,6 @@
|
|
|
1
1
|
export {startBatch, stopBatch} from './batch';
|
|
2
2
|
export {effect, type Effect} from './effect';
|
|
3
|
-
export {
|
|
4
|
-
isArray,
|
|
5
|
-
isComputed,
|
|
6
|
-
isEffect,
|
|
7
|
-
isReactive,
|
|
8
|
-
isSignal,
|
|
9
|
-
} from './helpers/is';
|
|
3
|
+
export {isArray, isComputed, isEffect, isReactive, isSignal} from './helpers/is';
|
|
10
4
|
export type {Unsubscribe} from './models';
|
|
11
5
|
export {array, type ReactiveArray} from './value/array';
|
|
12
6
|
export {computed, type Computed} from './value/computed';
|
package/src/value/array.ts
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
import type {GenericCallback} from '@oscarpalmer/atoms/models';
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
METHODS_UPDATE,
|
|
5
|
-
NAME_ARRAY,
|
|
6
|
-
} from '../constants';
|
|
7
|
-
import {getReactiveValueInProxy, setValueInProxy} from '../helpers/proxy';
|
|
2
|
+
import {METHODS_AFFECTING_LENGTH, METHODS_UPDATE, NAME_ARRAY, PROPERTY_LENGTH} from '../constants';
|
|
3
|
+
import {emityProxyValues, getReactiveValueInProxy, setValueInProxy} from '../helpers/proxy';
|
|
8
4
|
import {emitValue, equalArrays, getValue} from '../helpers/value';
|
|
9
5
|
import type {ReactiveOptions, ReactiveState, Unsubscribe} from '../models';
|
|
10
6
|
import {noop, subscribe} from '../subscription';
|
|
@@ -28,11 +24,7 @@ export class ReactiveArray<Item> extends Reactive<Item[], Item> {
|
|
|
28
24
|
* Set the length of the array
|
|
29
25
|
*/
|
|
30
26
|
set length(value: number) {
|
|
31
|
-
if (
|
|
32
|
-
typeof value === 'number' &&
|
|
33
|
-
value >= 0 &&
|
|
34
|
-
value !== this.state.value.length
|
|
35
|
-
) {
|
|
27
|
+
if (typeof value === 'number' && value >= 0 && value !== this.state.value.length) {
|
|
36
28
|
this.get().length = value;
|
|
37
29
|
}
|
|
38
30
|
}
|
|
@@ -73,9 +65,7 @@ export class ReactiveArray<Item> extends Reactive<Item[], Item> {
|
|
|
73
65
|
* @param callback Callback to evaluate each item
|
|
74
66
|
* @return Computed array of filtered items
|
|
75
67
|
*/
|
|
76
|
-
filter(
|
|
77
|
-
callback: (item: Item, index: number, array: Item[]) => boolean,
|
|
78
|
-
): Computed<Item[]> {
|
|
68
|
+
filter(callback: (item: Item, index: number, array: Item[]) => boolean): Computed<Item[]> {
|
|
79
69
|
return computed(() => this.get().filter(callback));
|
|
80
70
|
}
|
|
81
71
|
|
|
@@ -102,11 +92,7 @@ export class ReactiveArray<Item> extends Reactive<Item[], Item> {
|
|
|
102
92
|
return getReactiveValueInProxy(this, this.#indiced, first, true).get();
|
|
103
93
|
}
|
|
104
94
|
|
|
105
|
-
|
|
106
|
-
return this.length;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
return getValue(this.state);
|
|
95
|
+
return first === PROPERTY_LENGTH ? this.length : getValue(this.state);
|
|
110
96
|
}
|
|
111
97
|
|
|
112
98
|
/**
|
|
@@ -114,9 +100,7 @@ export class ReactiveArray<Item> extends Reactive<Item[], Item> {
|
|
|
114
100
|
* @param callback Callback to transform each item
|
|
115
101
|
* @return Computed array of mapped items
|
|
116
102
|
*/
|
|
117
|
-
map<Mapped>(
|
|
118
|
-
callback: (item: Item, index: number, array: Item[]) => Mapped,
|
|
119
|
-
): Computed<Mapped[]> {
|
|
103
|
+
map<Mapped>(callback: (item: Item, index: number, array: Item[]) => Mapped): Computed<Mapped[]> {
|
|
120
104
|
return computed(() => this.get().map(callback));
|
|
121
105
|
}
|
|
122
106
|
|
|
@@ -127,37 +111,33 @@ export class ReactiveArray<Item> extends Reactive<Item[], Item> {
|
|
|
127
111
|
* Use this only if you're modifying nested data that would be ignored by equality checks._
|
|
128
112
|
*/
|
|
129
113
|
notify(): void {
|
|
130
|
-
|
|
114
|
+
emityProxyValues(this.state, this.#indiced);
|
|
131
115
|
}
|
|
132
116
|
|
|
133
117
|
/**
|
|
134
118
|
* @inheritdoc
|
|
135
119
|
*/
|
|
136
|
-
peek(): Item[];
|
|
120
|
+
override peek(): Item[];
|
|
137
121
|
|
|
138
122
|
/**
|
|
139
123
|
* Get the value at an index _(without reactivity)_
|
|
140
124
|
* @param index Index of item to get _(if negative, starts from the end)_
|
|
141
125
|
* @returns Item at index, or `undefined` if it doesn't exist
|
|
142
126
|
*/
|
|
143
|
-
peek(index: number): Item | undefined;
|
|
127
|
+
override peek(index: number): Item | undefined;
|
|
144
128
|
|
|
145
129
|
/**
|
|
146
130
|
* Get the length of the array _(without reactivity)_
|
|
147
131
|
* @returns Length of the array
|
|
148
132
|
*/
|
|
149
|
-
peek(property: 'length'): number;
|
|
133
|
+
override peek(property: 'length'): number;
|
|
150
134
|
|
|
151
|
-
peek(value?: unknown): unknown {
|
|
135
|
+
override peek(value?: unknown): unknown {
|
|
152
136
|
if (value === 'length') {
|
|
153
137
|
return this.#size.peek();
|
|
154
138
|
}
|
|
155
139
|
|
|
156
|
-
|
|
157
|
-
return this.state.value.at(value);
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
return [...this.state.value];
|
|
140
|
+
return typeof value === 'number' ? this.state.value.at(value) : [...this.state.value];
|
|
161
141
|
}
|
|
162
142
|
|
|
163
143
|
/**
|
|
@@ -222,17 +202,13 @@ export class ReactiveArray<Item> extends Reactive<Item[], Item> {
|
|
|
222
202
|
* @returns Removed items
|
|
223
203
|
*/
|
|
224
204
|
splice(from: number, to?: number, ...items: Item[]): Item[] {
|
|
225
|
-
return this.state.value.splice(
|
|
226
|
-
from,
|
|
227
|
-
to ?? this.state.value.length,
|
|
228
|
-
...items,
|
|
229
|
-
);
|
|
205
|
+
return this.state.value.splice(from, to ?? this.state.value.length, ...items);
|
|
230
206
|
}
|
|
231
207
|
|
|
232
208
|
/**
|
|
233
209
|
* @inheritdoc
|
|
234
210
|
*/
|
|
235
|
-
subscribe(callback: (value: Item[]) => void): Unsubscribe;
|
|
211
|
+
override subscribe(callback: (value: Item[]) => void): Unsubscribe;
|
|
236
212
|
|
|
237
213
|
/**
|
|
238
214
|
* Subscribe to changes at a specific index
|
|
@@ -240,22 +216,11 @@ export class ReactiveArray<Item> extends Reactive<Item[], Item> {
|
|
|
240
216
|
* @param callback Callback for changes
|
|
241
217
|
* @returns Unsubscribe callback
|
|
242
218
|
*/
|
|
243
|
-
subscribe(
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
): Unsubscribe;
|
|
247
|
-
|
|
248
|
-
subscribe(
|
|
249
|
-
first: number | GenericCallback,
|
|
250
|
-
second?: GenericCallback,
|
|
251
|
-
): Unsubscribe {
|
|
219
|
+
override subscribe(index: number, callback: (value: Item | undefined) => void): Unsubscribe;
|
|
220
|
+
|
|
221
|
+
override subscribe(first: number | GenericCallback, second?: GenericCallback): Unsubscribe {
|
|
252
222
|
if (typeof first === 'number' && typeof second === 'function') {
|
|
253
|
-
return getReactiveValueInProxy(
|
|
254
|
-
this,
|
|
255
|
-
this.#indiced,
|
|
256
|
-
first,
|
|
257
|
-
true,
|
|
258
|
-
).subscribe(second);
|
|
223
|
+
return getReactiveValueInProxy(this, this.#indiced, first, true).subscribe(second);
|
|
259
224
|
}
|
|
260
225
|
|
|
261
226
|
return typeof first === 'function' ? subscribe(this.state, first) : noop;
|
|
@@ -286,13 +251,10 @@ export class ReactiveArray<Item> extends Reactive<Item[], Item> {
|
|
|
286
251
|
/**
|
|
287
252
|
* Create a reactive array
|
|
288
253
|
* @param value Initial array of items
|
|
289
|
-
* @param options
|
|
254
|
+
* @param options Reactivity options
|
|
290
255
|
* @returns Reactive array
|
|
291
256
|
*/
|
|
292
|
-
export function array<Item>(
|
|
293
|
-
value: Item[],
|
|
294
|
-
options?: ReactiveOptions<Item>,
|
|
295
|
-
): ReactiveArray<Item> {
|
|
257
|
+
export function array<Item>(value: Item[], options?: ReactiveOptions<Item>): ReactiveArray<Item> {
|
|
296
258
|
return new ReactiveArray(Array.isArray(value) ? value : [], options);
|
|
297
259
|
}
|
|
298
260
|
|
|
@@ -307,14 +269,10 @@ function updateArray<Item>(
|
|
|
307
269
|
const previousLength = array.length;
|
|
308
270
|
|
|
309
271
|
return (...args: unknown[]): unknown => {
|
|
310
|
-
const result = (array[type as never] as (...args: unknown[]) => unknown)(
|
|
311
|
-
...args,
|
|
312
|
-
);
|
|
272
|
+
const result = (array[type as never] as (...args: unknown[]) => unknown)(...args);
|
|
313
273
|
|
|
314
274
|
if (
|
|
315
|
-
affectsLength
|
|
316
|
-
? array.length !== previousLength
|
|
317
|
-
: !equalArrays(state, previousArray, array)
|
|
275
|
+
affectsLength ? array.length !== previousLength : !equalArrays(state, previousArray, array)
|
|
318
276
|
) {
|
|
319
277
|
emitValue(state);
|
|
320
278
|
|
package/src/value/computed.ts
CHANGED
|
@@ -67,6 +67,9 @@ export class Computed<Value> extends Reactive<Value> {
|
|
|
67
67
|
|
|
68
68
|
/**
|
|
69
69
|
* Create a computed value
|
|
70
|
+
* @param callback Callback to compute the value
|
|
71
|
+
* @param options Reactivity options
|
|
72
|
+
* @returns Computed value
|
|
70
73
|
*/
|
|
71
74
|
export function computed<Value>(
|
|
72
75
|
callback: () => Value,
|
package/src/value/reactive.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import {NAME_MORA} from '../constants';
|
|
1
2
|
import type {ReactiveOptions, ReactiveState, Unsubscribe} from '../models';
|
|
2
3
|
import {subscribe, unsubscribe} from '../subscription';
|
|
3
4
|
|
|
@@ -17,7 +18,7 @@ export abstract class Reactive<Value, Equal = Value> {
|
|
|
17
18
|
this.state.equal = options.equal;
|
|
18
19
|
}
|
|
19
20
|
|
|
20
|
-
Object.defineProperty(this,
|
|
21
|
+
Object.defineProperty(this, NAME_MORA, {
|
|
21
22
|
value: name,
|
|
22
23
|
});
|
|
23
24
|
}
|
package/src/value/signal.ts
CHANGED
|
@@ -39,12 +39,9 @@ export class Signal<Value> extends Reactive<Value> {
|
|
|
39
39
|
/**
|
|
40
40
|
* Create a reactive value
|
|
41
41
|
* @param value Initial value
|
|
42
|
-
* @param options
|
|
42
|
+
* @param options Reactivity options
|
|
43
43
|
* @returns Reactive value
|
|
44
44
|
*/
|
|
45
|
-
export function signal<Value>(
|
|
46
|
-
value: Value,
|
|
47
|
-
options?: ReactiveOptions<Value>,
|
|
48
|
-
): Signal<Value> {
|
|
45
|
+
export function signal<Value>(value: Value, options?: ReactiveOptions<Value>): Signal<Value> {
|
|
49
46
|
return new Signal<Value>(value, options);
|
|
50
47
|
}
|