Skip to content

Instantly share code, notes, and snippets.

View mfbx9da4's full-sized avatar

David Alberto Adler mfbx9da4

View GitHub Profile
@mfbx9da4
mfbx9da4 / cloudSettings
Last active September 18, 2020 16:43
Visual Studio Code Settings Sync Gist
{"lastUpload":"2020-09-18T15:19:54.936Z","extensionVersion":"v3.4.3"}
@mfbx9da4
mfbx9da4 / debouncedChunkedQueue.ts
Last active September 19, 2023 08:01
An implementation of a debounced chunked async queue. An async function may be called many times over some period of time. We want to first debounce the calls to the function and batch up all those arguments into one argument. Secondly all executions of that async function should be serialized in a FIFO manner.
export const debouncedChunkedQueue = <T>(
fn: (items: T[]) => Promise<void> | void,
delay = 1000
) => {
let items: T[] = []
let started = false
const push = (item: T) => {
items.push(item)
if (!started) start()
}
@mfbx9da4
mfbx9da4 / observableStateTree.ts
Last active December 28, 2020 13:48
An observable tree data structure. An observable state tree is a normal nestable dictionary except that listeners can be bound to any subtree of the tree. (This has since moved to a repo https://github.com/mfbx9da4/observable-state-tree)
import { assert } from '../utils/assert'
export type Callback = (value: any, prevValue: any) => void
type DestroyCallback = () => void
interface ListenerNode {
parent: Symbol | ListenerNode
children: Record<string, ListenerNode>
prevValue: any
@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'
@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 {}
};

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
class Foo {
public a string
public b number
}
function getDefault(fieldAsString: 'a' | 'b') {
if (field === 'a') return 'asdf'
if (field === 'b') return 3
}
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) {
@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 = {