Skip to content

Instantly share code, notes, and snippets.

@zenparsing
zenparsing / CustomEventHub.js
Last active February 22, 2016 02:02
A simple custom event hub for simple DOM apps
let CustomEvent = window.CustomEvent;
if (typeof CustomEvent !== "function") {
// Internet Explorer
CustomEvent = function(name, options = {}) {
let {
bubbles = false,
cancelable = false,
detail
} = options;
@zenparsing
zenparsing / combinators-with-implicit-cancel-tokens.js
Last active February 9, 2016 18:34
Promise-Observable Combinators with Implicit Cancel Tokens
function takeUntil(control) {
return new Observable(next => new Promise((resolve, reject) => {
this.forEach(next).then(resolve, reject);
control.forEach(resolve).catch(reject);
}));
}
function switch() {
@zenparsing
zenparsing / promise-observable-combinators.js
Last active February 9, 2016 18:34
Promise Observable Combinators
function takeUntil(control) {
return new Observable((next, cancel) => new Promise((resolve, reject) => {
this.forEach(next, cancel).then(resolve, reject);
control.forEach(resolve, cancel).catch(reject);
}));
}
function switch() {
const DELETED_TOKEN = {};
/*
A map class, implemented with private slots, which has the following properties:
- The presence of a mapping k => v in a SlotMap does not by itself make k or v reachable
- For each mapping k => v in a SlotMap m (whether m is reachable or not), if k is reachable then v is reachable
*/
@zenparsing
zenparsing / module-or-script-1.js
Last active January 7, 2016 16:05
Issues with Automatic Module Detection
// OK if a script, but not ok if a "module".
// We'll have to remember this error and location as we parse.
with (x) {}
// The export declaration means that we need to throw a parse error
// using the location of the "with" statement.
export const A = "a";
// V8 doesn't remember strict mode errors like this, and the V8 team has
// previously objected to features which seem to require it.
@zenparsing
zenparsing / Promise.js
Created December 28, 2013 03:48
Minimal ES6 Promise
function enqueueMicrotask(fn) {
// Enqueue a function on the System-defined FIFO
// microtask queue.
}
var $status = "Promise#status",
$value = "Promise#value",
$onResolve = "Promise#onResolve",
$onReject = "Promise#onReject";
@zenparsing
zenparsing / strict-error-detection.md
Last active December 30, 2015 00:59
Simplified algorithm for detection of strict-mode errors in ECMAScript 6.

A context object is associated with any AST node which may have "strictness". Each context has a strictness state: UNKOWN, STRICT, and NOT_STRICT. In order to account for arrow functions, parenthesized expressions are also assigned a context.

When a context C is pushed:

  • set C.parent to the current context object
  • set C.strictError to null
  • if C.parent.strict is STRICT
    • set C.strict to STRICT
  • else
  • set C.strict to UNKNOWN
@zenparsing
zenparsing / gist:7662879
Last active December 29, 2015 11:19
Code Paster
// Use this gist to get syntax highlighted code for use in word processors. Just preview a comment
// that contains code.
@zenparsing
zenparsing / switch-n.js
Created October 7, 2015 14:11
Switch using cancellation functions
// For a nested stream, emits the elements of the inner stream contained within the
// most recent outer stream
function switch(stream, n) {
return new Observable(sink => {
let inner = [];
let outer = stream.subscribe({
@zenparsing
zenparsing / zip-subscribe-returns-function.js
Last active September 28, 2015 14:56
Observable zip with Various Return Types
function zip(list) {
return new Observable(sink => {
function trySend() {
// If every buffer has at least one element, then send an array
// containing every first element and remove those elements from the
// buffer
if (streams.every(s => s.buffer.length > 0))