| 33 | ) => void; |
| 34 | |
| 35 | export const getDefinableFunctions = <Thing, RowValue>( |
| 36 | store: Store, |
| 37 | getDefaultThing: () => Thing, |
| 38 | validateRowValue: (value: any) => RowValue, |
| 39 | addListener: AddListener, |
| 40 | callListeners: CallListeners, |
| 41 | ): [ |
| 42 | getStore: () => Store, |
| 43 | getThingIds: () => Ids, |
| 44 | forEachThing: (cb: (id: Id, value: Thing) => void) => void, |
| 45 | hasThing: (id: Id) => boolean, |
| 46 | getTableId: (id: Id) => Id, |
| 47 | getThing: (id: Id) => Thing | undefined, |
| 48 | setThing: (id: Id, thing?: Thing) => boolean | Map<string, Thing>, |
| 49 | setDefinition: (id: Id, tableId: Id) => void, |
| 50 | setDefinitionAndListen: ( |
| 51 | id: Id, |
| 52 | tableId: Id, |
| 53 | onChanged: OnChangedDecl<RowValue>, |
| 54 | getRowValue: (getCell: GetCell, rowId: Id) => RowValue, |
| 55 | getSortKey?: (getCell: GetCell, rowId: Id) => SortKey, |
| 56 | ) => void, |
| 57 | delDefinition: (id: Id) => void, |
| 58 | addThingIdsListener: (listener: () => void) => void, |
| 59 | destroy: () => void, |
| 60 | ] => { |
| 61 | const hasRow = store.hasRow; |
| 62 | const tableIds: IdMap<Id> = mapNew(); |
| 63 | const things: IdMap<Thing> = mapNew(); |
| 64 | const thingIdListeners: IdSet2 = mapNew(); |
| 65 | const allRowValues: IdMap2<RowValue> = mapNew(); |
| 66 | const allSortKeys: IdMap2<SortKey> = mapNew(); |
| 67 | const storeListenerIds: IdSet2 = mapNew(); |
| 68 | |
| 69 | const getStore = (): Store => store; |
| 70 | |
| 71 | const getThingIds = (): Ids => mapKeys(tableIds); |
| 72 | |
| 73 | const forEachThing = (cb: (id: Id, value: Thing) => void): void => |
| 74 | mapForEach(things, cb); |
| 75 | |
| 76 | const hasThing = (id: Id): boolean => collHas(things, id); |
| 77 | |
| 78 | const getTableId = (id: Id): Id => mapGet(tableIds, id) as Id; |
| 79 | |
| 80 | const getThing = (id: Id): Thing | undefined => mapGet(things, id); |
| 81 | |
| 82 | const setThing = (id: Id, thing: Thing | undefined): IdMap<Thing> => |
| 83 | mapSet(things, id, thing) as IdMap<Thing>; |
| 84 | |
| 85 | const addStoreListeners = (id: Id, ...listenerIds: Ids): void => { |
| 86 | const set = mapEnsure(storeListenerIds, id, setNew); |
| 87 | arrayForEach(listenerIds, (listenerId) => setAdd(set, listenerId)); |
| 88 | }; |
| 89 | |
| 90 | const delStoreListeners = (id: Id): void => |
| 91 | ifNotUndefined(mapGet(storeListenerIds, id), (allListenerIds) => { |
| 92 | arrayForEach(collValues(allListenerIds), (listenerId: Id) => { |