Skip to content

Instantly share code, notes, and snippets.

@cartant
Created December 12, 2020 00:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cartant/2755d2b3f85fa650b617e6cabdc823b3 to your computer and use it in GitHub Desktop.
Save cartant/2755d2b3f85fa650b617e6cabdc823b3 to your computer and use it in GitHub Desktop.
export const windowBy = <K, T>(
keyFn: (t: T) => K
): OperatorFunction<T, GroupedObservable<K, T>> => (source) => source.pipe(
publish((sharedSource) => {
const newWindow = sharedSource.pipe(
distinctUntilChanged((a, b) => keyFn(a) === keyFn(b))
);
return sharedSource.pipe(
groupBy(keyFn, undefined, () =>
newWindow.pipe(
// Skip the first window as that is what created the current group.
skip(1)
)
)
);
})
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment