Skip to content

Instantly share code, notes, and snippets.

@codenickycode
codenickycode / rxjs-observable-operator-pipeline.md
Created February 6, 2023 22:36
[RxJS: Observable Operator Pipeline] Create an observer with an operator pipeline #rxjs #observable #angular

Angular Docs: Rx Library | Operators

import { of } from 'rxjs';
import { filter, map } from 'rxjs/operators';

const squareOdd = of(1, 2, 3, 4, 5)
  .pipe(
    filter(n => n % 2 !== 0),
 map(n => n * n)
@codenickycode
codenickycode / rxjs-observable-vs-promise.md
Last active February 6, 2023 22:58
[RxJS: Observable vs Promise] Observable vs. Promise Cheat Sheet #rxjs #observable #promise #angular

https://v12.angular.io/guide/comparing-observables#cheat-sheet

OPERATION OBSERVABLE PROMISE
Creation new Observable((observer) => { observer.next(123); }); new Promise((resolve, reject) => { resolve(123); });
Transform obs.pipe(map((value) => value * 2)); promise.then((value) => value * 2);
Subscribe sub = obs.subscribe((value) => { console.log(value) }); promise.then((value) => { console.log(value); });
Unsubscribe sub.unsubscribe(); Implied by promise resolution.
@codenickycode
codenickycode / rxjs-higher-order-maps.md
Last active February 10, 2023 14:32
[RxJS: Higher-Order Maps] Use cases for most common higher-order map operators #rxjs #angular #operators

From Reactive Patterns for RxJS with Angular Chapter 7: Transforming Streams

Operator Use Case
concatMap If the order is important and you need to process operations in sequence while waiting for completion.
mergeMap If the order is not important and you need to process operations in parallel to enhance performance.
switchMap If you need to put a cancellation logic to release resources and take always the most recent information.
exhaustMap To ignore new observables while the current one is still ongoing.
@codenickycode
codenickycode / deferredPromise.ts
Last active April 19, 2023 21:57
[TS: Deferred Promise] Create a promise and pass along its res and rej #typescript #javascript #promise
// Andrew Burgess, YouTube
// https://www.youtube.com/watch?v=Yvhad4zdPqI
export class Deferred<T, E = unknown> {
promise: Promise<T>;
resolve: (value: T) => void = () => null;
reject: (reason?: E) => void = () => null;
constructor() {
this.promise = new Promise({resolve, reject} => {
@codenickycode
codenickycode / pyenv.sh
Created June 12, 2023 17:21
[Managing Python Env] Install pyenv and setup shell to manage versions #python #env
# Install pyenv, to manage Python versions
brew update
brew install pyenv
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc
# Install the supported version of Python, as defined in .python-version
pyenv install
@codenickycode
codenickycode / dump-repo.sh
Last active July 13, 2024 17:34
[dump-repo] Dump the contents of an entire git repo into a text file at the root #bash #git
# A really insane command that dumps all of the text content of a directory into
# a single pathname-delimited text file. I use this to share my repo with claude,
# in lieu of a co-pilot.
# ⚠️ WARNING: You should definitely add repo.txt to your .gitignore !!
alias dump-repo='
# 2>/dev/null suppresses error messages if not in a Git repo
git_root=$(git rev-parse --show-toplevel 2>/dev/null)
if [ -z "$git_root" ]; then
echo "Not in a git repository"
@codenickycode
codenickycode / cache-control-remix.tsx
Last active October 25, 2024 00:19
Cache-Control header
import { json } from "@remix-run/node";
import type { LoaderFunction } from "@remix-run/node";
// For session-specific data
export const sessionLoader: LoaderFunction = async ({ request }) => {
const data = await fetchSessionData();
return json(data, {
headers: {
"Cache-Control": "private, max-age=3600", // Cache for 1 hour for this user only