models.d.ts 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import type { UncheckedIndexedAccess } from '../uncheckedindexed';
  2. import type { Draft } from 'immer';
  3. import type { PayloadAction } from '../createAction';
  4. import type { GetSelectorsOptions } from './state_selectors';
  5. import type { CastAny, Id as Compute } from '../tsHelpers';
  6. /**
  7. * @public
  8. */
  9. export type EntityId = number | string;
  10. /**
  11. * @public
  12. */
  13. export type Comparer<T> = (a: T, b: T) => number;
  14. /**
  15. * @public
  16. */
  17. export type IdSelector<T, Id extends EntityId> = (model: T) => Id;
  18. /**
  19. * @public
  20. */
  21. export type Update<T, Id extends EntityId> = {
  22. id: Id;
  23. changes: Partial<T>;
  24. };
  25. /**
  26. * @public
  27. */
  28. export interface EntityState<T, Id extends EntityId> {
  29. ids: Id[];
  30. entities: Record<Id, T>;
  31. }
  32. /**
  33. * @public
  34. */
  35. export interface EntityDefinition<T, Id extends EntityId> {
  36. selectId: IdSelector<T, Id>;
  37. sortComparer: false | Comparer<T>;
  38. }
  39. export type PreventAny<S, T, Id extends EntityId> = CastAny<S, EntityState<T, Id>>;
  40. export type DraftableEntityState<T, Id extends EntityId> = EntityState<T, Id> | Draft<EntityState<T, Id>>;
  41. /**
  42. * @public
  43. */
  44. export interface EntityStateAdapter<T, Id extends EntityId> {
  45. addOne<S extends DraftableEntityState<T, Id>>(state: PreventAny<S, T, Id>, entity: T): S;
  46. addOne<S extends DraftableEntityState<T, Id>>(state: PreventAny<S, T, Id>, action: PayloadAction<T>): S;
  47. addMany<S extends DraftableEntityState<T, Id>>(state: PreventAny<S, T, Id>, entities: readonly T[] | Record<Id, T>): S;
  48. addMany<S extends DraftableEntityState<T, Id>>(state: PreventAny<S, T, Id>, entities: PayloadAction<readonly T[] | Record<Id, T>>): S;
  49. setOne<S extends DraftableEntityState<T, Id>>(state: PreventAny<S, T, Id>, entity: T): S;
  50. setOne<S extends DraftableEntityState<T, Id>>(state: PreventAny<S, T, Id>, action: PayloadAction<T>): S;
  51. setMany<S extends DraftableEntityState<T, Id>>(state: PreventAny<S, T, Id>, entities: readonly T[] | Record<Id, T>): S;
  52. setMany<S extends DraftableEntityState<T, Id>>(state: PreventAny<S, T, Id>, entities: PayloadAction<readonly T[] | Record<Id, T>>): S;
  53. setAll<S extends DraftableEntityState<T, Id>>(state: PreventAny<S, T, Id>, entities: readonly T[] | Record<Id, T>): S;
  54. setAll<S extends DraftableEntityState<T, Id>>(state: PreventAny<S, T, Id>, entities: PayloadAction<readonly T[] | Record<Id, T>>): S;
  55. removeOne<S extends DraftableEntityState<T, Id>>(state: PreventAny<S, T, Id>, key: Id): S;
  56. removeOne<S extends DraftableEntityState<T, Id>>(state: PreventAny<S, T, Id>, key: PayloadAction<Id>): S;
  57. removeMany<S extends DraftableEntityState<T, Id>>(state: PreventAny<S, T, Id>, keys: readonly Id[]): S;
  58. removeMany<S extends DraftableEntityState<T, Id>>(state: PreventAny<S, T, Id>, keys: PayloadAction<readonly Id[]>): S;
  59. removeAll<S extends DraftableEntityState<T, Id>>(state: PreventAny<S, T, Id>): S;
  60. updateOne<S extends DraftableEntityState<T, Id>>(state: PreventAny<S, T, Id>, update: Update<T, Id>): S;
  61. updateOne<S extends DraftableEntityState<T, Id>>(state: PreventAny<S, T, Id>, update: PayloadAction<Update<T, Id>>): S;
  62. updateMany<S extends DraftableEntityState<T, Id>>(state: PreventAny<S, T, Id>, updates: ReadonlyArray<Update<T, Id>>): S;
  63. updateMany<S extends DraftableEntityState<T, Id>>(state: PreventAny<S, T, Id>, updates: PayloadAction<ReadonlyArray<Update<T, Id>>>): S;
  64. upsertOne<S extends DraftableEntityState<T, Id>>(state: PreventAny<S, T, Id>, entity: T): S;
  65. upsertOne<S extends DraftableEntityState<T, Id>>(state: PreventAny<S, T, Id>, entity: PayloadAction<T>): S;
  66. upsertMany<S extends DraftableEntityState<T, Id>>(state: PreventAny<S, T, Id>, entities: readonly T[] | Record<Id, T>): S;
  67. upsertMany<S extends DraftableEntityState<T, Id>>(state: PreventAny<S, T, Id>, entities: PayloadAction<readonly T[] | Record<Id, T>>): S;
  68. }
  69. /**
  70. * @public
  71. */
  72. export interface EntitySelectors<T, V, Id extends EntityId> {
  73. selectIds: (state: V) => Id[];
  74. selectEntities: (state: V) => Record<Id, T>;
  75. selectAll: (state: V) => T[];
  76. selectTotal: (state: V) => number;
  77. selectById: (state: V, id: Id) => Compute<UncheckedIndexedAccess<T>>;
  78. }
  79. /**
  80. * @public
  81. */
  82. export interface EntityAdapter<T, Id extends EntityId> extends EntityStateAdapter<T, Id> {
  83. selectId: IdSelector<T, Id>;
  84. sortComparer: false | Comparer<T>;
  85. getInitialState(): EntityState<T, Id>;
  86. getInitialState<S extends object>(state: S): EntityState<T, Id> & S;
  87. getSelectors(selectState?: undefined, options?: GetSelectorsOptions): EntitySelectors<T, EntityState<T, Id>, Id>;
  88. getSelectors<V>(selectState: (state: V) => EntityState<T, Id>, options?: GetSelectorsOptions): EntitySelectors<T, V, Id>;
  89. }