r-state-tree 0.1.0-rc9 → 0.3.1
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/README.md +119 -0
- package/{lib → dist}/api.d.ts +9 -8
- package/dist/computedProxy.d.ts +2 -0
- package/{lib → dist}/decorators.d.ts +7 -10
- package/dist/index.d.ts +7 -0
- package/dist/model/ChildModelsAdministration.d.ts +35 -0
- package/{lib → dist}/model/Model.d.ts +13 -14
- package/dist/model/ModelAdministration.d.ts +37 -0
- package/{lib → dist}/model/idMap.d.ts +8 -7
- package/dist/observables/array.d.ts +21 -0
- package/dist/observables/collection.d.ts +43 -0
- package/dist/observables/date.d.ts +4 -0
- package/dist/observables/index.d.ts +6 -0
- package/dist/observables/internal/Administration.d.ts +17 -0
- package/dist/observables/internal/NodeMap.d.ts +23 -0
- package/dist/observables/internal/lookup.d.ts +14 -0
- package/dist/observables/internal/utils.d.ts +9 -0
- package/dist/observables/object.d.ts +25 -0
- package/dist/observables/preact.d.ts +68 -0
- package/dist/r-state-tree.cjs +2419 -0
- package/dist/r-state-tree.js +2419 -0
- package/{lib → dist}/store/Store.d.ts +16 -16
- package/{lib → dist}/store/StoreAdministration.d.ts +31 -35
- package/{lib → dist}/types.d.ts +77 -73
- package/dist/utils.d.ts +5 -0
- package/package.json +36 -28
- package/lib/computedProxy.d.ts +0 -2
- package/lib/graph.d.ts +0 -2
- package/lib/index.d.ts +0 -8
- package/lib/lobx.d.ts +0 -10
- package/lib/model/ModelAdministration.d.ts +0 -40
- package/lib/r-state-tree.js +0 -1119
- package/lib/r-state-tree.js.map +0 -1
- package/lib/r-state-tree.module.js +0 -1086
- package/lib/r-state-tree.module.js.map +0 -1
- package/lib/utils.d.ts +0 -2
package/README.md
CHANGED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
# r-state-tree
|
|
2
|
+
|
|
3
|
+
Reactive state management featuring store trees, computed child stores, and snapshot utilities backed by `@preact/signals-core`.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add r-state-tree
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Stores
|
|
12
|
+
|
|
13
|
+
Stores describe reactive state containers composed into a tree.
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import Store, { createStore, mount } from "r-state-tree";
|
|
17
|
+
import { child } from "r-state-tree/decorators";
|
|
18
|
+
|
|
19
|
+
class TodoStore extends Store {
|
|
20
|
+
props = { title: "" };
|
|
21
|
+
|
|
22
|
+
get title() {
|
|
23
|
+
return this.props.title;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
class AppStore extends Store {
|
|
28
|
+
@child get todo() {
|
|
29
|
+
return createStore(TodoStore, { title: "Write docs" });
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const app = mount(createStore(AppStore));
|
|
34
|
+
app.todo.title; // "Write docs"
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### Updating props
|
|
38
|
+
|
|
39
|
+
Stores receive reactive `props` objects. Use `updateStore` to change them.
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import { updateStore } from "r-state-tree";
|
|
43
|
+
|
|
44
|
+
updateStore(app.todo, { title: "Ship release" });
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Actions and batching
|
|
48
|
+
|
|
49
|
+
`runInBatch` groups updates to avoid redundant reactions.
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
import { runInBatch } from "r-state-tree";
|
|
53
|
+
|
|
54
|
+
runInBatch(() => {
|
|
55
|
+
app.todo.props.title = "Refactor";
|
|
56
|
+
});
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Models and snapshots
|
|
60
|
+
|
|
61
|
+
Models capture persistent state with snapshot utilities.
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
import Model from "r-state-tree/model/Model";
|
|
65
|
+
import {
|
|
66
|
+
applySnapshot,
|
|
67
|
+
onSnapshot,
|
|
68
|
+
onSnapshotDiff,
|
|
69
|
+
toSnapshot,
|
|
70
|
+
} from "r-state-tree";
|
|
71
|
+
|
|
72
|
+
class TodoModel extends Model {
|
|
73
|
+
static types = {
|
|
74
|
+
title: stateType,
|
|
75
|
+
assignee: modelRefType,
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
title = "";
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const todo = new TodoModel();
|
|
82
|
+
|
|
83
|
+
const stop = onSnapshot(todo, (snapshot) => {
|
|
84
|
+
console.log(snapshot.title);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
applySnapshot(todo, { title: "Learn signals" });
|
|
88
|
+
stop();
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Snapshot diffs
|
|
92
|
+
|
|
93
|
+
Use `onSnapshotDiff` to receive undo/redo payloads.
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
const off = onSnapshotDiff(todo, ({ undo, redo }) => {
|
|
97
|
+
// undo and redo contain patch-like snapshots
|
|
98
|
+
});
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Testing
|
|
102
|
+
|
|
103
|
+
The repository ships with Vitest suites covering stores, models, containers, and observable primitives.
|
|
104
|
+
|
|
105
|
+
```bash
|
|
106
|
+
pnpm test
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Build & publishing
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
pnpm build
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
The build emits CommonJS, ESM bundles, and type declarations under `dist/`.
|
|
116
|
+
|
|
117
|
+
## License
|
|
118
|
+
|
|
119
|
+
MIT
|
package/{lib → dist}/api.d.ts
RENAMED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import { Snapshot } from "./types";
|
|
2
|
-
import Store from "./store/Store";
|
|
3
|
-
import Model from "./model/Model";
|
|
4
|
-
export declare function mount<T extends Store>(container: T): T;
|
|
5
|
-
export declare function unmount<S extends Store>(container: S): void;
|
|
6
|
-
export declare function toSnapshot<T extends Model>(model: T): Snapshot<T>;
|
|
7
|
-
export declare function onSnapshot<T extends Model>(model: T, callback: (snapshot: Snapshot<T>, model: T) => void): () => void;
|
|
8
|
-
export declare function
|
|
1
|
+
import { Snapshot, SnapshotDiff } from "./types";
|
|
2
|
+
import Store from "./store/Store";
|
|
3
|
+
import Model from "./model/Model";
|
|
4
|
+
export declare function mount<T extends Store>(container: T): T;
|
|
5
|
+
export declare function unmount<S extends Store>(container: S): void;
|
|
6
|
+
export declare function toSnapshot<T extends Model>(model: T): Snapshot<T>;
|
|
7
|
+
export declare function onSnapshot<T extends Model>(model: T, callback: (snapshot: Snapshot<T>, model: T) => void): () => void;
|
|
8
|
+
export declare function onSnapshotDiff<T extends Model>(model: T, callback: (snapshotDiff: SnapshotDiff<T>, model: T) => void): () => void;
|
|
9
|
+
export declare function applySnapshot<T extends Model>(model: T, snapshot: Snapshot<T>): T;
|
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
export declare const
|
|
2
|
-
export declare const
|
|
3
|
-
export declare const
|
|
4
|
-
export declare const
|
|
5
|
-
export declare const
|
|
6
|
-
export declare const
|
|
7
|
-
export declare const
|
|
8
|
-
export declare const modelRefs: any;
|
|
9
|
-
export declare const identifier: any;
|
|
10
|
-
export declare const state: any;
|
|
1
|
+
export declare const child: any;
|
|
2
|
+
export declare const children: any;
|
|
3
|
+
export declare const model: any;
|
|
4
|
+
export declare const modelRef: any;
|
|
5
|
+
export declare const modelRefs: any;
|
|
6
|
+
export declare const identifier: any;
|
|
7
|
+
export declare const state: any;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import Store, { createStore, updateStore } from "./store/Store";
|
|
2
|
+
import Model from "./model/Model";
|
|
3
|
+
import { mount, unmount, onSnapshot, toSnapshot, applySnapshot, onSnapshotDiff } from "./api";
|
|
4
|
+
export { createStore, Store, Model, mount, unmount, updateStore, onSnapshot, onSnapshotDiff, toSnapshot, applySnapshot, };
|
|
5
|
+
export { observable, source, reportChanged, reportObserved, getSignal, isObservable, createSignal, createAtom, createComputed, createReaction, createListener, runInBatch, runInUntracked, createEffect, Observable, } from "./observables";
|
|
6
|
+
export * from "./decorators";
|
|
7
|
+
export type { Configuration, Snapshot, IdType, SnapshotDiff } from "./types";
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { ArrayAdministration } from "../observables";
|
|
2
|
+
export declare function notifyArrayUpdate<T>(arr: T[], index: number, oldValue: T, newValue: T): void;
|
|
3
|
+
export declare function notifySpliceArray<T>(arr: T[], index: number, added: T[], removed: T[]): void;
|
|
4
|
+
export declare function observe<T>(obj: object, method: MutationListener<T>): () => void;
|
|
5
|
+
export type UpdateArrayEvent<T> = {
|
|
6
|
+
object: T[];
|
|
7
|
+
type: "updateArray";
|
|
8
|
+
index: number;
|
|
9
|
+
oldValue: T;
|
|
10
|
+
newValue: T;
|
|
11
|
+
};
|
|
12
|
+
export type SpliceArrayEvent<T> = {
|
|
13
|
+
object: T[];
|
|
14
|
+
type: "spliceArray";
|
|
15
|
+
index: number;
|
|
16
|
+
added: T[];
|
|
17
|
+
removed: T[];
|
|
18
|
+
};
|
|
19
|
+
export type MutationEvent<T> = UpdateArrayEvent<T> | SpliceArrayEvent<T>;
|
|
20
|
+
export type MutationListener<T> = (ev: MutationEvent<T>) => void;
|
|
21
|
+
export type HasListener<T> = {
|
|
22
|
+
listener: ObservableListener<T>;
|
|
23
|
+
};
|
|
24
|
+
declare class ObservableListener<T> {
|
|
25
|
+
private listeners;
|
|
26
|
+
private notifying;
|
|
27
|
+
subscribe(l: MutationListener<T>): () => void;
|
|
28
|
+
get size(): number;
|
|
29
|
+
notify(ev: MutationEvent<T>): void;
|
|
30
|
+
}
|
|
31
|
+
export declare class ChildModelsAdministration<T> extends ArrayAdministration<T> {
|
|
32
|
+
set(index: number, newValue: T): void;
|
|
33
|
+
spliceWithArray(index: number, deleteCount?: number | undefined, newItems?: T[] | undefined): T[];
|
|
34
|
+
}
|
|
35
|
+
export {};
|
|
@@ -1,14 +1,13 @@
|
|
|
1
|
-
import { Snapshot } from "../types";
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
static
|
|
5
|
-
static
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
1
|
+
import { Snapshot } from "../types";
|
|
2
|
+
export default class Model {
|
|
3
|
+
static types: object;
|
|
4
|
+
static childTypes: object;
|
|
5
|
+
static create<T extends Model = Model>(this: {
|
|
6
|
+
new (...args: unknown[]): T;
|
|
7
|
+
}, snapshot?: Snapshot<T>, ...args: unknown[]): T;
|
|
8
|
+
constructor();
|
|
9
|
+
get parent(): Model | null;
|
|
10
|
+
modelDidInit(snapshot?: Snapshot<this>, ...args: unknown[]): void;
|
|
11
|
+
modelDidAttach(): void;
|
|
12
|
+
modelWillDetach(): void;
|
|
13
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { PreactObjectAdministration, ComputedNode, AtomNode } from "../observables";
|
|
2
|
+
import Model from "../model/Model";
|
|
3
|
+
import { ModelConfiguration, Snapshot, SnapshotChange } from "../types";
|
|
4
|
+
export declare function getConfigurationFromSnapshot(snapshot: object): ModelConfiguration<unknown> | undefined;
|
|
5
|
+
export declare function getModelAdm<T extends Model>(model: T): ModelAdministration;
|
|
6
|
+
export declare class ModelAdministration extends PreactObjectAdministration<any> {
|
|
7
|
+
static proxyTraps: ProxyHandler<object>;
|
|
8
|
+
configuration: ModelConfiguration<any>;
|
|
9
|
+
parent: ModelAdministration | null;
|
|
10
|
+
referencedAtoms: Map<PropertyKey, AtomNode>;
|
|
11
|
+
referencedModels: Map<PropertyKey, ComputedNode<Model[]>>;
|
|
12
|
+
activeModels: Set<PropertyKey>;
|
|
13
|
+
root: ModelAdministration;
|
|
14
|
+
private modelsTraceUnsub;
|
|
15
|
+
private writeInProgress;
|
|
16
|
+
private computedSnapshot;
|
|
17
|
+
private snapshotMap;
|
|
18
|
+
parentName: PropertyKey | null;
|
|
19
|
+
setConfiguration(configuration: ModelConfiguration<any>): void;
|
|
20
|
+
private getReferencedAtom;
|
|
21
|
+
private setState;
|
|
22
|
+
private setId;
|
|
23
|
+
private setModel;
|
|
24
|
+
private setModels;
|
|
25
|
+
private getModel;
|
|
26
|
+
private getModels;
|
|
27
|
+
private getModelRef;
|
|
28
|
+
private getModelRefs;
|
|
29
|
+
private setModelRef;
|
|
30
|
+
private setModelRefs;
|
|
31
|
+
private attach;
|
|
32
|
+
private detach;
|
|
33
|
+
private toJSON;
|
|
34
|
+
onSnapshotChange(onChange: SnapshotChange<any>): () => void;
|
|
35
|
+
loadSnapshot(snapshot: Snapshot<any>): void;
|
|
36
|
+
getSnapshot(): Snapshot<any>;
|
|
37
|
+
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { IdType } from "../types";
|
|
2
|
-
import Model from "./Model";
|
|
3
|
-
export declare function
|
|
4
|
-
export declare function
|
|
5
|
-
export declare function
|
|
6
|
-
export declare function
|
|
7
|
-
export declare function
|
|
1
|
+
import { IdType } from "../types";
|
|
2
|
+
import Model from "./Model";
|
|
3
|
+
export declare function onSnapshotLoad<T>(fn: () => T): T;
|
|
4
|
+
export declare function setIdentifier(model: Model, id: string | number): void;
|
|
5
|
+
export declare function getIdentifier(model: Model): IdType | undefined;
|
|
6
|
+
export declare function getModelById(root: Model, id: IdType): Model | undefined;
|
|
7
|
+
export declare function onModelAttached(model: Model): void;
|
|
8
|
+
export declare function onModelDetached(model: Model): void;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { AtomNode } from "./preact";
|
|
2
|
+
import { Administration } from "./internal/Administration";
|
|
3
|
+
import { SignalMap } from "./internal/NodeMap";
|
|
4
|
+
export declare class ArrayAdministration<T> extends Administration<T[]> {
|
|
5
|
+
valuesMap: SignalMap<number>;
|
|
6
|
+
keysAtom: AtomNode;
|
|
7
|
+
static proxyTraps: ProxyHandler<Array<unknown>>;
|
|
8
|
+
static methods: Partial<{
|
|
9
|
+
[K in keyof typeof Array.prototype as (typeof Array.prototype)[K] extends Function ? K : never]: (typeof Array.prototype)[K];
|
|
10
|
+
}>;
|
|
11
|
+
constructor(source?: T[]);
|
|
12
|
+
protected reportObserveDeep(): void;
|
|
13
|
+
getNode(key?: number): unknown;
|
|
14
|
+
get(index: number): T | undefined;
|
|
15
|
+
set(index: number, newValue: T): void;
|
|
16
|
+
getArrayLength(): number;
|
|
17
|
+
setArrayLength(newLength: number): void;
|
|
18
|
+
spliceWithArray(index: number, deleteCount?: number, newItems?: T[]): T[];
|
|
19
|
+
spliceItemsIntoValues(index: number, deleteCount: number, newItems: T[]): T[];
|
|
20
|
+
onArrayChanged(lengthChanged?: boolean, index?: number, count?: number): void;
|
|
21
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { AtomNode } from "./preact";
|
|
2
|
+
import { Administration } from "./internal/Administration";
|
|
3
|
+
import { AtomMap, SignalMap } from "./internal/NodeMap";
|
|
4
|
+
type Collection<K, V> = Set<K> | Map<K, V>;
|
|
5
|
+
export declare class CollectionAdministration<K, V = K> extends Administration<Collection<K, V>> {
|
|
6
|
+
isMap: boolean;
|
|
7
|
+
hasMap: AtomMap<K>;
|
|
8
|
+
valuesMap: SignalMap<K>;
|
|
9
|
+
keysAtom: AtomNode;
|
|
10
|
+
static proxyTraps: ProxyHandler<Set<unknown> | Map<unknown, unknown>>;
|
|
11
|
+
static methods: {
|
|
12
|
+
clear: (this: any) => unknown;
|
|
13
|
+
forEach: (this: any) => unknown;
|
|
14
|
+
has: (this: any) => unknown;
|
|
15
|
+
add: (this: any) => unknown;
|
|
16
|
+
set: (this: any) => unknown;
|
|
17
|
+
get: (this: any) => unknown;
|
|
18
|
+
delete: (this: any) => unknown;
|
|
19
|
+
entries: (this: any) => unknown;
|
|
20
|
+
keys: (this: any) => unknown;
|
|
21
|
+
values: (this: any) => unknown;
|
|
22
|
+
[Symbol.iterator]: (this: any) => unknown;
|
|
23
|
+
};
|
|
24
|
+
constructor(source: Collection<K, V>);
|
|
25
|
+
private hasEntry;
|
|
26
|
+
private onCollectionChange;
|
|
27
|
+
protected reportObserveDeep(): void;
|
|
28
|
+
getNode(key?: K): unknown;
|
|
29
|
+
clear(): void;
|
|
30
|
+
forEach(callbackFn: (value: V, key: K, collection: Collection<K, V>) => void, thisArg?: unknown): void;
|
|
31
|
+
get size(): number;
|
|
32
|
+
add(value: K): this;
|
|
33
|
+
delete(value: K): boolean;
|
|
34
|
+
has(value: K): boolean;
|
|
35
|
+
entries(): IterableIterator<[K, V]>;
|
|
36
|
+
keys(): IterableIterator<K>;
|
|
37
|
+
get(key: K): V | undefined;
|
|
38
|
+
set(key: K, value: V): this;
|
|
39
|
+
values(): IterableIterator<V>;
|
|
40
|
+
[Symbol.iterator](): IterableIterator<[K, V] | V>;
|
|
41
|
+
[Symbol.toStringTag]: string;
|
|
42
|
+
}
|
|
43
|
+
export {};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { getAdministration, getSource, isObservable, getObservable, getObservableClassInstance, getInternalNode, createObservableWithCustomAdministration, } from "./internal/lookup";
|
|
2
|
+
export { ArrayAdministration } from "./array";
|
|
3
|
+
export { CollectionAdministration } from "./collection";
|
|
4
|
+
export { DateAdministration } from "./date";
|
|
5
|
+
export { ObjectAdministration } from "./object";
|
|
6
|
+
export { AtomNode, createReaction, SignalNode, createAtom, runInBatch, createComputed, createSignal, runInUntracked, ListenerNode, createListener, ComputedNode, PreactObjectAdministration, getSignal, source, reportChanged, reportObserved, observable, createEffect, Observable, } from "./preact";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { ObservedAtomNode } from "../preact";
|
|
2
|
+
import { SignalMap } from "./NodeMap";
|
|
3
|
+
export declare class Administration<T extends object = any> {
|
|
4
|
+
static readonly proxyTraps: ProxyHandler<object>;
|
|
5
|
+
readonly proxy: T;
|
|
6
|
+
readonly source: T;
|
|
7
|
+
readonly atom: ObservedAtomNode;
|
|
8
|
+
protected valuesMap?: SignalMap;
|
|
9
|
+
protected isObserved: boolean;
|
|
10
|
+
private forceObservedAtoms?;
|
|
11
|
+
constructor(source: T);
|
|
12
|
+
protected flushChange(): void;
|
|
13
|
+
getNode(): unknown;
|
|
14
|
+
reportChanged(): void;
|
|
15
|
+
protected reportObserveDeep(): void;
|
|
16
|
+
reportObserved(deep?: boolean): void;
|
|
17
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { AtomNode, ObservedAtomNode, SignalNode } from "../preact";
|
|
2
|
+
declare class NodeMap<K = unknown, GraphNode extends AtomNode | SignalNode<unknown> = AtomNode | SignalNode<unknown>> {
|
|
3
|
+
private map;
|
|
4
|
+
private weakMap;
|
|
5
|
+
private observedAtom?;
|
|
6
|
+
private cleanUpRegistered;
|
|
7
|
+
constructor(observedAtom?: ObservedAtomNode);
|
|
8
|
+
private registerCleanup;
|
|
9
|
+
protected createNode(_initialValue?: unknown): GraphNode;
|
|
10
|
+
get(key: unknown): GraphNode | undefined;
|
|
11
|
+
delete(key: K): void;
|
|
12
|
+
add(key: K, atom: GraphNode): void;
|
|
13
|
+
getOrCreate(key: K, value?: unknown): GraphNode;
|
|
14
|
+
reportObserved(key: K, value?: unknown): void;
|
|
15
|
+
reportChanged(key: K, value?: unknown): void;
|
|
16
|
+
}
|
|
17
|
+
export declare class AtomMap<K = unknown> extends NodeMap<K, AtomNode> {
|
|
18
|
+
protected createNode(): AtomNode;
|
|
19
|
+
}
|
|
20
|
+
export declare class SignalMap<K = unknown, V = unknown> extends NodeMap<K, SignalNode<V>> {
|
|
21
|
+
protected createNode(initialValue?: unknown): SignalNode<V>;
|
|
22
|
+
}
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { CollectionAdministration } from "../collection";
|
|
2
|
+
import { PreactObjectAdministration } from "../preact";
|
|
3
|
+
import { ArrayAdministration } from "../array";
|
|
4
|
+
import { DateAdministration } from "../date";
|
|
5
|
+
import { Administration } from "./Administration";
|
|
6
|
+
export declare function getAdministration<T extends object>(obj: T): T extends Set<infer S> ? CollectionAdministration<S> : T extends Map<infer K, infer V> ? CollectionAdministration<K, V> : T extends Array<infer R> ? ArrayAdministration<R> : T extends Date ? DateAdministration : PreactObjectAdministration<any>;
|
|
7
|
+
export declare function getSource<T>(obj: T): T;
|
|
8
|
+
export declare function getAction<T extends Function>(fn: T): T;
|
|
9
|
+
export declare function getObservableClassInstance<T extends object>(value: T): T;
|
|
10
|
+
export declare function getObservableIfExists<T>(value: T): T | undefined;
|
|
11
|
+
export declare function createObservableWithCustomAdministration<T>(value: T, Adm: new (obj: any) => Administration): T;
|
|
12
|
+
export declare function getObservable<T>(value: T): T;
|
|
13
|
+
export declare function isObservable(obj: unknown): boolean;
|
|
14
|
+
export declare function getInternalNode(obj: object, key?: PropertyKey): any;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { AtomNode, ComputedNode, SignalNode } from "../preact";
|
|
2
|
+
export declare function defaultEquals<T>(a: T, b: T): boolean;
|
|
3
|
+
export declare function isNonPrimitive(val: unknown): val is object;
|
|
4
|
+
export declare function isPropertyKey(val: unknown): val is string | number | symbol;
|
|
5
|
+
export type PropertyType = "action" | "computed" | "observable";
|
|
6
|
+
export declare function getPropertyType(key: PropertyKey, obj: object): PropertyType;
|
|
7
|
+
export declare function getPropertyDescriptor(obj: object, key: PropertyKey): PropertyDescriptor | undefined;
|
|
8
|
+
export declare function isPlainObject(value: unknown): value is object;
|
|
9
|
+
export declare function resolveNode(node: SignalNode<unknown> | AtomNode | ComputedNode<unknown>): unknown;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { AtomNode, ComputedNode } from "./preact";
|
|
2
|
+
import { PropertyType } from "./internal/utils";
|
|
3
|
+
import { Administration } from "./internal/Administration";
|
|
4
|
+
import { AtomMap, SignalMap } from "./internal/NodeMap";
|
|
5
|
+
export declare class ObjectAdministration<T extends object> extends Administration<T> {
|
|
6
|
+
keysAtom: AtomNode;
|
|
7
|
+
hasMap: AtomMap<PropertyKey>;
|
|
8
|
+
valuesMap: SignalMap<PropertyKey>;
|
|
9
|
+
computedMap: Map<PropertyKey, ComputedNode<T[keyof T]>>;
|
|
10
|
+
types: Map<PropertyKey, PropertyType>;
|
|
11
|
+
static proxyTraps: ProxyHandler<object>;
|
|
12
|
+
constructor(source?: T);
|
|
13
|
+
private get;
|
|
14
|
+
private set;
|
|
15
|
+
private getComputed;
|
|
16
|
+
private callComputed;
|
|
17
|
+
private getType;
|
|
18
|
+
protected reportObserveDeep(): void;
|
|
19
|
+
reportChanged(): void;
|
|
20
|
+
getNode(key?: keyof T): unknown;
|
|
21
|
+
read(key: keyof T): unknown;
|
|
22
|
+
write(key: keyof T, newValue: T[keyof T]): void;
|
|
23
|
+
has(key: keyof T): boolean;
|
|
24
|
+
remove(key: keyof T): void;
|
|
25
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { Signal, ReadonlySignal } from "@preact/signals-core";
|
|
2
|
+
import { ObjectAdministration } from "./object";
|
|
3
|
+
export { isObservable } from "./index";
|
|
4
|
+
export declare class PreactObjectAdministration<T extends object> extends ObjectAdministration<T> {
|
|
5
|
+
static proxyTraps: ProxyHandler<object>;
|
|
6
|
+
}
|
|
7
|
+
export interface AtomNode {
|
|
8
|
+
reportObserved(): void;
|
|
9
|
+
reportChanged(val?: unknown): void;
|
|
10
|
+
node?: Signal<unknown>;
|
|
11
|
+
}
|
|
12
|
+
export type ObservedAtomNode = AtomNode & {
|
|
13
|
+
observing: boolean;
|
|
14
|
+
onObservedStateChange(callback: (observing: boolean) => void): () => void;
|
|
15
|
+
};
|
|
16
|
+
export interface SignalNode<T> {
|
|
17
|
+
reportObserved(): void;
|
|
18
|
+
reportChanged(val: T): void;
|
|
19
|
+
node?: Signal<T>;
|
|
20
|
+
get: () => T;
|
|
21
|
+
set: (value: T) => T;
|
|
22
|
+
}
|
|
23
|
+
export interface ComputedNode<T> {
|
|
24
|
+
get(): T;
|
|
25
|
+
node?: ReadonlySignal<T>;
|
|
26
|
+
clear: () => void;
|
|
27
|
+
}
|
|
28
|
+
export interface ListenerNode {
|
|
29
|
+
dispose: () => void;
|
|
30
|
+
track: <T>(trackFn: () => T) => T;
|
|
31
|
+
isDisposed: boolean;
|
|
32
|
+
}
|
|
33
|
+
export declare function createObservedAtom(): {
|
|
34
|
+
node: Signal<number>;
|
|
35
|
+
reportObserved(): number;
|
|
36
|
+
reportChanged(): number;
|
|
37
|
+
readonly observing: boolean;
|
|
38
|
+
onObservedStateChange(callback: (observing: boolean) => void): () => void;
|
|
39
|
+
};
|
|
40
|
+
export declare function createSignal<T>(initialValue: T): SignalNode<T>;
|
|
41
|
+
export declare function createAtom(): AtomNode;
|
|
42
|
+
export declare function createEffect(fn: () => void): () => void;
|
|
43
|
+
export declare function runInUntracked<T>(fn: () => T): T;
|
|
44
|
+
export declare function createReaction<T>(fn: () => T, callback: (value: T) => void): () => void;
|
|
45
|
+
export type Listener = {
|
|
46
|
+
dispose: () => void;
|
|
47
|
+
track: <T>(trackFn: () => T) => T;
|
|
48
|
+
isDisposed: boolean;
|
|
49
|
+
callback(listener: Listener): void;
|
|
50
|
+
};
|
|
51
|
+
export declare function createListener(callback: (listener: Listener) => void): Listener;
|
|
52
|
+
export declare function createComputed<T>(fn: () => T, context?: unknown): ComputedNode<T>;
|
|
53
|
+
export type PreactObservable<T> = T extends Function ? T : T extends Map<infer K, infer V> ? Map<K, PreactObservable<V>> : T extends Array<infer V> ? Array<PreactObservable<V>> : T extends Set<infer V> ? Set<PreactObservable<V>> : T extends WeakSet<infer V> ? WeakSet<PreactObservable<V>> : T extends WeakMap<infer K, infer V> ? WeakMap<K, PreactObservable<V>> : {
|
|
54
|
+
[key in keyof T]: T[key] extends object ? PreactObservable<T[key]> : T[key];
|
|
55
|
+
} & {
|
|
56
|
+
readonly [key in keyof T as T[key] extends object ? never : `$${string & key}`]?: Signal<T[key]>;
|
|
57
|
+
};
|
|
58
|
+
export declare function observable<T>(obj: T): PreactObservable<T>;
|
|
59
|
+
export declare function source<T>(obj: PreactObservable<T> | T): T;
|
|
60
|
+
export declare function runInBatch<T>(fn: () => T): T;
|
|
61
|
+
export declare class Observable {
|
|
62
|
+
constructor();
|
|
63
|
+
}
|
|
64
|
+
export declare function reportChanged<T extends object>(obj: T): T;
|
|
65
|
+
export declare function reportObserved<T extends object>(obj: T, opts?: {
|
|
66
|
+
deep?: boolean;
|
|
67
|
+
}): T;
|
|
68
|
+
export declare function getSignal<T extends object>(obj: T, key: keyof T): Signal<T[keyof T]>;
|