Skip to content

Instantly share code, notes, and snippets.

View mfbx9da4's full-sized avatar

David Alberto Adler mfbx9da4

View GitHub Profile
const wait = () => new Promise((r) => setTimeout(r, 10));
const fakeDbOp = async () => {
console.log("fakeDbOp");
await wait(10);
return 1;
};
const fakeTrxDbOp = async () => {
console.log("fakeTrxDbOp");
@mfbx9da4
mfbx9da4 / machine.js
Created July 12, 2021 09:12
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
export const isMainThread = () => typeof window !== 'undefined' && typeof importScripts === 'function'
import isNil from 'lodash/isNil'
import { createPubSub } from 'https://gist.githubusercontent.com/mfbx9da4/896c0fa0439da9f87aab1883ec586b30/raw/808023dd8a1c014ebe81ff4d17d2748fddf8dc62/createPubSub.ts'
import { uuid } from 'short-uuid'
import { isMainThread } from 'https://gist.githubusercontent.com/mfbx9da4/de9e6a398d1847a0d11e5a20256bd0d9/raw/f8979aa8b241e778996cfda71398b42af0994202/isMainThread.ts'
import { createDeferredPromise } from 'https://gist.githubusercontent.com/mfbx9da4/76baa3051bbd044de18d1c1ac0c254dd/raw/853e5169f69a0c1cefe12816ff1ba25b36c1d366/createDeferredPromise.ts'
/**
* The goal of this module is to keep track of the navigation history
* in a single page app. It monitors `push()`, `back()`, `forward()` and
* `replace()` navigation events. It offers a number of features that
* the native history API does not support:
@mfbx9da4
mfbx9da4 / 1. observableLocalStorage.ts
Last active May 24, 2021 13:57
A wrapper around the local storage API which adds react hooks so that components can reactively update in response to storage changes.
import { createObservable as createObservableInner } from 'onin-shared/build/createObservable'
import { useEffect, useState } from 'react'
import { isMainThread } from './isMainThread'
export type Key = string
export type Value = string | undefined
const createObservable = (x: Value) => createObservableInner(x)
type Observable = ReturnType<typeof createObservable>
const Observables = {
class VersionedMap {
constructor(iterableOrMap, version = 0) {
this.map =
iterableOrMap instanceof Map ? iterableOrMap : new Map(iterableOrMap);
this.version = version;
this.mutated = false;
}
set(key, value) {
if (this.map.get(key) !== value) {
class Foo {
public a string
public b number
}
function getDefault(fieldAsString: 'a' | 'b') {
if (field === 'a') return 'asdf'
if (field === 'b') return 3
}

Javascript is single threaded but it is not by any means immue to race conditions. Below is a swiss army knife of ways to deal with race conditions.

Single process

  • In memory queues
  • Mutexes

Multi process

  • UPDATE commands
  • Queues
@mfbx9da4
mfbx9da4 / fileLock.js
Created January 28, 2021 15:24
Acquire a lock across processes using the file system
const fs = require("fs");
const acquireLock = async (identifier, retries = 5, delay = 1000) => {
const pathname = `${identifier}.lock`;
const acquire = () => {
try {
fs.mkdirSync(pathname);
return true;
} catch {}
};
@mfbx9da4
mfbx9da4 / machine.js
Created December 31, 2020 17:55
Generated by XState Viz: https://xstate.js.org/viz
Machine({
id: 'Scroll view',
initial: 'chat_closed',
context: {
dog: null
},
states: {
chat_closed: {
on: {
open: 'chat_open'