Skip to content

Commit 6d1172b

Browse files
committed
fix(overmind): remove support for modules
modules add a lot of complexity and should be handled in user space BREAKING CHANGE: modules are no longer supported
1 parent a986d6c commit 6d1172b

File tree

2 files changed

+22
-252
lines changed

2 files changed

+22
-252
lines changed

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

Lines changed: 1 addition & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import App, { TAction, TConfig, DynamicAction, dynamicModule } from './'
1+
import App, { DynamicAction, TAction, TConfig, dynamicModule } from './'
22

33
describe('Overmind', () => {
44
test('should instantiate app with state', () => {
@@ -14,78 +14,6 @@ describe('Overmind', () => {
1414

1515
expect(app.state.foo).toEqual('bar')
1616
})
17-
test('should instantiate app with namespaces', () => {
18-
const fooAction: Action<string> = ({ run }) => run(() => {})
19-
20-
const foo = {
21-
state: {
22-
foo: 'bar',
23-
},
24-
actions: {
25-
foo: fooAction,
26-
},
27-
}
28-
const bar = {
29-
state: {
30-
bar: 'baz',
31-
},
32-
effects: {
33-
hello: () => 'hello',
34-
},
35-
actions: {
36-
bar: fooAction,
37-
},
38-
}
39-
type Config = TConfig<{
40-
modules: {
41-
foo: typeof foo
42-
bar: typeof bar
43-
}
44-
}>
45-
46-
type Action<Input = void, Output = any> = TAction<Input, Output, Config>
47-
48-
const config = {
49-
modules: {
50-
foo,
51-
bar,
52-
},
53-
}
54-
55-
const app = new App(config)
56-
57-
expect(app.state.foo.foo).toEqual('bar')
58-
expect(app.state.bar.bar).toEqual('baz')
59-
expect(app.actions.foo.foo('mip')).toEqual('mip')
60-
expect(app.actions.bar.bar('bop')).toEqual('bop')
61-
})
62-
test('should allow namespaced modules to be functions', () => {
63-
const foo = dynamicModule((namespace) => {
64-
const fooAction: DynamicAction<string> = ({ mutate }) =>
65-
mutate((state) => (state[namespace].foo = 'bar2'))
66-
67-
return {
68-
state: {
69-
foo: 'bar',
70-
},
71-
actions: {
72-
foo: fooAction,
73-
},
74-
}
75-
})
76-
77-
const config = {
78-
modules: {
79-
foo,
80-
},
81-
}
82-
83-
const app = new App(config)
84-
85-
expect(app.state.foo.foo).toEqual('bar')
86-
expect(app.actions.foo.foo('mip')).toEqual('mip')
87-
expect(app.state.foo.foo).toEqual('bar2')
88-
})
8917
})
9018

9119
describe('OPERATORS', () => {

packages/node_modules/overmind/src/index.ts

Lines changed: 21 additions & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
import { ActionChain } from 'action-chain'
2-
import { EventEmitter } from 'betsy'
3-
import ProxyStateTree from 'proxy-state-tree'
4-
import Devtools, { Message, safeValue } from './Devtools'
51
import ActionClass, {
6-
IValueAction,
7-
INoValueAction,
82
Compose,
3+
INoValueAction,
4+
IValueAction,
95
createOperators,
106
} from './Action'
11-
import Reaction from './reaction'
7+
import Devtools, { Message, safeValue } from './Devtools'
128
import {
139
DynamicModule,
14-
SubType,
15-
Events,
1610
EventType,
11+
Events,
1712
Options,
13+
SubType,
1814
} from './internalTypes'
15+
16+
import { ActionChain } from 'action-chain'
17+
import { EventEmitter } from 'betsy'
18+
import ProxyStateTree from 'proxy-state-tree'
19+
import Reaction from './reaction'
20+
1921
export { default as derive } from './derived'
2022
export { default as compute } from './computed'
2123

@@ -37,15 +39,6 @@ export type Configuration = {
3739
effects?: any
3840
actions?: any
3941
reactions?: any
40-
modules?: {
41-
[namespace: string]: {
42-
onInitialize?: any
43-
state?: any
44-
effects?: any
45-
actions?: any
46-
reactions?: any
47-
}
48-
}
4942
}
5043

5144
/*
@@ -59,43 +52,9 @@ interface IApp extends IEffects {
5952
state: IState
6053
}
6154

62-
export type TState<Config extends Configuration> = [Config['state']] extends [
63-
undefined
64-
]
65-
? {
66-
[P in keyof SubType<Config['modules'], { state: {} }>]: SubType<
67-
Config['modules'],
68-
{ state: {} }
69-
>[P]['state']
70-
}
71-
: [Config['modules']] extends [undefined]
72-
? Config['state']
73-
: Config['state'] &
74-
{
75-
[P in keyof SubType<Config['modules'], { state: {} }>]: SubType<
76-
Config['modules'],
77-
{ state: {} }
78-
>[P]['state']
79-
}
55+
export type TState<Config extends Configuration> = Config['state']
8056

81-
export type TEffects<Config extends Configuration> = [
82-
Config['effects']
83-
] extends [undefined]
84-
? {
85-
[P in keyof SubType<Config['modules'], { effects: object }>]: SubType<
86-
Config['modules'],
87-
{ effects: object }
88-
>[P]['effects']
89-
}
90-
: [Config['modules']] extends [undefined]
91-
? Config['effects']
92-
: Config['effects'] &
93-
{
94-
[P in keyof SubType<Config['modules'], { effects: object }>]: SubType<
95-
Config['modules'],
96-
{ effects: object }
97-
>[P]['effects']
98-
}
57+
export type TEffects<Config extends Configuration> = Config['effects']
9958

10059
export type Mutate<Value = any> = (state: IApp['state'], value: Value) => void
10160

@@ -162,54 +121,9 @@ export type Compute<Input, Output> = (
162121
) => (state: IApp['state']) => Output
163122

164123
export type TConfig<Config extends Configuration> = {
165-
state: [Config['state']] extends [undefined]
166-
? {
167-
[P in keyof SubType<Config['modules'], { state: {} }>]: SubType<
168-
Config['modules'],
169-
{ state: {} }
170-
>[P]['state']
171-
}
172-
: [Config['modules']] extends [undefined]
173-
? Config['state']
174-
: Config['state'] &
175-
{
176-
[P in keyof SubType<Config['modules'], { state: {} }>]: SubType<
177-
Config['modules'],
178-
{ state: {} }
179-
>[P]['state']
180-
}
181-
effects: [Config['effects']] extends [undefined]
182-
? {
183-
[P in keyof SubType<Config['modules'], { effects: object }>]: SubType<
184-
Config['modules'],
185-
{ effects: object }
186-
>[P]['effects']
187-
}
188-
: [Config['modules']] extends [undefined]
189-
? Config['effects']
190-
: Config['effects'] &
191-
{
192-
[P in keyof SubType<
193-
Config['modules'],
194-
{ effects: object }
195-
>]: SubType<Config['modules'], { effects: object }>[P]['effects']
196-
}
197-
actions: [Config['actions']] extends [undefined]
198-
? {
199-
[P in keyof SubType<Config['modules'], { actions: object }>]: SubType<
200-
Config['modules'],
201-
{ actions: object }
202-
>[P]['actions']
203-
}
204-
: [Config['modules']] extends [undefined]
205-
? Config['actions']
206-
: Config['actions'] &
207-
{
208-
[P in keyof SubType<
209-
Config['modules'],
210-
{ actions: object }
211-
>]: SubType<Config['modules'], { actions: object }>[P]['actions']
212-
}
124+
state: Config['state'] & {}
125+
effects: Config['effects'] & {}
126+
actions: Config['actions'] & {}
213127
reactions: any
214128
namespaces: any
215129
}
@@ -258,10 +172,6 @@ export default class App<
258172
}
259173
}
260174

261-
/*
262-
Mutate module functions into module objects
263-
*/
264-
this.mutateModuleFunctionsIntoModules(configuration)
265175
/*
266176
Set up an eventHub to trigger information from derived, computed and reactions
267177
*/
@@ -342,36 +252,11 @@ export default class App<
342252
this.proxyStateTree = proxyStateTree
343253
this.eventHub = eventHub
344254

345-
const initializers = this.getInitializers(configuration)
346-
347-
if (!initializers.length) {
348-
return
349-
}
350-
351-
const rootInitializer =
352-
initializers[0].name === 'onInitialize' ? initializers.shift() : null
353-
354-
let onInitialize
355-
356-
if (initializers.length) {
357-
onInitialize = operators.parallel(initializers)
358-
}
359-
360-
if (rootInitializer) {
361-
onInitialize = operators.compose(rootInitializer)
362-
}
363-
364-
// @ts-ignore
365-
onInitialize.displayName = 'onInitialize'
366-
onInitialize(undefined)
367-
}
368-
private mutateModuleFunctionsIntoModules(config: Configuration) {
369-
if (config.modules) {
370-
Object.keys(config.modules).forEach((key) => {
371-
if (typeof config.modules[key] === 'function') {
372-
config.modules[key] = (config.modules[key] as any)(key)
373-
}
374-
})
255+
if (configuration.onInitialize) {
256+
const onInitialize = operators.compose(configuration.onInitialize)
257+
// @ts-ignore
258+
onInitialize.displayName = 'onInitialize'
259+
onInitialize(undefined)
375260
}
376261
}
377262
private initializeDevtools(host, actionChain, eventHub, proxyStateTree) {
@@ -485,41 +370,9 @@ export default class App<
485370
if (configuration.state) {
486371
state = this.transformStateToPlainObject(configuration.state)
487372
}
488-
if (configuration.modules) {
489-
Object.keys(configuration.modules).reduce((aggr, key) => {
490-
if (configuration.modules[key].state) {
491-
return Object.assign(aggr, {
492-
[key]: this.transformStateToPlainObject(
493-
configuration.modules[key].state
494-
),
495-
})
496-
}
497-
498-
return aggr
499-
}, state)
500-
}
501373

502374
return state
503375
}
504-
private getInitializers(configuration) {
505-
let initializers = []
506-
if (configuration.onInitialize) {
507-
initializers.push(configuration.onInitialize)
508-
}
509-
if (configuration.modules) {
510-
initializers = Object.keys(configuration.modules).reduce((aggr, key) => {
511-
if (configuration.modules[key].onInitialize) {
512-
configuration.modules[key].onInitialize.displayName =
513-
key + '.onInitialize'
514-
return aggr.concat(configuration.modules[key].onInitialize)
515-
}
516-
517-
return aggr
518-
}, initializers)
519-
}
520-
521-
return initializers
522-
}
523376
private transformStateToPlainObject(state: {}) {
524377
return Object.keys(state).reduce((aggr, key) => {
525378
aggr[key] = state[key]
@@ -532,17 +385,6 @@ export default class App<
532385
if (configuration.actions) {
533386
actions = configuration.actions
534387
}
535-
if (configuration.modules) {
536-
Object.keys(configuration.modules).reduce((aggr, key) => {
537-
if (configuration.modules[key].actions) {
538-
return Object.assign(aggr, {
539-
[key]: configuration.modules[key].actions,
540-
})
541-
}
542-
543-
return aggr
544-
}, actions)
545-
}
546388

547389
const evaluatedActions = Object.keys(actions).reduce((aggr, name) => {
548390
if (typeof actions[name] === 'function') {

0 commit comments

Comments
 (0)