0.2.0 - Mid migration

This commit is contained in:
Daniel Mason 2022-04-25 14:47:15 +12:00
parent 139e6a915e
commit 7e38fdbd7d
42393 changed files with 5358157 additions and 62 deletions

View file

@ -0,0 +1,11 @@
import type { Comparer, IdSelector, EntityAdapter } from './models';
/**
*
* @param options
*
* @public
*/
export declare function createEntityAdapter<T>(options?: {
selectId?: IdSelector<T>;
sortComparer?: false | Comparer<T>;
}): EntityAdapter<T>;

View file

@ -0,0 +1,8 @@
import type { EntityState } from './models';
export declare function getInitialEntityState<V>(): EntityState<V>;
export declare function createInitialStateFactory<V>(): {
getInitialState: {
(): EntityState<V>;
<S extends object>(additionalState: S): EntityState<V> & S;
};
};

View file

@ -0,0 +1,2 @@
export { createEntityAdapter } from './create_adapter';
export { Dictionary, EntityState, EntityAdapter, Update, IdSelector, Comparer, } from './models';

View file

@ -0,0 +1,97 @@
import type { PayloadAction } from '../createAction';
import type { IsAny } from '../tsHelpers';
/**
* @public
*/
export declare type EntityId = number | string;
/**
* @public
*/
export declare type Comparer<T> = (a: T, b: T) => number;
/**
* @public
*/
export declare type IdSelector<T> = (model: T) => EntityId;
/**
* @public
*/
export interface DictionaryNum<T> {
[id: number]: T | undefined;
}
/**
* @public
*/
export interface Dictionary<T> extends DictionaryNum<T> {
[id: string]: T | undefined;
}
/**
* @public
*/
export declare type Update<T> = {
id: EntityId;
changes: Partial<T>;
};
/**
* @public
*/
export interface EntityState<T> {
ids: EntityId[];
entities: Dictionary<T>;
}
/**
* @public
*/
export interface EntityDefinition<T> {
selectId: IdSelector<T>;
sortComparer: false | Comparer<T>;
}
export declare type PreventAny<S, T> = IsAny<S, EntityState<T>, S>;
/**
* @public
*/
export interface EntityStateAdapter<T> {
addOne<S extends EntityState<T>>(state: PreventAny<S, T>, entity: T): S;
addOne<S extends EntityState<T>>(state: PreventAny<S, T>, action: PayloadAction<T>): S;
addMany<S extends EntityState<T>>(state: PreventAny<S, T>, entities: readonly T[] | Record<EntityId, T>): S;
addMany<S extends EntityState<T>>(state: PreventAny<S, T>, entities: PayloadAction<readonly T[] | Record<EntityId, T>>): S;
setOne<S extends EntityState<T>>(state: PreventAny<S, T>, entity: T): S;
setOne<S extends EntityState<T>>(state: PreventAny<S, T>, action: PayloadAction<T>): S;
setMany<S extends EntityState<T>>(state: PreventAny<S, T>, entities: readonly T[] | Record<EntityId, T>): S;
setMany<S extends EntityState<T>>(state: PreventAny<S, T>, entities: PayloadAction<readonly T[] | Record<EntityId, T>>): S;
setAll<S extends EntityState<T>>(state: PreventAny<S, T>, entities: readonly T[] | Record<EntityId, T>): S;
setAll<S extends EntityState<T>>(state: PreventAny<S, T>, entities: PayloadAction<readonly T[] | Record<EntityId, T>>): S;
removeOne<S extends EntityState<T>>(state: PreventAny<S, T>, key: EntityId): S;
removeOne<S extends EntityState<T>>(state: PreventAny<S, T>, key: PayloadAction<EntityId>): S;
removeMany<S extends EntityState<T>>(state: PreventAny<S, T>, keys: readonly EntityId[]): S;
removeMany<S extends EntityState<T>>(state: PreventAny<S, T>, keys: PayloadAction<readonly EntityId[]>): S;
removeAll<S extends EntityState<T>>(state: PreventAny<S, T>): S;
updateOne<S extends EntityState<T>>(state: PreventAny<S, T>, update: Update<T>): S;
updateOne<S extends EntityState<T>>(state: PreventAny<S, T>, update: PayloadAction<Update<T>>): S;
updateMany<S extends EntityState<T>>(state: PreventAny<S, T>, updates: ReadonlyArray<Update<T>>): S;
updateMany<S extends EntityState<T>>(state: PreventAny<S, T>, updates: PayloadAction<ReadonlyArray<Update<T>>>): S;
upsertOne<S extends EntityState<T>>(state: PreventAny<S, T>, entity: T): S;
upsertOne<S extends EntityState<T>>(state: PreventAny<S, T>, entity: PayloadAction<T>): S;
upsertMany<S extends EntityState<T>>(state: PreventAny<S, T>, entities: readonly T[] | Record<EntityId, T>): S;
upsertMany<S extends EntityState<T>>(state: PreventAny<S, T>, entities: PayloadAction<readonly T[] | Record<EntityId, T>>): S;
}
/**
* @public
*/
export interface EntitySelectors<T, V> {
selectIds: (state: V) => EntityId[];
selectEntities: (state: V) => Dictionary<T>;
selectAll: (state: V) => T[];
selectTotal: (state: V) => number;
selectById: (state: V, id: EntityId) => T | undefined;
}
/**
* @public
*/
export interface EntityAdapter<T> extends EntityStateAdapter<T> {
selectId: IdSelector<T>;
sortComparer: false | Comparer<T>;
getInitialState(): EntityState<T>;
getInitialState<S extends object>(state: S): EntityState<T> & S;
getSelectors(): EntitySelectors<T, EntityState<T>>;
getSelectors<V>(selectState: (state: V) => EntityState<T>): EntitySelectors<T, V>;
}

View file

@ -0,0 +1,2 @@
import type { IdSelector, Comparer, EntityStateAdapter } from './models';
export declare function createSortedStateAdapter<T>(selectId: IdSelector<T>, sort: Comparer<T>): EntityStateAdapter<T>;

View file

@ -0,0 +1,5 @@
import type { EntityState, PreventAny } from './models';
import type { PayloadAction } from '../createAction';
import { IsAny } from '../tsHelpers';
export declare function createSingleArgumentStateOperator<V>(mutator: (state: EntityState<V>) => void): <S extends EntityState<V>>(state: IsAny<S, EntityState<V>, S>) => S;
export declare function createStateOperator<V, R>(mutator: (arg: R, state: EntityState<V>) => void): <S extends EntityState<V>>(state: S, arg: R | PayloadAction<R>) => S;

View file

@ -0,0 +1,7 @@
import type { EntityState, EntitySelectors } from './models';
export declare function createSelectorsFactory<T>(): {
getSelectors: {
(): EntitySelectors<T, EntityState<T>>;
<V>(selectState: (state: V) => EntityState<T>): EntitySelectors<T, V>;
};
};

View file

@ -0,0 +1,2 @@
import type { EntityStateAdapter, IdSelector } from './models';
export declare function createUnsortedStateAdapter<T>(selectId: IdSelector<T>): EntityStateAdapter<T>;

View file

@ -0,0 +1,4 @@
import type { EntityState, IdSelector, Update, EntityId } from './models';
export declare function selectIdValue<T>(entity: T, selectId: IdSelector<T>): EntityId;
export declare function ensureEntitiesArray<T>(entities: readonly T[] | Record<EntityId, T>): readonly T[];
export declare function splitAddedUpdatedEntities<T>(newEntities: readonly T[] | Record<EntityId, T>, selectId: IdSelector<T>, state: EntityState<T>): [T[], Update<T>[]];