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
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"); |
// 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: |
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
Multi process
UPDATE
commandsconst fs = require("fs"); | |
const acquireLock = async (identifier, retries = 5, delay = 1000) => { | |
const pathname = `${identifier}.lock`; | |
const acquire = () => { | |
try { | |
fs.mkdirSync(pathname); | |
return true; | |
} catch {} | |
}; |
Machine({ | |
id: 'Scroll view', | |
initial: 'chat_closed', | |
context: { | |
dog: null | |
}, | |
states: { | |
chat_closed: { | |
on: { | |
open: 'chat_open' |