2023-06-23 12:12:12 -07:00
|
|
|
import type { EntitiesTransaction, Entity, EntityListState, ImportPosition } from './types';
|
2022-12-04 14:58:13 -08:00
|
|
|
|
2022-12-04 15:26:28 -08:00
|
|
|
const ENTITIES_IMPORT = 'ENTITIES_IMPORT' as const;
|
2023-03-14 12:13:49 -07:00
|
|
|
const ENTITIES_DELETE = 'ENTITIES_DELETE' as const;
|
2023-03-22 12:34:10 -07:00
|
|
|
const ENTITIES_DISMISS = 'ENTITIES_DISMISS' as const;
|
2023-03-22 17:45:02 -07:00
|
|
|
const ENTITIES_INCREMENT = 'ENTITIES_INCREMENT' as const;
|
2022-12-04 15:26:28 -08:00
|
|
|
const ENTITIES_FETCH_REQUEST = 'ENTITIES_FETCH_REQUEST' as const;
|
|
|
|
const ENTITIES_FETCH_SUCCESS = 'ENTITIES_FETCH_SUCCESS' as const;
|
|
|
|
const ENTITIES_FETCH_FAIL = 'ENTITIES_FETCH_FAIL' as const;
|
2023-03-22 16:17:28 -07:00
|
|
|
const ENTITIES_INVALIDATE_LIST = 'ENTITIES_INVALIDATE_LIST' as const;
|
2023-06-23 12:12:12 -07:00
|
|
|
const ENTITIES_TRANSACTION = 'ENTITIES_TRANSACTION' as const;
|
2022-12-04 14:58:13 -08:00
|
|
|
|
|
|
|
/** Action to import entities into the cache. */
|
2023-04-11 08:04:31 -07:00
|
|
|
function importEntities(entities: Entity[], entityType: string, listKey?: string, pos?: ImportPosition) {
|
2022-12-04 14:58:13 -08:00
|
|
|
return {
|
|
|
|
type: ENTITIES_IMPORT,
|
|
|
|
entityType,
|
|
|
|
entities,
|
|
|
|
listKey,
|
2023-04-11 08:04:31 -07:00
|
|
|
pos,
|
2022-12-04 14:58:13 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-03-15 14:52:09 -07:00
|
|
|
interface DeleteEntitiesOpts {
|
|
|
|
preserveLists?: boolean
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteEntities(ids: Iterable<string>, entityType: string, opts: DeleteEntitiesOpts = {}) {
|
2023-03-14 12:13:49 -07:00
|
|
|
return {
|
|
|
|
type: ENTITIES_DELETE,
|
|
|
|
ids,
|
|
|
|
entityType,
|
2023-03-15 14:52:09 -07:00
|
|
|
opts,
|
2023-03-14 12:13:49 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-03-22 12:34:10 -07:00
|
|
|
function dismissEntities(ids: Iterable<string>, entityType: string, listKey: string) {
|
|
|
|
return {
|
|
|
|
type: ENTITIES_DISMISS,
|
|
|
|
ids,
|
|
|
|
entityType,
|
|
|
|
listKey,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-03-22 17:45:02 -07:00
|
|
|
function incrementEntities(entityType: string, listKey: string, diff: number) {
|
|
|
|
return {
|
|
|
|
type: ENTITIES_INCREMENT,
|
|
|
|
entityType,
|
|
|
|
listKey,
|
|
|
|
diff,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-12-04 15:26:28 -08:00
|
|
|
function entitiesFetchRequest(entityType: string, listKey?: string) {
|
|
|
|
return {
|
|
|
|
type: ENTITIES_FETCH_REQUEST,
|
|
|
|
entityType,
|
|
|
|
listKey,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-03-22 16:47:10 -07:00
|
|
|
function entitiesFetchSuccess(
|
|
|
|
entities: Entity[],
|
|
|
|
entityType: string,
|
|
|
|
listKey?: string,
|
2023-04-11 08:04:31 -07:00
|
|
|
pos?: ImportPosition,
|
2023-03-22 16:47:10 -07:00
|
|
|
newState?: EntityListState,
|
|
|
|
overwrite = false,
|
|
|
|
) {
|
2022-12-04 15:26:28 -08:00
|
|
|
return {
|
|
|
|
type: ENTITIES_FETCH_SUCCESS,
|
|
|
|
entityType,
|
|
|
|
entities,
|
|
|
|
listKey,
|
2023-04-11 08:04:31 -07:00
|
|
|
pos,
|
2022-12-04 15:53:56 -08:00
|
|
|
newState,
|
2023-03-22 16:47:10 -07:00
|
|
|
overwrite,
|
2022-12-04 15:26:28 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-12-04 15:53:56 -08:00
|
|
|
function entitiesFetchFail(entityType: string, listKey: string | undefined, error: any) {
|
2022-12-04 15:26:28 -08:00
|
|
|
return {
|
|
|
|
type: ENTITIES_FETCH_FAIL,
|
|
|
|
entityType,
|
|
|
|
listKey,
|
2022-12-04 15:53:56 -08:00
|
|
|
error,
|
2022-12-04 15:26:28 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-03-22 16:17:28 -07:00
|
|
|
function invalidateEntityList(entityType: string, listKey: string) {
|
|
|
|
return {
|
|
|
|
type: ENTITIES_INVALIDATE_LIST,
|
|
|
|
entityType,
|
|
|
|
listKey,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-06-23 12:12:12 -07:00
|
|
|
function entitiesTransaction(transaction: EntitiesTransaction) {
|
|
|
|
return {
|
|
|
|
type: ENTITIES_TRANSACTION,
|
|
|
|
transaction,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-12-04 14:58:13 -08:00
|
|
|
/** Any action pertaining to entities. */
|
2022-12-04 15:26:28 -08:00
|
|
|
type EntityAction =
|
|
|
|
ReturnType<typeof importEntities>
|
2023-03-14 12:13:49 -07:00
|
|
|
| ReturnType<typeof deleteEntities>
|
2023-03-22 12:34:10 -07:00
|
|
|
| ReturnType<typeof dismissEntities>
|
2023-03-22 17:45:02 -07:00
|
|
|
| ReturnType<typeof incrementEntities>
|
2022-12-04 15:26:28 -08:00
|
|
|
| ReturnType<typeof entitiesFetchRequest>
|
|
|
|
| ReturnType<typeof entitiesFetchSuccess>
|
2023-03-22 16:17:28 -07:00
|
|
|
| ReturnType<typeof entitiesFetchFail>
|
2023-06-23 12:12:12 -07:00
|
|
|
| ReturnType<typeof invalidateEntityList>
|
|
|
|
| ReturnType<typeof entitiesTransaction>;
|
2022-12-04 14:58:13 -08:00
|
|
|
|
|
|
|
export {
|
|
|
|
ENTITIES_IMPORT,
|
2023-03-14 12:13:49 -07:00
|
|
|
ENTITIES_DELETE,
|
2023-03-22 12:34:10 -07:00
|
|
|
ENTITIES_DISMISS,
|
2023-03-22 17:45:02 -07:00
|
|
|
ENTITIES_INCREMENT,
|
2022-12-04 15:26:28 -08:00
|
|
|
ENTITIES_FETCH_REQUEST,
|
|
|
|
ENTITIES_FETCH_SUCCESS,
|
|
|
|
ENTITIES_FETCH_FAIL,
|
2023-03-22 16:17:28 -07:00
|
|
|
ENTITIES_INVALIDATE_LIST,
|
2023-06-23 12:12:12 -07:00
|
|
|
ENTITIES_TRANSACTION,
|
2022-12-04 14:58:13 -08:00
|
|
|
importEntities,
|
2023-03-14 12:13:49 -07:00
|
|
|
deleteEntities,
|
2023-03-22 12:34:10 -07:00
|
|
|
dismissEntities,
|
2023-03-22 17:45:02 -07:00
|
|
|
incrementEntities,
|
2022-12-04 15:26:28 -08:00
|
|
|
entitiesFetchRequest,
|
|
|
|
entitiesFetchSuccess,
|
|
|
|
entitiesFetchFail,
|
2023-03-22 16:17:28 -07:00
|
|
|
invalidateEntityList,
|
2023-06-23 12:12:12 -07:00
|
|
|
entitiesTransaction,
|
2023-03-15 14:52:09 -07:00
|
|
|
};
|
|
|
|
|
2023-06-23 12:12:12 -07:00
|
|
|
export type { DeleteEntitiesOpts, EntityAction };
|