Skip to content

Instantly share code, notes, and snippets.

View SlotMap.js
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 Feb 9, 2016
Promise Observable Combinators
View promise-observable-combinators.js
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() {
@zenparsing
zenparsing / module-or-script-1.js
Last active Jan 7, 2016
Issues with Automatic Module Detection
View module-or-script-1.js
// 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 / switch-n.js
Created Oct 7, 2015
Switch using cancellation functions
View switch-n.js
// 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 Sep 28, 2015
Observable zip with Various Return Types
View zip-subscribe-returns-function.js
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 / my-array-with-privates.js
Last active Sep 17, 2015
Self Hosted Array Supporting Private State
View my-array-with-privates.js
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 / consuming-observable-with-generator.js
Created Sep 10, 2015
Consuming Observables with a Generator
View consuming-observable-with-generator.js
(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();
View flip-it-and-reverse-it.js
function toObservable(asyncIter) {
return new Observable(sink => {
let abort = false;
(async _=> {
try {
for async (let x of asyncIter) {
if (abort) return;
sink.next(x);
@zenparsing
zenparsing / toObservable.js
Created May 28, 2015
Async Iterator to Observable
View toObservable.js
function toObservable(asyncIter) {
return new Observable(sink => {
let abort = false;
(async _=> {
try {
for async (let x of asyncIter) {
if (abort) return;
sink.next(x);
@zenparsing
zenparsing / async-iterators.js
Last active Aug 29, 2015
mergeStreams with Async Iterators and Observables
View async-iterators.js
async function* flattenStreams(asyncIterList) {
function deferred() {
let capability = {};
capability.promise = new Promise((resolve, reject) => {
capability.resolve = resolve;
capability.reject = reject;
});
return capability;
}
You can’t perform that action at this time.