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 / ThreadController.cs
Created April 25, 2023 21:52
Enqueue tasks on unity main thread
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Threading.Tasks;
public class ThreadController : MonoBehaviour
{
private static ThreadController _instance = null;
private static readonly Queue<Action> _executionQueue = new Queue<Action>();
@afterburn
afterburn / BoneSynchronizer.cs
Last active April 14, 2023 21:32
Synchronizes the bones of one SkinnedMeshRenderer with another
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteAlways]
public class BoneSynchronizer : MonoBehaviour
{
public SkinnedMeshRenderer proxy;
void Start()
@afterburn
afterburn / localstorage.js
Last active May 14, 2019 08:53
LocalStorage helper class that automatically converts string values to their respective data types.
class LocalStorage {
static metadata = {}
static set (key, value) {
let storedValue = value
if (typeof value === 'object') {
storedValue = JSON.stringify(value)
}
if (!LocalStorage.metadata[key]) {
LocalStorage.metadata[key] = {}
@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)
}
})
}
@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 / 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)
'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 / 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)
@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 / 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
});