@arcote.tech/arc 0.0.15 → 0.0.17

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.
Files changed (42) hide show
  1. package/dist/collection/collection.d.ts +27 -13
  2. package/dist/collection/index.d.ts +0 -1
  3. package/dist/collection/queries/abstract-collection-query.d.ts +7 -8
  4. package/dist/collection/queries/one-item.d.ts +1 -1
  5. package/dist/collection/queries/util.d.ts +2 -2
  6. package/dist/context/context.d.ts +21 -10
  7. package/dist/context/element.d.ts +7 -7
  8. package/dist/context/query.d.ts +6 -3
  9. package/dist/data-storage/ForkStoreState.d.ts +1 -1
  10. package/dist/data-storage/StoreState.d.ts +37 -22
  11. package/dist/data-storage/data-storage-forked.d.ts +2 -2
  12. package/dist/data-storage/data-storage-master.d.ts +5 -3
  13. package/dist/data-storage/data-storage.abstract.d.ts +47 -0
  14. package/dist/data-storage/data-storage.interface.d.ts +14 -5
  15. package/dist/data-storage/deep-merge.d.ts +6 -0
  16. package/dist/data-storage/index.d.ts +1 -1
  17. package/dist/data-storage/master-store-state.d.ts +26 -13
  18. package/dist/data-storage/store-state-fork.d.ts +10 -1
  19. package/dist/data-storage/store-state-master.d.ts +11 -1
  20. package/dist/data-storage/store-state.abstract.d.ts +15 -4
  21. package/dist/data-storage/store-state.d.ts +37 -22
  22. package/dist/elements/date.d.ts +1 -1
  23. package/dist/elements/index.d.ts +1 -0
  24. package/dist/elements/object copy.d.ts +29 -0
  25. package/dist/elements/object.d.ts +15 -0
  26. package/dist/elements/record.d.ts +19 -0
  27. package/dist/elements/state.d.ts +2 -0
  28. package/dist/index.d.ts +1 -0
  29. package/dist/index.js +491 -141
  30. package/dist/rtc/index.d.ts +1 -1
  31. package/dist/rtc/messages.d.ts +3 -3
  32. package/dist/rtc/rtc.d.ts +4 -3
  33. package/dist/state/db.d.ts +1 -0
  34. package/dist/state/index.d.ts +1 -2
  35. package/dist/state/query-builder.d.ts +3 -2
  36. package/dist/state/query.d.ts +10 -17
  37. package/dist/state/state-change.d.ts +1 -0
  38. package/dist/state/state.d.ts +20 -16
  39. package/dist/state/util.d.ts +1 -0
  40. package/dist/utils/deep-merge.d.ts +6 -0
  41. package/dist/utils.d.ts +10 -1
  42. package/package.json +1 -1
@@ -1,24 +1,22 @@
1
- import type { ArcContextElement } from "../context/element";
1
+ import { ArcContextElement } from "../context/element";
2
2
  import type { ArcIdAny } from "../elements/id";
3
3
  import { type ArcObjectAny } from "../elements/object";
4
- import { objectUtil, type util } from "../utils";
5
- import type { CollectionChange } from "./collection-change";
4
+ import { objectUtil, type DeepPartial, type util } from "../utils";
6
5
  import type { DataStorage } from "../data-storage";
7
- import type { ArcDefault } from "../elements/default";
8
6
  import { ArcAllItemsQueryBuilder } from "./query-builders/all-items";
9
7
  import { ArcIndexedItemsQueryBuilder } from "./query-builders/indexed";
10
8
  import { ArcOneItemQueryBuilder } from "./query-builders/one-item";
11
9
  export type CollectionItem<C extends ArcCollectionAny | ArcIndexedCollectionAny> = objectUtil.simplify<{
12
10
  _id: string;
13
11
  } & ReturnType<C["deserialize"]>>;
14
- export type Deserialize<Id extends ArcIdAny, Schema extends ArcObjectAny | ArcDefault<ArcObjectAny>> = objectUtil.simplify<{
12
+ export type Deserialize<Id extends ArcIdAny, Schema extends ArcObjectAny> = objectUtil.simplify<{
15
13
  _id: ReturnType<Id["deserialize"]>;
16
14
  } & ReturnType<Schema["deserialize"]>>;
17
15
  type CollectionQueryBuilder<T extends ArcCollection<any, any, any>> = {
18
16
  all: () => ArcAllItemsQueryBuilder<T>;
19
17
  one: (id: util.GetType<T["id"]> | undefined) => ArcOneItemQueryBuilder<T>;
20
18
  };
21
- type CollectionCommandContext<Id extends ArcIdAny, Schema extends ArcObjectAny | ArcDefault<ArcObjectAny>> = {
19
+ type CollectionCommandContext<Id extends ArcIdAny, Schema extends ArcObjectAny> = {
22
20
  one: (id: util.GetType<Id>) => Deserialize<Id, Schema>;
23
21
  remove: (id: util.GetType<Id>) => Promise<any>;
24
22
  all: () => Deserialize<Id, Schema>[];
@@ -26,9 +24,25 @@ type CollectionCommandContext<Id extends ArcIdAny, Schema extends ArcObjectAny |
26
24
  id: util.GetType<Id>;
27
25
  }>;
28
26
  set: (id: util.GetType<Id>, data: util.FirstArgument<Schema["serialize"]>) => Promise<any>;
29
- modify: (id: util.GetType<Id>, data: objectUtil.addQuestionMarks<Partial<util.FirstArgument<Schema["serialize"]>>>) => Promise<any>;
27
+ edit: (id: util.GetType<Id>, editCallback: (item: Deserialize<Id, Schema>) => Promise<void> | void) => Promise<void>;
28
+ modify: (id: util.GetType<Id>, data: objectUtil.addQuestionMarks<DeepPartial<util.FirstArgument<Schema["serialize"]>>>) => Promise<any>;
30
29
  };
31
- export declare class ArcCollection<Name extends string, Id extends ArcIdAny, Schema extends ArcObjectAny | ArcDefault<ArcObjectAny>> implements ArcContextElement<CollectionChange, CollectionChange> {
30
+ export declare class ArcCollection<Name extends string, Id extends ArcIdAny, Schema extends ArcObjectAny> extends ArcContextElement<{
31
+ type: "delete";
32
+ from: CollectionItem<ArcCollection<Name, Id, Schema>>;
33
+ } | {
34
+ type: "set";
35
+ to: CollectionItem<ArcCollection<Name, Id, Schema>>;
36
+ } | {
37
+ type: "modify";
38
+ from: CollectionItem<ArcCollection<Name, Id, Schema>>;
39
+ to: CollectionItem<ArcCollection<Name, Id, Schema>>;
40
+ changes: objectUtil.simplify<DeepPartial<util.FirstArgument<Schema["serialize"]>>>;
41
+ } | {
42
+ type: "mutate";
43
+ from: CollectionItem<ArcCollection<Name, Id, Schema>>;
44
+ to: CollectionItem<ArcCollection<Name, Id, Schema>>;
45
+ }> {
32
46
  readonly name: Name;
33
47
  readonly id: Id;
34
48
  readonly schema: Schema;
@@ -43,17 +57,17 @@ export declare class ArcCollection<Name extends string, Id extends ArcIdAny, Sch
43
57
  _id: util.FirstArgument<Id["deserialize"]>;
44
58
  } & objectUtil.addQuestionMarks<util.FirstArgument<Schema["deserialize"]>>>): Deserialize<Id, Schema>;
45
59
  queryBuilder(): CollectionQueryBuilder<ArcCollection<Name, Id, Schema>>;
46
- commandContext(dataStorage: DataStorage): CollectionCommandContext<Id, Schema>;
60
+ commandContext(dataStorage: DataStorage, publishEvent: (event: this["$event"]) => Promise<void>): CollectionCommandContext<Id, Schema>;
47
61
  indexBy<Indexes extends keyof ReturnType<Schema["deserialize"]>, const I extends {
48
62
  [name: string]: Indexes[];
49
63
  }>(indexes: I): ArcIndexedCollection<Name, Id, Schema, keyof ReturnType<Schema["deserialize"]>, I>;
50
64
  }
51
- export declare class ArcIndexedCollection<Name extends string, Id extends ArcIdAny, Schema extends ArcObjectAny | ArcDefault<ArcObjectAny>, Indexes extends keyof ReturnType<Schema["deserialize"]>, const I extends {
65
+ export declare class ArcIndexedCollection<Name extends string, Id extends ArcIdAny, Schema extends ArcObjectAny, Indexes extends keyof ReturnType<Schema["deserialize"]>, const I extends {
52
66
  [name: string]: Indexes[];
53
67
  }> extends ArcCollection<Name, Id, Schema> {
54
68
  readonly indexes: I;
55
69
  constructor(name: Name, id: Id, schema: Schema, options: ArcCollectionOptions<Id, Schema>, indexes: I);
56
- commandContext(dataStorage: DataStorage): CollectionCommandContext<Id, Schema> & {
70
+ commandContext(dataStorage: DataStorage, publishEvent: (event: this["$event"]) => Promise<void>): CollectionCommandContext<Id, Schema> & {
57
71
  [func in keyof I]: (args: {
58
72
  [Key in I[func][number]]: util.GetType<Schema>[Key];
59
73
  }) => Deserialize<Id, Schema>[];
@@ -66,9 +80,9 @@ export declare class ArcIndexedCollection<Name extends string, Id extends ArcIdA
66
80
  }
67
81
  export type ArcCollectionAny = ArcCollection<any, any, any>;
68
82
  export type ArcIndexedCollectionAny = ArcIndexedCollection<any, any, any, any, any>;
69
- type ArcCollectionOptions<Id extends ArcIdAny, Schema extends ArcObjectAny | ArcDefault<ArcObjectAny>> = {
83
+ type ArcCollectionOptions<Id extends ArcIdAny, Schema extends ArcObjectAny> = {
70
84
  sort?: (a: Deserialize<Id, Schema>, b: Deserialize<Id, Schema>) => number;
71
85
  };
72
- export declare function collection<Name extends string, Id extends ArcIdAny, Schema extends ArcObjectAny | ArcDefault<ArcObjectAny>>(name: Name, id: Id, schema: Schema, options?: ArcCollectionOptions<Id, Schema>): ArcCollection<Name, Id, Schema>;
86
+ export declare function collection<Name extends string, Id extends ArcIdAny, Schema extends ArcObjectAny>(name: Name, id: Id, schema: Schema, options?: ArcCollectionOptions<Id, Schema>): ArcCollection<Name, Id, Schema>;
73
87
  export {};
74
88
  //# sourceMappingURL=collection.d.ts.map
@@ -1,5 +1,4 @@
1
1
  export * from "./collection";
2
- export * from "./collection-change";
3
2
  export * from "./queries/abstract-collection-query";
4
3
  export * from "./queries/util";
5
4
  //# sourceMappingURL=index.d.ts.map
@@ -1,15 +1,14 @@
1
- import type { ArcQuery } from "../../context/query";
1
+ import { ArcQuery, type ArcQueryListener } from "../../context/query";
2
2
  import type { DataStorage, ListenerEvent, StoreState } from "../../data-storage";
3
3
  import type { ArcCollectionAny, CollectionItem } from "../collection";
4
- export type CollectionQueryListener<Response> = (result: Response) => void;
5
- export declare abstract class ArcCollectionQuery<Collection extends ArcCollectionAny, Response> implements ArcQuery<Response> {
4
+ export declare abstract class ArcCollectionQuery<Collection extends ArcCollectionAny, Result> extends ArcQuery<Result> {
6
5
  protected collection: Collection;
7
- lastResult?: Response;
8
- private listener?;
6
+ protected bindedChangeHandler: (changes: ListenerEvent<CollectionItem<Collection>>[]) => void;
7
+ private store;
9
8
  constructor(collection: Collection);
10
- run(dataStorage: DataStorage, listener?: CollectionQueryListener<Response>): Promise<Response>;
11
- protected abstract onChange(change: ListenerEvent<CollectionItem<Collection>>): Response | false;
12
- protected abstract fetch(store: StoreState<CollectionItem<Collection>>): Promise<Response>;
9
+ run(dataStorage: DataStorage, listener?: ArcQueryListener<Result>): Promise<Result>;
10
+ protected abstract onChange(change: ListenerEvent<CollectionItem<Collection>>): Result | false;
11
+ protected abstract fetch(store: StoreState<CollectionItem<Collection>>): Promise<Result>;
13
12
  protected changeHandler(changes: ListenerEvent<CollectionItem<Collection>>[]): void;
14
13
  unsubscribe(): void;
15
14
  private nextResult;
@@ -9,7 +9,7 @@ export declare class ArcOneItemQuery<Collection extends ArcCollectionAny> extend
9
9
  _id: string;
10
10
  } & ReturnType<Collection["deserialize"]> extends infer T ? { [KeyType in keyof T]: ({
11
11
  _id: string;
12
- } & ReturnType<Collection["deserialize"]>)[KeyType]; } : never) | undefined;
12
+ } & ReturnType<Collection["deserialize"]>)[KeyType]; } : never) | null;
13
13
  protected fetch(store: StoreState<CollectionItem<Collection>>): Promise<({
14
14
  _id: string;
15
15
  } & ReturnType<Collection["deserialize"]> extends infer T ? { [KeyType in keyof T]: ({
@@ -1,3 +1,3 @@
1
- import type { ArcContextAny, ArcQuery, QueryFactoryFunction } from "../../context";
2
- export type QueryBuilderFunctionResult<QB extends QueryFactoryFunction<ArcContextAny>> = ReturnType<ReturnType<QB>["toQuery"]> extends ArcQuery<infer Result> ? Result : never;
1
+ import type { ArcContextAny, QueryFactoryFunction } from "../../context";
2
+ export type QueryBuilderFunctionResult<QB extends QueryFactoryFunction<ArcContextAny>> = ReturnType<ReturnType<QB>["toQuery"]>["lastResult"];
3
3
  //# sourceMappingURL=util.d.ts.map
@@ -1,21 +1,32 @@
1
1
  import type { DataStorage } from "../data-storage";
2
- import type { Commands } from "./commands";
3
- import type { ArcContextElementAny, ArcContextElementChanges } from "./element";
4
- declare class ArcContext<const C extends ArcContextElementAny[]> {
5
- readonly elements: C;
6
- readonly commands: Commands<ArcContext<C>>;
2
+ import type { objectUtil } from "../utils";
3
+ import type { CommandContext, Commands, CommandsClient } from "./commands";
4
+ import type { ArcContextElement, ArcContextElementAny } from "./element";
5
+ declare class ArcContext<const C extends ArcContextElementAny[], const Cmds extends Commands<ArcContext<C, any, any>>, const L extends Listener<ArcContext<C, any, any>>[]> {
7
6
  readonly version: number;
7
+ readonly elements: C;
8
+ readonly commands: Cmds;
9
+ readonly listeners?: L | undefined;
8
10
  elementsMap: {
9
11
  [key: string]: C[number];
10
12
  };
11
- constructor(elements: C, commands: Commands<ArcContext<C>>, version: number);
13
+ constructor(version: number, elements: C, commands: Cmds, listeners?: L | undefined);
12
14
  queryBuilder(): {
13
15
  [Collection in C[number] as Collection["name"]]: ReturnType<Collection["queryBuilder"]>;
14
16
  };
15
- commandContext(dataStorage: DataStorage): { [ContextElement in C[number] as ContextElement["name"]]: ReturnType<ContextElement["commandContext"]>; };
17
+ commandContext(client: string, dataStorage: DataStorage, publishEvent: (event: any) => Promise<void>): { [ContextElement in C[number] as ContextElement["name"]]: ReturnType<ContextElement["commandContext"]>; } & {
18
+ $client: string;
19
+ };
20
+ commandsClient(client: string, dataStorage: DataStorage, catchErrorCallback: (error: any) => void): CommandsClient<Cmds>;
16
21
  }
17
- export type ArcContextAny = ArcContext<ArcContextElementAny[]>;
18
- export type ArcContextChanges<C extends ArcContextAny> = ArcContextElementChanges<C["elements"][number]>;
19
- export declare function context<const Elements extends ArcContextElementAny[], Cmds extends Commands<ArcContext<Elements>>>(elements: Elements, commands: Cmds, version: number): ArcContext<Elements>;
22
+ export type ArcContextAny = ArcContext<ArcContextElementAny[], Commands<any>, any>;
23
+ export type ElementEvents<Element extends ArcContextElementAny> = Element extends ArcContextElement<any> ? Element["$event"] : never;
24
+ export type ContextEvents<Context extends ArcContextAny> = {
25
+ [Element in Context["elements"][number] as Element["name"]]: objectUtil.simplify<{
26
+ element: Element["name"];
27
+ } & ElementEvents<Element>>;
28
+ }[Context["elements"][number]["name"]];
29
+ export type Listener<Context extends ArcContextAny> = (event: ContextEvents<Context>, context: CommandContext<Context>) => Promise<void> | void;
30
+ export declare function context<const Elements extends ArcContextElementAny[], Cmds extends Commands<ArcContext<Elements, any, any>>, L extends Listener<ArcContext<Elements, Cmds, any>>[]>(version: number, elements: Elements, commands: Cmds, listeners?: L): ArcContext<Elements, Cmds, L>;
20
31
  export {};
21
32
  //# sourceMappingURL=context.d.ts.map
@@ -1,10 +1,10 @@
1
1
  import type { DataStorage } from "../data-storage";
2
- export interface ArcContextElement<Change, Event> {
3
- name: string;
4
- queryBuilder(): any;
5
- commandContext(dataStorage: DataStorage): unknown;
6
- deserialize?: (data: any) => any;
2
+ export declare abstract class ArcContextElement<const Event> {
3
+ readonly $event: Event;
4
+ abstract name: string;
5
+ abstract queryBuilder(): any;
6
+ abstract commandContext(dataStorage: DataStorage, publishEvent: (event: Event) => Promise<void>): any;
7
+ abstract deserialize(data: any): any;
7
8
  }
8
- export type ArcContextElementAny = ArcContextElement<any, any>;
9
- export type ArcContextElementChanges<Element extends ArcContextElementAny> = Element extends ArcContextElement<infer Change, any> ? Change : never;
9
+ export type ArcContextElementAny = ArcContextElement<any>;
10
10
  //# sourceMappingURL=element.d.ts.map
@@ -1,7 +1,10 @@
1
1
  import type { DataStorage } from "../data-storage";
2
- export interface ArcQuery<Result> {
3
- run(store: DataStorage, callback?: (data: Result) => void): Promise<Result>;
4
- unsubscribe(): void;
2
+ export type ArcQueryListener<Result> = (result: Result) => void;
3
+ export declare abstract class ArcQuery<Result> {
4
+ lastResult: Result;
5
+ listener?: ArcQueryListener<Result>;
6
+ abstract run(store: DataStorage, callback?: ArcQueryListener<Result>): Promise<Result>;
7
+ abstract unsubscribe(): void;
5
8
  }
6
9
  export type ArcQueryAny = ArcQuery<any>;
7
10
  //# sourceMappingURL=query.d.ts.map
@@ -1,5 +1,5 @@
1
1
  import type { QueryListenerCallback, StoreStateChange } from "./data-storage";
2
- import type { DataStorage } from "./data-storage.interface";
2
+ import type { DataStorage } from "./data-storage.abstract";
3
3
  import type { MasterStoreState } from "./store-state-master";
4
4
  import { StoreState } from "./store-state.abstract";
5
5
  export declare class ForkStoreState<
@@ -1,24 +1,39 @@
1
- import { ForkStoreState, QueryListener, QueryListenerCallback, StoreStateChange } from "./data-storage";
2
- import type { DataStorage } from "./data-storage.interface";
3
- export declare abstract class StoreState<Item extends {
1
+ import {
2
+ ForkStoreState,
3
+ QueryListener,
4
+ QueryListenerCallback,
5
+ StoreStateChange,
6
+ } from "./data-storage";
7
+ import type { DataStorage } from "./data-storage.abstract";
8
+ export declare abstract class StoreState<
9
+ Item extends {
4
10
  _id: string;
5
- }> {
6
- storeName: string;
7
- protected dataStorage: DataStorage;
8
- protected deserialize?: ((data: any) => Item) | undefined;
9
- protected listeners: Set<QueryListener<Item>>;
10
- protected indexQueryCache: Map<string, Item[]>;
11
- constructor(storeName: string, dataStorage: DataStorage, deserialize?: ((data: any) => Item) | undefined);
12
- abstract applyChanges(changes: StoreStateChange<Item>[]): Promise<void>;
13
- fork(): ForkStoreState<Item>;
14
- findByIndex(index: string, data: any, listener?: QueryListenerCallback<Item>): Promise<Item[]>;
15
- set(item: Item): void;
16
- remove(id: string): void;
17
- modify(id: string, data: Partial<Item>): void;
18
- unsubscribe(listener: QueryListener<Item>): void;
19
- private notifyListeners;
20
- private getAllLocalItems;
21
- private mergeWithLocalModifications;
22
- private matchesIndex;
11
+ },
12
+ > {
13
+ storeName: string;
14
+ protected dataStorage: DataStorage;
15
+ protected deserialize?: ((data: any) => Item) | undefined;
16
+ protected listeners: Set<QueryListener<Item>>;
17
+ protected indexQueryCache: Map<string, Item[]>;
18
+ constructor(
19
+ storeName: string,
20
+ dataStorage: DataStorage,
21
+ deserialize?: ((data: any) => Item) | undefined
22
+ );
23
+ abstract applyChanges(changes: StoreStateChange<Item>[]): Promise<void>;
24
+ fork(): ForkStoreState<Item>;
25
+ findByIndex(
26
+ index: string,
27
+ data: any,
28
+ listener?: QueryListenerCallback<Item>
29
+ ): Promise<Item[]>;
30
+ set(item: Item): void;
31
+ remove(id: string): void;
32
+ modify(id: string, data: Partial<Item>): void;
33
+ unsubscribe(listener: QueryListener<Item>): void;
34
+ private notifyListeners;
35
+ private getAllLocalItems;
36
+ private mergeWithLocalModifications;
37
+ private matchesIndex;
23
38
  }
24
- //# sourceMappingURL=StoreState.d.ts.map
39
+ //# sourceMappingURL=StoreState.d.ts.map
@@ -1,7 +1,7 @@
1
1
  import type { ReadTransaction, ReadWriteTransaction } from "../db";
2
- import type { DataStorage } from "./data-storage.interface";
2
+ import { DataStorage } from "./data-storage.abstract";
3
3
  import { StoreState } from "./store-state.abstract";
4
- export declare class ForkedDataStorage implements DataStorage {
4
+ export declare class ForkedDataStorage extends DataStorage {
5
5
  private master;
6
6
  private stores;
7
7
  constructor(master: DataStorage);
@@ -2,19 +2,21 @@ import type { ArcContextAny } from "../context";
2
2
  import type { DatabaseAdapter, ReadTransaction, ReadWriteTransaction } from "../db";
3
3
  import type { RealTimeCommunicationAdapterFactory } from "../rtc/rtc";
4
4
  import { ForkedDataStorage } from "./data-storage-forked";
5
- import type { DataStorage } from "./data-storage.interface";
5
+ import { DataStorage, type DataStorageChanges } from "./data-storage.abstract";
6
6
  import type { StoreState } from "./store-state.abstract";
7
- export declare class MasterDataStorage implements DataStorage {
7
+ export declare class MasterDataStorage extends DataStorage {
8
8
  private dbAdapter;
9
9
  private arcContext;
10
10
  private stores;
11
11
  private rtcAdapter;
12
- constructor(dbAdapter: Promise<DatabaseAdapter>, rtcAdapterFactory: RealTimeCommunicationAdapterFactory, arcContext: ArcContextAny);
12
+ constructor(dbAdapter: Promise<DatabaseAdapter> | DatabaseAdapter, rtcAdapterFactory: RealTimeCommunicationAdapterFactory, arcContext: ArcContextAny);
13
13
  getReadTransaction(): Promise<ReadTransaction>;
14
14
  getReadWriteTransaction(): Promise<ReadWriteTransaction>;
15
15
  getStore<Item extends {
16
16
  _id: string;
17
17
  }>(storeName: string): StoreState<Item>;
18
+ applyChanges(changes: DataStorageChanges[]): Promise<void[]>;
19
+ commitChanges(changes: DataStorageChanges[]): Promise<void>;
18
20
  fork(): ForkedDataStorage;
19
21
  sync(progressCallback: ({ store, size }: {
20
22
  store: string;
@@ -0,0 +1,47 @@
1
+ import { type Patch } from "mutative";
2
+ import type { ReadTransaction, ReadWriteTransaction } from "../db";
3
+ import type { ForkedDataStorage } from "./data-storage-forked";
4
+ import type { StoreState } from "./store-state.abstract";
5
+ export declare abstract class DataStorage {
6
+ abstract getStore<Item extends {
7
+ _id: string;
8
+ }>(storeName: string, deserialize?: (data: any) => Item): StoreState<Item>;
9
+ abstract fork(): ForkedDataStorage;
10
+ abstract getReadTransaction(): Promise<ReadTransaction>;
11
+ abstract getReadWriteTransaction(): Promise<ReadWriteTransaction>;
12
+ commitChanges(changes: DataStorageChanges[]): Promise<void>;
13
+ }
14
+ export type StoreStateChange<Item> = {
15
+ type: "set";
16
+ data: Item;
17
+ } | {
18
+ type: "delete";
19
+ id: string;
20
+ } | {
21
+ type: "modify";
22
+ id: string;
23
+ data: Partial<Item>;
24
+ } | {
25
+ type: "mutate";
26
+ id: string;
27
+ patches: Patch[];
28
+ };
29
+ export type QueryListenerCallback<Item> = (events: ListenerEvent<Item>[]) => void;
30
+ export type QueryListener<Item> = {
31
+ callback: QueryListenerCallback<Item>;
32
+ id?: string;
33
+ };
34
+ export type ListenerEvent<Item> = {
35
+ type: "set";
36
+ item: Item;
37
+ id: string;
38
+ } | {
39
+ type: "delete";
40
+ item: null;
41
+ id: string;
42
+ };
43
+ export type DataStorageChanges = {
44
+ store: string;
45
+ changes: StoreStateChange<any>[];
46
+ };
47
+ //# sourceMappingURL=data-storage.abstract.d.ts.map
@@ -1,13 +1,14 @@
1
+ import { type Patch } from "mutative";
1
2
  import type { ReadTransaction, ReadWriteTransaction } from "../db";
2
3
  import type { ForkedDataStorage } from "./data-storage-forked";
3
4
  import type { StoreState } from "./store-state.abstract";
4
- export interface DataStorage {
5
- getStore<Item extends {
5
+ export declare abstract class DataStorage {
6
+ abstract getStore<Item extends {
6
7
  _id: string;
7
8
  }>(storeName: string, deserialize?: (data: any) => Item): StoreState<Item>;
8
- fork(): ForkedDataStorage;
9
- getReadTransaction(): Promise<ReadTransaction>;
10
- getReadWriteTransaction(): Promise<ReadWriteTransaction>;
9
+ abstract fork(): ForkedDataStorage;
10
+ abstract getReadTransaction(): Promise<ReadTransaction>;
11
+ abstract getReadWriteTransaction(): Promise<ReadWriteTransaction>;
11
12
  }
12
13
  export type StoreStateChange<Item> = {
13
14
  type: "set";
@@ -19,6 +20,10 @@ export type StoreStateChange<Item> = {
19
20
  type: "modify";
20
21
  id: string;
21
22
  data: Partial<Item>;
23
+ } | {
24
+ type: "mutate";
25
+ id: string;
26
+ patches: Patch[];
22
27
  };
23
28
  export type QueryListenerCallback<Item> = (events: ListenerEvent<Item>[]) => void;
24
29
  export type QueryListener<Item> = {
@@ -34,4 +39,8 @@ export type ListenerEvent<Item> = {
34
39
  item: null;
35
40
  id: string;
36
41
  };
42
+ export type DataStorageChanges = {
43
+ store: string;
44
+ changes: StoreStateChange<any>[];
45
+ };
37
46
  //# sourceMappingURL=data-storage.interface.d.ts.map
@@ -0,0 +1,6 @@
1
+ type DeepMergeable = {
2
+ [key: string]: any;
3
+ };
4
+ export declare function deepMerge<T extends DeepMergeable>(target: T, source: Partial<T>): T;
5
+ export {};
6
+ //# sourceMappingURL=deep-merge.d.ts.map
@@ -1,6 +1,6 @@
1
1
  export * from "./data-storage-forked";
2
2
  export * from "./data-storage-master";
3
- export * from "./data-storage.interface";
3
+ export * from "./data-storage.abstract";
4
4
  export * from "./store-state-fork";
5
5
  export * from "./store-state-master";
6
6
  export * from "./store-state.abstract";
@@ -1,17 +1,30 @@
1
1
  import type { QueryListenerCallback, StoreStateChange } from "./data-storage";
2
- import type { DataStorage } from "./data-storage.interface";
2
+ import type { DataStorage } from "./data-storage.abstract";
3
3
  import { StoreState } from "./store-state.abstract";
4
- export declare class MasterStoreState<Item extends {
4
+ export declare class MasterStoreState<
5
+ Item extends {
5
6
  _id: string;
6
- }> extends StoreState<Item> {
7
- protected items: Map<string, Item>;
8
- protected isComplete: boolean;
9
- constructor(storeName: string, dataStorage: DataStorage, deserialize?: (data: any) => Item);
10
- applyChanges(changes: StoreStateChange<Item>[]): Promise<void>;
11
- findById(id: string, listener?: QueryListenerCallback<Item>): Promise<Item | undefined>;
12
- findByIndex(index: string, data: any, listener?: QueryListenerCallback<Item>): Promise<Item[]>;
13
- findAll(listener?: QueryListenerCallback<Item>): Promise<Item[]>;
14
- private mergeWithLocalModifications;
15
- private matchesIndex;
7
+ },
8
+ > extends StoreState<Item> {
9
+ protected items: Map<string, Item>;
10
+ protected isComplete: boolean;
11
+ constructor(
12
+ storeName: string,
13
+ dataStorage: DataStorage,
14
+ deserialize?: (data: any) => Item
15
+ );
16
+ applyChanges(changes: StoreStateChange<Item>[]): Promise<void>;
17
+ findById(
18
+ id: string,
19
+ listener?: QueryListenerCallback<Item>
20
+ ): Promise<Item | undefined>;
21
+ findByIndex(
22
+ index: string,
23
+ data: any,
24
+ listener?: QueryListenerCallback<Item>
25
+ ): Promise<Item[]>;
26
+ findAll(listener?: QueryListenerCallback<Item>): Promise<Item[]>;
27
+ private mergeWithLocalModifications;
28
+ private matchesIndex;
16
29
  }
17
- //# sourceMappingURL=master-store-state.d.ts.map
30
+ //# sourceMappingURL=master-store-state.d.ts.map
@@ -1,4 +1,4 @@
1
- import type { DataStorage, QueryListenerCallback, StoreStateChange } from "./data-storage.interface";
1
+ import type { DataStorage, ListenerEvent, QueryListenerCallback, StoreStateChange } from "./data-storage.abstract";
2
2
  import { StoreState } from "./store-state.abstract";
3
3
  export declare class ForkedStoreState<Item extends {
4
4
  _id: string;
@@ -7,6 +7,15 @@ export declare class ForkedStoreState<Item extends {
7
7
  protected changedItems: Map<string, Item | null>;
8
8
  changes: StoreStateChange<Item>[];
9
9
  constructor(storeName: string, dataStorage: DataStorage, master: StoreState<Item>, deserialize?: (data: any) => Item);
10
+ applyChangeAndReturnEvent(change: StoreStateChange<Item>): Promise<{
11
+ from: Item | null;
12
+ to: Item | null;
13
+ event: ListenerEvent<Item>;
14
+ }>;
15
+ applyChange(change: StoreStateChange<Item>): Promise<{
16
+ from: Item | null;
17
+ to: Item | null;
18
+ }>;
10
19
  applyChanges(changes: StoreStateChange<Item>[]): Promise<void>;
11
20
  findById(id: string, listener?: QueryListenerCallback<Item>): Promise<Item | null | undefined>;
12
21
  findByIndex(index: string, data: any, listener?: QueryListenerCallback<Item>): Promise<Item[]>;
@@ -1,4 +1,5 @@
1
- import type { DataStorage, QueryListenerCallback, StoreStateChange } from "./data-storage.interface";
1
+ import type { ReadWriteTransaction } from "../db";
2
+ import type { DataStorage, ListenerEvent, QueryListenerCallback, StoreStateChange } from "./data-storage.abstract";
2
3
  import { StoreState } from "./store-state.abstract";
3
4
  export declare class MasterStoreState<Item extends {
4
5
  _id: string;
@@ -6,6 +7,15 @@ export declare class MasterStoreState<Item extends {
6
7
  private items;
7
8
  private isComplete;
8
9
  constructor(storeName: string, dataStorage: DataStorage, deserialize?: (data: any) => Item);
10
+ applyChangeAndReturnEvent(transaction: ReadWriteTransaction, change: StoreStateChange<Item>): Promise<{
11
+ from: Item | null;
12
+ to: Item | null;
13
+ event: ListenerEvent<Item>;
14
+ }>;
15
+ applyChange(change: StoreStateChange<Item>): Promise<{
16
+ from: Item | null;
17
+ to: Item | null;
18
+ }>;
9
19
  applyChanges(changes: StoreStateChange<Item>[]): Promise<void>;
10
20
  findById(id: string, listener?: QueryListenerCallback<Item>): Promise<Item | null | undefined>;
11
21
  findByIndex(index: string, data: any, listener?: QueryListenerCallback<Item>): Promise<Item[]>;
@@ -1,4 +1,4 @@
1
- import type { DataStorage, ListenerEvent, QueryListener, QueryListenerCallback, StoreStateChange } from "./data-storage.interface";
1
+ import type { DataStorage, ListenerEvent, QueryListener, QueryListenerCallback, StoreStateChange } from "./data-storage.abstract";
2
2
  import { ForkedStoreState } from "./store-state-fork";
3
3
  export declare abstract class StoreState<Item extends {
4
4
  _id: string;
@@ -6,8 +6,12 @@ export declare abstract class StoreState<Item extends {
6
6
  storeName: string;
7
7
  protected dataStorage: DataStorage;
8
8
  protected deserialize?: ((data: any) => Item) | undefined;
9
- protected listeners: Set<QueryListener<Item>>;
9
+ protected listeners: Map<QueryListenerCallback<Item>, QueryListener<Item>>;
10
10
  constructor(storeName: string, dataStorage: DataStorage, deserialize?: ((data: any) => Item) | undefined);
11
+ abstract applyChange(change: StoreStateChange<Item>): Promise<{
12
+ from: Item | null;
13
+ to: Item | null;
14
+ }>;
11
15
  abstract applyChanges(changes: StoreStateChange<Item>[]): Promise<void>;
12
16
  abstract findById(id: string, listener?: QueryListenerCallback<Item>): Promise<Item | null | undefined>;
13
17
  abstract findAll(listener?: QueryListenerCallback<Item>): Promise<Item[]>;
@@ -15,8 +19,15 @@ export declare abstract class StoreState<Item extends {
15
19
  fork(): ForkedStoreState<Item>;
16
20
  set(item: Item): Promise<void>;
17
21
  remove(id: string): Promise<void>;
18
- modify(id: string, data: Partial<Item>): Promise<void>;
19
- unsubscribe(listener: QueryListener<Item>): void;
22
+ modify(id: string, data: Partial<Item>): Promise<{
23
+ from: Item | null;
24
+ to: Item | null;
25
+ }>;
26
+ mutate(id: string, editCallback: (item: Item) => Promise<void>): Promise<{
27
+ from: Item | null;
28
+ to: Item | null;
29
+ }>;
30
+ unsubscribe(listener: QueryListenerCallback<Item>): void;
20
31
  protected notifyListeners(events: ListenerEvent<Item>[]): void;
21
32
  }
22
33
  //# sourceMappingURL=store-state.abstract.d.ts.map
@@ -1,24 +1,39 @@
1
- import { ForkStoreState, QueryListener, QueryListenerCallback, StoreStateChange } from "./data-storage";
2
- import type { DataStorage } from "./data-storage.interface";
3
- export declare abstract class StoreState<Item extends {
1
+ import {
2
+ ForkStoreState,
3
+ QueryListener,
4
+ QueryListenerCallback,
5
+ StoreStateChange,
6
+ } from "./data-storage";
7
+ import type { DataStorage } from "./data-storage.abstract";
8
+ export declare abstract class StoreState<
9
+ Item extends {
4
10
  _id: string;
5
- }> {
6
- storeName: string;
7
- protected dataStorage: DataStorage;
8
- protected deserialize?: ((data: any) => Item) | undefined;
9
- protected listeners: Set<QueryListener<Item>>;
10
- protected indexQueryCache: Map<string, Item[]>;
11
- constructor(storeName: string, dataStorage: DataStorage, deserialize?: ((data: any) => Item) | undefined);
12
- abstract applyChanges(changes: StoreStateChange<Item>[]): Promise<void>;
13
- fork(): ForkStoreState<Item>;
14
- findByIndex(index: string, data: any, listener?: QueryListenerCallback<Item>): Promise<Item[]>;
15
- set(item: Item): void;
16
- remove(id: string): void;
17
- modify(id: string, data: Partial<Item>): void;
18
- unsubscribe(listener: QueryListener<Item>): void;
19
- private notifyListeners;
20
- private getAllLocalItems;
21
- private mergeWithLocalModifications;
22
- private matchesIndex;
11
+ },
12
+ > {
13
+ storeName: string;
14
+ protected dataStorage: DataStorage;
15
+ protected deserialize?: ((data: any) => Item) | undefined;
16
+ protected listeners: Set<QueryListener<Item>>;
17
+ protected indexQueryCache: Map<string, Item[]>;
18
+ constructor(
19
+ storeName: string,
20
+ dataStorage: DataStorage,
21
+ deserialize?: ((data: any) => Item) | undefined
22
+ );
23
+ abstract applyChanges(changes: StoreStateChange<Item>[]): Promise<void>;
24
+ fork(): ForkStoreState<Item>;
25
+ findByIndex(
26
+ index: string,
27
+ data: any,
28
+ listener?: QueryListenerCallback<Item>
29
+ ): Promise<Item[]>;
30
+ set(item: Item): void;
31
+ remove(id: string): void;
32
+ modify(id: string, data: Partial<Item>): void;
33
+ unsubscribe(listener: QueryListener<Item>): void;
34
+ private notifyListeners;
35
+ private getAllLocalItems;
36
+ private mergeWithLocalModifications;
37
+ private matchesIndex;
23
38
  }
24
- //# sourceMappingURL=store-state.d.ts.map
39
+ //# sourceMappingURL=store-state.d.ts.map