|
4 | 4 | IConfiguration, |
5 | 5 | MODE_SSR, |
6 | 6 | Overmind, |
| 7 | + IContext, |
7 | 8 | } from 'overmind' |
8 | 9 | import { |
9 | 10 | Ref, |
@@ -37,20 +38,20 @@ export const withOvermind = ( |
37 | 38 | }) |
38 | 39 | } |
39 | 40 |
|
40 | | -export interface StateHook<Config extends IConfiguration> { |
41 | | - (): Ref<Overmind<Config>['state']> |
42 | | - <CB extends (state: Overmind<Config>['state']) => object>( |
43 | | - cb: CB |
44 | | - ): CB extends (state: Overmind<Config>['state']) => infer O |
| 41 | +export interface StateHook<Context extends IContext<{ state: {} }>> { |
| 42 | + (): Ref<Context['state']> |
| 43 | + <CB extends (state: Context['state']) => object>(cb: CB): CB extends ( |
| 44 | + state: Context['state'] |
| 45 | + ) => infer O |
45 | 46 | ? O extends object |
46 | 47 | ? Ref<O> |
47 | 48 | : never |
48 | 49 | : never |
49 | 50 | } |
50 | 51 |
|
51 | | -export function createStateHook<Config extends IConfiguration>(): StateHook< |
52 | | - Config |
53 | | -> { |
| 52 | +export function createStateHook< |
| 53 | + Context extends IContext<{ state: {} }> |
| 54 | +>(): StateHook<Context> { |
54 | 55 | const componentId = nextComponentId++ |
55 | 56 | let componentInstanceId = 0 |
56 | 57 | return ((cb: any) => { |
@@ -138,61 +139,63 @@ export function createStateHook<Config extends IConfiguration>(): StateHook< |
138 | 139 | }) as any |
139 | 140 | } |
140 | 141 |
|
141 | | -export interface ActionsHook<Config extends IConfiguration> { |
142 | | - (): Ref<Overmind<Config>['actions']> |
143 | | - <CB extends (actions: Overmind<Config>['actions']) => object>( |
144 | | - cb: CB |
145 | | - ): CB extends (actions: Overmind<Config>['actions']) => infer O |
| 142 | +export interface ActionsHook<Context extends IContext<{ actions: {} }>> { |
| 143 | + (): Ref<Context['actions']> |
| 144 | + <CB extends (actions: Context['actions']) => object>(cb: CB): CB extends ( |
| 145 | + actions: Context['actions'] |
| 146 | + ) => infer O |
146 | 147 | ? O extends object |
147 | 148 | ? Ref<O> |
148 | 149 | : never |
149 | 150 | : never |
150 | 151 | } |
151 | 152 |
|
152 | | -export function createActionsHook<Config extends IConfiguration>(): ActionsHook< |
153 | | - Config |
154 | | -> { |
155 | | - return ((cb?: any): Overmind<Config>['actions'] => { |
| 153 | +export function createActionsHook< |
| 154 | + Context extends IContext<{ actions: {} }> |
| 155 | +>(): ActionsHook<Context> { |
| 156 | + return ((cb?: any): Context['actions'] => { |
156 | 157 | const overmindInstance = inject<any>('overmind') |
157 | 158 |
|
158 | 159 | return cb ? cb(overmindInstance.actions) : overmindInstance.actions |
159 | 160 | }) as any |
160 | 161 | } |
161 | 162 |
|
162 | | -export interface EffectsHook<Config extends IConfiguration> { |
163 | | - (): Ref<Overmind<Config>['effects']> |
164 | | - <CB extends (effects: Overmind<Config>['effects']) => object>( |
165 | | - cb: CB |
166 | | - ): CB extends (effects: Overmind<Config>['effects']) => infer O |
| 163 | +export interface EffectsHook<Context extends IContext<{ effects: {} }>> { |
| 164 | + (): Ref<Context['effects']> |
| 165 | + <CB extends (effects: Context['effects']) => object>(cb: CB): CB extends ( |
| 166 | + effects: Context['effects'] |
| 167 | + ) => infer O |
167 | 168 | ? O extends object |
168 | 169 | ? Ref<O> |
169 | 170 | : never |
170 | 171 | : never |
171 | 172 | } |
172 | 173 |
|
173 | | -export function createEffectsHook<Config extends IConfiguration>(): EffectsHook< |
174 | | - Config |
175 | | -> { |
176 | | - return ((cb?: any): Overmind<Config>['effects'] => { |
| 174 | +export function createEffectsHook< |
| 175 | + Context extends IContext<{ effects: {} }> |
| 176 | +>(): EffectsHook<Context> { |
| 177 | + return ((cb?: any): Context['effects'] => { |
177 | 178 | const overmindInstance = inject<any>('overmind') |
178 | 179 |
|
179 | 180 | return cb ? cb(overmindInstance.effects) : overmindInstance.effects |
180 | 181 | }) as any |
181 | 182 | } |
182 | 183 |
|
183 | | -export function createReactionHook<Config extends IConfiguration>() { |
184 | | - return (): Overmind<Config>['reaction'] => { |
| 184 | +export function createReactionHook<Context extends IContext<{ state: {} }>>() { |
| 185 | + return (): Context['reaction'] => { |
185 | 186 | const overmindInstance = inject<any>('overmind') |
186 | 187 |
|
187 | 188 | return overmindInstance.reaction |
188 | 189 | } |
189 | 190 | } |
190 | 191 |
|
191 | | -export function createHooks<Config extends IConfiguration>() { |
| 192 | +export function createHooks< |
| 193 | + Context extends IContext<{ state: {}; actions: {}; effects: {} }> |
| 194 | +>() { |
192 | 195 | return { |
193 | | - state: createStateHook<Config>(), |
194 | | - actions: createActionsHook<Config>(), |
195 | | - effects: createEffectsHook<Config>(), |
196 | | - reaction: createReactionHook<Config>(), |
| 196 | + state: createStateHook<Context>(), |
| 197 | + actions: createActionsHook<Context>(), |
| 198 | + effects: createEffectsHook<Context>(), |
| 199 | + reaction: createReactionHook<Context>(), |
197 | 200 | } |
198 | 201 | } |
0 commit comments