Skip to content

Commit ec02f46

Browse files
Merge pull request cerebral#142 from cerebral/typesRefactor
refactor(overmind): refactor context to be static, allow operators to…
2 parents fdc4b9a + 1198b92 commit ec02f46

File tree

5 files changed

+105
-87
lines changed

5 files changed

+105
-87
lines changed

packages/node_modules/overmind-devtools/src/app/actions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Action, pipe, OnInitialize, Operator } from 'overmind'
1+
import { Action, pipe, OnInitialize, Operator, map } from 'overmind'
22
import {
33
Message,
44
Tab,

packages/node_modules/overmind/src/index.ts

Lines changed: 77 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
TDerive,
2121
TOperator,
2222
TReaction,
23+
TValueContext,
2324
} from './types'
2425

2526
export * from './types'
@@ -34,13 +35,15 @@ type App = BaseApp & IApp
3435

3536
export type Action<Value = void> = TAction<App, Value>
3637

37-
export type Context<Value> = TContext<App, Value>
38+
export type Context = TContext<App>
3839

3940
export type Derive<Value, Parent = any> = TDerive<App, Value, Parent>
4041

4142
export type Reaction = TReaction<App>
4243

43-
export type OnInitialize = (context: Context<App['actions']>) => void
44+
export type OnInitialize = (
45+
context: TValueContext<TContext<App>, App['actions']>
46+
) => void
4447

4548
const isPlainObject = require('is-plain-object')
4649
const IS_PRODUCTION = process.env.NODE_ENV === 'production'
@@ -467,44 +470,46 @@ export class Overmind<Config extends Configuration> implements BaseApp {
467470
OPERATORS
468471
needs to be in this file for typing override to work
469472
*/
470-
export type Operator<C, RC> = TOperator<Context<C>, Context<RC>>
471-
472-
export function pipe<A, B, C>(aOperator: Operator<A, B>): Operator<A, C>
473-
474-
export function pipe<A, B, C, D>(
475-
aOperator: Operator<A, B>,
476-
bOperator: Operator<B, C>
477-
): Operator<A, D>
478-
479-
export function pipe<A, B, C, D, E>(
480-
aOperator: Operator<A, B>,
481-
bOperator: Operator<B, C>,
482-
cOperator: Operator<C, D>
483-
): Operator<A, E>
484-
485-
export function pipe<A, B, C, D, E, F>(
486-
aOperator: Operator<A, B>,
487-
bOperator: Operator<B, C>,
488-
cOperator: Operator<C, D>,
489-
dOperator: Operator<D, E>
490-
): Operator<A, F>
491-
492-
export function pipe<A, B, C, D, E, F, G>(
493-
aOperator: Operator<A, B>,
494-
bOperator: Operator<B, C>,
495-
cOperator: Operator<C, D>,
496-
dOperator: Operator<D, E>,
497-
eOperator: Operator<E, F>
498-
): Operator<A, G>
499-
500-
export function pipe<A, B, C, D, E, F, G, H>(
501-
aOperator: Operator<A, B>,
502-
bOperator: Operator<B, C>,
503-
cOperator: Operator<C, D>,
504-
dOperator: Operator<D, E>,
505-
eOperator: Operator<E, F>,
506-
fOperator: Operator<F, G>
507-
): Operator<A, H>
473+
export type Operator<Input, Output> = TOperator<Input, Output, Context>
474+
475+
export function pipe<BaseContext, A, B, C>(
476+
aOperator: TOperator<A, B, BaseContext>
477+
): TOperator<A, C, BaseContext>
478+
479+
export function pipe<BaseContext, A, B, C, D>(
480+
aOperator: TOperator<A, B, BaseContext>,
481+
bOperator: TOperator<B, C, BaseContext>
482+
): TOperator<A, D, BaseContext>
483+
484+
export function pipe<BaseContext, A, B, C, D, E>(
485+
aOperator: TOperator<A, B, BaseContext>,
486+
bOperator: TOperator<B, C, BaseContext>,
487+
cOperator: TOperator<C, D, BaseContext>
488+
): TOperator<A, E, BaseContext>
489+
490+
export function pipe<BaseContext, A, B, C, D, E, F>(
491+
aOperator: TOperator<A, B, BaseContext>,
492+
bOperator: TOperator<B, C, BaseContext>,
493+
cOperator: TOperator<C, D, BaseContext>,
494+
dOperator: TOperator<D, E, BaseContext>
495+
): TOperator<A, F, BaseContext>
496+
497+
export function pipe<BaseContext, A, B, C, D, E, F, G>(
498+
aOperator: TOperator<A, B, BaseContext>,
499+
bOperator: TOperator<B, C, BaseContext>,
500+
cOperator: TOperator<C, D, BaseContext>,
501+
dOperator: TOperator<D, E, BaseContext>,
502+
eOperator: TOperator<E, F, BaseContext>
503+
): TOperator<A, G, BaseContext>
504+
505+
export function pipe<BaseContext, A, B, C, D, E, F, G, H>(
506+
aOperator: TOperator<A, B, BaseContext>,
507+
bOperator: TOperator<B, C, BaseContext>,
508+
cOperator: TOperator<C, D, BaseContext>,
509+
dOperator: TOperator<D, E, BaseContext>,
510+
eOperator: TOperator<E, F, BaseContext>,
511+
fOperator: TOperator<F, G, BaseContext>
512+
): TOperator<A, H, BaseContext>
508513

509514
export function pipe(...operators) {
510515
const instance = (err, context, next, final = next) => {
@@ -612,9 +617,9 @@ function createNextPath(next) {
612617
}
613618
}
614619

615-
export function map<Input, Output>(
616-
operation: (input: Context<Input>) => Output
617-
): Operator<Input, Output extends Promise<infer U> ? U : Output> {
620+
export function map<Input, Output, BaseContext = Context>(
621+
operation: (input: TValueContext<BaseContext, Input>) => Output
622+
): TOperator<Input, Output extends Promise<infer U> ? U : Output, BaseContext> {
618623
const instance = (err, context, next) => {
619624
if (err) next(err)
620625
else {
@@ -630,9 +635,9 @@ export function map<Input, Output>(
630635
return instance
631636
}
632637

633-
export function run<Input>(
634-
operation: (input: Context<Input>) => void
635-
): Operator<Input, Input> {
638+
export function run<Input, BaseContext = Context>(
639+
operation: (input: TValueContext<BaseContext, Input>) => void
640+
): TOperator<Input, Input, BaseContext> {
636641
const instance = (err, context, next) => {
637642
if (err) next(err)
638643
else {
@@ -648,9 +653,9 @@ export function run<Input>(
648653
return instance
649654
}
650655

651-
export function forEach<Input extends any[]>(
652-
forEachItemOperator: Operator<Input[0], any>
653-
): Operator<Input, Input> {
656+
export function forEach<Input extends any[], BaseContext = Context>(
657+
forEachItemOperator: TOperator<Input[0], any, BaseContext>
658+
): TOperator<Input, Input, BaseContext> {
654659
const instance = (err, context, next) => {
655660
if (err) next(err)
656661
else {
@@ -691,9 +696,9 @@ export function forEach<Input extends any[]>(
691696
return instance
692697
}
693698

694-
export function parallel<Input>(
695-
operators: Operator<Input, any>[]
696-
): Operator<Input, Input> {
699+
export function parallel<Input, BaseContext = Context>(
700+
operators: TOperator<Input, any, BaseContext>[]
701+
): TOperator<Input, Input, BaseContext> {
697702
const instance = (err, context, next) => {
698703
if (err) next(err)
699704
else {
@@ -733,9 +738,9 @@ export function parallel<Input>(
733738
return instance
734739
}
735740

736-
export function filter<Input>(
737-
operation: (input: Context<Input>) => boolean
738-
): Operator<Input, Input> {
741+
export function filter<Input, BaseContext = Context>(
742+
operation: (input: TValueContext<BaseContext, Input>) => boolean
743+
): TOperator<Input, Input, BaseContext> {
739744
const instance = (err, context, next, final) => {
740745
if (err) next(err)
741746
else {
@@ -756,8 +761,8 @@ export function filter<Input>(
756761
return instance
757762
}
758763

759-
export function mutate<Input>(
760-
operation: (input: Context<Input>) => void
764+
export function mutate<Input, BaseContext = Context>(
765+
operation: (input: TValueContext<BaseContext, Input>) => void
761766
): Operator<Input, Input> {
762767
const instance = (err, context, next) => {
763768
if (err) next(err)
@@ -782,11 +787,12 @@ export function mutate<Input>(
782787

783788
export function fork<
784789
Input,
785-
Paths extends { [key: string]: Operator<Input, any> }
790+
Paths extends { [key: string]: Operator<Input, any> },
791+
BaseContext = Context
786792
>(
787-
operation: (input: Context<Input>) => keyof Paths,
793+
operation: (input: TValueContext<BaseContext, Input>) => keyof Paths,
788794
paths: Paths
789-
): Operator<Input, Input> {
795+
): TOperator<Input, Input, BaseContext> {
790796
const instance = (err, context, next) => {
791797
if (err) next(err)
792798
else {
@@ -812,13 +818,13 @@ export function fork<
812818
return instance
813819
}
814820

815-
export function when<Input, OutputA, OutputB>(
816-
operation: (input: Context<Input>) => boolean,
821+
export function when<Input, OutputA, OutputB, BaseContext = Context>(
822+
operation: (input: TValueContext<BaseContext, Input>) => boolean,
817823
paths: {
818-
true: Operator<Input, OutputA>
819-
false: Operator<Input, OutputB>
824+
true: TOperator<Input, OutputA, BaseContext>
825+
false: TOperator<Input, OutputB, BaseContext>
820826
}
821-
): Operator<Input, OutputA | OutputB> {
827+
): TOperator<Input, OutputA | OutputB, BaseContext> {
822828
const instance = (err, context, next) => {
823829
if (err) next(err)
824830
else {
@@ -849,7 +855,9 @@ export function when<Input, OutputA, OutputB>(
849855
return instance
850856
}
851857

852-
export function wait<Input>(ms: number): Operator<Input, Input> {
858+
export function wait<Input, BaseContext = Context>(
859+
ms: number
860+
): TOperator<Input, Input, BaseContext> {
853861
const instance = (err, context, next) => {
854862
if (err) next(err)
855863
else {
@@ -863,7 +871,9 @@ export function wait<Input>(ms: number): Operator<Input, Input> {
863871
return instance
864872
}
865873

866-
export function debounce<Input>(ms: number): Operator<Input, Input> {
874+
export function debounce<Input, BaseContext = Context>(
875+
ms: number
876+
): TOperator<Input, Input, BaseContext> {
867877
let timeout
868878
let previousFinal
869879
const instance = (err, context, next, final) => {

packages/node_modules/overmind/src/internalTypes.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,10 @@ type TOperationValue<T> = T extends (
139139

140140
type NestedActions =
141141
| {
142-
[key: string]: TAction<any, any> | TOperator<any, any> | NestedActions
142+
[key: string]:
143+
| TAction<any, any>
144+
| TOperator<any, any, any>
145+
| NestedActions
143146
}
144147
| undefined
145148

@@ -148,7 +151,7 @@ export type ResolveActions<Actions extends NestedActions> = {
148151
? [TActionValue<Actions[T]>] extends [void]
149152
? () => Promise<ReturnType<Actions[T]>>
150153
: (value: TActionValue<Actions[T]>) => Promise<ReturnType<Actions[T]>>
151-
: Actions[T] extends TOperator<any, any>
154+
: Actions[T] extends TOperator<any, any, any>
152155
? [TOperationValue<Actions[T]>] extends [void]
153156
? () => Promise<ReturnType<Actions[T]>>
154157
: (value: TOperationValue<Actions[T]>) => Promise<ReturnType<Actions[T]>>

packages/node_modules/overmind/src/reaction.test.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Overmind, TAction, TApp, TReaction } from './'
1+
import { Overmind, TAction, TApp, TReaction, run, TContext } from './'
22

33
describe('Reaction', () => {
44
test('should instantiate app with reactions', () => {
@@ -34,13 +34,10 @@ describe('Reaction', () => {
3434
test('should react to nested changes', () => {
3535
let hasRunReaction = false
3636
const foo: Action = ({ state }) => (state.foo[0].completed = true)
37-
const react: Reaction = (reaction) =>
38-
reaction(
39-
(state) => state.foo,
40-
() => {
41-
hasRunReaction = true
42-
}
43-
)
37+
const hasRun = run<void, TContext<IApp>>(() => {
38+
hasRunReaction = true
39+
})
40+
const react: Reaction = (reaction) => reaction((state) => state.foo, hasRun)
4441
const config = {
4542
state: {
4643
foo: [

packages/node_modules/overmind/src/types.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,37 @@ export interface TApp<Config extends Configuration> {
2727
effects: Config['effects'] & {}
2828
}
2929

30-
export type TContext<App extends BaseApp, Value> = TBaseContext<App> & {
30+
export type TContext<App extends BaseApp> = TBaseContext<App>
31+
32+
export type TValueContext<
33+
BaseContext extends TContext<any>,
34+
Value
35+
> = BaseContext & {
3136
value: Value
3237
}
3338

3439
export type TAction<App extends BaseApp, Value> = (
35-
context: TContext<App, Value>
40+
context: TValueContext<TContext<App>, Value>
3641
) => any
3742

38-
export type TOperator<C, RC> = (
43+
export type TOperator<Input, Output, OperatorContext extends TContext<any>> = (
3944
err: Error | null,
40-
val: C,
41-
next: (err: Error | null, val?: RC) => void,
42-
final?: (err, Error, val?: RC) => void
45+
val: TValueContext<OperatorContext, Input>,
46+
next: (
47+
err: Error | null,
48+
val?: TValueContext<OperatorContext, Output>
49+
) => void,
50+
final?: (err, Error, val?: TValueContext<OperatorContext, Output>) => void
4351
) => void
4452

4553
export type TDerive<App extends BaseApp, Value, Parent = any> = (
46-
state: App['state'],
47-
parent: Parent
54+
parent: Parent,
55+
state: App['state']
4856
) => Value
4957

5058
export type TReaction<App extends BaseApp> = (
5159
reaction: (
5260
getState: (state: App['state']) => any,
53-
action: TAction<App, void>
61+
action: TAction<App, void> | TOperator<void, any, TContext<App>>
5462
) => any
5563
) => any

0 commit comments

Comments
 (0)