Skip to content

Commit cca150b

Browse files
refactor(views): expose overmind as overmind instead of app
1 parent a19c095 commit cca150b

File tree

92 files changed

+1025
-1008
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+1025
-1008
lines changed

packages/node_modules/overmind-angular/src/index.ts

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
import { BaseApp, EventType, Overmind } from 'overmind'
1+
import { EventType, Overmind, TApp, Configuration } from 'overmind'
2+
23
// @ts-ignore
34
import { NgZone } from '@angular/core'
45

5-
export type TConnect<App extends BaseApp> = {
6-
app: {
7-
state: App['state']
8-
actions: App['actions']
6+
export type TConnect<Config extends Configuration> = {
7+
overmind: {
8+
state: TApp<Config>['state']
9+
actions: TApp<Config>['actions']
910
reaction: (
1011
name: string,
11-
stateCb: (state: App['state']) => any,
12+
stateCb: (state: TApp<Config>['state']) => any,
1213
Function
1314
) => void
1415
}
1516
}
1617

1718
let nextComponentId = 0
1819

19-
export const createConnect = <App extends Overmind<any>>(app: App) => () => {
20+
export const createConnect = <A extends Overmind<any>>(overmind: A) => () => {
2021
const componentId = nextComponentId++
2122
let componentInstanceId = 0
2223

@@ -26,12 +27,14 @@ export const createConnect = <App extends Overmind<any>>(app: App) => () => {
2627
const targetNgAfterContentInit = target.prototype.ngAfterContentInit
2728
const targetNgAfterViewInit = target.prototype.ngAfterViewInit
2829
const targetNgAfterViewChecked = target.prototype.ngAfterViewChecked
29-
const reactionFactory = app.createReactionFactory(target.constructor.name)
30+
const reactionFactory = overmind.createReactionFactory(
31+
target.constructor.name
32+
)
3033

3134
target.prototype.ngOnInit = function() {
32-
this.app = {
33-
state: app.state,
34-
actions: app.actions,
35+
this.overmind = {
36+
state: overmind.state,
37+
actions: overmind.actions,
3538
reaction: reactionFactory.add,
3639
}
3740
this.__componentInstanceId = componentInstanceId++
@@ -55,7 +58,7 @@ export const createConnect = <App extends Overmind<any>>(app: App) => () => {
5558
}
5659

5760
target.prototype.ngAfterContentInit = function() {
58-
this.__currentTrackId = app.trackState()
61+
this.__currentTrackId = overmind.trackState()
5962

6063
if (targetNgAfterContentInit) {
6164
targetNgAfterContentInit.apply(target)
@@ -64,22 +67,22 @@ export const createConnect = <App extends Overmind<any>>(app: App) => () => {
6467

6568
target.prototype.ngAfterViewInit = function() {
6669
const ngZ = new NgZone({ enableLongStackTrace: false })
67-
const paths = app.clearTrackState(this.__currentTrackId)
70+
const paths = overmind.clearTrackState(this.__currentTrackId)
6871

69-
app.eventHub.emitAsync(EventType.COMPONENT_ADD, {
72+
overmind.eventHub.emitAsync(EventType.COMPONENT_ADD, {
7073
componentId,
7174
componentInstanceId: this.__componentInstanceId,
7275
name: this.constructor.name || '',
7376
paths: Array.from(paths),
7477
})
75-
this.__listener = app.addMutationListener(paths, (flushId) => {
78+
this.__listener = overmind.addMutationListener(paths, (flushId) => {
7679
this.__shouldUpdatePaths = true
7780
if (this.cdr) {
7881
this.cdr.markForCheck()
7982
}
8083
ngZ.run(() => {})
8184

82-
app.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
85+
overmind.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
8386
componentId,
8487
componentInstanceId: this.__componentInstanceId,
8588
name: this.constructor.name || '',
@@ -94,7 +97,7 @@ export const createConnect = <App extends Overmind<any>>(app: App) => () => {
9497

9598
target.prototype.ngDoCheck = function() {
9699
if (this.__shouldUpdatePaths) {
97-
this.__currentTrackId = app.trackState()
100+
this.__currentTrackId = overmind.trackState()
98101
}
99102
if (targetNgDoCheck) {
100103
targetNgDoCheck.apply(target)
@@ -103,10 +106,10 @@ export const createConnect = <App extends Overmind<any>>(app: App) => () => {
103106

104107
target.prototype.ngAfterViewChecked = function() {
105108
if (this.__shouldUpdatePaths) {
106-
const paths = app.clearTrackState(this.__currentTrackId)
109+
const paths = overmind.clearTrackState(this.__currentTrackId)
107110
this.__listener.update(paths)
108111
this.__shouldUpdatePaths = false
109-
app.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
112+
overmind.eventHub.emitAsync(EventType.COMPONENT_UPDATE, {
110113
componentId,
111114
componentInstanceId: this.__componentInstanceId,
112115
name: this.constructor.name || '',
@@ -124,7 +127,7 @@ export const createConnect = <App extends Overmind<any>>(app: App) => () => {
124127
if (targetNgOnDestroy) {
125128
targetNgOnDestroy.apply(target)
126129
}
127-
app.eventHub.emitAsync(EventType.COMPONENT_REMOVE, {
130+
overmind.eventHub.emitAsync(EventType.COMPONENT_REMOVE, {
128131
componentId,
129132
componentInstanceId: this.__componentInstanceId,
130133
name: this.constructor.name || '',

packages/node_modules/overmind-components/src/Component.test.tsx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Overmind } from 'overmind'
22
import { testRender } from './testRender'
3-
import { h } from './'
4-
import { TComponent } from './Component'
3+
import { Component, h, useOvermind } from './'
54
import { Null } from './Null'
65

76
describe('COMPONENT', () => {
@@ -91,7 +90,6 @@ describe('COMPONENT', () => {
9190
</div>
9291
)
9392

94-
console.log(test.getUpdates())
9593
expect(test.getUpdates().length).toBe(2)
9694

9795
expect(target.children[0].children[0].vtree).toBeInstanceOf(Null)
@@ -111,7 +109,7 @@ describe('COMPONENT', () => {
111109
expect(target.children[0].vtree.vtree.children[0].el.nodeValue).toBe('foo')
112110
})
113111
test('should update on state changes', () => {
114-
const app = new Overmind({
112+
const config = {
115113
state: {
116114
foo: true,
117115
},
@@ -120,14 +118,18 @@ describe('COMPONENT', () => {
120118
state.foo = false
121119
},
122120
},
123-
})
121+
}
122+
const app = new Overmind(config)
123+
124+
const TestComp: Component = () => {
125+
const { state } = useOvermind<typeof config>()
126+
return state.foo ? <span>foo</span> : <div>bar</div>
127+
}
124128

125-
const TestComp: TComponent<typeof app> = ({ state }) =>
126-
state.foo ? <span>foo</span> : <div>bar</div>
127129
const test = testRender(app, <TestComp />)
128130
const target = test.getTargetElement()
129131

130-
app.actions.changeFoo({})
132+
app.actions.changeFoo()
131133

132134
expect(test.getUpdates().length).toBe(8)
133135

packages/node_modules/overmind-components/src/Component.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,4 @@
11
import { Null } from './Null'
2-
import { TApp, Configuration } from 'overmind'
3-
4-
export type TComponent<Config extends Configuration, Props = {}> = (
5-
props: Props extends { children: any }
6-
? {
7-
state: TApp<Config>['state']
8-
actions: TApp<Config>['actions']
9-
} & Props
10-
: {
11-
state: TApp<Config>['state']
12-
actions: TApp<Config>['actions']
13-
} & Props & { children: any }
14-
) => any
152

163
export type FormEvent<T> = {
174
bubbles: boolean
@@ -80,8 +67,6 @@ export class Component {
8067
this.vtree =
8168
this.tag({
8269
...this.props,
83-
state: app.state,
84-
actions: app.actions,
8570
children: this.children.length === 1 ? this.children[0] : this.children,
8671
}) || new Null()
8772
const paths = app.clearTrackState(trackId)
@@ -184,8 +169,6 @@ export class Component {
184169
const newVTree =
185170
this.tag({
186171
...next.props,
187-
state: app.state,
188-
actions: app.actions,
189172
children: this.children.length === 1 ? this.children[0] : this.children,
190173
}) || new Null()
191174
const paths = app.clearTrackState(trackId)

packages/node_modules/overmind-components/src/hooks.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
import { Component } from './Component'
2+
import { TApp, IConfig } from 'overmind'
3+
4+
export function useOvermind<Config extends IConfig>(): {
5+
state: TApp<Config>['state']
6+
actions: TApp<Config>['actions']
7+
} {
8+
const component = Component.current
9+
10+
return {
11+
state: component.context.app.state,
12+
actions: component.context.app.actions,
13+
}
14+
}
215

316
export function useState<T>(initialState: T): [T, (update: T) => void] {
417
const component = Component.current

packages/node_modules/overmind-components/src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import { Null } from './Null'
44
import { Literal } from './Literal'
55
import { Events } from './Events'
66

7-
export { TComponent, FormEvent } from './Component'
7+
export { FormEvent } from './Component'
8+
9+
export type Component<Props = object> = (
10+
props: Props extends { children: any } ? Props : Props & { children?: any }
11+
) => JSX.Element
812

913
export * from './hooks'
1014

packages/node_modules/overmind-components/src/render.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Overmind } from 'overmind'
22
import { testRender } from './testRender'
33
import { h } from './'
4-
import { Null } from 'overmind-components/src/Null'
4+
import { Null } from './Null'
55

66
describe('RENDER', () => {
77
test.only('should create element', () => {

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Overmind, TConfig } from 'overmind'
2-
import { TComponent } from 'overmind-components'
32

43
import * as actions from './actions'
54
import * as effects from './effects'
@@ -16,10 +15,6 @@ declare module 'overmind' {
1615
interface IConfig extends TConfig<typeof config> {}
1716
}
1817

19-
export type Component<Props = {}> = TComponent<typeof config, Props>
20-
21-
const app = new Overmind(config, {
18+
export default new Overmind(config, {
2219
devtools: false,
2320
})
24-
25-
export default app

packages/node_modules/overmind-devtools/src/components/Action/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { h } from 'overmind-components'
2-
import { Component } from '../../app'
1+
import { h, Component, useOvermind } from 'overmind-components'
32
import * as styles from './styles'
43
import ActionOperator from '../ActionOperator'
54
import Flush from '../ActionFlush'
65
import { getActionId, getOperatorId } from '../../app/utils'
76
import { css } from 'emotion'
87

9-
const Action: Component = ({ state, actions }) => {
8+
const Action: Component = () => {
9+
const { state, actions } = useOvermind()
1010
const flushReference =
1111
state.currentApp.flushByActionId[getActionId(state.currentAction)]
1212
const flush =

packages/node_modules/overmind-devtools/src/components/ActionFlush/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { h } from 'overmind-components'
2-
import { Component } from '../../app'
1+
import { h, Component, useOvermind } from 'overmind-components'
32
import { Flush as FlushType } from '../../app/types'
43
import * as actionStyles from '../Action/styles'
54
import * as styles from './styles'
@@ -11,7 +10,8 @@ type Props = {
1110
flush: FlushType
1211
}
1312

14-
const ActionFlush: Component<Props> = ({ flush, state, actions }) => {
13+
const ActionFlush: Component<Props> = ({ flush }) => {
14+
const { state, actions } = useOvermind()
1515
const isExpanded = state.expandAllActionDetails || !flush.isCollapsed
1616

1717
return flush.components.length || flush.derived.length ? (

packages/node_modules/overmind-devtools/src/components/ActionFlush/styles.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export const flush = css`
44
flex: 1;
55
display: flex;
66
flex-direction: column;
7-
padding: 0 var(--padding-2);
7+
padding: 0 var(--padding-3);
88
cursor: pointer;
99
border: 1px solid var(--color-black-1);
1010
background-color: #fff;

0 commit comments

Comments
 (0)