( definition: EntityDefinition<Data, View>, db: ClientDb, cleanup: CleanupObject )
| 71 | * This allows distinguishing between 'system' and 'user' events where user is only making changes with client. |
| 72 | */ |
| 73 | export function createEntityStore<Data, View>( |
| 74 | definition: EntityDefinition<Data, View>, |
| 75 | db: ClientDb, |
| 76 | cleanup: CleanupObject |
| 77 | ): EntityStore<Data, View> { |
| 78 | type StoreEntity = Entity<Data, View>; |
| 79 | |
| 80 | const { config } = definition; |
| 81 | /** |
| 82 | * Keep 2 'versions' of items list. Array and id<>item map for quick 'by id' access. |
| 83 | */ |
| 84 | const items = observable.array<StoreEntity>([]); |
| 85 | const itemsMap = observable.object<Record<string, Entity<Data, View>>>({}); |
| 86 | |
| 87 | const getIsEntityAccessable = |
| 88 | config.rootFilter && |
| 89 | cachedComputed(function getIsEntityAccessable(entity: StoreEntity) { |
| 90 | return config.rootFilter!(entity, db); |
| 91 | }); |
| 92 | |
| 93 | const getRootSource = cachedComputed( |
| 94 | function getSourceForQueryInput(): Entity<Data, View>[] { |
| 95 | let output = items as StoreEntity[]; |
| 96 | |
| 97 | if (config.rootFilter) { |
| 98 | output = output.filter((entity) => getIsEntityAccessable!(entity)); |
| 99 | } |
| 100 | |
| 101 | return output; |
| 102 | }, |
| 103 | { equals: areArraysShallowEqual } |
| 104 | ); |
| 105 | |
| 106 | const sortItems = cachedComputed((items: StoreEntity[]) => { |
| 107 | if (!config.defaultSort) { |
| 108 | return items; |
| 109 | } |
| 110 | |
| 111 | return sortBy(items, config.defaultSort); |
| 112 | }); |
| 113 | |
| 114 | const allItems = cachedComputedWithoutArgs(() => { |
| 115 | return sortItems(getRootSource()); |
| 116 | }); |
| 117 | |
| 118 | const propIndexMap = new Map< |
| 119 | keyof Data | keyof View, |
| 120 | QueryIndex<Data, View, IndexableKey<Data & View>> |
| 121 | >(); |
| 122 | |
| 123 | function getEntityId(entity: Entity<Data, View>) { |
| 124 | const id = `${entity[config.idField!]}`; |
| 125 | |
| 126 | return id; |
| 127 | } |
| 128 | |
| 129 | const createOrReuseQuery = deepMemoize( |
| 130 | function createOrReuseQuery( |
no test coverage detected