Skip to content

Instantly share code, notes, and snippets.

View afterburn's full-sized avatar
🏠
Working from home

Kevin Karsopawiro afterburn

🏠
Working from home
View GitHub Profile
@afterburn
afterburn / PubSub.js
Last active August 6, 2017 10:57
Basic JavaScript PubSub implementation.
function PubSub() {
this.events = [];
}
PubSub.prototype.on = function(namespace, fn) {
this.events[namespace] = this.events[namespace] || [];
this.events[namespace].push(fn);
}
PubSub.prototype.off = function(namespace, fn) {
0xD037f46F20B4a9E2c0521c0B4C93d2c9742D2bAb
const electron = require('electron');
const { app, BrowserWindow } = electron;
const path = require('path');
const url = require('url');
class Application {
constructor() {
this.windows = {};
app.on('ready', () => this.createWindow('main'));
app.on('activate', () => this.createWindow('main'));
@afterburn
afterburn / Checklist for making sessions persist cross-domain using Express & MongoDB.md
Last active November 8, 2018 15:35
Checklist for making sessions persist cross-domain using Express & MongoDB

Checklist for making sessions persist cross-domain using Express & MongoDB

  1. Enable withCredentials and crossDomain if you make AJAX requests to your API with jQuery.

Note - Make sure you change your AJAX setup before any AJAX requests are executed or this will silently fail.

$.ajaxSetup({
	xhrFields: { withCredentials: true },
	crossDomain: true
});
@afterburn
afterburn / eventemitter.js
Last active January 15, 2018 10:31
Simple EventEmitter
class EventEmitter {
constructor () {
this.events = {}
}
on (event, callback) {
if (!this.events.hasOwnProperty(event)) {
this.events[event] = []
}
this.events[event].push(callback)
@afterburn
afterburn / InlineWorker.js
Last active November 2, 2018 15:16
Inline web worker
class WebWorker {
constructor (code) {
code = `onmessage = (e) => (${code.toString()})(e.data)`
this.blob = window.URL.createObjectURL(new window.Blob([code], { type: 'application/javascript' }))
}
start (initialData, callback) {
if (typeof callback === 'function') {
this.worker = new window.Worker(this.blob)
this.worker.onmessage = (e) => callback(e.data)
'use strict';(function(I){function w(c,a,d){var l=0,b=[],g=0,f,n,k,e,h,q,y,p,m=!1,t=[],r=[],u,z=!1;d=d||{};f=d.encoding||"UTF8";u=d.numRounds||1;if(u!==parseInt(u,10)||1>u)throw Error("numRounds must a integer >= 1");if(0===c.lastIndexOf("SHA-",0))if(q=function(b,a){return A(b,a,c)},y=function(b,a,l,f){var g,e;if("SHA-224"===c||"SHA-256"===c)g=(a+65>>>9<<4)+15,e=16;else throw Error("Unexpected error in SHA-2 implementation");for(;b.length<=g;)b.push(0);b[a>>>5]|=128<<24-a%32;a=a+l;b[g]=a&4294967295;
b[g-1]=a/4294967296|0;l=b.length;for(a=0;a<l;a+=e)f=A(b.slice(a,a+e),f,c);if("SHA-224"===c)b=[f[0],f[1],f[2],f[3],f[4],f[5],f[6]];else if("SHA-256"===c)b=f;else throw Error("Unexpected error in SHA-2 implementation");return b},p=function(b){return b.slice()},"SHA-224"===c)h=512,e=224;else if("SHA-256"===c)h=512,e=256;else throw Error("Chosen SHA variant is not supported");else throw Error("Chosen SHA variant is not supported");k=B(a,f);n=x(c);this.setHMACKey=function(b,a,g){var e;if(!0===m)throw Error("HMAC key al
@afterburn
afterburn / queue.js
Last active January 25, 2019 13:28
Queue implementation in JavaScript
class Queue {
constructor (onTaskComplete) {
this.tasks = []
this.tasksCompleted = 0
this.processing = false
this.onTaskComplete = onTaskComplete || (() => {})
}
enqueue (task) {
this.tasks.push(task)
@afterburn
afterburn / get-bounding-client-rect-polyfill.js
Created October 30, 2018 13:22
Polyfill for cross browser getBoundingClientRect function
Element.prototype._getBoundingClientRect = Element.prototype.getBoundingClientRect
Element.prototype.getBoundingClientRect = function () {
const rect = Element.prototype._getBoundingClientRect.call(this)
rect.x = rect.left
rect.y = rect.top
return rect
}
@afterburn
afterburn / deepfreeze.js
Last active May 14, 2019 08:26
Recursively freezes an object to make it fully immutable.
function deepFreeze (obj) {
Object.freeze(obj)
Object.values(obj).forEach((value) => {
if (typeof value === 'object') {
deepFreeze(value)
}
})
}