Skip to content

Commit

Permalink
Merge branch 'feat/state-combinator' of github.com:motorcyclets/motor…
Browse files Browse the repository at this point in the history
…cycle into feat/state-combinator

Signed-off-by: Frederik Krautwald <fkrautwald@gmail.com>
  • Loading branch information
Frikki committed Oct 11, 2017
2 parents 07e6172 + a3132b6 commit 9212793
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
27 changes: 27 additions & 0 deletions packages/stream/src/combinators/state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,33 @@ export const test: Test = describe(`state`, [

return collectEvents(sut).then(equal(expected))
}),

it(`does not emit repeated values`, ({ equal }) => {
const a$ = mergeArray([at(0, 0), at(100, 100), at(200, 200)])
const b$ = mergeArray([at(1, 1), at(2, 0)])

const expected = [0, 1, 100, 101, 200, 201]
const sut = state(add, a$, b$)

return collectEvents(sut).then(equal(expected))
}),

it(`emits latest value to late subscribers`, ({ equal }, done) => {
const a$ = mergeArray([at(0, 0), at(500, 100), at(1000, 200)])
const b$ = mergeArray([at(150, 1), at(300, 2)])

const sut = state(add, a$, b$)

setTimeout(() => {
const expected = [100, 101, 103, 200, 201, 203]
collectEvents(sut)
.then(equal(expected))
.then(() => done())
.catch(done)
}, 550)

collectEvents(sut)
}),
]),
])

Expand Down
4 changes: 3 additions & 1 deletion packages/stream/src/combinators/state.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Stream } from '@motorcycle/types'
import { curry3 } from '167'
import { hold } from './hold'
import { scan } from './scan'
import { skipRepeats } from './skipRepeats'
import { switchMap } from './switchMap'

/**
Expand Down Expand Up @@ -40,7 +42,7 @@ function __state<A, B>(
seed$: Stream<B>,
values$: Stream<A>
): Stream<B> {
return switchMap(seed => scan(f, seed, values$), seed$)
return hold(skipRepeats(switchMap(seed => scan(f, seed, values$), seed$)))
}

export interface State {
Expand Down

0 comments on commit 9212793

Please sign in to comment.