Skip to content

Instantly share code, notes, and snippets.

@RWalkling
Created February 21, 2020 06:44
Show Gist options
  • Select an option

  • Save RWalkling/d99bee569bd853a2e5efc7e19bd1dd21 to your computer and use it in GitHub Desktop.

Select an option

Save RWalkling/d99bee569bd853a2e5efc7e19bd1dd21 to your computer and use it in GitHub Desktop.
rxjs utils
import { get } from 'lodash'
export interface EqualByProperties<TObject extends object> {
(): (object1: TObject, object2: TObject) => boolean
<TKey extends keyof TObject>(key: TKey): EqualByProperties<TObject>
<TKey1 extends keyof TObject, TKey2 extends keyof TObject[TKey1]>(
...key: readonly [TKey1, TKey2]
): EqualByProperties<TObject>
<TKey1 extends keyof TObject, TKey2 extends keyof TObject[TKey1], TKey3 extends keyof TObject[TKey1][TKey2]>(
...key: readonly [TKey1, TKey2, TKey3]
): EqualByProperties<TObject>
<
TKey1 extends keyof TObject,
TKey2 extends keyof TObject[TKey1],
TKey3 extends keyof TObject[TKey1][TKey2],
TKey4 extends keyof TObject[TKey1][TKey2][TKey3]
>(
...key: readonly [TKey1, TKey2, TKey3, TKey4]
): EqualByProperties<TObject>
}
const equalByProperties = (paths: ReadonlyArray<readonly PropertyKey[]> = []) => (...path: readonly PropertyKey[]) =>
(path.length === 0
? (object1: object, object2: object) => paths.every(path => get(object1, path) === get(object2, path))
: equalByProperties(paths.concat([path]))) as any
export default equalByProperties as <TObject extends object>() => EqualByProperties<TObject>
import { BehaviorSubject, Observable, pipe } from 'rxjs'
import { UnaryFunction } from 'rxjs/src/internal/types'
import { map } from 'rxjs/operators'
export default function pipeBehaviorSubject<TValue, TResult>(
subject: BehaviorSubject<TValue>,
pipe: UnaryFunction<Observable<TValue>, Observable<TResult>>,
) {
const piped = subject.pipe(pipe)
let initialValue: TResult
piped.subscribe(value => (initialValue = value)).unsubscribe()
// @ts-ignore
const outSubject = new BehaviorSubject(initialValue)
piped.subscribe(outSubject)
return outSubject
}
import { Subject } from 'rxjs'
export default function relay<TData>(next: (value: TData) => void): Subject<TData> {
const subject = new Subject<TData>()
subject.subscribe(next)
return subject
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment