This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Idle LocalStorage</title> | |
<script defer> | |
function uniqId() { | |
return Math.random().toString(36).slice(2); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Idle Broadcast</title> | |
<script defer> | |
function uniqId() { | |
return Math.random().toString(36).slice(2); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function waitValue(cb, delay = 1000) { | |
return new Promise((resolve) => { | |
queueMicrotask(() => { | |
const value = cb(); | |
if (value != null) { | |
resolve(value); | |
} else { | |
const interval = setInterval(() => { | |
const value = cb(); | |
if (value != null) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mutex = (key: string, timeout: number) => { | |
return new Promise<() => void>((resolve) => { | |
const expires = window.localStorage.getItem(key); | |
const lock = () => { | |
const expiresDate = new Date(); | |
expiresDate.setTime(expiresDate.getTime() + timeout); | |
window.localStorage.setItem(key, expiresDate.toString()); | |
resolve(() => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MapSet<K, V> extends Map<K, Set<V>> { | |
add(key: K, value: V) { | |
let set = this.get(key); | |
if (!set) { | |
set = new Set(); | |
this.set(key, set); | |
} | |
set.add(value); | |
return this; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MapSet extends Map { | |
add(key, value) { | |
let set = this.get(key); | |
if (!set) { | |
set = new Set(); | |
this.set(key, set); | |
} | |
set.add(value); | |
return this; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function singlePromise(cb) { | |
let promise = null; | |
return function(...args) { | |
if (!promise) { | |
promise = new Promise(async (resolve) => { | |
resolve(await cb(...args)); | |
promise = null; | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect, useMemo, useRef } from 'react'; | |
export interface Listener<T> { | |
(value: T): void; | |
} | |
export interface ObservableRef<T> { | |
(value: T): void; | |
current: T; | |
subscribe(listener: Listener<T>): () => void; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Observer<T> { | |
subscribe(listener: (value: T) => void): () => void; | |
emit(value: T): void; | |
} | |
function createObserver<T>(): Observer<T> { | |
const listeners = new Set<(value: T) => void>(); | |
return { | |
subscribe(listener: (value: T) => void) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
unsigned int ElfHash(const unsigned char *s) | |
{ | |
unsigned int h = 0, high; | |
while (*s) | |
{ | |
h = (h << 4) + *s++; | |
if (high = h & 0xF0000000) | |
h ^= high >> 24; |
NewerOlder