Skip to content

Commit 3a9c1eb

Browse files
Merge pull request cerebral#190 from cerebral/refactorEffects
feat(overmind): remove flattening of effects to actions, use effects …
2 parents f7b9f3f + 763e38f commit 3a9c1eb

File tree

28 files changed

+159
-179
lines changed

28 files changed

+159
-179
lines changed

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,14 @@ import {
3333
export const onInitialize: OnInitialize = async ({
3434
value: app,
3535
state,
36-
storage,
37-
connector,
36+
effects,
3837
}) => {
39-
const port = await storage.get<string>('currentPort')
38+
const port = await effects.storage.get<string>('currentPort')
4039
if (port) {
4140
state.port = port
4241
}
43-
connector.onMessage(app.actions.onMessage)
44-
connector.connect(state.port)
42+
effects.connector.onMessage(app.actions.onMessage)
43+
effects.connector.connect(state.port)
4544
}
4645

4746
const handleClientMessage: Operator<Message, any> = pipe(
@@ -81,12 +80,12 @@ export const setError: Action<string> = ({ value: error, state }) =>
8180
export const changeNewPortValue: Action<string> = ({ value: port, state }) =>
8281
(state.newPortValue = String(Number(port)))
8382

84-
export const addConnection: Action = ({ state, connector }) => {
83+
export const addConnection: Action = ({ state, effects }) => {
8584
state.error = null
8685
state.isConnecting = true
8786
state.port = state.newPortValue
8887
state.newPortValue = ''
89-
connector.connect(state.port)
88+
effects.connector.connect(state.port)
9089
}
9190

9291
export const changeTab: Action<Tab> = ({ value: tab, state }) =>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ describe('Config', () => {
5151
const merged = merge(
5252
{
5353
actions: {
54-
loadConfigB: ({ lazy }) => {
55-
return lazy.loadConfig('configB')
54+
loadConfigB: ({ effects }) => {
55+
return effects.lazy.loadConfig('configB')
5656
},
5757
},
5858
},

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ function createDefaultOvermind() {
1616
context.state.foo = 'bar2'
1717
}
1818
const changeFooWithEffect: Action = (context) => {
19-
context.state.foo = context.hello()
19+
context.state.foo = context.effects.hello()
2020
}
2121
const waitAndChangeFoo: Action = (context) => {
22-
return context.wait().then(() => {
22+
return context.effects.wait().then(() => {
2323
context.state.foo = 'bar2'
2424
})
2525
}

packages/node_modules/overmind/src/index.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -295,16 +295,14 @@ export class Overmind<Config extends Configuration> implements Configuration {
295295
return execution
296296
}
297297
private createContext(value, execution, tree) {
298-
return Object.assign(
299-
{
300-
value,
301-
state: tree.state,
302-
actions: this.actions,
303-
execution,
304-
proxyStateTree: this.proxyStateTree,
305-
},
306-
this.trackEffects(this.effects, execution)
307-
)
298+
return {
299+
value,
300+
state: tree.state,
301+
actions: this.actions,
302+
execution,
303+
proxyStateTree: this.proxyStateTree,
304+
effects: this.trackEffects(this.effects, execution),
305+
}
308306
}
309307
private scopeValue(value: any, tree: TTree) {
310308
if (!value) {
@@ -764,14 +762,12 @@ function createContext(context, value, path?) {
764762
path: path || context.execution.path,
765763
}
766764

767-
return Object.assign(
768-
{
769-
...context,
770-
value,
771-
execution: newExecution,
772-
},
773-
context.execution.trackEffects(newExecution)
774-
)
765+
return {
766+
...context,
767+
value,
768+
execution: newExecution,
769+
effects: context.execution.trackEffects(newExecution),
770+
}
775771
}
776772

777773
function createNextPath(next) {

packages/node_modules/overmind/src/internalTypes.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,10 @@ export interface Events {
137137

138138
// ============= PRIVATE TYPES FOR APP
139139

140-
export type TBaseContext<Config extends Configuration> = Config['effects'] & {
140+
export type TBaseContext<Config extends Configuration> = {
141141
state: ResolveState<Config['state']>
142142
actions: ResolveActions<Config['actions']>
143+
effects: Config['effects']
143144
execution: any
144145
}
145146

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ describe('Mock', () => {
1313
const state: State = {
1414
foo: 'bar',
1515
}
16-
const test: Action = ({ state, effect, actions }) => {
17-
state.foo = effect()
16+
const test: Action = ({ state, effects, actions }) => {
17+
state.foo = effects.effect()
1818
}
1919
const actions = { test }
2020
const effect = () => 'bar2'

packages/node_modules/overmind/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export type TStateObject =
3333
export interface IConfig<Config extends Configuration> {
3434
state: Config['state']
3535
actions: Config['actions']
36-
effects: Config['effects'] & {}
36+
effects: Config['effects']
3737
}
3838

3939
// This is the type of the `app` argument passed in components.

packages/overmind-website/api/action.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
h(Example, { name: "api/action"})
55
```
66

7-
An action is where you write the logic of the application. Every action receives one argument called the **context**. This context holds:
7+
An action is where you write the logic of the application. Every action receives one argument and that is the configuration of your application:
88

9-
`{ state, actions, ...effects }`
9+
`{ state, actions, effects }`
1010

11-
This context gives Overmind the primarily the ability to understand what state you are changing and what effects you are running. You can also use other actions defined in your application. Additionally this context makes your actions highly testable as it can easily be mocked.
11+
This *injected* configuration allows Overmind to understand from where you are chaing state and running effects. You can also use other actions defined in your application. Additionally with *injection* your actions become highly testable as it can easily be mocked.
1212

1313
State changes are restricted to these actions. That means if you try to change the state outside of an action you will get an error. The state changes are also scoped to the action. That means it does not matter if you perform the state change asynchronously, either by defining the action as an **async** function or for example use a **setTimeout**. You can change the state at any time within the action.

packages/overmind-website/api/config.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Allows you to namespace configurations by a key.
2222
h(Example, { name: "api/config_namespaced" })
2323
```
2424

25-
## lazy (beta)
25+
## lazy
2626

2727
You can also lazy load configurations. You do this by giving each configuration a key with a function that returns the config when called. To actually load the configurations you can either call an effect or an action with the key of the configuration to load.
2828

packages/overmind-website/api/connect.md

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)