Skip to content

Instantly share code, notes, and snippets.

🌲
lost in the woods

Tim Branyen tbranyen

🌲
lost in the woods
Block or report user

Report or block tbranyen

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
@tbranyen
tbranyen / webapp-node-api.js
Last active Aug 20, 2018
WebApp Node API Example (Experimental JS Bundler) w/ Virtual Modules
View webapp-node-api.js
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 / backbone_pushstate_router.js
Last active Jul 30, 2018
hijack links for pushState in Backbone
View backbone_pushstate_router.js
// All navigation that is relative should be passed through the navigate
// method, to be processed by the router. If the link has a `data-bypass`
// attribute, bypass the delegation completely.
$(document).on("click", "a[href]:not([data-bypass])", function(evt) {
// Get the absolute anchor href.
var href = { prop: $(this).prop("href"), attr: $(this).attr("href") };
// Get the absolute root.
var root = location.protocol + "//" + location.host + Application.root;
// Ensure the root is part of the anchor href, meaning it's relative.
@tbranyen
tbranyen / use.js
Created Jan 13, 2012
A RequireJS compatible plugin to provide shimming capabilities declaratively.
View use.js
(function() {
var buildMap = {};
/* RequireJS Use Plugin v0.2.0
* Copyright 2012, Tim Branyen (@tbranyen)
* use.js may be freely distributed under the MIT license.
*/
define({
version: "0.2.0",
@tbranyen
tbranyen / importScript.js
Last active Apr 23, 2018
Native `import()` polyfill (Note: can't name it `import` due to reserved word limitations)
View importScript.js
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;
}
View promise-getters.md

Functions rescue us in this case because functions are lazy. But now, we can’t chain these things with .then() anymore.

function lazyPromise() {
  return new Promise(resolve => setTimeout(resolve, 1000))
    .then(() => console.log('Chain off all'));
}
// The only location you can't chain off is the function

// Chains just fine this way
@tbranyen
tbranyen / defines.js
Last active Feb 1, 2018
AMD Flavors.
View defines.js
// Anonymous empty module.
define();
// Anonymous values.
define({});
define(true);
define(1234);
define(null);
define(undefined);
@tbranyen
tbranyen / concept.js
Last active Aug 28, 2017
DOM Diffing Specification Concept
View concept.js
// Create virtual document and documentElement.
const vdoc = document.createVirtualDocument();
const root = vdoc.createDocumentElement('html');
// <template/> tags should be able to be converted as well.
const someTemplate = document.querySelector('template#some-template');
// Add some SVG.
root.appendChild(vdoc.createElement({
nodeName: 'svg',
namespaceURI: 'http://www.w3.org/2000/svg',
View fixture.js
const { render } = require('react-dom');
const mount = document.querySelector('main');
class Root {
render() {
return (
<div>Hello world</div>
);
}
}
@tbranyen
tbranyen / example.js
Last active Jul 6, 2017
Getting to React Component w/ diffHTML
View example.js
import { innerHTML } from 'diffhtml'
// Start simple...
innerHTML(document.body, `
<marquee>Go for it!</marquee>
`)
// Move to template engine if you want
innerHTML(document.body, mustache.render(`
<div>
View abortable-promise.js
class AbortablePromise extends Promise {
constructor(fn) {
let _reject = null;
super((resolve, reject) => {
_reject = reject;
return fn(resolve, reject);
});
this.abort = () => {
You can’t perform that action at this time.