bigbuffet-rw/app/soapbox/entity-store/actions.ts

139 lines
3.2 KiB
TypeScript
Raw Normal View History

2023-06-23 12:12:12 -07:00
import type { EntitiesTransaction, Entity, EntityListState, ImportPosition } from './types';
2022-12-04 14:58:13 -08:00
const ENTITIES_IMPORT = 'ENTITIES_IMPORT' as const;
2023-03-14 12:13:49 -07:00
const ENTITIES_DELETE = 'ENTITIES_DELETE' as const;
const ENTITIES_DISMISS = 'ENTITIES_DISMISS' as const;
2023-03-22 17:45:02 -07:00
const ENTITIES_INCREMENT = 'ENTITIES_INCREMENT' as const;
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;
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. */
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,
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
};
}
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,
};
}
function entitiesFetchRequest(entityType: string, listKey?: string) {
return {
type: ENTITIES_FETCH_REQUEST,
entityType,
listKey,
};
}
function entitiesFetchSuccess(
entities: Entity[],
entityType: string,
listKey?: string,
pos?: ImportPosition,
newState?: EntityListState,
overwrite = false,
) {
return {
type: ENTITIES_FETCH_SUCCESS,
entityType,
entities,
listKey,
pos,
2022-12-04 15:53:56 -08:00
newState,
overwrite,
};
}
2022-12-04 15:53:56 -08:00
function entitiesFetchFail(entityType: string, listKey: string | undefined, error: any) {
return {
type: ENTITIES_FETCH_FAIL,
entityType,
listKey,
2022-12-04 15:53:56 -08:00
error,
};
}
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. */
type EntityAction =
ReturnType<typeof importEntities>
2023-03-14 12:13:49 -07:00
| ReturnType<typeof deleteEntities>
| ReturnType<typeof dismissEntities>
2023-03-22 17:45:02 -07:00
| ReturnType<typeof incrementEntities>
| ReturnType<typeof entitiesFetchRequest>
| ReturnType<typeof entitiesFetchSuccess>
| 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,
ENTITIES_DISMISS,
2023-03-22 17:45:02 -07:00
ENTITIES_INCREMENT,
ENTITIES_FETCH_REQUEST,
ENTITIES_FETCH_SUCCESS,
ENTITIES_FETCH_FAIL,
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,
dismissEntities,
2023-03-22 17:45:02 -07:00
incrementEntities,
entitiesFetchRequest,
entitiesFetchSuccess,
entitiesFetchFail,
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 };