Replace Subscription with Subscribe, add NetworkContext

This commit is contained in:
Jon Staab
2024-03-28 09:56:17 -07:00
parent 46e91c54ec
commit 59511be1aa
9 changed files with 393 additions and 157 deletions
-13
View File
@@ -1,13 +0,0 @@
const ctx = new Map<string, any>()
export const getContext = (k: string) => ctx.get(k)
export const setContext = (k: string, d: any) => ctx.set(k, d)
export const withContext = (k: string, d: any, f: () => void) => {
const o = ctx.get(k)
ctx.set(k, d)
f()
ctx.set(k, o)
}
+19
View File
@@ -1,3 +1,4 @@
import {throttle} from 'throttle-debounce'
import {bech32, utf8} from "@scure/base"
export const now = () => Math.round(Date.now() / 1000)
@@ -12,6 +13,14 @@ export const prop = (k: string) => <T>(x: Record<string, T>) => x[k]
export const identity = <T>(x: T) => x
export const max = (xs: number[]) => xs.reduce((a, b) => Math.max(a, b), 0)
export const min = (xs: number[]) => xs.reduce((a, b) => Math.min(a, b), 0)
export const sum = (xs: number[]) => xs.reduce((a, b) => a + b, 0)
export const avg = (xs: number[]) => sum(xs) / xs.length
export const between = (low: number, high: number, n: number) => n > low && n < high
export const flatten = <T>(xs: T[]) => xs.flatMap(identity)
@@ -62,6 +71,16 @@ export const pushToKey = <T>(m: Record<string, T[]> | Map<string, T[]>, k: strin
return m
}
export const batch = <T>(t: number, f: (xs: T[]) => void) => {
const xs: T[] = []
const cb = throttle(t, () => xs.length > 0 && f(xs.splice(0)))
return (x: T) => {
xs.push(x)
cb()
}
}
export const hexToBech32 = (prefix: string, url: string) =>
bech32.encode(prefix, bech32.toWords(utf8.decode(url)), false)
-1
View File
@@ -1,4 +1,3 @@
export * from './Context'
export * from './Deferred'
export * from './Emitter'
export * from './Fluent'