Skip to content

Instantly share code, notes, and snippets.

View tbranyen's full-sized avatar

Tim Branyen tbranyen

View GitHub Profile
@tbranyen
tbranyen / backbone.collectioncache.js
Created June 4, 2012 06:37
Backbone.Collection caching by URL
/*!
* backbone.collectioncache.js v0.0.2
* Copyright 2012, Tim Branyen (@tbranyen)
* backbone.collectioncache.js may be freely distributed under the MIT license.
*/
(function(window) {
"use strict";
// Dependencies
@tbranyen
tbranyen / page.js
Created April 19, 2013 14:35
Creating pages with Backbone and Layout Manager.
// Application.
var app = require("app");
// Page specific Views.
var Views = require("modules/page/views");
module.exports = Backbone.Model.extend({
defaults: {
title: "Unknown Page"
},
function makePromise() {
return new Promise(() => {});
}
async function main() {
console.log('start');
await makePromise();
console.log('end');
}
@tbranyen
tbranyen / acceleration.sh
Created July 28, 2017 22:39
ThinkPad X1 Carbon 5th Generation libinput acceleration
touchpad_id=$(xinput --list | grep "TouchPad" | xargs -n 1 | grep "id=" | sed 's/id=//g')
accel_speed_code=$(xinput --list-props $touchpad_id | awk '/Accel Speed \(/ {print $4}' | grep -o '[0-9]\+')
# Default acceleration is too slow (non-existent)
xinput --set-prop $touchpad_id $accel_speed_code .75
@tbranyen
tbranyen / make-cancellable.js
Last active December 11, 2018 10:02
Make Cancellable Promise
Promise.makeCancellable = promise => {
const deferred = {};
const promise = new Promise((resolve, reject) => {
deferred.reject = reject;
// Invoke the fetch argument with the matching args... only resolve if not
// aborted.
promise
.then(resp => !deferred.aborted && resolve(resp))
.catch(ex => !deferred.aborted && reject(ex));
@tbranyen
tbranyen / hook-class.js
Created December 7, 2018 17:55
React Hooks in a Class
import { useState, Component } from 'react';
class MyComponent extends Component {
render() {
const [ count, setCount ] = this.state;
return (
<div>{count}</div>
);
}
@tbranyen
tbranyen / cjs-treeshake.js
Last active September 27, 2018 17:17
CommonJS Tree Shaking Test Case
it('will treekshake a static named import', async () => {
const input = register('./a')`
const { a } = require('./b');
console.log(a);
`;
register('./b')`
function b() {}
exports.a = 'hello world';
@tbranyen
tbranyen / cjs.js
Last active August 30, 2018 18:47
CJS to ESM
const something = require('something');
const { somethingElse } = require('something-else');
const { exports } = module;
exports.somethingElse = somethingElse;
module.exports = something;
Object.assign(module.exports, exports);
@tbranyen
tbranyen / webapp-node-api.js
Last active August 20, 2018 04:50
WebApp Node API Example (Experimental JS Bundler) w/ Virtual Modules
const { webapp, register } = require('webapp');
async function main() {
const input = register('./input.js')`
const msg = 'Hello world!';
console.log(msg);
`;
const { source } = await webapp({
input,
@tbranyen
tbranyen / importScript.js
Last active April 23, 2018 14:47
Native `import()` polyfill (Note: can't name it `import` due to reserved word limitations)
Object.defineProperty(window, Symbol.for('registry'), {
value: new Map(),
});
window.importScript = src => {
const registry = window[Symbol.for('registry')];
if (registry.has(src)) {
return registry.get(src).promise;
}