Skip to content

Commit

Permalink
refactor: movel types for each trait to a .model.ts file
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Guerrero committed Apr 17, 2024
1 parent 10a867d commit 4dc592b
Show file tree
Hide file tree
Showing 24 changed files with 562 additions and 443 deletions.
54 changes: 54 additions & 0 deletions libs/ngrx-traits/signals/src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,57 @@ export * from './with-entities-loading-call/with-entities-loading-call';
export * from './with-logger/with-logger';
export * from './with-calls/with-calls';
export * from './with-sync-to-web-storage/with-sync-to-web-storage';
export { NamedCallStateMethods } from './with-call-status/with-call-status.model';
export { NamedCallStateComputed } from './with-call-status/with-call-status.model';
export { NamedCallState } from './with-call-status/with-call-status.model';
export { CallStateMethods } from './with-call-status/with-call-status.model';
export { CallStateComputed } from './with-call-status/with-call-status.model';
export { CallState } from './with-call-status/with-call-status.model';
export { CallStatus } from './with-call-status/with-call-status.model';
export { ExtractCallParams } from './with-calls/with-calls.model';
export { ExtractCallResultType } from './with-calls/with-calls.model';
export { CallConfig } from './with-calls/with-calls.model';
export { Call } from './with-calls/with-calls.model';
export { NamedEntitiesFilterMethods } from './with-entities-filter/with-entities-local-filter.model';
export { EntitiesFilterMethods } from './with-entities-filter/with-entities-local-filter.model';
export { NamedEntitiesFilterComputed } from './with-entities-filter/with-entities-local-filter.model';
export { EntitiesFilterComputed } from './with-entities-filter/with-entities-local-filter.model';
export { NamedEntitiesFilterState } from './with-entities-filter/with-entities-local-filter.model';
export { EntitiesFilterState } from './with-entities-filter/with-entities-local-filter.model';
export { NamedEntitiesPaginationLocalMethods } from './with-entities-pagination/with-entities-local-pagination.model';
export { EntitiesPaginationLocalMethods } from './with-entities-pagination/with-entities-local-pagination.model';
export { NamedEntitiesPaginationLocalComputed } from './with-entities-pagination/with-entities-local-pagination.model';
export { EntitiesPaginationLocalComputed } from './with-entities-pagination/with-entities-local-pagination.model';
export { NamedEntitiesPaginationLocalState } from './with-entities-pagination/with-entities-local-pagination.model';
export { EntitiesPaginationLocalState } from './with-entities-pagination/with-entities-local-pagination.model';
export { NamedEntitiesPaginationRemoteMethods } from './with-entities-pagination/with-entities-remote-pagination.model';
export { NamedEntitiesPaginationSetResultMethods } from './with-entities-pagination/with-entities-remote-pagination.model';
export { EntitiesPaginationRemoteMethods } from './with-entities-pagination/with-entities-remote-pagination.model';
export { NamedEntitiesPaginationRemoteComputed } from './with-entities-pagination/with-entities-remote-pagination.model';
export { EntitiesPaginationRemoteComputed } from './with-entities-pagination/with-entities-remote-pagination.model';
export { NamedEntitiesPaginationRemoteState } from './with-entities-pagination/with-entities-remote-pagination.model';
export { EntitiesPaginationRemoteState } from './with-entities-pagination/with-entities-remote-pagination.model';
export { PaginationState } from './with-entities-pagination/with-entities-remote-pagination.model';
export { NamedEntitiesPaginationInfiniteMethods } from './with-entities-pagination/with-entities-remote-scroll-pagination.model';
export { EntitiesPaginationInfiniteMethods } from './with-entities-pagination/with-entities-remote-scroll-pagination.model';
export { NamedEntitiesPaginationInfiniteComputed } from './with-entities-pagination/with-entities-remote-scroll-pagination.model';
export { EntitiesPaginationInfiniteComputed } from './with-entities-pagination/with-entities-remote-scroll-pagination.model';
export { NamedEntitiesPaginationInfiniteState } from './with-entities-pagination/with-entities-remote-scroll-pagination.model';
export { EntitiesPaginationInfiniteState } from './with-entities-pagination/with-entities-remote-scroll-pagination.model';
export { InfinitePaginationState } from './with-entities-pagination/with-entities-remote-scroll-pagination.model';
export { NamedEntitiesMultiSelectionMethods } from './with-entities-selection/with-entities-multi-selection.model';
export { EntitiesMultiSelectionMethods } from './with-entities-selection/with-entities-multi-selection.model';
export { NamedEntitiesMultiSelectionComputed } from './with-entities-selection/with-entities-multi-selection.model';
export { EntitiesMultiSelectionComputed } from './with-entities-selection/with-entities-multi-selection.model';
export { NamedEntitiesMultiSelectionState } from './with-entities-selection/with-entities-multi-selection.model';
export { EntitiesMultiSelectionState } from './with-entities-selection/with-entities-multi-selection.model';
export { NamedEntitiesSingleSelectionMethods } from './with-entities-selection/with-entities-single-selection.model';
export { EntitiesSingleSelectionMethods } from './with-entities-selection/with-entities-single-selection.model';
export { NamedEntitiesSingleSelectionComputed } from './with-entities-selection/with-entities-single-selection.model';
export { EntitiesSingleSelectionComputed } from './with-entities-selection/with-entities-single-selection.model';
export { NamedEntitiesSingleSelectionState } from './with-entities-selection/with-entities-single-selection.model';
export { EntitiesSingleSelectionState } from './with-entities-selection/with-entities-single-selection.model';
export { NamedEntitiesSortMethods } from './with-entities-sort/with-entities-local-sort.model';
export { EntitiesSortMethods } from './with-entities-sort/with-entities-local-sort.model';
export { NamedEntitiesSortState } from './with-entities-sort/with-entities-local-sort.model';
export { EntitiesSortState } from './with-entities-sort/with-entities-local-sort.model';
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Signal } from '@angular/core';

export type CallStatus = 'init' | 'loading' | 'loaded' | { error: unknown };
export type CallState = {
callStatus: CallStatus;
};
export type CallStateComputed = {
isLoading: Signal<boolean>;
} & {
isLoaded: Signal<boolean>;
} & {
error: Signal<string | null>;
};
export type CallStateMethods = {
setLoading: () => void;
} & {
setLoaded: () => void;
} & {
setError: (error?: unknown) => void;
};
export type NamedCallState<Prop extends string> = {
[K in Prop as `${K}CallStatus`]: CallStatus;
};
export type NamedCallStateComputed<Prop extends string> = {
[K in Prop as `is${Capitalize<string & K>}Loading`]: Signal<boolean>;
} & {
[K in Prop as `is${Capitalize<string & K>}Loaded`]: Signal<boolean>;
} & {
[K in Prop as `${K}Error`]: Signal<string | null>;
};
export type NamedCallStateMethods<Prop extends string> = {
[K in Prop as `set${Capitalize<string & K>}Loading`]: () => void;
} & {
[K in Prop as `set${Capitalize<string & K>}Loaded`]: () => void;
} & {
[K in Prop as `set${Capitalize<string & K>}Error`]: (error?: unknown) => void;
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,17 @@ import {
withState,
} from '@ngrx/signals';

import {
CallState,
CallStateComputed,
CallStateMethods,
CallStatus,
NamedCallState,
NamedCallStateComputed,
NamedCallStateMethods,
} from './with-call-status.model';
import { getWithCallStatusKeys } from './with-call-status.util';

export type CallStatus = 'init' | 'loading' | 'loaded' | { error: unknown };

export type CallState = {
callStatus: CallStatus;
};
export type CallStateComputed = {
isLoading: Signal<boolean>;
} & {
isLoaded: Signal<boolean>;
} & {
error: Signal<string | null>;
};
export type CallStateMethods = {
setLoading: () => void;
} & {
setLoaded: () => void;
} & {
setError: (error?: unknown) => void;
};
export type NamedCallState<Prop extends string> = {
[K in Prop as `${K}CallStatus`]: CallStatus;
};
export type NamedCallStateComputed<Prop extends string> = {
[K in Prop as `is${Capitalize<string & K>}Loading`]: Signal<boolean>;
} & {
[K in Prop as `is${Capitalize<string & K>}Loaded`]: Signal<boolean>;
} & {
[K in Prop as `${K}Error`]: Signal<string | null>;
};
export type NamedCallStateMethods<Prop extends string> = {
[K in Prop as `set${Capitalize<string & K>}Loading`]: () => void;
} & {
[K in Prop as `set${Capitalize<string & K>}Loaded`]: () => void;
} & {
[K in Prop as `set${Capitalize<string & K>}Error`]: (error?: unknown) => void;
};

/**
* Generates necessary state, computed and methods for call progress status to the store
* @param config - Configuration object
Expand Down
22 changes: 22 additions & 0 deletions libs/ngrx-traits/signals/src/lib/with-calls/with-calls.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Observable } from 'rxjs';

export type Call<Params extends readonly any[] = any[], Result = any> = (
...args: Params
) => Observable<Result> | Promise<Result>;
export type CallConfig<
Params extends readonly any[] = any[],
Result = any,
PropName extends string = string,
> = {
call: Call<Params, Result>;
resultProp?: PropName;
mapPipe?: 'switchMap' | 'concatMap' | 'exhaustMap';
};
export type ExtractCallResultType<T extends Call | CallConfig> =
T extends Call<any, infer R>
? R
: T extends CallConfig<any, infer R>
? R
: never;
export type ExtractCallParams<T extends Call | CallConfig> =
T extends Call<infer P> ? P : T extends CallConfig<infer P> ? P : never;
31 changes: 7 additions & 24 deletions libs/ngrx-traits/signals/src/lib/with-calls/with-calls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import {
first,
from,
map,
Observable,
of,
pipe,
switchMap,
Expand All @@ -36,32 +35,16 @@ import {
CallStatus,
NamedCallState,
NamedCallStateComputed,
} from '../with-call-status/with-call-status';
} from '../with-call-status/with-call-status.model';
import { getWithCallStatusKeys } from '../with-call-status/with-call-status.util';
import {
Call,
CallConfig,
ExtractCallParams,
ExtractCallResultType,
} from './with-calls.model';
import { getWithCallKeys } from './with-calls.util';

type Call<Params extends readonly any[] = any[], Result = any> = (
...args: Params
) => Observable<Result> | Promise<Result>;
type CallConfig<
Params extends readonly any[] = any[],
Result = any,
PropName extends string = string,
> = {
call: Call<Params, Result>;
resultProp?: PropName;
mapPipe?: 'switchMap' | 'concatMap' | 'exhaustMap';
};

export type ExtractCallResultType<T extends Call | CallConfig> =
T extends Call<any, infer R>
? R
: T extends CallConfig<any, infer R>
? R
: never;
export type ExtractCallParams<T extends Call | CallConfig> =
T extends Call<infer P> ? P : T extends CallConfig<infer P> ? P : never;

/**
* Generates necessary state, computed and methods to track the progress of the call
* and store the result of the call
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Signal } from '@angular/core';

export type EntitiesFilterState<Filter> = { entitiesFilter: Filter };
export type NamedEntitiesFilterState<Collection extends string, Filter> = {
[K in Collection as `${K}Filter`]: Filter;
};
export type EntitiesFilterComputed = {
isEntitiesFilterChanged: Signal<boolean>;
};
export type NamedEntitiesFilterComputed<Collection extends string> = {
[K in Collection as `is${Capitalize<string & K>}FilterChanged`]: Signal<boolean>;
};
export type EntitiesFilterMethods<Filter> = {
filterEntities: (
options:
| {
filter: Filter;
debounce?: number;
patch?: false | undefined;
forceLoad?: boolean;
}
| {
filter: Partial<Filter>;
debounce?: number;
patch: true;
forceLoad?: boolean;
},
) => void;
resetEntitiesFilter: () => void;
};
export type NamedEntitiesFilterMethods<Collection extends string, Filter> = {
[K in Collection as `filter${Capitalize<string & K>}Entities`]: (
options:
| {
filter: Filter;
debounce?: number;
patch?: false | undefined;
forceLoad?: boolean;
}
| {
filter: Partial<Filter>;
debounce?: number;
patch: true;
forceLoad?: boolean;
},
) => void;
} & {
[K in Collection as `reset${Capitalize<string & K>}Filter`]: () => void;
};
Original file line number Diff line number Diff line change
Expand Up @@ -22,56 +22,14 @@ import {
debounceFilterPipe,
getWithEntitiesFilterKeys,
} from './with-entities-filter.util';

export type EntitiesFilterState<Filter> = { entitiesFilter: Filter };
export type NamedEntitiesFilterState<Collection extends string, Filter> = {
[K in Collection as `${K}Filter`]: Filter;
};

export type EntitiesFilterComputed = {
isEntitiesFilterChanged: Signal<boolean>;
};
export type NamedEntitiesFilterComputed<Collection extends string> = {
[K in Collection as `is${Capitalize<string & K>}FilterChanged`]: Signal<boolean>;
};

export type EntitiesFilterMethods<Filter> = {
filterEntities: (
options:
| {
filter: Filter;
debounce?: number;
patch?: false | undefined;
forceLoad?: boolean;
}
| {
filter: Partial<Filter>;
debounce?: number;
patch: true;
forceLoad?: boolean;
},
) => void;
resetEntitiesFilter: () => void;
};
export type NamedEntitiesFilterMethods<Collection extends string, Filter> = {
[K in Collection as `filter${Capitalize<string & K>}Entities`]: (
options:
| {
filter: Filter;
debounce?: number;
patch?: false | undefined;
forceLoad?: boolean;
}
| {
filter: Partial<Filter>;
debounce?: number;
patch: true;
forceLoad?: boolean;
},
) => void;
} & {
[K in Collection as `reset${Capitalize<string & K>}Filter`]: () => void;
};
import {
EntitiesFilterComputed,
EntitiesFilterMethods,
EntitiesFilterState,
NamedEntitiesFilterComputed,
NamedEntitiesFilterMethods,
NamedEntitiesFilterState,
} from './with-entities-local-filter.model';

/**
* Generates necessary state, computed and methods for locally filtering entities in the store,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ import type { StateSignal } from '@ngrx/signals/src/state-signal';
import { pipe, tap } from 'rxjs';

import { combineFunctions, getWithEntitiesKeys } from '../util';
import type {
import {
CallStateMethods,
NamedCallStateMethods,
} from '../with-call-status/with-call-status';
} from '../with-call-status/with-call-status.model';
import { getWithCallStatusKeys } from '../with-call-status/with-call-status.util';
import {
debounceFilterPipe,
getWithEntitiesFilterKeys,
} from './with-entities-filter.util';
import type {
import {
EntitiesFilterMethods,
EntitiesFilterState,
NamedEntitiesFilterMethods,
NamedEntitiesFilterState,
} from './with-entities-local-filter';
} from './with-entities-local-filter.model';

/**
* Generates necessary state, computed and methods for remotely filtering entities in the store,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ import {
NamedCallState,
NamedCallStateComputed,
NamedCallStateMethods,
} from '../with-call-status/with-call-status';
} from '../with-call-status/with-call-status.model';
import { getWithCallStatusKeys } from '../with-call-status/with-call-status.util';
import { EntitiesPaginationRemoteMethods } from '../with-entities-pagination/with-entities-remote-pagination';
import { EntitiesPaginationRemoteMethods } from '../with-entities-pagination/with-entities-remote-pagination.model';
import { getWithEntitiesRemotePaginationKeys } from '../with-entities-pagination/with-entities-remote-pagination.util';

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { getWithEntitiesInfinitePaginationKeys } from '../../../../../../libs/ng
import {
EntitiesPaginationInfiniteMethods,
NamedEntitiesPaginationInfiniteMethods,
} from './with-entities-remote-scroll-pagination';
} from './with-entities-remote-scroll-pagination.model';

export function getInfiniteScrollDataSource<Entity, Collection extends string>(
options:
Expand Down
Loading

0 comments on commit 4dc592b

Please sign in to comment.