Skip to content

Instantly share code, notes, and snippets.

View armanozak's full-sized avatar

Levent Arman Özak armanozak

View GitHub Profile
@armanozak
armanozak / split-concat.ts
Created May 10, 2023 07:12
Split & Concat Types w/ Delimiter
/*
* Note: This is just for fun.
*/
type Split<T extends string, Delimiter extends string = ""> =
"" extends T
? []
: T extends `${infer Head}${Delimiter}${infer Tail}`
? [Head, ...Split<Tail, Delimiter>]
: [T];
@armanozak
armanozak / rxjs-7_connectable-resetOnDisconnect.ts
Created May 4, 2021 11:13
[What's New in RxJS 7] RxJS 7 connectable resetOnDisconnect #blog #rxjs
import { connectable, interval, merge, of, Subject, zip } from "rxjs";
import { filter, map } from "rxjs/operators";
const chars$ = zip(interval(1000), of("A", "b", "C", "D", "e", "f", "G")).pipe(
map(([, char]) => char)
);
const connectableChars$ = connectable(chars$, {
connector: () => new Subject(),
resetOnDisconnect: true
});
@armanozak
armanozak / rxjs-7_custom-error-call-stack.ts
Created May 3, 2021 09:22
[What's New in RxJS 7] Custom errors get call stack back in RxJS 7 #blog #rxjs
import { EMPTY } from "rxjs";
import { first } from "rxjs/operators";
EMPTY.pipe(first()).subscribe({ error: console.warn });
// (synchronously) EmptyErrorImpl
// v6.6.7 has no call stack, but v7 does
@armanozak
armanozak / rxjs-7_config-onUnhandledError.ts
Created May 3, 2021 09:22
[What's New in RxJS 7] RxJS 7 config.onUnhandledError #blog #rxjs
import { config, throwError } from "rxjs";
config.onUnhandledError = console.warn;
throwError(() => "TEST ERROR 1").subscribe();
throwError(() => "TEST ERROR 2").subscribe({ error: console.warn });
// (synchronously) TEST ERROR 2
// (asynchronously) TEST ERROR 1
@armanozak
armanozak / rxjs-7_animationFrames.ts
Created May 3, 2021 09:21
[What's New in RxJS 7] RxJS 7 animationFrames #blog #rxjs
import { animationFrames, combineLatest, concat } from "rxjs";
import { endWith, map, takeWhile } from "rxjs/operators";
const h1 = document.querySelector("h1")!;
combineLatest({
x: tween(0, 200, 3600),
y: wave(25, 1200, 3)
}).subscribe(({ x, y }) => {
h1.style.transform = `translate3d(${x}px, ${y}px, 0)`;
@armanozak
armanozak / rxjs-7_connectable.ts
Created May 3, 2021 09:20
[What's New in RxJS 7] RxJS 7 connectable #blog #rxjs
import { connectable, merge, of } from "rxjs";
import { filter, map } from "rxjs/operators";
const chars$ = of("A", "b", "C", "D", "e", "f", "G");
const connectableChars$ = connectable(chars$);
const lower$ = connectableChars$.pipe(
filter(x => x.toLowerCase() === x),
map(x => `lower ${x.toUpperCase()}`)
);
@armanozak
armanozak / rxjs-7_connect.ts
Created May 3, 2021 09:19
[What's New in RxJS 7] RxJS 7 connect #blog #rxjs
import { merge, of } from "rxjs";
import { connect, filter, map } from "rxjs/operators";
const chars$ = of("A", "b", "C", "D", "e", "f", "G");
chars$
.pipe(
connect(shared$ =>
merge(
shared$.pipe(
@armanozak
armanozak / rxjs-7_share.ts
Created May 3, 2021 09:19
[What's New in RxJS 7] RxJS 7 share #blog #rxjs
import { interval, of, ReplaySubject, zip } from "rxjs";
import { map, share } from "rxjs/operators";
const shared$ = zip(interval(1000), of("A", "B", "C", "D", "E")).pipe(
map(([, char]) => char),
share({
connector: () => new ReplaySubject(3),
resetOnComplete: false,
resetOnError: false,
resetOnRefCountZero: false
@armanozak
armanozak / rxjs-6_shareReplay.ts
Created May 3, 2021 09:18
[What's New in RxJS 7] RxJS 6 shareReplay #blog #rxjs
import { interval, of, zip } from "rxjs";
import { map, shareReplay } from "rxjs/operators";
const shared$ = zip(interval(1000), of("A", "B", "C", "D", "E")).pipe(
map(([, char]) => char),
shareReplay({ refCount: true, bufferSize: 3 })
);
shared$.subscribe(console.log);
// (~1s apart) A, B, C, D, E
@armanozak
armanozak / rxjs-7_retry-resetOnSuccess.ts
Created May 3, 2021 09:17
[What's New in RxJS 7] RxJS 7 retry resetOnSuccess #blog #rxjs
import { defer, from } from "rxjs";
import { retry, tap } from "rxjs/operators";
const values = ["_", 0, 1, 0, 2, 0, 3, 0, 0, 0, 4];
defer(() => {
values.shift();
return from(values);
})
.pipe(