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 authored and gabrielguerrero committed Apr 17, 2024
1 parent b67f00c commit db372bd
Show file tree
Hide file tree
Showing 26 changed files with 573 additions and 503 deletions.
10 changes: 9 additions & 1 deletion libs/ngrx-traits/signals/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
export * from './util';
export * from './with-call-status/with-call-status';
export * from './with-call-status/with-call-status.model';
export * from './with-entities-filter/with-entities-local-filter';
export * from './with-entities-filter/with-entities-local-filter.model';
export * from './with-entities-filter/with-entities-remote-filter';
export * from './with-entities-pagination/with-entities-local-pagination';
export * from './with-entities-pagination/with-entities-local-pagination.model';
export * from './with-entities-pagination/with-entities-remote-pagination';
export * from './with-entities-pagination/with-entities-remote-pagination.model';
export * from './with-entities-pagination/with-entities-remote-scroll-pagination';
export * from './with-entities-pagination/with-entities-remote-scroll-pagination.model';
export * from './with-entities-pagination/signal-infinite-datasource';
export {
Sort,
SortDirection,
} from './with-entities-sort/with-entities-sort.utils';
export * from './with-entities-sort/with-entities-local-sort';
export * from './with-entities-sort/with-entities-local-sort.model';
export * from './with-entities-sort/with-entities-remote-sort';
export * from './with-entities-selection/with-entities-single-selection';
export * from './with-entities-selection/with-entities-single-selection.model';
export * from './with-entities-selection/with-entities-multi-selection';
export * from './with-entities-selection/with-entities-multi-selection.model';
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-calls/with-calls.model';
export * from './with-sync-to-web-storage/with-sync-to-web-storage';
2 changes: 0 additions & 2 deletions libs/ngrx-traits/signals/src/lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ export function capitalize(name: string) {
return name.charAt(0).toUpperCase() + name.slice(1);
}

export type Prettify<T> = { [K in keyof T]: T[K] } & {};

export function getWithEntitiesKeys(config?: { collection?: string }) {
const collection = config?.collection;
return {
Expand Down
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 CallStatusState = {
callStatus: CallStatus;
};
export type CallStatusComputed = {
isLoading: Signal<boolean>;
} & {
isLoaded: Signal<boolean>;
} & {
error: Signal<string | null>;
};
export type CallStatusMethods = {
setLoading: () => void;
} & {
setLoaded: () => void;
} & {
setError: (error?: unknown) => void;
};
export type NamedCallStatusState<Prop extends string> = {
[K in Prop as `${K}CallStatus`]: CallStatus;
};
export type NamedCallStatusComputed<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 NamedCallStatusMethods<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 {
CallStatus,
CallStatusComputed,
CallStatusMethods,
CallStatusState,
NamedCallStatusComputed,
NamedCallStatusMethods,
NamedCallStatusState,
} 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 Expand Up @@ -80,9 +52,9 @@ export function withCallStatus(config?: {
}): SignalStoreFeature<
{ state: {}; signals: {}; methods: {} },
{
state: CallState;
signals: CallStateComputed;
methods: CallStateMethods;
state: CallStatusState;
signals: CallStatusComputed;
methods: CallStatusMethods;
}
>;

Expand Down Expand Up @@ -127,9 +99,9 @@ export function withCallStatus<Prop extends string>(
): SignalStoreFeature<
{ state: {}; signals: {}; methods: {} },
{
state: NamedCallState<Prop>;
signals: NamedCallStateComputed<Prop>;
methods: NamedCallStateMethods<Prop>;
state: NamedCallStatusState<Prop>;
signals: NamedCallStatusComputed<Prop>;
methods: NamedCallStatusMethods<Prop>;
}
>;
export function withCallStatus<Prop extends string>({
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;
39 changes: 11 additions & 28 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,42 +26,25 @@ import {
first,
from,
map,
Observable,
of,
pipe,
switchMap,
} from 'rxjs';

import {
CallStatus,
NamedCallState,
NamedCallStateComputed,
} from '../with-call-status/with-call-status';
NamedCallStatusComputed,
NamedCallStatusState,
} 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 Expand Up @@ -110,14 +93,14 @@ export function withCalls<
): SignalStoreFeature<
Input,
Input & {
state: NamedCallState<keyof Calls & string> & {
state: NamedCallStatusState<keyof Calls & string> & {
[K in keyof Calls as Calls[K] extends CallConfig
? Calls[K]['resultProp'] extends string
? Calls[K]['resultProp']
: `${K & string}Result`
: `${K & string}Result`]: ExtractCallResultType<Calls[K]>;
};
signals: NamedCallStateComputed<keyof Calls & string>;
signals: NamedCallStatusComputed<keyof Calls & string>;
methods: {
[K in keyof Calls]: (...arg: ExtractCallParams<Calls[K]>) => void;
};
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
Loading

0 comments on commit db372bd

Please sign in to comment.