Skip to content

Instantly share code, notes, and snippets.

Copying and pasting from StackOverflow

Cameron Nokes ccnokes

Copying and pasting from StackOverflow
Block or report user

Report or block ccnokes

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
ccnokes /
Last active Jan 6, 2020
Bash script that checks for apps that use Electron. Detailed a bit more here:
if [[ -z "$target" ]]; then
echo "ERROR: You must provide a directory to check" >&2
exit 1
check() {
ccnokes / ObservableMap.ts
Last active Nov 5, 2019
A Map class with built-in event emitter that emits a `update` event whenever something changes
View ObservableMap.ts
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);"update", {
action: "set",
currentValue: this.get(key)
View event-emitter.js
class EventEmitter {
constructor() { = new EventTarget();
on(eventName, listener) {
return, listener);
once(eventName, listener) {
return, listener, { once: true });
ccnokes / event-emitter.js
Created Oct 30, 2019
Event emitter using the native DOM APIs: EventTarget and CustomEvent
View event-emitter.js
// Who needs eventemitter3, mitt, or some other library when you can use native DOM APIs? 😁
let eventEmitter = new EventTarget();
eventEmitter.addEventListener('test', console.log); // CustomEvent { type: 'test', detail: 123, ... }
eventEmitter.dispatchEvent(new CustomEvent('test', { detail: 123 }));
ccnokes / wait-with-abortsignal.js
Last active Sep 11, 2019
Use AbortController to implement custom async task cancelling logic. Expounded upon here:
View wait-with-abortsignal.js
function wait(ms, opts = {}) {
return new Promise((resolve, reject) => {
let timerId = setTimeout(resolve, ms);
if (opts.signal) {
// implement aborting logic for our async operation
opts.signal.addEventListener('abort', event => {
ccnokes / AbortController.ts
Created Aug 22, 2019
Minimal AbortController implementation for node.js that doesn't have to shim DOM stuff
View AbortController.ts
import { EventEmitter } from 'events';
class AbortController {
private events = new EventEmitter();
aborted = false;
signal = {
get aborted() {
return this.aborted;
ccnokes / idObj.ts
Last active Aug 22, 2019
Sometimes in React you need a unique `key` to represent an object's identity. Instead of generating an id when you fetch it from an API or generating an id on the fly or using indexes (both will lead to bugs), you can use this which gives you a stable, unique id for an object. Demo: Demo with React: htt…
View idObj.ts
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;
ccnokes / rafScheduler.ts
Created Jun 18, 2019
requestAnimationFrame scheduler
View rafScheduler.ts
* 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) {
ccnokes /
Created Mar 28, 2019
Convert all stashes to patches in a git repository
stash_count=$(git stash list | wc -l)
while [[ $i -lt $stash_count ]]; do
git stash show -p stash@{$i} > "stash-$i.patch"
i=$(( $i + 1 ))
ccnokes / Batch.ts
Last active Oct 2, 2019
A class that manages the logic of batching items together within a timeframe and then passing those items to a function. Sandbox:
View Batch.ts
class Deferred<Type> {
promise: Promise<Type>;
// TODO type these
resolve: any;
reject: any;
then: any;
catch: any;
constructor() {
this.promise = new Promise((resolve, reject) => {
You can’t perform that action at this time.