r-state-tree 0.3.1 → 0.4.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.
@@ -1,16 +1,14 @@
1
- import { Props, Context, StoreConfiguration } from "../types";
1
+ import { Props, StoreConfiguration } from "../types";
2
2
  export declare function allowNewStore<T>(fn: () => T): T;
3
3
  export declare function createStore<K extends Store<T>, T extends Props>(Type: new (props: T) => K, props?: T): K;
4
4
  export declare function updateStore<K extends Store<T>, T extends Props>(store: K, props: T): K;
5
5
  export declare function types<T extends Store>(config: Partial<StoreConfiguration<T>>): Partial<StoreConfiguration<T>>;
6
- export default class Store<PropsType extends Props = Props, ContextType extends Context = Context> {
7
- static types: unknown;
6
+ export default class Store<PropsType extends Props = Props> {
7
+ static get types(): StoreConfiguration<unknown>;
8
8
  props: PropsType;
9
9
  constructor(props: PropsType);
10
10
  get key(): string | number | undefined;
11
- get context(): ContextType;
12
- createReaction<T>(track: () => T, callback: (a: T) => void): () => void;
13
- provideContext(): null | Record<string, unknown>;
11
+ reaction<T>(track: () => T, callback: (a: T) => void): () => void;
14
12
  storeDidMount(): void;
15
13
  storeWillUnmount(): void;
16
14
  }
@@ -1,31 +1,30 @@
1
1
  import { PreactObjectAdministration as ObjectAdministration } from "../observables";
2
2
  import Store from "./Store";
3
- import { StoreConfiguration, Props, Context } from "../types";
3
+ import { StoreConfiguration, Props } from "../types";
4
4
  export declare function updateProps(props: Props, newProps: Props): void;
5
5
  export declare function getStoreAdm(store: Store): StoreAdministration;
6
6
  export declare class StoreAdministration<StoreType extends Store = Store> extends ObjectAdministration<Store> {
7
7
  static proxyTraps: ProxyHandler<object>;
8
- configuration: StoreConfiguration<StoreType>;
9
8
  parent: StoreAdministration | null;
10
9
  mounted: boolean;
11
- computedContext: Context;
12
- private contextReactionUnsub;
10
+ private contextCache;
13
11
  private childStoreDataMap;
14
12
  private reactionsUnsub;
15
- setConfiguration(configuration: StoreConfiguration<StoreType>): void;
13
+ private configurationGetter?;
14
+ setConfiguration(configurationGetter: () => StoreConfiguration<StoreType>): void;
15
+ private get configuration();
16
16
  private createChildStore;
17
17
  private setStoreList;
18
18
  private setSingleStore;
19
19
  private updateStore;
20
- private updateStores;
21
20
  private getComputedGetter;
22
21
  private initializeStore;
23
- private initializeStores;
24
22
  private getStore;
25
- private getStores;
26
23
  private getModelRef;
27
24
  isRoot(): boolean;
28
- createReaction<T>(track: () => T, callback: (a: T) => void): () => void;
25
+ getContextValue<T>(contextId: symbol, provideSymbol: symbol, defaultValue: T | undefined, hasDefault: boolean): T;
26
+ private lookupContextValue;
27
+ reaction<T>(track: () => T, callback: (a: T) => void): () => void;
29
28
  mount(parent?: StoreAdministration | null): void;
30
29
  unmount(): void;
31
30
  }
package/dist/types.d.ts CHANGED
@@ -23,19 +23,21 @@ export type StoreCtor<S extends Store = Store> = (new (...args: ConstructorParam
23
23
  export type ChildModel = Model | Model[] | null;
24
24
  export type IdType = string | number;
25
25
  export declare enum CommonCfgTypes {
26
- child = "child",
27
- children = "children"
26
+ child = "child"
28
27
  }
29
28
  export declare enum ModelCfgTypes {
30
29
  state = "state",
31
30
  id = "id",
32
- modelRef = "modelRef",
33
- modelRefs = "modelRefs"
31
+ modelRef = "modelRef"
34
32
  }
35
33
  export declare enum StoreCfgTypes {
36
34
  model = "model"
37
35
  }
38
- export type ConfigurationTypes = CommonCfgTypes | ModelCfgTypes | StoreCfgTypes;
36
+ export declare enum ObservableCfgTypes {
37
+ observable = "observable",
38
+ computed = "computed"
39
+ }
40
+ export type ConfigurationTypes = CommonCfgTypes | ModelCfgTypes | StoreCfgTypes | ObservableCfgTypes;
39
41
  export type ConfigurationType = {
40
42
  type: ConfigurationTypes;
41
43
  childType?: Function;
@@ -44,16 +46,10 @@ export type ConfigurationType = {
44
46
  export type ModelConfiguration<T> = Record<PropertyKey, ConfigurationType>;
45
47
  export type StoreConfiguration<T> = Record<PropertyKey, ConfigurationType>;
46
48
  export type Configuration<T> = ModelConfiguration<T> | StoreConfiguration<T>;
47
- type NonFunctionPropertyNames<T> = {
48
- [K in keyof T]: T[K] extends Function ? never : K;
49
- }[keyof T];
50
- type ChildrenToSnapshot<T> = {
51
- [K in keyof T]: T[K] extends Model ? Snapshot<T[K]> : T[K] extends Array<infer R> ? R extends Model ? Array<Snapshot<R>> : T[K] : T[K];
52
- };
53
- type Nullable<T> = {
54
- [K in keyof T]: T[K] | null;
49
+ type SnapshotValue<T> = T extends Model ? Snapshot<T> : T extends Array<infer R> ? R extends Model ? Array<Snapshot<R>> : T : T;
50
+ export type Snapshot<T extends Model = Model> = {
51
+ [K in keyof T]?: T[K] extends infer U ? U extends Model | Model[] ? SnapshotValue<U> | null : U extends null | undefined ? null : SnapshotValue<Exclude<U, null | undefined>> | null : never;
55
52
  };
56
- export type Snapshot<T extends Model = Model> = Nullable<Partial<ChildrenToSnapshot<Pick<T, NonFunctionPropertyNames<T>>>>>;
57
53
  export type SnapshotDiff<T extends Model = Model> = {
58
54
  undo: Snapshot<T>;
59
55
  redo: Snapshot<T>;
@@ -66,12 +62,12 @@ export type RefSnapshot = {
66
62
  export declare const childType: ((childType: Function) => ConfigurationType) & {
67
63
  type: CommonCfgTypes;
68
64
  };
69
- export declare const childrenType: ((childType: Function) => ConfigurationType) & {
70
- type: CommonCfgTypes;
71
- };
72
65
  export declare const stateType: ConfigurationType;
73
- export declare const modelRefType: ConfigurationType;
74
- export declare const modelRefsType: ConfigurationType;
66
+ export declare const modelRefType: ((childType: Function) => ConfigurationType) & {
67
+ type: ModelCfgTypes;
68
+ };
75
69
  export declare const idType: ConfigurationType;
76
70
  export declare const modelType: ConfigurationType;
71
+ export declare const observableType: ConfigurationType;
72
+ export declare const computedType: ConfigurationType;
77
73
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "r-state-tree",
3
- "version": "0.3.1",
3
+ "version": "0.4.1",
4
4
  "description": "reactive state management library",
5
5
  "main": "dist/r-state-tree.cjs",
6
6
  "module": "dist/r-state-tree.js",
@@ -38,6 +38,7 @@
38
38
  },
39
39
  "homepage": "https://github.com/melnikov-s/r-state-tree#readme",
40
40
  "devDependencies": {
41
+ "@tsmetadata/polyfill": "^1.1.3",
41
42
  "@typescript-eslint/eslint-plugin": "^5.8.1",
42
43
  "@typescript-eslint/parser": "^5.8.1",
43
44
  "eslint": "^8.5.0",