Skip to content

Commit d9baf31

Browse files
committed
refactor(overmind): simplify types by removing Context and TContext and using App
1 parent 97ee320 commit d9baf31

File tree

4 files changed

+82
-97
lines changed

4 files changed

+82
-97
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { mutate, forEach, fork, Operator, when, map, EventType } from 'overmind'
1+
import { mutate, forEach, fork, Operator, when, map } from 'overmind'
22
import {
33
Message,
44
AppMessage,

packages/node_modules/overmind/src/index.ts

Lines changed: 72 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { EventEmitter } from 'betsy'
22
import { IS_PROXY, ProxyStateTree } from 'proxy-state-tree'
3-
43
import { Derived } from './derived'
54
import { Devtools, Message, safeValue } from './Devtools'
65
import {
@@ -16,7 +15,6 @@ import {
1615
BaseApp,
1716
Configuration,
1817
TAction,
19-
TContext,
2018
TDerive,
2119
TOperator,
2220
TReaction,
@@ -35,8 +33,6 @@ type App = BaseApp & IApp
3533

3634
export type Action<Value = void> = TAction<App, Value>
3735

38-
export type Context = TContext<App>
39-
4036
export type Derive<Parent extends object, Value> = TDerive<
4137
App,
4238
ResolveState<Parent>,
@@ -45,9 +41,7 @@ export type Derive<Parent extends object, Value> = TDerive<
4541

4642
export type Reaction = TReaction<App>
4743

48-
export type OnInitialize = (
49-
context: TValueContext<TContext<App>, App['actions']>
50-
) => void
44+
export type OnInitialize = (context: TValueContext<App, App['actions']>) => void
5145

5246
const isPlainObject = require('is-plain-object')
5347
const IS_PRODUCTION = process.env.NODE_ENV === 'production'
@@ -483,46 +477,46 @@ export class Overmind<Config extends Configuration> implements BaseApp {
483477
OPERATORS
484478
needs to be in this file for typing override to work
485479
*/
486-
export type Operator<Input, Output> = TOperator<Context, Input, Output>
487-
488-
export function pipe<BaseContext, A, B, C>(
489-
aOperator: TOperator<BaseContext, A, B>
490-
): TOperator<BaseContext, A, C>
491-
492-
export function pipe<BaseContext, A, B, C, D>(
493-
aOperator: TOperator<BaseContext, A, B>,
494-
bOperator: TOperator<BaseContext, B, C>
495-
): TOperator<BaseContext, A, D>
496-
497-
export function pipe<BaseContext, A, B, C, D, E>(
498-
aOperator: TOperator<BaseContext, A, B>,
499-
bOperator: TOperator<BaseContext, B, C>,
500-
cOperator: TOperator<BaseContext, C, D>
501-
): TOperator<BaseContext, A, E>
502-
503-
export function pipe<BaseContext, A, B, C, D, E, F>(
504-
aOperator: TOperator<BaseContext, A, B>,
505-
bOperator: TOperator<BaseContext, B, C>,
506-
cOperator: TOperator<BaseContext, C, D>,
507-
dOperator: TOperator<BaseContext, D, E>
508-
): TOperator<BaseContext, A, F>
509-
510-
export function pipe<BaseContext, A, B, C, D, E, F, G>(
511-
aOperator: TOperator<BaseContext, A, B>,
512-
bOperator: TOperator<BaseContext, B, C>,
513-
cOperator: TOperator<BaseContext, C, D>,
514-
dOperator: TOperator<BaseContext, D, E>,
515-
eOperator: TOperator<BaseContext, E, F>
516-
): TOperator<BaseContext, A, G>
517-
518-
export function pipe<BaseContext, A, B, C, D, E, F, G, H>(
519-
aOperator: TOperator<BaseContext, A, B>,
520-
bOperator: TOperator<BaseContext, B, C>,
521-
cOperator: TOperator<BaseContext, C, D>,
522-
dOperator: TOperator<BaseContext, D, E>,
523-
eOperator: TOperator<BaseContext, E, F>,
524-
fOperator: TOperator<BaseContext, F, G>
525-
): TOperator<BaseContext, A, H>
480+
export type Operator<Input, Output> = TOperator<App, Input, Output>
481+
482+
export function pipe<App extends BaseApp, A, B, C>(
483+
aOperator: TOperator<App, A, B>
484+
): TOperator<App, A, C>
485+
486+
export function pipe<App extends BaseApp, A, B, C, D>(
487+
aOperator: TOperator<App, A, B>,
488+
bOperator: TOperator<App, B, C>
489+
): TOperator<App, A, D>
490+
491+
export function pipe<App extends BaseApp, A, B, C, D, E>(
492+
aOperator: TOperator<App, A, B>,
493+
bOperator: TOperator<App, B, C>,
494+
cOperator: TOperator<App, C, D>
495+
): TOperator<App, A, E>
496+
497+
export function pipe<App extends BaseApp, A, B, C, D, E, F>(
498+
aOperator: TOperator<App, A, B>,
499+
bOperator: TOperator<App, B, C>,
500+
cOperator: TOperator<App, C, D>,
501+
dOperator: TOperator<App, D, E>
502+
): TOperator<App, A, F>
503+
504+
export function pipe<App extends BaseApp, A, B, C, D, E, F, G>(
505+
aOperator: TOperator<App, A, B>,
506+
bOperator: TOperator<App, B, C>,
507+
cOperator: TOperator<App, C, D>,
508+
dOperator: TOperator<App, D, E>,
509+
eOperator: TOperator<App, E, F>
510+
): TOperator<App, A, G>
511+
512+
export function pipe<App extends BaseApp, A, B, C, D, E, F, G, H>(
513+
aOperator: TOperator<App, A, B>,
514+
bOperator: TOperator<App, B, C>,
515+
cOperator: TOperator<App, C, D>,
516+
dOperator: TOperator<App, D, E>,
517+
eOperator: TOperator<App, E, F>,
518+
fOperator: TOperator<App, F, G>
519+
): TOperator<App, A, H>
526520

527521
export function pipe(...operators) {
528522
const instance = (err, context, next, final = next) => {
@@ -634,9 +628,9 @@ function createNextPath(next) {
634628
}
635629
}
636630

637-
export function map<Input, Output, BaseContext = Context>(
638-
operation: (input: TValueContext<BaseContext, Input>) => Output
639-
): TOperator<BaseContext, Input, Output extends Promise<infer U> ? U : Output> {
631+
export function map<Input, Output, TheApp extends BaseApp = App>(
632+
operation: (input: TValueContext<TheApp, Input>) => Output
633+
): TOperator<TheApp, Input, Output extends Promise<infer U> ? U : Output> {
640634
const instance = (err, context, next) => {
641635
if (err) next(err)
642636
else {
@@ -652,9 +646,9 @@ export function map<Input, Output, BaseContext = Context>(
652646
return instance
653647
}
654648

655-
export function run<Input, BaseContext = Context>(
656-
operation: (input: TValueContext<BaseContext, Input>) => any
657-
): TOperator<BaseContext, Input, Input> {
649+
export function run<Input, TheApp extends BaseApp = App>(
650+
operation: (input: TValueContext<TheApp, Input>) => any
651+
): TOperator<TheApp, Input, Input> {
658652
const instance = (err, context, next) => {
659653
if (err) next(err)
660654
else {
@@ -675,9 +669,9 @@ export function run<Input, BaseContext = Context>(
675669
return instance
676670
}
677671

678-
export function forEach<Input extends any[], BaseContext = Context>(
679-
forEachItemOperator: TOperator<BaseContext, Input[0], any>
680-
): TOperator<BaseContext, Input, Input> {
672+
export function forEach<Input extends any[], TheApp extends BaseApp = App>(
673+
forEachItemOperator: TOperator<TheApp, Input[0], any>
674+
): TOperator<TheApp, Input, Input> {
681675
const instance = (err, context, next) => {
682676
if (err) next(err)
683677
else {
@@ -718,9 +712,9 @@ export function forEach<Input extends any[], BaseContext = Context>(
718712
return instance
719713
}
720714

721-
export function parallel<Input, BaseContext = Context>(
722-
operators: TOperator<BaseContext, Input, any>[]
723-
): TOperator<BaseContext, Input, Input> {
715+
export function parallel<Input, TheApp extends BaseApp = App>(
716+
operators: TOperator<TheApp, Input, any>[]
717+
): TOperator<TheApp, Input, Input> {
724718
const instance = (err, context, next) => {
725719
if (err) next(err)
726720
else {
@@ -760,9 +754,9 @@ export function parallel<Input, BaseContext = Context>(
760754
return instance
761755
}
762756

763-
export function filter<Input, BaseContext = Context>(
764-
operation: (input: TValueContext<BaseContext, Input>) => boolean
765-
): TOperator<BaseContext, Input, Input> {
757+
export function filter<Input, TheApp extends BaseApp = App>(
758+
operation: (input: TValueContext<TheApp, Input>) => boolean
759+
): TOperator<TheApp, Input, Input> {
766760
const instance = (err, context, next, final) => {
767761
if (err) next(err)
768762
else {
@@ -783,9 +777,9 @@ export function filter<Input, BaseContext = Context>(
783777
return instance
784778
}
785779

786-
export function mutate<Input, BaseContext = Context>(
787-
operation: (input: TValueContext<BaseContext, Input>) => void
788-
): Operator<Input, Input> {
780+
export function mutate<Input, TheApp extends BaseApp = App>(
781+
operation: (input: TValueContext<TheApp, Input>) => void
782+
): TOperator<TheApp, Input, Input> {
789783
const instance = (err, context, next) => {
790784
if (err) next(err)
791785
else {
@@ -809,12 +803,12 @@ export function mutate<Input, BaseContext = Context>(
809803

810804
export function fork<
811805
Input,
812-
Paths extends { [key: string]: Operator<Input, any> },
813-
BaseContext = Context
806+
Paths extends { [key: string]: TOperator<TheApp, Input, any> },
807+
TheApp extends BaseApp = App
814808
>(
815-
operation: (input: TValueContext<BaseContext, Input>) => keyof Paths,
809+
operation: (input: TValueContext<TheApp, Input>) => keyof Paths,
816810
paths: Paths
817-
): TOperator<BaseContext, Input, Input> {
811+
): TOperator<TheApp, Input, Input> {
818812
const instance = (err, context, next) => {
819813
if (err) next(err)
820814
else {
@@ -840,13 +834,13 @@ export function fork<
840834
return instance
841835
}
842836

843-
export function when<Input, OutputA, OutputB, BaseContext = Context>(
844-
operation: (input: TValueContext<BaseContext, Input>) => boolean,
837+
export function when<Input, OutputA, OutputB, TheApp extends BaseApp = App>(
838+
operation: (input: TValueContext<TheApp, Input>) => boolean,
845839
paths: {
846-
true: TOperator<BaseContext, Input, OutputA>
847-
false: TOperator<BaseContext, Input, OutputB>
840+
true: TOperator<TheApp, Input, OutputA>
841+
false: TOperator<TheApp, Input, OutputB>
848842
}
849-
): TOperator<BaseContext, Input, OutputA | OutputB> {
843+
): TOperator<TheApp, Input, OutputA | OutputB> {
850844
const instance = (err, context, next) => {
851845
if (err) next(err)
852846
else {
@@ -877,9 +871,9 @@ export function when<Input, OutputA, OutputB, BaseContext = Context>(
877871
return instance
878872
}
879873

880-
export function wait<Input, BaseContext = Context>(
874+
export function wait<Input, TheApp extends BaseApp = App>(
881875
ms: number
882-
): TOperator<BaseContext, Input, Input> {
876+
): TOperator<TheApp, Input, Input> {
883877
const instance = (err, context, next) => {
884878
if (err) next(err)
885879
else {
@@ -893,9 +887,9 @@ export function wait<Input, BaseContext = Context>(
893887
return instance
894888
}
895889

896-
export function debounce<Input, BaseContext = Context>(
890+
export function debounce<Input, TheApp extends BaseApp = App>(
897891
ms: number
898-
): TOperator<BaseContext, Input, Input> {
892+
): TOperator<TheApp, Input, Input> {
899893
let timeout
900894
let previousFinal
901895
const instance = (err, context, next, final) => {

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

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

33
describe('Reaction', () => {
44
test('should instantiate app with reactions', () => {
@@ -34,7 +34,7 @@ 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 hasRun = run<void, TContext<IApp>>(() => {
37+
const hasRun = run<void, IApp>(() => {
3838
hasRunReaction = true
3939
})
4040
const react: Reaction = (reaction) => reaction((state) => state.foo, hasRun)

packages/node_modules/overmind/src/types.ts

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { ResolveActions, ResolveState, TBaseContext } from './internalTypes'
2-
import { Operator } from './'
32

43
/** ===== PUBLIC API
54
*/
@@ -27,27 +26,19 @@ export interface TApp<Config extends Configuration> {
2726
effects: Config['effects'] & {}
2827
}
2928

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

3933
export type TAction<App extends BaseApp, Value> = (
40-
context: TValueContext<TContext<App>, Value>
34+
context: TValueContext<App, Value>
4135
) => any
4236

43-
export type TOperator<OperatorContext extends TContext<any>, Input, Output> = (
37+
export type TOperator<App extends BaseApp, Input, Output> = (
4438
err: Error | null,
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
39+
val: TValueContext<App, Input>,
40+
next: (err: Error | null, val?: TValueContext<App, Output>) => void,
41+
final?: (err, Error, val?: TValueContext<App, Output>) => void
5142
) => void
5243

5344
export type TDerive<App extends BaseApp, Parent extends object, Value> = (
@@ -58,6 +49,6 @@ export type TDerive<App extends BaseApp, Parent extends object, Value> = (
5849
export type TReaction<App extends BaseApp> = (
5950
reaction: (
6051
getState: (state: App['state']) => any,
61-
action: TAction<App, void> | TOperator<TContext<App>, void, any>
52+
action: TAction<App, void> | TOperator<App, void, any>
6253
) => any
6354
) => any

0 commit comments

Comments
 (0)