Skip to content

Commit

Permalink
feat(overmind): remove fromOperator as it causes typing issues
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
fromOperator is removed, use plain operators
  • Loading branch information
christianalfoni committed Jan 28, 2019
1 parent 44b4724 commit 0370e12
Show file tree
Hide file tree
Showing 13 changed files with 133 additions and 175 deletions.
14 changes: 6 additions & 8 deletions packages/node_modules/overmind-devtools/src/overmind/actions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Action, pipe, OnInitialize, Operator, fromOperator } from 'overmind'
import { Action, pipe, OnInitialize, Operator } from 'overmind'
import {
Message,
Tab,
Expand Down Expand Up @@ -69,13 +69,11 @@ const handleClientMessage: Operator<Message, any> = pipe(
})
)

export const onMessage: Action<Message> = fromOperator(
pipe(
isPortExistsMessage({
true: setPortExists,
false: handleClientMessage,
})
)
export const onMessage: Operator<Message> = pipe(
isPortExistsMessage({
true: setPortExists,
false: handleClientMessage,
})
)

export const setError: Action<string> = ({ value: error, state }) =>
Expand Down
48 changes: 20 additions & 28 deletions packages/node_modules/overmind/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,10 +340,24 @@ export class Overmind<Config extends Configuration> implements Configuration {
this.eventHub.emit(EventType.ACTION_START, execution)

action[IS_OPERATOR]
? resolve(
action(
this.createContext(value, execution, this.proxyStateTree)
)
? action(
null,
{
value,
state: this.proxyStateTree.state,
actions: this.actions,
execution,
effects: this.trackEffects(this.effects, execution),
},
(err, finalContext) => {
finalContext &&
this.eventHub.emit(EventType.ACTION_END, {
...finalContext.execution,
operatorId: finalContext.execution.operatorId - 1,
})
if (err) reject(err)
else resolve(this.options.testMode && finalContext.execution)
}
)
: resolve(
action(
Expand Down Expand Up @@ -613,28 +627,6 @@ export type Operator<Input = void, Output = Input> = IOperator<
Output
>

export function fromOperator<Config, Input>(
operator: IOperator<Config, Input, any>
): IAction<Config, Input> {
const func = (context) => {
return new Promise((resolve, reject) => {
operator(null, context, (err, finalContext) => {
finalContext &&
context.execution.emit(EventType.ACTION_END, {
...finalContext.execution,
operatorId: finalContext.execution.operatorId - 1,
})
if (err) reject(err)
else resolve()
})
})
}

func[IS_OPERATOR] = true

return func
}

export function pipe<Config extends Configuration, A, B>(
aOperator: IOperator<Config, A, B>
): IOperator<Config, A, B>
Expand Down Expand Up @@ -758,14 +750,14 @@ function stopDebugOperator(context, value) {
value.then((promiseValue) => {
context.execution.emit(EventType.OPERATOR_END, {
...context.execution,
result: promiseValue,
result: safeValue(promiseValue),
isAsync: true,
})
})
} else {
context.execution.emit(EventType.OPERATOR_END, {
...context.execution,
result: value,
result: safeValue(value),
isAsync: false,
})
}
Expand Down
8 changes: 8 additions & 0 deletions packages/node_modules/overmind/src/internalTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ export type ResolveActions<
? [TActionValue<Actions[T]>] extends [void]
? () => Promise<void>
: (value: TActionValue<Actions[T]>) => Promise<void>
: Actions[T] extends IOperator<any, any, any>
? [TOperationValue<Actions[T]>] extends [void]
? () => Promise<void>
: (value: TOperationValue<Actions[T]>) => Promise<void>
: Actions[T] extends NestedActions
? ResolveActions<Actions[T]>
: never
Expand All @@ -199,6 +203,10 @@ export type ResolveMockActions<
? [TActionValue<Actions[T]>] extends [void]
? () => Promise<MockResult>
: (value: TActionValue<Actions[T]>) => Promise<MockResult>
: Actions[T] extends IOperator<any, any, any>
? [TOperationValue<Actions[T]>] extends [void]
? () => Promise<MockResult>
: (value: TOperationValue<Actions[T]>) => Promise<MockResult>
: Actions[T] extends NestedMockActions
? ResolveMockActions<Actions[T]>
: never
Expand Down
34 changes: 11 additions & 23 deletions packages/node_modules/overmind/src/operators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,16 @@ import {
action,
parallel,
IConfig,
fromOperator,
IAction,
} from './'

describe.only('OPERATORS', () => {
describe('OPERATORS', () => {
test('map', () => {
expect.assertions(1)
const operator: Operator<string> = pipe(
const test: Operator<string> = pipe(
map(({ value }) => value.toUpperCase()),
action(({ value, state }) => (state.foo = value))
)
const test: Action<string> = fromOperator(operator)

const state = {
foo: 'bar',
Expand Down Expand Up @@ -51,13 +49,11 @@ describe.only('OPERATORS', () => {

test('map (async)', () => {
expect.assertions(1)
const operator: Operator<string> = pipe(
const test: Operator<string> = pipe(
map(({ value }) => Promise.resolve(value.toUpperCase())),
action(({ value, state }) => (state.foo = value))
)

const test: Action<string> = fromOperator(operator)

const state = {
foo: 'bar',
}
Expand Down Expand Up @@ -85,13 +81,12 @@ describe.only('OPERATORS', () => {
test('forEach', () => {
expect.assertions(1)
let runCount = 0
const operator: Operator<string[]> = pipe(
const test: Operator<string[]> = pipe(
forEach((_, val, next) => {
runCount++
next(null, val)
})
)
const test: Action<string[]> = fromOperator(operator)

const config = {
actions: {
Expand All @@ -115,7 +110,7 @@ describe.only('OPERATORS', () => {
test('parallel', () => {
expect.assertions(1)
let runCount = 0
const operator: Operator<string> = pipe(
const test: Operator<string> = pipe(
parallel(
(_, value, next) => {
runCount++
Expand All @@ -127,7 +122,6 @@ describe.only('OPERATORS', () => {
}
)
)
const test: Action<string> = fromOperator(operator)

const config = {
actions: {
Expand All @@ -150,12 +144,11 @@ describe.only('OPERATORS', () => {

test('filter - truthy', () => {
expect.assertions(1)
const operator: Operator<string> = pipe(
const test: Operator<string> = pipe(
filter(({ value }) => value === 'foo'),
map(({ value }) => value.toUpperCase()),
action(({ value, state }) => (state.foo = value))
)
const test: Action<string> = fromOperator(operator)

const state = {
foo: 'bar',
Expand All @@ -181,12 +174,11 @@ describe.only('OPERATORS', () => {
})

test('filter - falsy', () => {
const operator: Operator<string> = pipe(
const test: Operator<string> = pipe(
filter(({ value }) => value === 'bar'),
map(({ value }) => value.toUpperCase()),
action(({ value, state }) => (state.foo = value))
)
const test: Action<string> = fromOperator(operator)

const state = {
foo: 'bar',
Expand All @@ -213,15 +205,14 @@ describe.only('OPERATORS', () => {

test('fork', () => {
expect.assertions(1)
const operator: Operator<string> = pipe(
const test: Operator<string> = pipe(
fork(() => 'foo', {
foo: pipe(
map(({ value }) => value.toUpperCase()),
action(({ value, state }) => (state.foo = value))
),
})
)
const test: Action<string> = fromOperator(operator)

const state = {
foo: 'bar',
Expand All @@ -248,7 +239,7 @@ describe.only('OPERATORS', () => {

test('when', () => {
expect.assertions(1)
const operator: Operator<string, string | number> = pipe(
const test: Operator<string, string | number> = pipe(
when(() => true, {
true: pipe(
map(({ value }) => value.toUpperCase()),
Expand All @@ -260,7 +251,6 @@ describe.only('OPERATORS', () => {
),
})
)
const test: Action<string> = fromOperator(operator)

const state = {
foo: 'bar',
Expand Down Expand Up @@ -289,8 +279,7 @@ describe.only('OPERATORS', () => {
test('wait', () => {
expect.assertions(1)
const runTime = Date.now()
const operator: Operator = wait(500)
const test: Action = fromOperator(operator)
const test: Operator = wait(500)

const config = {
actions: {
Expand All @@ -313,11 +302,10 @@ describe.only('OPERATORS', () => {

test('debounce', () => {
expect.assertions(1)
const operator: Operator = pipe(
const test: Operator = pipe(
debounce(100),
action(({ state }) => state.runCount++)
)
const test: Action = fromOperator(operator)
const state = {
runCount: 0,
}
Expand Down
8 changes: 3 additions & 5 deletions packages/overmind-website/examples/api/operators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ export default (ts) =>
code: `
import { Operator, action } from 'overmind'
export const changeFoo: Action = fromOperator(
action(({ state }) => {
state.foo = 'bar'
})
)
export const changeFoo: Operator = action(({ state }) => {
state.foo = 'bar'
})
`,
},
]
Expand Down
28 changes: 12 additions & 16 deletions packages/overmind-website/examples/api/operators_pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,38 @@ export default (ts) =>
? [
{
code: `
import { Action, Operator, fromOperator, pipe, debounce } from 'overmind'
import { Action, Operator, pipe, debounce } from 'overmind'
import { QueryResult } from './state'
import {
setQuery,
filterValidQuery,
queryResult
} from './operators'
export const search: Action<string> = fromOperator(
pipe(
setQuery,
filterValidQuery,
debounce(200),
queryResult
)
export const search: Action<string> = pipe(
setQuery,
filterValidQuery,
debounce(200),
queryResult
)
`,
},
]
: [
{
code: `
import { fromOperator, pipe, debounce } from 'overmind'
import { pipe, debounce } from 'overmind'
import {
setQuery,
filterValidQuery,
queryResult
} from './operators'
export const search = fromOperator(
pipe(
setQuery,
filterValidQuery,
debounce(200),
queryResult
)
export const search = pipe(
setQuery,
filterValidQuery,
debounce(200),
queryResult
)
`,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,31 @@ export default (ts) =>
{
fileName: 'overmind/actions.ts',
code: `
import { Action, fromOperator, action } from 'overmind'
import { Action, Operator, action } from 'overmind'
export const plainAction: Action = ({ value, state }) => {
}
export const functionlAction: Action = fromOperator(
action(({ value, state }) => {
export const functionlAction: Operator = action(({ value, state }) => {
})
)
})
`,
},
]
: [
{
fileName: 'overmind/actions.js',
code: `
import { fromOperator, action } from 'overmind'
import { action } from 'overmind'
export const plainAction = ({ value, state }) => {
}
export const functionlAction = fromOperator(
action(({ value, state }) => {
export const functionlAction = action(({ value, state }) => {
})
)
})
`,
},
]
Loading

0 comments on commit 0370e12

Please sign in to comment.