Skip to content

Commit

Permalink
bugfix: Allow async effects for effectOn (typescript issue) (#864)
Browse files Browse the repository at this point in the history
The docs states that async effects should be allowed in an `effectOn`.

This works in JS, but not in TS - due to the type definition.

Updates the type defs to allow for async effects.
  • Loading branch information
jmyrland authored Sep 18, 2023
1 parent 406f0c7 commit 827f1df
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
8 changes: 5 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,7 @@ export function effectOn<
actions: Actions<Model>,
change: Change<Resolvers>,
helpers: Helpers<Model, StoreModel, Injections>,
) => undefined | void | Dispose,
) => undefined | void | Dispose | Promise<Dispose>,
): EffectOn<Model, StoreModel, Injections>;

// #endregion
Expand Down Expand Up @@ -1042,8 +1042,10 @@ export interface PersistConfig<Model extends object> {
mergeStrategy?: 'mergeDeep' | 'mergeShallow' | 'overwrite';
migrations?: {
migrationVersion: number;
[key: number]: (state: Partial<Model & { [key: string | number]: any }>) => void;
}
[key: number]: (
state: Partial<Model & { [key: string | number]: any }>,
) => void;
};
storage?: 'localStorage' | 'sessionStorage' | PersistStorage;
transformers?: Array<Transformer>;
}
Expand Down
8 changes: 8 additions & 0 deletions tests/typescript/effect-on.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface TodosModel {
foo: string;
setFoo: Action<TodosModel, string>;
onStateChanged: EffectOn<TodosModel, StoreModel, Injections>;
onStateChangedAsync: EffectOn<TodosModel, StoreModel, Injections>;
}

interface StoreModel {
Expand Down Expand Up @@ -52,4 +53,11 @@ const todosModel: TodosModel = {
return () => console.log('dispose');
},
),
onStateChangedAsync: effectOn(
[(state) => state.items],
// Should not generate error for async effect
async (actions, change, helpers) => {
return () => console.log('dispose');
},
),
};

0 comments on commit 827f1df

Please sign in to comment.