Skip to content

Commit 7fd4272

Browse files
fix(overmind): remove Derive and ResolveState
BREAKING CHANGE: Derive type no longer exists
1 parent 594b394 commit 7fd4272

File tree

5 files changed

+42
-69
lines changed

5 files changed

+42
-69
lines changed

packages/node_modules/overmind/src/config/statechart.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import {
22
IConfig,
33
IConfiguration,
4-
IDerive,
54
filter,
65
map,
76
mutate,
87
pipe,
8+
derived
99
} from '../'
1010

1111
const ACTIONS = 'ACTIONS'
@@ -235,7 +235,7 @@ export function statechart<C extends IConfiguration, Charts extends Statecharts
235235
state: C['state'] & {
236236
states: Array<(string | number)[]>
237237
actions: { [N in keyof C['actions']]: boolean }
238-
matches: IDerive<any, any, (match: Match<Charts>) => boolean>
238+
matches: (match: Match<Charts>) => boolean
239239
}
240240
actions: C['actions']
241241
effects: C['effects']
@@ -309,7 +309,7 @@ export function statechart<C extends IConfiguration, Charts extends Statecharts
309309
state: Object.assign(state, {
310310
states: getInitialStates(charts),
311311
actions: ((state) => getCanTransitionActions(actions, charts, state)) as any,
312-
matches: (state) => (match) => {
312+
matches: derived((state: any) => (match) => {
313313
const matchPaths = getMatchPaths(match)
314314
const statesWithoutRootChartIndicator = state.states.map((statePath) => statePath.filter((path) => path !== CHART))
315315

@@ -336,7 +336,7 @@ export function statechart<C extends IConfiguration, Charts extends Statecharts
336336
}
337337

338338
return true
339-
},
339+
}),
340340
}),
341341
actions: Object.keys(actions).reduce((aggr, key) => {
342342
aggr[key] = pipe(

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

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { PROXY_TREE } from 'proxy-state-tree'
22

3-
import { EventType, IAction, IDerive, IState, Overmind, derived } from './'
3+
import { EventType, IAction, IState, Overmind, derived } from './'
44

55
type State = {
66
foo: string
@@ -21,11 +21,6 @@ describe('Derived', () => {
2121
state,
2222
}
2323

24-
type Config = typeof config
25-
26-
interface Derive<Parent extends IState, Value>
27-
extends IDerive<Config, Parent, Value> {}
28-
2924
const app = new Overmind(config)
3025

3126
expect(app.state.upperFoo).toEqual('BAR')
@@ -118,8 +113,6 @@ describe('Derived', () => {
118113
actions: typeof config.actions
119114
}
120115
interface Action<Input = void> extends IAction<Config, Input> {}
121-
interface Derive<Parent extends IState, Value>
122-
extends IDerive<Config, Parent, Value> {}
123116

124117
const app = new Overmind(config)
125118
const trackStateTree = app.getTrackStateTree()
@@ -253,8 +246,7 @@ describe('Derived', () => {
253246
}
254247
type Config = typeof config
255248
interface Action<Input = void> extends IAction<Config, Input> {}
256-
interface Derive<Parent extends IState, Value>
257-
extends IDerive<Config, Parent, Value> {}
249+
258250

259251
const app = new Overmind(config)
260252

@@ -267,22 +259,26 @@ describe('Derived', () => {
267259
expect(state.upperFoo).toBe('BAR2')
268260
}
269261

270-
const derived: Derive<State, string> = (_, parent) =>
271-
parent.foo.toUpperCase()
262+
type State = {
263+
foo: string
264+
upperFoo: string
265+
}
266+
267+
const state: State = {
268+
foo: 'bar',
269+
upperFoo: derived((_, parent: State) => parent.foo.toUpperCase()),
270+
}
271+
272272

273273
const config = {
274-
state: {
275-
foo: 'bar',
276-
upperFoo: derived,
277-
},
274+
state,
278275
actions: {
279276
changeFoo,
280277
},
281278
}
282279
type Config = typeof config
283280
interface Action<Input = void> extends IAction<Config, Input> {}
284-
interface Derive<Parent extends IState, Value>
285-
extends IDerive<Config, Parent, Value> {}
281+
286282

287283
const app = new Overmind(config)
288284

@@ -292,29 +288,33 @@ describe('Derived', () => {
292288
expect.assertions(4)
293289
let runCount = 0
294290
const changeFoo: Action = ({ state }) => {
295-
state.foo = 'bar2'
296-
expect(state.upperFoo).toBe('BAR2')
291+
state.nestedDerived.foo = 'bar2'
292+
expect(state.nestedDerived.upperFoo).toBe('BAR2')
297293
}
298294

299-
const derived: Derive<State, string> = (parent) => parent.foo.toUpperCase()
295+
type State = {
296+
nestedDerived: {
297+
foo: string
298+
upperFoo: string
299+
}
300+
}
300301

301-
const nestedDerived: Derive<State, string> = (parent) =>
302-
parent.upperFoo + '!!!'
302+
const state: State = {
303+
nestedDerived: {
304+
foo: 'bar',
305+
upperFoo: derived((parent: State["nestedDerived"]) => parent.foo.toUpperCase())
306+
}
307+
}
303308

304309
const config = {
305-
state: {
306-
foo: 'bar',
307-
nestedDerived,
308-
upperFoo: derived,
309-
},
310+
state,
310311
actions: {
311312
changeFoo,
312313
},
313314
}
314315
type Config = typeof config
315316
interface Action<Input = void> extends IAction<Config, Input> {}
316-
interface Derive<Parent extends IState, Value>
317-
extends IDerive<Config, Parent, Value> {}
317+
318318

319319
const app = new Overmind(config)
320320

@@ -323,9 +323,9 @@ describe('Derived', () => {
323323
function render() {
324324
runCount++
325325
if (runCount === 1) {
326-
expect(trackStateTree.state.nestedDerived).toBe('BAR!!!')
326+
expect(trackStateTree.state.nestedDerived.upperFoo).toBe('BAR')
327327
} else {
328-
expect(trackStateTree.state.nestedDerived).toBe('BAR2!!!')
328+
expect(trackStateTree.state.nestedDerived.upperFoo).toBe('BAR2')
329329
}
330330
trackStateTree.stopTracking()
331331
}

packages/node_modules/overmind/src/index.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import {
2323
NestedPartial,
2424
Options,
2525
ResolveActions,
26-
ResolveState,
2726
SSRMode,
2827
TestMode
2928
} from './internalTypes'
@@ -41,7 +40,6 @@ import {
4140
IAction,
4241
IConfiguration,
4342
IContext,
44-
IDerive,
4543
IOnInitialize,
4644
IOperator,
4745
IReaction,
@@ -66,15 +64,15 @@ import {
6664

6765
export * from './types'
6866

69-
export { createOperator, createMutationOperator, ResolveState, ResolveActions }
67+
export { createOperator, createMutationOperator, ResolveActions }
7068

7169
export { MODE_DEFAULT, MODE_TEST, MODE_SSR } from './utils'
7270

7371
export { SERIALIZE, rehydrate } from './rehydrate'
7472

7573
export { Statemachine, statemachine } from './statemachine'
7674

77-
export const derived = <D extends IDerive<any, any, any>>(cb: D): D extends IDerive<any, any, infer O> ? O : never => cb as any
75+
export const derived = <D extends (state: IState, rootState: IState) => any>(cb: D): D extends (state: IState, rootState: IState) => infer O ? O : never => cb as any
7876

7977
/** This type can be overwriten by app developers if they want to avoid
8078
* typing and then they can import `Action`, `Operation` etc. directly from
@@ -92,9 +90,6 @@ export interface Action<Value = void, ReturnValue = void>
9290
export interface AsyncAction<Value = void, ReturnValue = void>
9391
extends IAction<Config, Value, Promise<ReturnValue>> {}
9492

95-
export interface Derive<Parent extends IState, Value>
96-
extends IDerive<Config, Parent, Value> {}
97-
9893
export interface OnInitialize extends IOnInitialize<Config> {}
9994

10095
export interface Reaction extends IReaction<Config> {}
@@ -198,7 +193,7 @@ export class Overmind<ThisConfig extends IConfiguration>
198193
eventHub: EventEmitter<Events>
199194
devtools: Devtools
200195
actions: ResolveActions<ThisConfig['actions']>
201-
state: ResolveState<ThisConfig['state']>
196+
state: ThisConfig['state']
202197
effects: ThisConfig['effects'] & {}
203198
delimiter: string
204199
constructor(

packages/node_modules/overmind/src/internalTypes.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -152,18 +152,6 @@ export interface Events {
152152

153153
// ============= PRIVATE TYPES FOR APP
154154

155-
export type ResolveState<State extends IState | null | undefined> = State extends undefined
156-
? State
157-
: {
158-
[P in keyof State]: State[P] extends (parent: ResolveState<IState>, root: any) => any
159-
? State[P] extends () => any ? State[P] : ReturnType<State[P]>
160-
: State[P] extends Array<any>
161-
? State[P]
162-
: State[P] extends IState | null
163-
? ResolveState<State[P]>
164-
: State[P]
165-
}
166-
167155
type NestedActions =
168156
| {
169157
[key: string]:

packages/node_modules/overmind/src/types.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Overmind } from './'
2-
import { ResolveActions, ResolveState } from './internalTypes'
2+
import { ResolveActions } from './internalTypes'
33
import { IS_OPERATOR } from './utils'
44

55
/** ===== PUBLIC API
@@ -17,7 +17,6 @@ export type IState = {
1717
[key: string]:
1818
| IState
1919
| string
20-
| IDerive<any, any, any>
2120
| number
2221
| boolean
2322
| object
@@ -33,15 +32,15 @@ export interface IConfig<ThisConfig extends IConfiguration> {
3332
}
3433

3534
export type IContext<ThisConfig extends IConfiguration> = {
36-
state: ResolveState<ThisConfig['state']>
35+
state: ThisConfig['state']
3736
actions: ResolveActions<ThisConfig['actions']>
3837
effects: ThisConfig['effects']
3938
revertable: (mutationsCallback: () => any) => () => void
4039
}
4140

4241
export interface IReaction<ThisConfig extends IConfiguration> {
4342
<O>(
44-
stateCallback: (state: ResolveState<ThisConfig['state']>) => O,
43+
stateCallback: (state: ThisConfig['state']) => O,
4544
updateCallback: (value: O) => void,
4645
options?: {
4746
nested?: boolean
@@ -74,15 +73,6 @@ export interface IOperator<
7473
[IS_OPERATOR]: true
7574
}
7675

77-
export type IDerive<
78-
ThisConfig extends IConfiguration,
79-
Parent extends IState,
80-
Value
81-
> = (
82-
parent: ResolveState<Parent>,
83-
state: ResolveState<ThisConfig['state']>
84-
) => Value
85-
8676
export interface IOnInitialize<ThisConfig extends IConfiguration> {
8777
(context: IContext<ThisConfig>, value: Overmind<ThisConfig>): void
8878
}

0 commit comments

Comments
 (0)