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
UPDATEcommands- Queues
| 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 commands| 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 {} | |
| }; |
| Machine({ | |
| id: 'Scroll view', | |
| initial: 'chat_closed', | |
| context: { | |
| dog: null | |
| }, | |
| states: { | |
| chat_closed: { | |
| on: { | |
| open: 'chat_open' |
| 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 |
| 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() | |
| } |
| {"lastUpload":"2020-09-18T15:19:54.936Z","extensionVersion":"v3.4.3"} |
Testing
