Skip to content

Instantly share code, notes, and snippets.

View timoxley's full-sized avatar

Tim Kevin Oxley timoxley

View GitHub Profile
const Emitter = require('events')
const emitter = new Emitter()
// Currently, (Node v14.12.0) this just prints 'iterating', waits a moment, then silently exits 0.
// But adding at least one event triggers the full flow of: iterating, caught, finally, rejected
// thinking maybe throw() should call errorHandler?
;(async () => {
const it = Emitter.on(emitter, 'test')
setTimeout(() => {
// emitter.emit('test') // uncomment to trigger full error flow
@timoxley
timoxley / todoMachine.js
Last active July 20, 2022 19:39
XState list processing machine
export function todoMachine ({ contextKey, task, ...opts }) {
return {
...opts,
initial: 'run',
states: {
run: {
invoke: {
src: (ctx, event) => (fn) => {
const itemDone = (key) => (data) => {
fn({ type: 'itemDone', data: { key, data } })
@timoxley
timoxley / index.js
Created July 27, 2018 09:45
Using Freedrum.rocks Sensors in Chrome
// Requires:
// JAZZ Plugin: https://jazz-soft.net/download/Jazz-Plugin/
// JAZZ-Midi Chrome Extension: https://chrome.google.com/webstore/detail/jazz-midi/jhdoobfdaejmldnpihidjemjcbpfmbkm
// Paste this into the Javascript console on *any* https website (sites using http won't work).
// Only works in Chrome
document.body.innerHTML = ''
async function addController () {
const serviceUuid = '03b80e5a-ede8-4b33-a751-6ce34ec4c700'
@timoxley
timoxley / .babelrc
Created May 8, 2018 11:20
Simple way to override & remove async/await regenerator transform nonsense from `babel-preset-react-app`.
{
"presets": [
"./config/babel-preset-myapp"
],
"plugins": [
"transform-class-properties"
]
}
@timoxley
timoxley / sine.js
Created November 22, 2017 03:23
Quick and dirty sine calculator
// JS implementation of series definition:
// https://en.wikipedia.org/wiki/Sine#Series_definition
function sine (x, iterations = 100) {
let v = 0
for (let i = 0; i < iterations; i++) {
v += (Math.pow(-1, i)/factorial((2 * i) + 1)) * Math.pow(x, (2 * i) + 1)
}
return v
}
@timoxley
timoxley / Readme.md
Last active July 1, 2017 23:13
ReactVR pointerEvents prop seems to do nothing

For facebookarchive/react-360#255

In this example I expected the event handlers from the "middle" box to never fire, and the events for the "behind" box to fire when cursor hovers over it, regardless of whether the cursor is also over the "middle" box. See the console to see a log of the events occurring.

@timoxley
timoxley / 1.rreaddir.js
Last active August 6, 2023 17:13
async/await recursive fs readdir
import { join } from 'path'
import { readdir, stat } from 'fs-promise'
async function rreaddir (dir, allFiles = []) {
const files = (await readdir(dir)).map(f => join(dir, f))
allFiles.push(...files)
await Promise.all(files.map(async f => (
(await stat(f)).isDirectory() && rreaddir(f, allFiles)
)))
return allFiles
@timoxley
timoxley / iteration.js
Last active October 1, 2016 16:21
Quick benchmark of various iteration styles
'use strict'
const tests = [
test(testFor),
test(testForAlt),
test(testForEach),
test(testReduceFunction),
test(testReduceArrows),
test(testForOf)
]
@timoxley
timoxley / Readme.md
Last active July 26, 2016 08:56
Convert "Algorithms on Graphs" graph format to dot format

todot.js

Convert Algorithms on Graphs graph format into dot format

Usage

cat input.txt | node todot.js
@timoxley
timoxley / Readme.md
Last active June 7, 2016 07:27
JS Pop Quiz: How well do you know your functions?

JS Pop Quiz: How well do you know your functions?

Given an Array of Functions fns, what argument(s) can you pass to fns.forEach such that each function in fns will execute, in order, without creating any anonymous (or named) functions or invoking the Function constructor?

Conditions

  • Do not use the function keyword, or arrow functions () => .
  • Do not invoke the Function constructor.
  • Do not use method definitions.
  • Function#bind & friends on the Function.prototype are ok.