Skip to content

Instantly share code, notes, and snippets.

@dfkaye
dfkaye / testing-worker-functions-independently.js
Last active Jun 30, 2022
testing worker functions independently ~ prior to parsing and embedding
View testing-worker-functions-independently.js
// 27 june 2022
// testing workers functions independently ~ prior to parsing and embedding
// yet one more step in the quest for making embedded workers testable
// from the console...
// ...continued from "communicating embedded web workers"
// at https://gist.github.com/dfkaye/c6d77fde86aea70ef6ae79bda2219582
// demonstrates how to *test* one or more worker script functions **as standalone functions** outside the worker context.
@dfkaye
dfkaye / communicating-embedded-worker-factories.js
Last active Jun 30, 2022
communicating embedded worker factories
View communicating-embedded-worker-factories.js
// 25 june 2022
// communicating embedded worker factories
// 29 june 2022
// - api finally works
// - fix Self parsing function (concat(";\n"))
// yet one more step in the quest for making embedded workers testable
// from the console...
@dfkaye
dfkaye / parse-function-body.js
Last active Jun 30, 2022
parse body of a function given constraints
View parse-function-body.js
// 24 June 2022
// parse the body of a function if its only parameter is self
// 25 june 2022
// - added full body test,
// - fixed trailing brace regexp (missing $).
// 29 June 2022
// - append semi-colon and linebreak to each parsed function body, to enable
// concatenating multiple functions in the next post, "communicating embedded
@dfkaye
dfkaye / import-an-embedded-worker-within-another-embedded-worker.js
Last active Jul 1, 2022
import an embedded worker within another embedded worker
View import-an-embedded-worker-within-another-embedded-worker.js
// 24 June 2022
// import an embedded worker within another embedded worker.
// 30 June 2022
// - cosmetic edits
// Next step in our quest to make embedded worker scripts testable in the console.
// The example creates two embedded workers, allowing one to import the other via
// importScripts(), and testing the same URL with fetch().
@dfkaye
dfkaye / self-serialize-by-prototype-as-worker.js
Last active Jun 30, 2022
self-serialize a command interface as an embedded worker; examples with object.prototype and class inheritance
View self-serialize-by-prototype-as-worker.js
// 18 june 2022
// self-serialize to a worker
// 21 june 2022
// self-serialize a command interface as an embedded worker
// 30 June 2022
// - slight cosmetic changes to prototype version
// - class implementation added (below)
@dfkaye
dfkaye / protect-against-hostile-worker-and-channel-messages-with-addressing.js
Last active Jun 22, 2022
protect against hostile worker and channel messages with addressing
View protect-against-hostile-worker-and-channel-messages-with-addressing.js
// 8 June 2022
// protect against hostile worker and channel messages with addressing
// 18-21 June 2022
// refactored onmessage to handle: {
// sender,
// token,
// action,
// value
// }]
@dfkaye
dfkaye / workers-and-message-channels.js
Last active Jun 8, 2022
communicate with workers through message channels
View workers-and-message-channels.js
// 7 June 2022
// communicate with workers through message channels
// Note: The following example won't run in browser pages with Content Security Policy
// that contains a worker-src directive that does not specify blob: as a value,
// such as a console in a GitHub gist (where I do lot of experimenting).
// If there is no worker-src attribute, then this should *just work*...
// The MessageChannel API lets us send messages between brower windows, tabs,
// iframes, and workers.
@dfkaye
dfkaye / misconceptions-worker-clone-terminate.js
Created Jun 7, 2022
Misconceptions about structured clone data and terminating workers
View misconceptions-worker-clone-terminate.js
// 7 June 2022
// Misconceptions about structured clone data and terminating workers.
// Messages to workers and channels do *not* need to be objects.
// They can be any primitive type that passes the structured
// cloning algorithm. Functions, DOM nodes, Symbols, Worker instances, etc.,
// are not cloned and will result in DOMExceptions.
// When terminating workers, it's best not to do that until a response has
// returned to the main or UI thread, or if you're cloning an invalid value
@dfkaye
dfkaye / react-useEffect-confusion.md
Last active Jun 6, 2022
on the confusion around React's useEffect hook
View react-useEffect-confusion.md

// 6 June 2022

On the confusion around React's useEffect hook

It's June 2022 and there is a concerted effort underway to clarify the intention and behavior of ReactJS's useEffect() hook.

We find developers using it inside functional components as a handler for running other logic outside of the component's render step, but wondering repeatedly why it runs more than once at various times.

That is due to the original design of useEffect(), which in turn is due to the original class-based architecture of React components.

@dfkaye
dfkaye / switch(true)-pattern.js
Created Jun 6, 2022
switch(true) pattern by Sean Barry
View switch(true)-pattern.js
// 5 June 2022
// Sean Barry's Using the switch(true) pattern in javascript
// https://seanbarry.dev/posts/switch-true-pattern
function s(v) {
switch(true) {
case v === 1:
console.log("strictly equals 1")
return;
case v == 1: