This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { EventEmitter } from 'events'; | |
class AbortSignal { | |
private events = new EventEmitter(); | |
constructor( | |
private getIsAborted: () => boolean | |
) {} | |
get aborted(): boolean { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ObservableMap<K, V> extends Map<K, V> { | |
readonly events = new EventEmitter(); | |
set(key: K, value: V) { | |
let previousValue = this.get(key); | |
super.set(key, value); | |
this.events.emit("update", { | |
key, | |
action: "set", | |
previousValue, | |
currentValue: this.get(key) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Deferred<Type> { | |
promise: Promise<Type>; | |
// TODO type these | |
resolve: any; | |
reject: any; | |
then: any; | |
catch: any; | |
constructor() { | |
this.promise = new Promise((resolve, reject) => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let map = new WeakMap(); // weakly holds all object refs (works in IE11+) | |
let n = 0; // global counter for ids | |
export function idObj(obj: any) { | |
if (map.has(obj)) { | |
return map.get(obj); | |
} else { | |
let key = String(++n); | |
map.set(obj, key); | |
return key; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A function for batching RAFs together | |
*/ | |
export default function RAFScheduler() { | |
let queue = []; | |
let rafId; | |
let scheduled = false; | |
const DURATION = 10; | |
return function scheduleRaf(cb: () => void) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
osascript <<EOD | |
tell application "Spotify" | |
activate | |
play track "spotify:track:4uLU6hMCjMI75M1A2tKUQC" | |
play | |
end tell | |
EOD |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# get the URL of the current Astronomy Picture of the Day (APOD) | |
apod_url=$(curl -s https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY | jq -r '.hdurl') | |
# get just the image name from the URL | |
filepath=$(basename "$apod_url") | |
# Now get the image and save it | |
curl -s -o "$filepath" "$apod_url" | |
# Use AppleScript to set it as the wallpaper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
stash_count=$(git stash list | wc -l) | |
i=0 | |
while [[ $i -lt $stash_count ]]; do | |
git stash show -p stash@{$i} > "stash-$i.patch" | |
i=$(( $i + 1 )) | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This works in the either the main or renderer processes. | |
const { requireTaskPool } = require('electron-remote'); | |
const work = requireTaskPool(require.resolve('./work')); | |
console.log('start work'); | |
// `work` will get executed concurrently in separate processes | |
work().then(result => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RICScheduler { | |
private queue: { task: () => void, id: number }[] = []; | |
private running = false; | |
private nextId = 0; | |
private ricId; | |
private id() { | |
return this.nextId++; | |
} |