Skip to content

Instantly share code, notes, and snippets.

@domenic
domenic / promise-finally-observability.js
Last active August 12, 2017 16:30
Promise.prototype.finally observability
// Current spec
function finally(onFinally) {
let C = SpeciesConstructor(this);
return this.then(
x => new C(r => r(onFinally())).then(() => x),
e => new C(r => r(onFinally())).then(() => { throw e; })
);
}
// Proposed, but rejected (potentially because of a miscommunication) spec
@domenic
domenic / async-gen-yield-return.md
Created June 7, 2017 20:30
Async generator next vs. return

Consider:

async function* f() {
  const x = Promise.resolve(1);
  const y = Promise.resolve(2);
  
  const fromX = yield x;
  return y;
}
@domenic
domenic / URL.js
Created March 2, 2017 17:36
Generated URL.js with webidl2js
"use strict";
const conversions = require("webidl-conversions");
const utils = require("./utils.js");
const Impl = require(".//URL-impl.js");
const impl = utils.implSymbol;
function URL(url) {
if (!this || this[impl] || !(this instanceof URL)) {
@domenic
domenic / 1-service-worker.js
Created February 14, 2017 23:45
Service worker stream transferring 2
"use strict";
const worker = new Worker("worker.js");
self.onfetch = e => {
e.respondWith(new Promise(resolve => {
const guid = generateGUID();
worker.addEventListener("message", function messageListener({ data: { readableStream, messageId } }) {
if (messageId !== guid) {
@domenic
domenic / 1-service-worker.js
Last active October 20, 2022 11:10
Service worker stream transferring
"use strict";
const worker = new Worker("worker.js");
self.onfetch = e => {
const transform = new TransformStream(); // creates an identity transform
e.respondWith(new Response(transform.readable));
// Give the worker the writable end. An identity transform stream will just shuffle
// bytes written there into transform.readable.
@domenic
domenic / redirecting-github-pages.md
Created February 10, 2017 19:28
Redirecting GitHub pages after a repository move

Redirecting GitHub Pages after a repository move

The problem

You have a repository, call it alice/repo. You would like to transfer it to the user bob, so it will become bob/repo.

However, you make heavy use of the GitHub Pages feature, so that people are often accessing https://alice.github.io/repo/. GitHub will helpfully redirect all of your repository stuff hosted on github.com after the move, but will not redirect the GitHub Pages hosted on github.io.

The solution

@domenic
domenic / web-components-reflection.md
Last active November 11, 2020 15:37
Web components reflection

Web components reflection

What is a subset of HTML's reflection rules which we should make easy for web components?

Summary of HTML

HTML's reflection algorithms include:

  • URL strings
  • Enumeration strings ("limited to only known values")
@domenic
domenic / mixins.md
Last active November 27, 2016 18:58
Mixins Web IDL syntax "conflict"

Discussion of the potential keyword mixin

Web IDL (which is used for writing web specs) would like to introduce a concept of "mixins", which work by adding properties and methods to the mixin target. Currently this is accomplished by

interface MixinTarget { };

[NoInterfaceObject]
interface Mixin { };
MixinTarget implements Mixin;
@domenic
domenic / adoptedCallback.js
Created October 6, 2016 19:27
A use for adoptedCallback
adoptedCallback(oldDocument, newDocument) {
const newWindow = newDocument.defaultView;
if (newWindow) {
// newDocument belongs to a window
const otherConstructor = newWindow.customElements.get(this.localName);
if (otherConstructor && otherConstructor._isFromPolymer) {
Object.setPrototypeOf(this, otherConstructor.prototype);
// Now any customizations that newWindow code has applied to the
@domenic
domenic / last-with-cancel.js
Created June 8, 2016 23:31
"last" with cancelation
"use strict";
var pending = { then: function () {} };
// operation must accept as its first argument a cancel token
// if your operation does not support cancelation, use a shim:
// last((_, ...args) => realOp(...args));
module.exports = operation => {
let latestPromise = null;
let previousCancel = null;