Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Demonstrate possible issue #37

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"standard": "^12.0.1",
"ts-node": "^8.3.0",
"typescript": "^3.5.2",
"typescript-language-server": "^0.3.8",
"uglify-js": "^3.1.1"
},
"dependencies": {
Expand Down
14 changes: 7 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Hold<A> extends MulticastSource<A> implements Stream<A>, Disposable, Sink<
private task?: ScheduledTask = undefined

run (sink: Sink<A>, scheduler: Scheduler): Disposable {
if (this._shouldScheduleFlush()) {
if (this._hasValue()) {
this._scheduleFlush(sink, scheduler)
}

Expand Down Expand Up @@ -59,13 +59,13 @@ class Hold<A> extends MulticastSource<A> implements Stream<A>, Disposable, Sink<
return this.held !== undefined
}

private _hasSinks (): boolean {
return this.sinks.length > 0
}
// private _hasSinks (): boolean {
// return this.sinks.length > 0
// }

private _shouldScheduleFlush (): boolean {
return this._hasValue() && this._hasSinks()
}
// private _shouldScheduleFlush (): boolean {
// return this._hasValue() && this._hasSinks()
// }

private _scheduleFlush (sink: Sink<A>, scheduler: Scheduler): void {
this.pendingSinks.push(sink)
Expand Down
36 changes: 34 additions & 2 deletions test/index-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import { hold } from '../src'
import { Scheduler, Sink, Stream, Time } from '@most/types'
import {
at,
combineArray,
mergeArray,
merge,
join,
map,
periodic,
runEffects,
scan,
switchLatest,
take,
tap,
propagateEventTask
Expand All @@ -25,7 +27,12 @@ const collect = <A>(stream: Stream<A>, scheduler: Scheduler): Promise<ReadonlyAr
.then(() => eventValues)
}

const verifyHold = <A>(f: (stream: Stream<number>, scheduler: Scheduler) => Promise<A>): Promise<[ReadonlyArray<number>, A]> => {
const verifyHold = <A>(
f: (
stream: Stream<number>,
scheduler: Scheduler
) => Promise<A>
): Promise<[ReadonlyArray<number>, A]> => {
const scheduler = newDefaultScheduler()
const s = hold(mergeArray([at(0, 0), at(10, 1), at(20, 2)]))

Expand Down Expand Up @@ -73,7 +80,7 @@ describe('hold', () => {
return asap(propagateEventTask('foo', sink), scheduler)
}
return {
dispose () {}
dispose () {} // asap(propagateEndTask(sink), scheduler)
}
}
}
Expand Down Expand Up @@ -101,6 +108,31 @@ describe('hold', () => {
it('should emit two events with hold for two observers', () => {
return test(hold(new Source()), ['foo', 'foo'])
})

it('shall pass', () => {
const hos: Stream<Stream<string>[]> = scan(
(acc: Stream<string>[], _s: void) => acc.concat([
hold(new Source())
]),
[],
take(2, periodic(3))
)

const flat: Stream<string> = take(2, switchLatest(
map(
arr => combineArray(
(...a: string[]) => a.join(),
arr
),
hos
)
))

const scheduler = newDefaultScheduler()
return collect(flat, scheduler).then(
events => eq(['foo', 'foo,foo'], events)
)
})
})

it(`should not propagate held event during the same tick as run`, () => {
Expand Down