Skip to content

Instantly share code, notes, and snippets.

@dfkaye
dfkaye / multiple-inheritance.js
Created August 18, 2024 08:18
simulate multiple inheritance in javascript
// 5 Aug 2024
// multiple inheritance
// "inspired" by tweet, 1 August 2024, from Colin McDonnell (and goaded by some
// benighted replies) at https://x.com/colinhacks/status/1819138095104905689
// Challenge: Implement merge() to handle multiple inheritance, as per example:
// ```
// class A {...}
// class B {...}
@dfkaye
dfkaye / chunked-response-stream.js
Created August 18, 2024 08:17
simulate chunked HTML response stream in the browser
// 15 July 2024
// simulate chunked HTML response stream in the browser
// 1. create the response stream parts
var encoder = new TextEncoder;
var text = `
<html lang="en">
@dfkaye
dfkaye / parse-bigint.js
Created August 18, 2024 08:15
parse bigint
// 1 July 2024
// parseBigInt
function parseBigInt(b, r) {
var n = String(b).split("n")[0];
return parseInt(n, r);
}
var base10 = ["2n.3", "5.5", "FFnA", "n.3"].map(function (v, i) {
return parseBigInt(v, 8);
@dfkaye
dfkaye / is-number-or-bigint.js
Created August 18, 2024 08:14
is-number with bigint support
// 22 June 2024
// is-number with bigint support
function isNumber(n) {
var m = typeof n == 'bigint'
? 0n
: 0;
return n - m === Object(n).valueOf();
}
@dfkaye
dfkaye / partial-order-timestamps.js
Created August 18, 2024 08:13
partial order timestamps, realizing the algorithm for processing event timestamps in Fidge (1988)
// 12 June 2024
// realizing the algorithm for processing event timestamps in Fidge (1988),
// "Timestamps in Message-Passing Systems That Preserve the Partial Ordering"
// https://fileadmin.cs.lth.se/cs/Personal/Amr_Ergawy/dist-algos-papers/4.pdf
// missing the query or aggregation step that collects the timestamps and sorts
// them by determinate vs. non-determinate ordering.
// determinate ordering should be singular (i.e., only one permutation) whereas
@dfkaye
dfkaye / pass-and-call-function-in-worker.js
Last active June 13, 2024 08:32
pass functions to workers and and call them in subsequent messages
// 8 June 2024
// pass functions to workers and and call them in subsequent messages.
// similar to gists in 2022:
// pass functions as strings to workers and revive them with the Function() constructor
// https://gist.github.com/dfkaye/527f163b6913b163a579bbeb01858593
// Not the same as "pass a stringified function in its own blob to a worker and
// import it using importScripts(blobUrl)."
// https://gist.github.com/dfkaye/ca9722cd247c6d907b3bbaf7273741e0
@dfkaye
dfkaye / visibility-close-restart-worker.js
Created May 31, 2024 21:37
visibility change and visibility state to drive web worker processing, closeand restart.
// 31 May 2024
// visibility change and visibility state to drive web worker processing, close
// and restart.
// previous version, pause and resume, at
// https://gist.github.com/dfkaye/3a9d6752301733fba81c740cf4fb68b2
var source = `
var count = 0;
var interval;
@dfkaye
dfkaye / visibility-pause-resume-worker.js
Last active May 31, 2024 21:38
visibility change and visibility state to drive web worker processing, pause and resume
// 30 May 2024
// visibility change and visibility state to drive web worker processing,
// pause and resume.
// pretty sure I'd done this in 2022 or 2023.
// next version implements close and restart...
// https://gist.github.com/dfkaye/ee75271dd58e489d3a3ab9d209e51e02
var source = `
@dfkaye
dfkaye / async-await-dependency-chain-test.js
Last active May 15, 2024 08:08
async/await dependency chains and what to do about them
// 13 May 2024
// async/await dependency chains
// 14 May 2024
// added explicit try-catch-finally to test error in "finalizer" function C.
// what
// laughing at ben lesh tweet complaining about async/await being difficult
// without explaining why:
@dfkaye
dfkaye / value-atom.js
Last active May 12, 2024 05:24
chapter 8 Data oriented programming non-blocking Atom
// 11 May 2024
// chapter 8 Data oriented programming
// atomic value swap alg
function Atom (state) {
return {
state,
atomicCompareAndSet(current, previous, next) {
return current === previous ? (state = next, true) : false;