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 / rx-online-offline.js
Last active Sep 13, 2019
Online/offline event observable with RxJS
View rx-online-offline.js
const { Observable } = require('rxjs/Observable');
function createOnline$() {
//merge several events into one
return Observable.merge(
//use .map() to transform the returned Event type into a true/false value
Observable.fromEvent(window, 'offline').map(() => false),
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 / preload-example.js
Created Feb 1, 2017
Electron preload script
View preload-example.js
// in preload scripts, we have access to node.js and electron APIs
// the remote web app will not have access, so this is safe
const { ipcRenderer: ipc, remote } = require('electron');
function init() {
// Expose a bridging API to by setting an global on `window`.
// We'll add methods to it here first, and when the remote web app loads,
// it'll add some additional methods as well.
ccnokes / axios-instance-config.js
Created Jul 6, 2017
Good default configuration for axios in node.js
View axios-instance-config.js
const axios = require('axios');
const http = require('http');
const https = require('https');
module.exports = axios.create({
//60 sec timeout
timeout: 60000,
//keepAlive pools and reuses TCP connections, so it's faster
httpAgent: new http.Agent({ keepAlive: true }),
ccnokes /
Last active Jul 29, 2019
Bash script to grep for unused dependencies in a node.js project
set -e
# function to grep for a dependency
grep_dep() {
# params: $1 = the string to grep for, $2 = directory to grep in
# [1]
grep --include="*.js" --exclude-dir="node_modules" -R --color -n "require\(.*$1.*\)" "$2"
# if grep returns 0 results, it has an exit code of 1. No results means dependency is not in use
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 Aug 13, 2018
Plays Rick Astley's "Never Gonna Give You Up" in Spotify
osascript <<EOD
tell application "Spotify"
play track "spotify:track:4uLU6hMCjMI75M1A2tKUQC"
end tell
ccnokes /
Last active May 18, 2019
Set your wallpaper to the current Astronomy Picture of the Day (APOD)
# get the URL of the current Astronomy Picture of the Day (APOD)
apod_url=$(curl -s | 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
You can’t perform that action at this time.