MCPcopy Create free account
hub / github.com/clientdb/clientdb / createClientDb

Function createClientDb

core/db.ts:30–118  ·  view source on GitHub ↗
(
  definitions: Array<EntityDefinition<any, any>>,
  { contexts }: ClientDbConfig = {}
)

Source from the content-addressed store, hash-verified

28}
29
30export function createClientDb(
31 definitions: Array<EntityDefinition<any, any>>,
32 { contexts }: ClientDbConfig = {}
33): ClientDb {
34 const clientsLookup = new Map<
35 EntityDefinition<any, any>,
36 EntityClient<any, any>
37 >();
38
39 const cleanup = createCleanupObject();
40
41 const events = createEventsEmmiter<ClientDbEvents>();
42
43 let isDestroyed = false;
44
45 function destroy() {
46 if (isDestroyed) {
47 throw new Error("Client DB is already destroyed");
48 }
49
50 runInAction(() => {
51 cleanup.clean();
52 });
53
54 isDestroyed = true;
55 }
56
57 const utils: ClientDbUtils = {
58 assertNotDestroyed(msg) {
59 assert(!isDestroyed, `${msg} - ClientDB is destroyed`);
60 },
61 };
62
63 const clientdb: ClientDb = {
64 entity: getEntityClient,
65 getContextValue,
66 destroy,
67 get isDestroyed() {
68 return isDestroyed;
69 },
70 utils,
71 events,
72 };
73
74 for (const definition of definitions) {
75 const client = createEntityClient(definition, {
76 db: clientdb,
77 cleanup,
78 });
79
80 clientsLookup.set(definition, client);
81 }
82
83 function getEntityClient<Data, View>(
84 definition: EntityDefinition<Data, View>
85 ): EntityClient<Data, View> {
86 const client = clientsLookup.get(definition);
87

Callers 2

createTestDbFunction · 0.90
createTestDbFunction · 0.90

Calls 3

createCleanupObjectFunction · 0.90
createEventsEmmiterFunction · 0.90
createEntityClientFunction · 0.90

Tested by 1

createTestDbFunction · 0.72