Skip to content

Instantly share code, notes, and snippets.

@zenparsing
zenparsing / consuming-observable-with-generator.js
Created September 10, 2015 15:12
Consuming Observables with a Generator
(function() { 'use strict';
function consume(observable, gen) {
return new Promise((resolve, reject) => {
if (typeof gen === "function") {
// Prime the generator to accept values
gen = gen();
gen.next();
@zenparsing
zenparsing / my-array-with-privates.js
Last active September 17, 2015 16:14
Self Hosted Array Supporting Private State
class X {
constructor(len) {
let target = Object.create(new.target.prototype);
let handler = {
// handler stuff - special logic for "length" and integer properties
// Otherwise, use target's properties
};
return Reflect.construct(Proxy, [target, handler], new.target);
@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))
@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 / 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 / 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 / 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 / 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.
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 / 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() {