Skip to content

Commit 91b0929

Browse files
docs(website): update to latest api changes
1 parent f10818e commit 91b0929

38 files changed

+5230
-5278
lines changed

package-lock.json

Lines changed: 4596 additions & 4563 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import {
99
import {
1010
forkEachMessage,
1111
ensureCurrentApp,
12-
addMessagesFromClient,
13-
createNewApp,
1412
addFlushAndRunMutations,
1513
updateComponent,
1614
addComponent,
@@ -26,6 +24,10 @@ import {
2624
addEffect,
2725
setPortExists,
2826
isPortExistsMessage,
27+
getMessages,
28+
addState,
29+
ensureApp,
30+
addClientMessages,
2931
} from './operators'
3032

3133
export const onInitialize: OnInitialize = async ({
@@ -44,9 +46,12 @@ export const onInitialize: OnInitialize = async ({
4446

4547
const handleClientMessage: Pipe<Message> = pipe(
4648
ensureCurrentApp,
49+
ensureApp,
50+
addClientMessages,
51+
getMessages,
4752
forkEachMessage({
4853
[AppMessageType.PORT_EXISTS]: setPortExists,
49-
[ExecutionType.INIT]: createNewApp,
54+
[ExecutionType.INIT]: addState,
5055
[ExecutionType.FLUSH]: addFlushAndRunMutations,
5156
[ExecutionType.DERIVED]: updateDerived,
5257
[ExecutionType.MUTATIONS]: addMutations,
@@ -60,8 +65,7 @@ const handleClientMessage: Pipe<Message> = pipe(
6065
[ExecutionType.OPERATOR_START]: addOperator,
6166
[ExecutionType.OPERATOR_END]: updateOperator,
6267
[ExecutionType.ACTION_END]: updateAction,
63-
}),
64-
addMessagesFromClient
68+
})
6569
)
6670

6771
export const onMessage: Pipe<Message> = pipe(

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

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { mutate, forEach, fork, Operator, when } from 'overmind'
1+
import { mutate, forEach, fork, Operator, when, map } from 'overmind'
22
import {
33
Message,
44
AppMessage,
@@ -47,12 +47,9 @@ export const isPortExistsMessage = (paths: {
4747
paths
4848
)
4949

50-
export const createNewApp = mutate<InitMessage>(({ value: message, state }) => {
50+
export const addState = mutate<InitMessage>(({ value: message, state }) => {
5151
state.isConnecting = false
52-
state.apps[message.appName] = createApp({
53-
name: message.appName,
54-
state: message.data.state,
55-
})
52+
state.apps[message.appName].state = message.data.state
5653
})
5754

5855
export const addFlushAndRunMutations = mutate<FlushMessage>(
@@ -79,9 +76,18 @@ export const addFlushAndRunMutations = mutate<FlushMessage>(
7976
}
8077
)
8178

82-
export const addMessagesFromClient = mutate<Message>(
79+
export const ensureApp = mutate<Message>(({ value: message, state }) => {
80+
if (!state.apps[message.appName]) {
81+
state.apps[message.appName] = createApp({
82+
name: message.appName,
83+
})
84+
}
85+
})
86+
87+
export const addClientMessages = mutate<Message>(
8388
({ value: message, state }) => {
8489
state.apps[message.appName].messages = message.messages
90+
.slice()
8591
.reverse()
8692
.concat(state.apps[message.appName].messages)
8793
}
@@ -285,11 +291,10 @@ export const addEffect = mutate<EffectMessage>(({ value: message, state }) => {
285291
operator.effects.push(effect)
286292
})
287293

294+
export const getMessages = map<Message, AppMessage<any>[]>(({ value }) =>
295+
value.messages.map((message) => ({ ...message, appName: value.appName }))
296+
)
297+
288298
export const forkEachMessage = (paths: {
289299
[key: string]: Operator<AppMessage<any>>
290-
}) =>
291-
forEach<Message, AppMessage<any>[]>(
292-
({ value }) =>
293-
value.messages.map((message) => ({ ...message, appName: value.appName })),
294-
fork(({ value }) => value.type, paths)
295-
)
300+
}) => forEach<AppMessage<any>[]>(fork(({ value }) => value.type, paths))

packages/node_modules/overmind/src/index.ts

Lines changed: 92 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -456,52 +456,73 @@ export class Overmind<Config extends Configuration> implements BaseApp {
456456

457457
export type Operator<C, RC = C> = TOperator<Context<C>, Context<RC>>
458458

459-
export function pipe<A, AR = A>(aOperator: Operator<A, AR>): Operator<A, AR>
460-
461-
export function pipe<A, AR = A, B = AR, BR = B>(
462-
aOperator: Operator<A, AR>,
463-
bOperator: Operator<AR, B>
464-
): Operator<A, BR>
465-
466-
export function pipe<A, AR = A, B = AR, BR = B, C = BR, CR = C>(
467-
aOperator: Operator<A, AR>,
468-
bOperator: Operator<AR, B>,
469-
cOperator: Operator<BR, C>
470-
): Operator<A, CR>
471-
472-
export function pipe<A, AR = A, B = AR, BR = B, C = BR, CR = C, D = CR, DR = D>(
473-
aOperator: Operator<A, AR>,
474-
bOperator: Operator<AR, B>,
475-
cOperator: Operator<BR, C>,
476-
dOperator: Operator<CR, D>
477-
): Operator<A, DR>
459+
export function pipe<A, B>(aOperator: Operator<A, B>): Operator<A, B>
460+
461+
export function pipe<A, B, C>(
462+
aOperator: Operator<A, B>,
463+
bOperator: Operator<B, C>
464+
): Operator<A, C>
465+
466+
export function pipe<A, B, C, D>(
467+
aOperator: Operator<A, B>,
468+
bOperator: Operator<B, C>,
469+
cOperator: Operator<C, D>
470+
): Operator<A, D>
471+
472+
export function pipe<A, B, C, D, E>(
473+
aOperator: Operator<A, B>,
474+
bOperator: Operator<B, C>,
475+
cOperator: Operator<C, D>,
476+
dOperator: Operator<D, E>
477+
): Operator<A, E>
478+
479+
export function pipe<A, B, C, D, E, F>(
480+
aOperator: Operator<A, B>,
481+
bOperator: Operator<B, C>,
482+
cOperator: Operator<C, D>,
483+
dOperator: Operator<D, E>,
484+
eOperator: Operator<E, F>
485+
): Operator<A, F>
486+
487+
export function pipe<A, B, C, D, E, F, G>(
488+
aOperator: Operator<A, B>,
489+
bOperator: Operator<B, C>,
490+
cOperator: Operator<C, D>,
491+
dOperator: Operator<D, E>,
492+
eOperator: Operator<E, F>,
493+
fOperator: Operator<F, G>
494+
): Operator<A, G>
478495

479496
export function pipe(...operators) {
480-
const instance = (err, val, next, final?) => {
481-
if (err) return next(err)
482-
const operatorsCopy = operators.slice()
483-
function runNext(err, val?) {
484-
const nextOperator = operatorsCopy.shift()
485-
if (nextOperator) {
486-
try {
487-
if (val instanceof Promise) {
488-
val
489-
.then((promiseVal) =>
490-
nextOperator(err, promiseVal, runNext, final || next)
491-
)
492-
.catch((promiseErr) =>
493-
nextOperator(promiseErr, val, runNext, final || next)
494-
)
495-
} else {
496-
nextOperator(err, val, runNext, final || next)
497+
const instance = (err, context, next, final = next) => {
498+
if (err) next(err)
499+
else {
500+
let operatorIndex = 0
501+
const run = (runErr, runContext) =>
502+
operators[operatorIndex++](runErr, runContext, runNextOperator, final)
503+
504+
const runNextOperator = (operatorError, operatorContext) => {
505+
if (operatorError) return next(operatorError)
506+
if (operatorIndex >= operators.length)
507+
return next(null, operatorContext)
508+
509+
if (operatorContext.value instanceof Promise) {
510+
operatorContext.value
511+
.then((promiseValue) =>
512+
run(null, { ...operatorContext, value: promiseValue })
513+
)
514+
.catch((promiseError) => next(promiseError, operatorContext))
515+
} else {
516+
try {
517+
run(null, operatorContext)
518+
} catch (operatorError) {
519+
next(operatorError, operatorContext)
497520
}
498-
} catch (err) {
499-
runNext(err)
500521
}
501-
} else if (err) next(err)
502-
else next(null, val)
522+
}
523+
524+
runNextOperator(null, context)
503525
}
504-
runNext(null, val)
505526
}
506527
instance[IS_PIPE] = true
507528
return instance
@@ -555,18 +576,17 @@ export const map = <Input, Output>(
555576
}
556577
}
557578

558-
export const forEach = <Input, Output extends any[]>(
559-
operation: (input: Context<Input>) => Output,
560-
forEachItemOperator: Operator<Output[0]>
579+
export const forEach = <Input extends any[]>(
580+
forEachItemOperator: Operator<Input[0]>
561581
): Operator<Input> => (err, context, next) => {
562582
if (err) next(err)
563583
else {
564584
const stopDebugOperator = startDebugOperator(
565585
'forEach',
566-
operation.name,
586+
'',
567587
context.execution
568588
)
569-
let array = operation(context)
589+
let array = context.value
570590
let evaluatingCount = array.length
571591

572592
let hasErrored = false
@@ -650,3 +670,29 @@ export function when<Input, Output = Input>(
650670
else paths.false(null, context, next)
651671
}
652672
}
673+
674+
export function wait<Input>(ms: number): Operator<Input> {
675+
return (err, context, next) => {
676+
if (err) next(err)
677+
else setTimeout(() => next(null, context), ms)
678+
}
679+
}
680+
681+
export function debounce<Input>(ms: number): Operator<Input> {
682+
let timeout
683+
let previousFinal
684+
return (err, value, next, final) => {
685+
if (err) {
686+
return next(err)
687+
}
688+
if (timeout) {
689+
clearTimeout(timeout)
690+
previousFinal(null, value)
691+
}
692+
previousFinal = final
693+
timeout = setTimeout(() => {
694+
timeout = null
695+
next(null, value)
696+
}, ms)
697+
}
698+
}

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

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
import { pipe, map, forEach, filter, fork, when, Pipe } from './'
1+
import {
2+
pipe,
3+
map,
4+
forEach,
5+
filter,
6+
fork,
7+
when,
8+
Pipe,
9+
wait,
10+
debounce,
11+
} from './'
212

313
function createMockAction(pipe) {
414
return (value) =>
@@ -49,7 +59,7 @@ describe('PIPE', () => {
4959
})
5060
test('map (async)', () => {
5161
expect.assertions(1)
52-
const test = pipe<string>(
62+
const test: Pipe<string> = pipe(
5363
map(({ value }) => Promise.resolve(value.toUpperCase()))
5464
)
5565

@@ -60,14 +70,11 @@ describe('PIPE', () => {
6070
test('forEach', () => {
6171
expect.assertions(1)
6272
let runCount = 0
63-
const test = pipe<string[]>(
64-
forEach(
65-
({ value }) => value,
66-
(_, val, next) => {
67-
runCount++
68-
next(null, val)
69-
}
70-
)
73+
const test: Pipe<string[]> = pipe(
74+
forEach((_, val, next) => {
75+
runCount++
76+
next(null, val)
77+
})
7178
)
7279

7380
return createMockAction(test)(['foo']).then(() => {
@@ -76,7 +83,7 @@ describe('PIPE', () => {
7683
})
7784
test('filter - truthy', () => {
7885
expect.assertions(1)
79-
const test = pipe<string>(
86+
const test: Pipe<string> = pipe(
8087
filter(({ value }) => value === 'foo'),
8188
map(({ value }) => value.toUpperCase())
8289
)
@@ -86,7 +93,7 @@ describe('PIPE', () => {
8693
})
8794
})
8895
test('filter - falsy', () => {
89-
const test = pipe<string>(
96+
const test: Pipe<string> = pipe(
9097
filter(({ value }) => value === 'bar'),
9198
map(({ value }) => value.toUpperCase())
9299
)
@@ -97,7 +104,7 @@ describe('PIPE', () => {
97104
})
98105
test('fork', () => {
99106
expect.assertions(1)
100-
const test = pipe<string>(
107+
const test: Pipe<string> = pipe(
101108
fork(() => 'foo', {
102109
foo: map(({ value }) => value.toUpperCase()),
103110
})
@@ -109,7 +116,7 @@ describe('PIPE', () => {
109116
})
110117
test('when', () => {
111118
expect.assertions(1)
112-
const test = pipe<string>(
119+
const test: Pipe<string> = pipe(
113120
when(() => true, {
114121
true: map(({ value }) => value.toUpperCase()),
115122
false: map(({ value }) => value),
@@ -120,5 +127,24 @@ describe('PIPE', () => {
120127
expect(value).toBe('FOO')
121128
})
122129
})
130+
test('wait', () => {
131+
expect.assertions(1)
132+
const runTime = Date.now()
133+
const test: Pipe<string> = pipe(wait(500))
134+
return createMockAction(test)('foo').then(() => {
135+
expect(Date.now() - runTime).toBeGreaterThanOrEqual(500)
136+
})
137+
})
138+
test('debounce', () => {
139+
expect.assertions(1)
140+
const test: Pipe<string> = pipe(
141+
debounce(100),
142+
map(({ value }) => value.toUpperCase())
143+
)
144+
const action = createMockAction(test)
145+
return Promise.all([action('foo'), action('foo')]).then((value) => {
146+
expect(value).toEqual(['foo', 'FOO'])
147+
})
148+
})
123149
})
124150
})

0 commit comments

Comments
 (0)