Skip to content

Commit

Permalink
Merge pull request #169 from cerebral/fixes5
Browse files Browse the repository at this point in the history
Fixes5
  • Loading branch information
christianalfoni authored Dec 21, 2018
2 parents 215562c + 1d54ded commit c96e3de
Show file tree
Hide file tree
Showing 16 changed files with 60 additions and 68 deletions.
5 changes: 0 additions & 5 deletions packages/node_modules/overmind-angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@
"es",
"react"
],
"engines": {
"browsers": [
"last 1 Chrome versions"
]
},
"dependencies": {
"@types/node": "^10.5.1",
"overmind": "next",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const ActionOperator: SFC<Props> = ({
const { state, actions } = useOvermind()
const isExpanded = state.expandAllActionDetails || !operator.isCollapsed

console.log(operator.mutations)
return (
<div>
{value === undefined ? null : (
Expand Down
7 changes: 1 addition & 6 deletions packages/node_modules/overmind-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@
"es",
"react"
],
"engines": {
"browsers": [
"last 1 Chrome versions"
]
},
"dependencies": {
"@types/node": "^10.5.1",
"overmind": "next",
Expand All @@ -45,7 +40,7 @@
"@types/jest": "^23.1.4",
"@types/react": "^16.7.13",
"jest": "^23.5.0",
"react": "^16.7.0-alpha.1",
"react": "^16.7.0-alpha.2",
"react-test-renderer": "^16.4.1",
"rimraf": "^2.6.2",
"ts-jest": "^23.10.4",
Expand Down
1 change: 1 addition & 0 deletions packages/node_modules/overmind-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ export const createConnect = <A extends Overmind<Configuration>>(
componentInstanceId = componentInstanceId++
currentFlushId = 0
componentDidMount() {
console.log(this.tree)
overmind.eventHub.emitAsync(EventType.COMPONENT_ADD, {
componentId: populatedComponent.__componentId,
componentInstanceId: this.componentInstanceId,
Expand Down
5 changes: 0 additions & 5 deletions packages/node_modules/overmind-vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@
"es",
"react"
],
"engines": {
"browsers": [
"last 1 Chrome versions"
]
},
"dependencies": {
"@types/node": "^10.5.1",
"overmind": "next",
Expand Down
5 changes: 0 additions & 5 deletions packages/node_modules/overmind/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@
"es",
"config"
],
"engines": {
"browsers": [
"last 1 Chrome versions"
]
},
"dependencies": {
"@types/node": "^10.5.1",
"is-plain-object": "^2.0.4",
Expand Down
10 changes: 5 additions & 5 deletions packages/node_modules/overmind/src/derived.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Overmind, TAction, TDerive, StateObject } from './'
import { Overmind, TAction, TDerive, TStateObject } from './'

type State = {
foo: string
Expand All @@ -23,7 +23,7 @@ describe.only('Derived', () => {
state: typeof state
}

type Derive<Parent extends StateObject, Value> = TDerive<
type Derive<Parent extends TStateObject, Value> = TDerive<
Config,
Parent,
Value
Expand Down Expand Up @@ -60,7 +60,7 @@ describe.only('Derived', () => {
actions: typeof config.actions
}
type Action<Input = void> = TAction<Config, Input>
type Derive<Parent extends StateObject, Value> = TDerive<
type Derive<Parent extends TStateObject, Value> = TDerive<
Config,
Parent,
Value
Expand Down Expand Up @@ -110,7 +110,7 @@ describe.only('Derived', () => {
actions: typeof config.actions
}
type Action<Input = void> = TAction<Config, Input>
type Derive<Parent extends StateObject, Value> = TDerive<
type Derive<Parent extends TStateObject, Value> = TDerive<
Config,
Parent,
Value
Expand Down Expand Up @@ -147,7 +147,7 @@ describe.only('Derived', () => {
actions: typeof config.actions
}
type Action<Input = void> = TAction<Config, Input>
type Derive<Parent extends StateObject, Value> = TDerive<
type Derive<Parent extends TStateObject, Value> = TDerive<
Config,
Parent,
Value
Expand Down
34 changes: 23 additions & 11 deletions packages/node_modules/overmind/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ import {
TConfig,
TDerive,
TOperator,
TReaction,
TValueContext,
TOnInitialize,
StateObject,
TStateObject,
} from './types'

export * from './types'
Expand All @@ -34,14 +33,12 @@ type TheConfig = IConfig & TConfig<{ actions: {} }>

export type Action<Value = void> = TAction<TheConfig, Value>

export type Derive<Parent extends StateObject, Value> = TDerive<
export type Derive<Parent extends TStateObject, Value> = TDerive<
TheConfig,
Parent,
Value
>

export type Reaction = TReaction<TheConfig>

export type OnInitialize = TOnInitialize<TheConfig>

const isPlainObject = require('is-plain-object')
Expand Down Expand Up @@ -179,6 +176,7 @@ export class Overmind<Config extends Configuration> implements Configuration {
operatorId: -1,
path: [],
emit: this.eventHub.emit.bind(this.eventHub),
send: this.devtools ? this.devtools.send.bind(this.devtools) : () => {},
}
}
private createContext(value, execution, tree) {
Expand Down Expand Up @@ -745,13 +743,27 @@ export function mutate<Input, Config extends Configuration = TheConfig>(
if (err) next(err)
else {
startDebugOperator('mutate', operation, context)
const mutationsTree = context.proxyStateTree.getMutationTree()
operation({ ...context, state: mutationsTree.state })
const newContext = createContext(context, context.value)
context.execution.emit(EventType.MUTATIONS, {
...newContext.execution,
mutations: mutationsTree.mutations,
const mutationTree = context.proxyStateTree.getMutationTree()
if (!IS_PRODUCTION) {
mutationTree.onMutation((mutation) => {
context.execution.emit(EventType.MUTATIONS, {
...context.execution,
operatorId: context.execution.operatorId + 1,
mutations: [mutation],
})
})
}
const maybePromise: any = operation({
...context,
state: mutationTree.state,
})
const newContext = createContext(
context,
maybePromise instanceof Promise
? maybePromise.then(() => context.value)
: context.value
)

stopDebugOperator(newContext)
next(null, newContext)
}
Expand Down
6 changes: 3 additions & 3 deletions packages/node_modules/overmind/src/internalTypes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IMutation, ProxyStateTree } from 'proxy-state-tree'
import { Configuration, TAction, TOperator, StateObject } from './types'
import { Configuration, TAction, TOperator, TStateObject } from './types'

export type SubType<Base, Condition> = Pick<
Base,
Expand Down Expand Up @@ -115,14 +115,14 @@ export type TBaseContext<Config extends Configuration> = Config['effects'] & {

type Derived = (parent: any, config: any) => any

export type ResolveState<State extends StateObject> = State extends undefined
export type ResolveState<State extends TStateObject> = State extends undefined
? {}
: {
[P in keyof State]: State[P] extends Derived
? ReturnType<State[P]>
: State[P] extends Array<any>
? State[P]
: State[P] extends StateObject
: State[P] extends TStateObject
? ResolveState<State[P]>
: State[P]
}
Expand Down
13 changes: 3 additions & 10 deletions packages/node_modules/overmind/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ export type BaseApp = {
actions: {}
}

export type StateObject =
export type TStateObject =
| {
[key: string]:
| StateObject
| TStateObject
| string
| TDerive<any, any, any>
| number
Expand Down Expand Up @@ -68,20 +68,13 @@ export type TOperator<Config extends Configuration, Input, Output> = (

export type TDerive<
Config extends Configuration,
Parent extends StateObject,
Parent extends TStateObject,
Value
> = (
parent: ResolveState<Parent>,
state: ResolveState<Config['state'] & {}>
) => Value

export type TReaction<Config extends Configuration> = (
reaction: (
getState: (state: ResolveState<Config['state']>) => any,
action: TAction<Config, void> | TOperator<Config, void, any>
) => any
) => any

export type TOnInitialize<Config extends Configuration> = (
context: TValueContext<Config, Overmind<Config>>
) => void
8 changes: 7 additions & 1 deletion packages/node_modules/proxy-state-tree/src/TrackStateTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export class TrackStateTree<T extends object> implements ITrackStateTree<T> {
master: IProxyStateTree<T>
pathDependencies: Set<string> = new Set()
callback: ITrackCallback
shouldTrack: boolean = false
state: T
proxifier: IProxifier<T>
constructor(master: IProxyStateTree<T>) {
Expand All @@ -24,6 +25,10 @@ export class TrackStateTree<T extends object> implements ITrackStateTree<T> {
return true
}
addTrackingPath(path: string) {
if (!this.shouldTrack) {
return
}

this.pathDependencies.add(path)

if (this.callback) {
Expand All @@ -32,7 +37,8 @@ export class TrackStateTree<T extends object> implements ITrackStateTree<T> {
}
track(cb?: ITrackCallback) {
this.master.changeTrackStateTree(this)

this.shouldTrack = true
setTimeout(() => (this.shouldTrack = false))
if (this.callback) {
for (let path of this.pathDependencies) {
this.master.removePathDependency(path, this.callback)
Expand Down
14 changes: 8 additions & 6 deletions packages/overmind-website/examples/guide/typescript/explicit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,24 @@ import {
TAction,
TOperator,
TDerive,
TReaction
TStateObject
} from 'overmind'
const config = {}
export type Config = TConfig<typeof config>
export type Config = TConfig<{
state: typeof config["state"]
actions: typeof config["actions"]
effects: typeof config["effects"]
}>
export type OnInitialize = TOnInitialize<Config>
export type Action<Input> = TAction<Config, Input>
export type Action<Input = void> = TAction<Config, Input>
export type Operator<Input, Output> = TOperator<Config, Input, Output>
export type Derive<Parent extends object, Output> = TDerive<Config, Parent, Output>
export type Reaction = TReaction<Config>
export type Derive<Parent extends TStateObject, Output> = TDerive<Config, Parent, Output>
`,
},
]
2 changes: 1 addition & 1 deletion packages/overmind-website/src/app/actions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Action, Operator, debounce, pipe } from 'overmind'
import { Page, RouteContext, GuideParams, VideoParams, Guide } from './types'
import { Page, RouteContext, GuideParams, VideoParams } from './types'
import * as o from './operators'

export const openHome: Action<RouteContext> = async ({ state, request }) => {
Expand Down
6 changes: 1 addition & 5 deletions packages/overmind-website/src/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import state from './state'
import onInitialize from './onInitialize'
import * as actions from './actions'
import * as effects from './effects'
import { createHook, createConnect, TConnect } from 'overmind-react'
import { createHook } from 'overmind-react'

const config = {
onInitialize,
Expand All @@ -16,8 +16,6 @@ declare module 'overmind' {
interface IConfig extends TConfig<typeof config> {}
}

export type Connect = TConnect<typeof config>

const app = new Overmind(
config,
process.env.NODE_ENV === 'production'
Expand All @@ -28,5 +26,3 @@ const app = new Overmind(
)

export const useOvermind = createHook(app)

export const connect = createConnect(app)
4 changes: 2 additions & 2 deletions packages/overmind-website/src/app/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type State = {
isLoadingSearchResult: boolean
searchResult: SearchResult[]
showSearchResult: boolean
test: Derive<State, Guide[]>
test: string
}

const state: State = {
Expand All @@ -44,7 +44,7 @@ const state: State = {
isLoadingSearchResult: false,
searchResult: [],
showSearchResult: false,
test: (state) => state.guides.filter((guide) => guide.type === 'beginner'),
test: '',
}

export default state
7 changes: 4 additions & 3 deletions packages/overmind-website/src/components/App/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createElement, SFC, useRef, useEffect } from 'react'
import { useOvermind, connect, Connect } from '../../app'
import { useOvermind } from '../../app'
import * as styles from './styles'
import TopBar from '../TopBar'
import FrontPage from '../FrontPage'
Expand Down Expand Up @@ -30,7 +30,8 @@ const fadeInPage = () => {
logo.style.opacity = '0'
}

const App: SFC<Connect> = ({ overmind: { state } }) => {
const App: SFC = () => {
const { state } = useOvermind()
const mainRef = useRef(null)
const isMobile = useIsMobile()
useScrollToTop(state.page)
Expand All @@ -53,4 +54,4 @@ const App: SFC<Connect> = ({ overmind: { state } }) => {
)
}

export default connect(App)
export default App

0 comments on commit c96e3de

Please sign in to comment.