Skip to content

Instantly share code, notes, and snippets.

View mfbx9da4's full-sized avatar

David Alberto Adler mfbx9da4

View GitHub Profile
@mfbx9da4
mfbx9da4 / BroadcastMethods.ts
Last active January 2, 2022 14:48
Remote procedure calls (RPCs) using BroadcastChannel in deno
// ---- START IMPORTS ----
export type AssertionExtra = (Record<string, unknown> & { name?: ErrorCode }) | ErrorCode
export function assert(predicate: any, message: string, extra: AssertionExtra = {}): asserts predicate {
if (!predicate) {
extra = typeof extra === 'string' ? { name: extra } : extra
if (!('name' in extra)) {
extra.name = ErrorCode.AssertionError
}
import { serve } from 'https://deno.land/std/http/server.ts'
// import { assert, ErrorCode } from './assert.ts'
export enum ErrorCode {
RaceNotFound = 'RaceNotFound',
RaceMemberNotFound = 'RaceMemberNotFound',
RaceAlreadyExists = 'RaceAlreadyExists',
RaceMemberAlreadyExists = 'RaceMemberAlreadyExists',
AssertionError = 'AssertionError',
}
const wrap = (obj, method) => {
const oldMethod = obj[method]
async function innerMethod(...args) {
const start = Date.now()
try {
const ret = await oldMethod.call(obj, ...args)
console.log(method, 'took', Date.now() - start, args[0])
return ret
} catch (error) {
console.log(method, 'took', Date.now() - start)
import AWS from 'aws-sdk'
import { uuid } from 'short-uuid'
import { yesno } from 'yesno-http'
yesno.spy()
const documentClient = new AWS.DynamoDB.DocumentClient({
apiVersion: 'latest',
})
function area(image) {
return image.width * image.height;
}
/** https://en.wikipedia.org/wiki/Halton_sequence */
function halton(index, base) {
let fraction = 1;
let result = 0;
while (index > 0) {
fraction /= base;
type Fn = (signal: AbortController['signal']) => Promise<unknown>
/** or woman or non-binary for that matter */
export const createOneManQueue = () => {
let task: Promise<unknown> = Promise.resolve()
let abortController = new AbortController()
const enqueue = (fn: Fn) => {
abortController.abort()
abortController = new AbortController()
task.finally(() => {
@mfbx9da4
mfbx9da4 / AbortSignal.ts
Last active August 9, 2021 11:27
Makes abort signals awaitable
export class AbortSignal {
public pending = false
public completed = false
public onAbort: () => void | undefined
private resolve: () => void | undefined
async abort() {
if (this.completed) return
const promise = new Promise<void>((r) => {
this.resolve = r
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");
@mfbx9da4
mfbx9da4 / machine.js
Created July 12, 2021 09:12
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
export const isMainThread = () => typeof window !== 'undefined' && typeof importScripts === 'function'