Skip to content

Instantly share code, notes, and snippets.

View schickling's full-sized avatar
Making the web better

Johannes Schickling schickling

Making the web better
View GitHub Profile
import { pipe } from '@effect/data/Function'
import * as Option from '@effect/data/Option'
import * as Cause from '@effect/io/Cause'
import * as Effect from '@effect/io/Effect'
import * as Exit from '@effect/io/Exit'
import * as Fiber from '@effect/io/Fiber'
import * as Queue from '@effect/io/Queue'
import * as Stream from '@effect/stream/Stream'
export const chainSwitch =
const makeEtaState = () => {
type TimestampInSeconds = number
const countBucketsForLast10Seconds: Map<TimestampInSeconds, number> = new Map()
const countRun = (numberOfItems: number) => {
const timestampInSeconds = Math.floor(Date.now() / 1000)
const count = countBucketsForLast10Seconds.get(timestampInSeconds) ?? 0
countBucketsForLast10Seconds.set(timestampInSeconds, count + numberOfItems)
}
@schickling
schickling / useStateWithReactiveInput.ts
Last active March 11, 2024 13:10
Reactive `useState` variant
import React from 'react'
/**
* A variant of `React.useState` which allows the `inputState` to change over time as well.
* Important: This hook is synchronous / single-render-pass (i.e. doesn't use `useEffect` or `setState` directly).
*
* Notes:
* - The output state is always reset to the input state in case the input state changes (i.e. the previous "external" `setStateAndRerender` call is forgotten)
* - This hook might not work properly with React Suspense
* - Also see this Tweet for more potential problems: https://twitter.com/schickling/status/1677317711104278528
{ lib, rev, pkgs ? import <nixpkgs> { } }:
let
inherit (pkgs) fetchzip lib stdenv;
inherit (pkgs.stdenv) mkDerivation;
inherit (pkgs.stdenv.hostPlatform) system;
selectSystem = attrs:
attrs.${system} or (throw "Unsupported system: ${system}");
/* eslint-disable prefer-arrow/prefer-arrow-functions */
import type * as otel from '@opentelemetry/api'
const noopSpan = {
setAttribute: () => null,
setAttributes: () => null,
addEvent: () => null,
setStatus: () => null,
updateName: () => null,
recordException: () => null,
import { describe, expect, it } from 'vitest'
import { Equal } from './index.js'
import * as Schema from './Schema.js'
import { Wrapper, wrapper } from './SchemaWrapper.js'
describe('wrapper', () => {
it('wrapper', () => {
const wrapped = wrapper(Schema.number)
const decoded = Schema.parse(wrapped)(10)
import React from 'react'
const PR = Math.round(window.devicePixelRatio || 1)
const FRAME_BAR_WIDTH = 2
export type FPSMeterProps = {
width?: number
height?: number
systemFps?: number
/* eslint-disable prefer-arrow/prefer-arrow-functions */
/**
* fork of https://github.com/astoilkov/main-thread-scheduling
*
* node compatibility
* removal of visible priority
* reversal of task order
*/
import * as Effect from '@effect/io/Effect'
// /// <reference no-default-lib="true" />
/// <reference lib="esnext" />
/// <reference lib="webworker" />
import { casesHandled } from '@mytunes/utils'
import { Effect, Either, Layer, Otel, pipe, ServiceContext, Stream, WorkerLive } from '@mytunes/utils/effect'
import { OtelMeterLive, OtelTracerLive } from '@mytunes/utils-frontend/tracing'
import type {
EventId,
@schickling
schickling / DataLoader.ts
Created April 10, 2023 20:37
Effect Dataloader
import * as Chunk from '@effect/data/Chunk'
import * as Context from '@effect/data/Context'
import * as Duration from '@effect/data/Duration'
import type * as Either from '@effect/data/Either'
import { pipe } from '@effect/data/Function'
import * as Deferred from '@effect/io/Deferred'
import * as FiberRefs from '@effect/io/FiberRefs'
import * as Queue from '@effect/io/Queue'
import * as Effect from './Effect.js'