Skip to content

Commit 8d275dc

Browse files
authored
fix: types (#83)
* fix: types compatibility * chore: typo for warning * chore: lint
1 parent 6e141c9 commit 8d275dc

File tree

5 files changed

+39
-19
lines changed

5 files changed

+39
-19
lines changed

src/plugins/effectsStateApis.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export default (): T.Plugin => {
3131
function useModelActionsState(name) {
3232
if (!warnedUseModelActionsState) {
3333
warnedUseModelActionsState = true;
34-
warning('`useModelActionsState` API has been detected, please use `useModelEffectsState` instead. \n\n\n https://github.com/ice-lab/icestore/blob/master/docs/upgrade-guidelines.md#usemodelactionsstate');
34+
warning('`useModelActionsState` API has been detected, please use `useModelEffectsState` instead. \n\n\n Visit https://github.com/ice-lab/icestore/blob/master/docs/upgrade-guidelines.md#usemodelactionsstate to learn about how to upgrade.');
3535
}
3636
return useModelEffectsState(name);
3737
}
@@ -41,7 +41,7 @@ export default (): T.Plugin => {
4141
return function(name: string, mapModelEffectsStateToProps?) {
4242
if (fieldSuffix === actionsSuffix && !warnedWithModelActionsState) {
4343
warnedWithModelActionsState = true;
44-
warning('`withModelActionsState` API has been detected, please use `withModelEffectsState` instead. \n\n\n https://github.com/ice-lab/icestore/blob/master/docs/upgrade-guidelines.md#withmodelactionsstate');
44+
warning('`withModelActionsState` API has been detected, please use `withModelEffectsState` instead. \n\n\n Visit https://github.com/ice-lab/icestore/blob/master/docs/upgrade-guidelines.md#withmodelactionsstate to learn about how to upgrade.');
4545
}
4646

4747
mapModelEffectsStateToProps = (mapModelEffectsStateToProps || ((effectsState) => ({ [`${name}${fieldSuffix}`]: effectsState })));

src/plugins/modelApis.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default (): T.Plugin => {
3030
function useModelActions(name: string) {
3131
if (!warnedUseModelActions) {
3232
warnedUseModelActions = true;
33-
warning('`useModelActions` API has been detected, please use `useModelDispatchers` instead. \n\n\n https://github.com/ice-lab/icestore/blob/master/docs/upgrade-guidelines.md#usemodelactions');
33+
warning('`useModelActions` API has been detected, please use `useModelDispatchers` instead. \n\n\n Visit https://github.com/ice-lab/icestore/blob/master/docs/upgrade-guidelines.md#usemodelactions to learn about how to upgrade.');
3434
}
3535
return useModelDispatchers(name);
3636
}
@@ -66,7 +66,7 @@ export default (): T.Plugin => {
6666
return function withModelDispatchers(name: string, mapModelDispatchersToProps?) {
6767
if (fieldSuffix === actionsSuffix && !warnedWithModelActions) {
6868
warnedWithModelActions = true;
69-
warning('`withModelActions` API has been detected, please use `withModelDispatchers` instead. \n\n\n https://github.com/ice-lab/icestore/blob/master/docs/upgrade-guidelines.md#withmodelactions');
69+
warning('`withModelActions` API has been detected, please use `withModelDispatchers` instead. \n\n\n Visit https://github.com/ice-lab/icestore/blob/master/docs/upgrade-guidelines.md#withmodelactions to learn about how to upgrade.');
7070
}
7171
mapModelDispatchersToProps = (mapModelDispatchersToProps || ((dispatch) => ({ [`${name}${fieldSuffix}`]: dispatch })));
7272
return (Component) => {

src/plugins/provider.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default ({ context }: ProviderConfig): T.Plugin => {
1313
const Provider = function(props: { children; initialStates? }) {
1414
const { children, initialStates } = props;
1515
if (initialStates) {
16-
warning('`initialStates` API has been detected, please use `createStore(model, { initialState })` instead. \n\n\n https://github.com/ice-lab/icestore/blob/master/docs/upgrade-guidelines.md#initialstate');
16+
warning('`initialStates` API has been detected, please use `createStore(model, { initialState })` instead. \n\n\n Visit https://github.com/ice-lab/icestore/blob/master/docs/upgrade-guidelines.md#initialstate to learn about how to upgrade.');
1717
Object.keys(initialStates).forEach(name => {
1818
const initialState = initialStates[name];
1919
if (initialState && store.dispatch[name].setState) {

src/types.ts

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ export type ExtractIModelDispatchersFromEffects<
5757
: {}
5858
: effects extends ModelEffects<any>
5959
? ExtractIModelDispatchersFromEffectsObject<effects>
60-
: {}
60+
: effects extends ConfigEffects<any> ?
61+
OldModelEffects<any> : {};
6162

6263
export type ExtractIModelDispatcherFromReducer<R> = R extends () => any
6364
? IcestoreDispatcher<void, void>
@@ -221,9 +222,7 @@ export interface ModelPluginAPI<M extends Models = Models> {
221222
}
222223

223224
export interface ProviderPluginAPI {
224-
Provider: ({ children }: {
225-
children: any;
226-
}) => JSX.Element;
225+
Provider: (props: { children: any; initialStates?: any }) => JSX.Element;
227226
}
228227

229228
export type PresetIcestore<
@@ -281,10 +280,12 @@ export interface ModelConfig<S = any, SS = S> {
281280
name?: string;
282281
state: S;
283282
baseReducer?: (state: SS, action: Action) => SS;
284-
reducers?: ModelReducers<S>;
283+
reducers?: ModelReducers<S> | ConfigReducers;
285284
effects?:
286285
| ModelEffects<any>
287-
| ((dispatch: IcestoreDispatch) => ModelEffects<any>);
286+
| ((dispatch: IcestoreDispatch) => ModelEffects<any>)
287+
| ConfigEffects;
288+
actions?: ConfigActions<S>; // @deprecated
288289
}
289290

290291
export interface PluginFactory extends Plugin {
@@ -385,9 +386,28 @@ declare global {
385386
}
386387

387388
/** @deprecated */
388-
export type ConfigPropTypeState<M extends ModelConfig> = ExtractIModelStateFromModelConfig<M>;
389-
export type ConfigPropTypeEffects<M extends ModelConfig> = ExtractIModelEffectsFromModelConfig<M>;
390-
export type ConfigPropTypeReducers<M extends ModelConfig> = ExtractIModelReducersFromModelConfig<M>;
391-
export type ModelActions<M extends ModelConfig> = ExtractIModelDispatchersFromModelConfig<M>;
392-
export type ModelEffectsState<M extends ModelConfig> = ExtractIModelEffectsStateFromModelConfig<M>;
393-
export type UseModelValue<M extends ModelConfig> = ExtractIModelFromModelConfig<M>;
389+
export type ConfigAction<S = any> = (prevState: S, payload?: any, actions?: any, globalActions?: any) => S | Promise<S>;
390+
export type ConfigEffect<S = any> = (state: S, payload?: any, actions?: any, globalActions?: any) => void | Promise<void>;
391+
export type ConfigReducer<S = any> = (state: S, payload?: any,) => S;
392+
export interface ConfigEffects<S = any> {
393+
[name: string]: ConfigEffect<S>;
394+
}
395+
export interface ConfigReducers<S = any> {
396+
[name: string]: ConfigReducer<S>;
397+
}
398+
export interface ConfigActions<S = any> {
399+
[name: string]: ConfigAction<S>;
400+
}
401+
export type Actions<A extends ConfigEffects> = {
402+
[K in keyof A]: (payload?: Parameters<A[K]>[1]) => void;
403+
}
404+
export type ConfigPropTypeState<C extends ModelConfig> = PropType<C, 'state'>;
405+
export type ConfigPropTypeActions<C extends ModelConfig> = PropType<C, 'actions'>;
406+
export type ConfigPropTypeEffects<C extends ModelConfig> = PropType<C, 'effects'>;
407+
export type ConfigPropTypeReducers<C extends ModelConfig> = PropType<C, 'reducers'>;
408+
export type ConfigMergedEffects<C extends ModelConfig> = ConfigPropTypeActions<C> & ConfigPropTypeEffects<C>;
409+
export type OldModelEffects<C extends ModelConfig> = Actions<ConfigMergedEffects<C>>;;
410+
export type ModelActions<C extends ModelConfig> = Actions<ConfigPropTypeReducers<C> & ConfigPropTypeEffects<C>>;
411+
export type ModelEffectsState<C extends ModelConfig> = ExtractIModelEffectsStateFromModelConfig<C>;
412+
export type ModelValue<C extends ModelConfig> = [ ConfigPropTypeState<C>, ModelActions<C>, ModelEffectsState<C> ];
413+
export type UseModelValue<C extends ModelConfig> = [ ConfigPropTypeState<C>, ModelActions<C> ];

src/utils/converter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export function convertEffects(originModels: any) {
1414
const model = originModels[name];
1515
const originEffects = model.effects;
1616
if (originEffects && !isFunction(originEffects)) {
17-
warning(`Model(${name}): Defining effects as objects has been detected, please use \`{ effects: () => ({ effectName: () => {} }) }\` instead. \n\n\n https://github.com/ice-lab/icestore/blob/master/docs/upgrade-guidelines.md#define-model-effects`);
17+
warning(`Model(${name}): Defining effects as objects has been detected, please use \`{ effects: () => ({ effectName: () => {} }) }\` instead. \n\n\n Visit https://github.com/ice-lab/icestore/blob/master/docs/upgrade-guidelines.md#define-model-effects to learn about how to upgrade.`);
1818
model.effects = (dispatch: any) => {
1919
const effects = {};
2020
Object.keys(originEffects).forEach(function(key) {
@@ -48,7 +48,7 @@ export function convertActions(originModels: any) {
4848
const model = originModels[name];
4949
const actions = model.actions;
5050
if (actions) {
51-
warning(`Model(${name}): The actions field has been detected, please use \`reducers\` and \`effects\` instead.`);
51+
warning(`Model(${name}): The actions field has been detected, please use \`reducers\` and \`effects\` instead. Visit https://github.com/ice-lab/icestore/blob/master/docs/upgrade-guidelines.md#define-model-actions to learn about how to upgrade.`);
5252
if (!model.reducers) {
5353
model.reducers = {};
5454
}

0 commit comments

Comments
 (0)