Skip to content

Instantly share code, notes, and snippets.

View barhatsor's full-sized avatar

Bar Hatsor barhatsor

View GitHub Profile
@barhatsor
barhatsor / Semaphore.ts
Last active October 14, 2026 01:16
Simple semaphore.
export class Semaphore {
readonly max: number;
private available: number;
private readonly waiters: (() => void)[] = [];
constructor(max: number) {
this.max = max;
this.available = max;
@barhatsor
barhatsor / asCompleted.ts
Last active October 14, 2026 01:15
asCompleted: iterate on promises as they settle.
/**
* Yields the values of the given promises in the order they settle,
* rather than the order they were passed in.
*
* If a promise rejects, iteration stops and the rejection is thrown.
* Promises that haven't settled yet keep running in the background.
*
* @example
* const userPicturePromises = users.map(async user => await user.getPicture());
@barhatsor
barhatsor / customElementTyping.ts
Created July 4, 2026 17:58
Strict Custom Element Type
type ElementAttribute = string | null
type StrictArrayTuple<Arr extends readonly unknown[], ArrTuple = Arr[number]> =
Arr[number][] extends Arr ? // widened array — `as const` forgotten
{ missingAsConstOnArr: never }
: Arr[number] extends ArrTuple ?
// brackets compare the types as single units. unbracketed, a naked (eg. non-indexed-access)
// type param like ArrTuple distributes per union member (TS rules). `never` is
// regarded by TypeScript as an empty union, so with no members to distribute over,
@barhatsor
barhatsor / AbortablePromise.ts
Last active July 9, 2026 07:36
Abortable Promise
/**
* Abortable Promise
* @remarks
* Call {@linkcode abort abort(reason?:)} to abort the signal and resolve the promise.
*/
export class AbortablePromise extends AbortController {
promise: Promise<typeof this.signal.reason>
// Bind method to allow destructuring
@barhatsor
barhatsor / PRs-and-CI-branch-ruleset.jsonc
Last active June 1, 2026 13:56
PRs and CI branch ruleset. To use in your repository, go to Settings > Rules > Rulesets > New ruleset > Import a ruleset > Select this file (without the jsonc comments).
{
"name": "PRs and CI",
"target": "branch",
"source_type": "Repository",
"enforcement": "active",
// Target specified branches
"conditions": {
"ref_name": {
"include": [
// Default branch (usually 'main')
@barhatsor
barhatsor / TransformStreamInterface.ts
Last active June 2, 2026 16:15
A simple wrapper for TransformStream.
/**
* A simple wrapper for `TransformStream`.
* @example
* class UpperCaseStream extends TransformStreamInterface<string, string> {
* transform(chunk: string) {
* return chunk.toUpperCase()
* }
* }
*
* // Usage:
@barhatsor
barhatsor / html-include-plugin-vite.js
Last active May 24, 2026 11:59
HTML Include Plugin for Vite
/*
* Tiny HTML include plugin for Vite.
* Usage: `<include src="./partials/header.html"></include>`
*/
import fs from 'fs'
import path from 'path'
function processIncludes(html, dir) {
return html.replaceAll(/<include src="(.+?)"><\/include>/g, (_match, src) => {
@barhatsor
barhatsor / default.tsconfig.json
Last active September 13, 2026 17:24
Useful TypeScript defaults.
{
// Note: This assumes TS6+.
"compilerOptions": {
// Treat all files as ES modules.
"moduleDetection": "force",
// For Node.js:
// "module": "nodenext",
// "types": ["node"],
@barhatsor
barhatsor / imports.md
Last active May 8, 2026 17:34
Proposal: Granular import with module namespace

Proposal: Granular import with module namespace

Barrel import with module namespace:
Problem: loses granular intent as well as potential tree-shaking benefits.

import * as util from './util'

Granular import without module namespace:
Problem: loses scope, conflicts with other local or imported module functions.

@barhatsor
barhatsor / Task.ts
Last active July 9, 2026 08:00
Task (a-la Swift)
/**
* Task (a-la [Swift](https://developer.apple.com/documentation/swift/task)).
*
* @example
* const task = new Task(async signal => {
* await Task.sleep({ for: 1000, signal })
* return 'hello'
* })
* task.abort()