Skip to content

Instantly share code, notes, and snippets.

View cartant's full-sized avatar

Nicholas Jamieson cartant

View GitHub Profile
import { expect } from "chai";
import { produce } from "immer";
const current = [42];
const next = produce(current, draft => { draft.push(54); });
expect(current).to.deep.equal([42]);
expect(next).to.deep.equal([42, 54]);
import { createAction, createReducer, props } from "@ngrx/store";
import { on } from "ts-action-immer";
const add = createAction("[Todos] Add", props<{ text: string }>());
const reset = createAction("[Todos] Reset");
type Todo = {
done: boolean;
text: string;
};
import { createAction, createReducer, on, props } from "@ngrx/store";
const add = createAction("[Todos] Add", props<{ text: string }>());
const reset = createAction("[Todos] Reset");
type Todo = {
done: boolean;
text: string;
};
const initialState = {
call(subscriber: Subscriber<R>, source: Observable<any>) {
const { selector } = this;
const subject = this.subjectFactory();
const subscription = selector(subject).subscribe(subscriber);
subscription.add(source.subscribe(subject));
return subscription;
}
import { concat, OperatorFunction, race, SchedulerLike, timer } from "rxjs";
import { mapTo, publish } from "rxjs/operators";
function startWithTimeout<T, S = T>(
value: S,
duration: number | Date, scheduler?: SchedulerLike
): OperatorFunction<T, S | T> {
return source => source.pipe(publish(published => race(
published,
concat(timer(duration, scheduler).pipe(mapTo(value)), published)
import { concat, of, race, timer } from "rxjs";
import { delay, mapTo, publish } from "rxjs/operators";
const source = of(42).pipe(delay(100));
const composed = source.pipe(
publish(published => race(
published,
concat(timer(10).pipe(mapTo(54)), published)
))
);
import { concat, ConnectableObservable, of, race, timer } from "rxjs";
import { delay, mapTo, publish } from "rxjs/operators";
const source = of(42).pipe(delay(100));
const published = source.pipe(
publish()
) as ConnectableObservable<number>;
const composed = race(
published,
concat(timer(10).pipe(mapTo(54)), published)
interface ShareReplayConfig {
bufferSize?: number;
windowTime?: number;
refCount: boolean;
scheduler?: SchedulerLike;
}
function shareReplay<T>(config: ShareReplayConfig): MonoTypeOperatorFunction<T>;
import { interval, timer } from "rxjs";
import { shareReplay, takeUntil } from "rxjs/operators";
import { log } from "./log";
const source = log(interval(100), "source");
const shared = log(source.pipe(shareReplay({
bufferSize: 1,
refCount: true
})), "shared");
import { defer, Observable } from "rxjs";
import { finalize, tap } from "rxjs/operators";
export const log = <T>(source: Observable<T>, name: string) => defer(() => {
console.log(`${name}: subscribed`);
return source.pipe(
tap({
next: value => console.log(`${name}: ${value}`),
complete: () => console.log(`${name}: complete`)
}),