Skip to content

Instantly share code, notes, and snippets.

💭
Just coding

Kyle Simpson getify

💭
Just coding
View GitHub Profile
@getify
getify / 1.js
Last active Jun 7, 2020
native JS async-iterator event stream
View 1.js
async function main() {
var btn = document.getElementById("my-button");
// eventStream here is a native JS async iterator
var eventStream = onEvent(btn)("click");
// this is the key thing: we can use a `for-await..of` loop
// to consume the async-iterator
for await (let evt of eventStream) {
console.log("click!");
@getify
getify / 1.js
Last active Jun 3, 2020
in JS, exploring async (promise-based) Haskell-style `do`-block syntax for the IO monad Demo: https://codepen.io/getify/pen/abvjRRK?editors=0011
View 1.js
// setup all the utilities to be used
var delay = (ms,cancel = new AbortController()) => {
var pr = new Promise(res => {
var intv = setTimeout(res,ms);
cancel.signal.addEventListener("abort",() => { clearTimeout(intv); res(); });
});
pr.abort = () => cancel.abort();
return pr;
};
View MicDevice.ahk
#include VA.ahk
SwitchMicDevice(micName) {
return VA_SetDefaultEndpoint(micName,1)
}
GetMicDeviceID(micName) {
Loop
{
deviceIDPtr := GetMicDevice(A_Index)
View keybase.md

Keybase proof

I hereby claim:

  • I am getify on github.
  • I am getify (https://keybase.io/getify) on keybase.
  • I have a public key ASCMGyYZgzXV7OoA4Aw_DzSwADaqpyFN2enUhGeeAbxaZgo

To claim this, I am signing this object:

@getify
getify / 1.js
Last active Mar 28, 2020
illustrating the hook/stale-closure problem
View 1.js
function SomeCounter() {
const [ counter, updateCounter ] = useState(0);
useEffect(function listening(){
const btn = document.getElementById("increment-counter-btn");
btn.addEventListener("click",onClick);
},[]);
useEffect(function logger(){
// this logger() is updated each time `counter` changes, so
@getify
getify / 1.js
Last active Sep 19, 2019
quick sketch of CAF
View 1.js
btn.addEventListener("send-btn",onSend);
send = CAF(send);
var prevSend = Promise.resolve();
function onSend(evt) {
prevSend.finally(function f(){
prevSend = send(
CAF.timeout(1000,"send took too long."),
{ some: "data" }
)
@getify
getify / 1.js
Last active Sep 13, 2019
microtask queue got me singin' the blues
View 1.js
// VERSION 1 -- broken
async function *main() {
yield ready;
}
var resolve1;
var resolve2;
var ready = new Promise(function c(res){
resolve1 = res;
View 1.js
// NOTE: To see this demo: https://codepen.io/getify/pen/LYPbmYG?editors=0012
var counter = 1;
function printMessage() {
console.log(`message ${counter++}`);
}
var schedule = Scheduler(/* debounceMinimum = */50,/* throttleMaximum = */500);
@getify
getify / 1.js
Last active Jul 11, 2019
what could a `replaceAll(..)` for JS strings, which handles non-global regexes as if they're global, look like?
View 1.js
function replaceAll(str,re,replacement) {
if (re.global) {
return str.replace(re,replacement);
}
else {
// do we need to "patch" the replacement function to pass correct arguments?
if (typeof replacement == "function") {
let fn = replacement;
replacement = function replacer(...args) {
// patch `offset` to correct position from whole string
@getify
getify / 1.js
Last active Jun 8, 2019
race-promises-pool
View 1.js
async function racePromisesPool([ ...prs ] = []) {
var raceWon = false;
var prListeners = prs.map(function listen(pr,idx){
return pr.then(function t(v){
if (!raceWon) {
raceWon = true;
prs.splice(idx,1); // remove the promise from the pool since it won the race
return v;
}
});
You can’t perform that action at this time.