Skip to content

Commit aeb9b92

Browse files
Merge pull request cerebral#183 from cerebral/proxyclasses
Proxyclasses
2 parents 8cf2d07 + 197f98e commit aeb9b92

File tree

15 files changed

+54
-52
lines changed

15 files changed

+54
-52
lines changed

packages/node_modules/overmind/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
ResolveState,
1919
Execution,
2020
ResolveMockActions,
21+
NestedPartial,
2122
} from './internalTypes'
2223
import { proxifyEffects } from './proxyfyEffects'
2324
import {
@@ -78,7 +79,7 @@ function deepCopy(obj) {
7879

7980
export function createMock<Config extends Configuration>(
8081
config: Config,
81-
mockedEffects?: Partial<Config['effects']>
82+
mockedEffects?: NestedPartial<Config['effects']>
8283
): {
8384
actions: ResolveMockActions<Config['actions']>
8485
state: ResolveState<Config['state']>

packages/node_modules/overmind/src/internalTypes.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ export type SubType<Base, Condition> = Pick<
66
{ [Key in keyof Base]: Base[Key] extends Condition ? Key : never }[keyof Base]
77
>
88

9+
export type NestedPartial<T> = T extends object
10+
? Partial<{ [P in keyof T]: NestedPartial<T[P]> }>
11+
: T
12+
913
export type Options = {
1014
name?: string
1115
devtools?: string | boolean

packages/node_modules/overmind/src/proxyfyEffects.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ function wrapEffect(target, prop, path, cb) {
1212
const effectId = currentEffectId++
1313

1414
return cb({
15-
func: target[prop],
15+
func: target[prop].bind(target),
1616
effectId,
1717
name: path,
1818
method: prop,

packages/overmind-website/examples/guide/getstarted/actions.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ export const loadPosts: Action = async ({ state, jsonPlaceholder }) => {
2121
view,
2222
`
2323
import { state } from './state'
24+
import * as effects from './effects'
2425
import * as actions from './actions'
2526
2627
const config = {
2728
state,
29+
effects,
2830
actions
2931
}
3032
`
@@ -43,6 +45,12 @@ export const overmind = new Overmind({
4345
isLoadingPosts: false,
4446
posts: []
4547
},
48+
effects: {
49+
getPosts() {
50+
return fetch('https://jsonplaceholder.typicode.com/posts')
51+
.then(response => response.json())
52+
}
53+
},
4654
actions: {
4755
loadPosts: async ({ state, jsonPlaceholder }) => {
4856
state.isLoadingPosts = true

packages/overmind-website/examples/guide/getstarted/effects.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@ export const jsonPlaceholder = {
2222
view,
2323
`
2424
import state from './state'
25-
import * as actions from './actions'
2625
import * as effects from './effects'
2726
2827
const config = {
2928
state,
30-
actions,
3129
effects
3230
}
3331
`
@@ -46,13 +44,6 @@ export const overmind = new Overmind({
4644
isLoadingPosts: false,
4745
posts: []
4846
},
49-
actions: {
50-
loadPosts: async ({ state, jsonPlaceholder }) => {
51-
state.isLoadingPosts = true
52-
state.posts = await jsonPlaceholder.getPosts()
53-
state.isLoadingPosts = false
54-
}
55-
},
5647
effects: {
5748
getPosts() {
5849
return fetch('https://jsonplaceholder.typicode.com/posts')

packages/overmind-website/guides/beginner/01_getstarted.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,20 @@ h(Example, { name: "guide/getstarted/connectapp" })
3636

3737
That is it, your component will now render whenever the state accessed changes.
3838

39-
## Loading posts
39+
## Effects
4040

41-
We want to load some posts from [jsonplaceholder](https://jsonplaceholder.typicode.com/) when the **Posts** component mounts. To run logic in Overmind you trigger **actions**. Let us define an action that is responsible for getting our application up and running.
41+
We want to load some posts from [jsonplaceholder](https://jsonplaceholder.typicode.com/) when the **Posts** component mounts. This requires a side effect and we can expose any kind of side effects to our Overmind instance. Think of it as injecting libraries and tools. For example, it could be the [axios](https://www.npmjs.com/package/axios) library itself, some class instance we created or just a plain object as we see in the example below. Doing this injection keeps our logic pure and Overmind knows when these injected libraries are accessed, giving you a better developer experience.
4242

4343
```marksy
44-
h(Example, { name: "guide/getstarted/actions" })
44+
h(Example, { name: "guide/getstarted/effects" })
4545
```
4646

47-
Let us see how we define this effect called **jsonPlaceholder**.
47+
## Loading the posts
4848

49-
## Effects
50-
51-
We can expose any kind of side effects to our Overmind instance. Think of it as injecting libraries and tools. For example, it could be the [axios](https://www.npmjs.com/package/axios) library itself, some class instance we created or just a plain object as we see in the example below. Doing this injection keeps our actions pure and Overmind knows when these injected libraries are accessed.
49+
To run logic in Overmind you trigger **actions**. Let us define an action that is responsible for getting our application up and running.
5250

5351
```marksy
54-
h(Example, { name: "guide/getstarted/effects" })
52+
h(Example, { name: "guide/getstarted/actions" })
5553
```
5654

5755
Finally we need to trigger our action and we will do that when the component mounts.

packages/overmind-website/src/components/App/styles.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { css } from 'emotion'
22

33
export const pageWrapper = css`
4-
min-height: calc(100vh - 198px);
4+
min-height: calc(100vh);
55
`
66

77
export const wrapper = css`

packages/overmind-website/src/components/Guides/styles.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export const wrapper = css`
88
margin-top: 50px;
99
margin-left: auto;
1010
margin-right: auto;
11+
justify-content: center;
1112
`
1213

1314
export const guide = css`

packages/overmind-website/src/components/Videos/styles.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const wrapper = css`
77
padding: var(--padding-5);
88
flex-wrap: wrap;
99
margin-top: 50px;
10+
justify-content: center;
1011
`
1112

1213
export const video = css`

packages/overmind-website/src/components/ViewSelector/index.tsx

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as ReactImage from '../../images/react.png'
44
import * as VueImage from '../../images/vue.png'
55
import * as AngularImage from '../../images/angular.png'
66
import * as TsImage from '../../images/ts.png'
7-
import * as TsImageGrayscale from '../../images/ts-grayscale.png'
7+
import * as JsImage from '../../images/js.png'
88
import Icon from '../Icon'
99
import * as styles from './styles'
1010
import { css } from 'emotion'
@@ -25,49 +25,44 @@ const ViewSelector: SFC = () => {
2525
}
2626

2727
const options = {
28+
vue: (
29+
<div className={styles.viewOption}>
30+
<img src={VueImage} width={25} />
31+
Vue
32+
<img className={styles.image} src={JsImage} width="20" height="20" />
33+
</div>
34+
),
2835
react: (
2936
<div className={styles.viewOption}>
3037
<img src={ReactImage} width={25} />
3138
React
39+
<img className={styles.image} src={JsImage} width="20" height="20" />
3240
</div>
3341
),
34-
vue: (
42+
react_ts: (
3543
<div className={styles.viewOption}>
36-
<img src={VueImage} width={25} />
37-
Vue
44+
<img src={ReactImage} width={25} />
45+
React
46+
<img className={styles.image} src={TsImage} width="20" height="20" />
3847
</div>
3948
),
40-
angular: (
49+
angular_ts: (
4150
<div className={styles.viewOption}>
4251
<img src={AngularImage} width={25} />
4352
Angular
53+
<img className={styles.image} src={TsImage} width="20" height="20" />
4454
</div>
4555
),
4656
}
4757

4858
return (
4959
<div className={styles.wrapper}>
50-
<div
51-
className={styles.tsImageWrapper}
52-
onClick={state.showViewHelp ? null : actions.toggleTypescript}
53-
>
54-
{state.typescript ? (
55-
<img className={styles.image} src={TsImage} width="20" height="20" />
56-
) : (
57-
<img
58-
className={css(styles.image, styles.grayscale)}
59-
src={TsImageGrayscale}
60-
width="20"
61-
height="20"
62-
/>
63-
)}
64-
</div>
6560
<div
6661
ref={selectorRef}
6762
onClick={state.showViewHelp ? null : onSelectorClick}
6863
className={css(styles.selector, isOpen && styles.selectorOpen)}
6964
>
70-
{options[state.theme]}
65+
{options[state.theme + (state.typescript ? '_ts' : '')]}
7166
<span className={styles.chevron}>
7267
<Icon>chevron-down</Icon>
7368
</span>

0 commit comments

Comments
 (0)