-
Notifications
You must be signed in to change notification settings - Fork 15
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
feat(concurrentConcatAll): Add custom operator for managing multiple concurrent inner subscriptions but maintaining the output order #26
Open
zakhenry
wants to merge
1
commit into
master
Choose a base branch
from
feat/concurrent-concat-all
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
projects/observable-webworker/src/lib/concurrent-concat-all.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { mergeAll } from 'rxjs/operators'; | ||
import { TestScheduler } from 'rxjs/testing'; | ||
import { concurrentConcatAll } from './concurrent-concat-all'; | ||
|
||
const createScheduler = () => { | ||
return new TestScheduler((actual, expected) => { | ||
// asserting the two objects are equal | ||
// e.g. using chai. | ||
expect(actual).toEqual(expected); | ||
}); | ||
}; | ||
|
||
// Don't reformat the carefully spaced marble diagrams | ||
// prettier-ignore | ||
fdescribe('concurrentConcatAll', () => { | ||
it('generate the stream correctly', () => { | ||
const testScheduler = createScheduler(); | ||
testScheduler.run(helpers => { | ||
const { cold, hot, expectObservable } = helpers; | ||
const x = cold( '--a---b--c---d--| '); | ||
const y = cold( '----e---f--g---|'); | ||
const e1 = hot( '--x------y-------| ', { x, y }); | ||
const expected = '----a---b--c-e-d-f--g---|'; | ||
|
||
expectObservable(e1.pipe(mergeAll())).toBe(expected); | ||
}); | ||
}); | ||
|
||
it('concatenates output of inner observables', () => { | ||
|
||
const testScheduler = createScheduler(); | ||
|
||
testScheduler.run(helpers => { | ||
const { cold, hot, expectObservable } = helpers; | ||
const x = cold( '--a---b--c---d------|'); | ||
const y = cold( '----e---f--g---|'); | ||
const e1 = hot( '--x---------------y--------------|', { x, y }); | ||
const expected = '----a---b--c---d------e---f--g---|'; | ||
|
||
expectObservable(e1.pipe(concurrentConcatAll())).toBe(expected); | ||
}); | ||
}); | ||
|
||
it('appends inner observables that completed first but sequenced after', () => { | ||
|
||
const testScheduler = createScheduler(); | ||
|
||
testScheduler.run(helpers => { | ||
const { cold, hot, expectObservable } = helpers; | ||
const x = cold( '--a---b--c---d--|'); | ||
const y = cold( '----e---f--g---|'); | ||
const e1 = hot( '--xy--------------------|', { x, y }); | ||
const expected = '----a---b--c---d--(efg)-|'; | ||
|
||
expectObservable(e1.pipe(concurrentConcatAll())).toBe(expected); | ||
}); | ||
}); | ||
|
||
it('appends inner observables overlap other observables', () => { | ||
|
||
const testScheduler = createScheduler(); | ||
|
||
testScheduler.run(helpers => { | ||
const { cold, hot, expectObservable } = helpers; | ||
const x = cold( '--a---b--c---d--|'); | ||
const y = cold( '----e---f--------g---|'); | ||
const e1 = hot( '--x------y--------------------|', { x, y }); | ||
const expected = '----a---b--c---d--(ef)----g---|'; | ||
|
||
expectObservable(e1.pipe(concurrentConcatAll())).toBe(expected); | ||
}); | ||
}); | ||
|
||
it('buffers completed inner observables', () => { | ||
|
||
const testScheduler = createScheduler(); | ||
|
||
testScheduler.run(helpers => { | ||
const { cold, hot, expectObservable } = helpers; | ||
const x = cold( '----------a---b--c---d--|'); | ||
const y = cold( '-e--f--g-|'); | ||
const e1 = hot( '--xy----------------------------|', { x, y }); | ||
const expected = '------------a---b--c---d--(efg)-|'; | ||
|
||
expectObservable(e1.pipe(concurrentConcatAll())).toBe(expected); | ||
}); | ||
}); | ||
|
||
it('passes on errors asap from inner observables', () => { | ||
|
||
const testScheduler = createScheduler(); | ||
|
||
testScheduler.run(helpers => { | ||
const { cold, hot, expectObservable } = helpers; | ||
const x = cold( '------------a---b--c---d--|'); | ||
const y = cold( '-e--f--g-#'); | ||
const e1 = hot( '--xy----------------------------|', { x, y }); | ||
const expected = '------------#'; | ||
|
||
expectObservable(e1.pipe(concurrentConcatAll())).toBe(expected); | ||
}); | ||
}); | ||
|
||
it('passes on errors asap from outer observables', () => { | ||
|
||
const testScheduler = createScheduler(); | ||
|
||
testScheduler.run(helpers => { | ||
const { cold, hot, expectObservable } = helpers; | ||
const x = cold( '------------a---b--c---d--|'); | ||
const y = cold( '-e--f--g-#'); | ||
const e1 = hot( '--xy----#------------------------|', { x, y }); | ||
const expected = '--------#'; | ||
|
||
expectObservable(e1.pipe(concurrentConcatAll())).toBe(expected); | ||
}); | ||
}); | ||
}); |
58 changes: 58 additions & 0 deletions
58
projects/observable-webworker/src/lib/concurrent-concat-all.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { NEVER, Observable } from 'rxjs'; | ||
import { catchError, finalize, map, mergeMap } from 'rxjs/operators'; | ||
|
||
function concurrentConcatAllOperator<T>(input$: Observable<Observable<T>>): Observable<T> { | ||
const buffer: Map<number, T[]> = new Map(); | ||
let completedBuffers: number[] = []; | ||
let currentIndex = 0; | ||
|
||
return new Observable<T>(observer => { | ||
const sub = input$ | ||
.pipe( | ||
mergeMap((inner$: Observable<T>, index: number) => { | ||
return inner$.pipe( | ||
map(v => { | ||
if (currentIndex === index) { | ||
observer.next(v); | ||
} else { | ||
if (!buffer.has(index)) { | ||
buffer.set(index, []); | ||
} | ||
buffer.get(index).push(v); | ||
} | ||
}), | ||
finalize(() => { | ||
if (currentIndex === index) { | ||
completedBuffers.sort().forEach(bufferIndex => { | ||
buffer.get(bufferIndex).forEach(v => observer.next(v)); | ||
buffer.delete(bufferIndex); | ||
}); | ||
|
||
currentIndex = completedBuffers.length ? completedBuffers.pop() + 1 : currentIndex + 1; | ||
if (buffer.has(currentIndex)) { | ||
buffer.get(currentIndex).forEach(v => observer.next(v)); | ||
} | ||
completedBuffers = []; | ||
} else { | ||
completedBuffers.push(index); | ||
} | ||
}), | ||
catchError(e => { | ||
observer.error(e); | ||
return NEVER; | ||
}), | ||
); | ||
}), | ||
) | ||
.subscribe({ | ||
error: e => observer.error(e), | ||
complete: () => observer.complete(), | ||
}); | ||
|
||
return () => sub.unsubscribe(); | ||
}); | ||
} | ||
|
||
export function concurrentConcatAll<T>(): (input$: Observable<Observable<T>>) => Observable<T> { | ||
return concurrentConcatAllOperator; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Observable } from 'rxjs'; | ||
import { fromWorkerPool, concurrentConcatAll } from 'observable-webworker'; | ||
|
||
export function computeHashes(files: File[]): Observable<string> { | ||
return fromWorkerPool<File, string>(() => new Worker('./transferable.worker', { type: 'module' }), files, { | ||
flattenOperator: concurrentConcatAll(), // <-- add this | ||
}); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to happen before merging