Skip to content

Instantly share code, notes, and snippets.

View bmeck's full-sized avatar

Bradley Farias bmeck

View GitHub Profile
@bmeck
bmeck / a.mjs
Last active December 28, 2017 18:01
creates a cycle to intentionally ensure only `a` can run `b`
import {ran} from 'b';
let ready;
// this function can be saved to a variable, but blows up if `b` has not run yet
export function onlyAvailableIfBHasRun() {
ready;
ran;
// ...
};
@bmeck
bmeck / actors.js
Last active December 6, 2017 15:14
90% sure this is buggy somewhere, but its a PoC
class LockingActor {
constructor(buffer, offset, bytes) {
let needle = offset;
// if our lock isn't aligned
if (needle % 4 !== 0) {
// align
needle += 4 - (needle % 4);
}
this.state = new Transitions(buffer, needle);
needle += this.lock.byteLength;

Various command line applications use an Interpreter Directive to define how they should be run.

#! js -m foo
#! node foo
@bmeck
bmeck / proposal-no-path-searching.md
Last active February 6, 2018 17:20
Removal of path searching / defining a hook for migration.

Problem

There has been no progress in working towards a single cohesive story for path resolution between Servers and Web. Notable discussion points relevant to this are:

  1. Node has a path searching algorithm.
  2. Web has not been able to gather support for any of the following:
    1. Build tooling as part of UX expectations (lack of interest)
    2. Smarter static web servers (lack of interest). PoC example at https://github.com/bmeck/esm-http-server
  3. A resolve based hook. (interest shown with desire for ~6 months of userland experimentation)
@bmeck
bmeck / sloppy.executor.js
Last active October 11, 2017 20:12
node --experimental-modules sloppy.mjs , using `with` to mutate an ESM namespace
module.exports = (...arguments) => {
// WHAT FRESH HELL IS THIS
with (arguments[0].exports) {
a = 123;
}
}

Avoid live "then()-ables"

Like a Promise, await will call any .then() function on its operand. This can be used to create values that change every time they are awaited.

let lastId = 1;
const id = {
  then(fn, errfn) {
    fn(lastId++);
 }
@bmeck
bmeck / cjs.js
Last active August 16, 2017 15:00
// THIS *REQUIRES SLOPPY MODE* DO NOT USE STRICT
// loads all the necessary files synchronously that can be used synchronously by CJS,
// does *NOT* evaluate them
// closely resembles CJS bundles of today
document.currentScript.entries = {
"x.json": {
evaluate(module, exports) {this.exports = {x:true}; this.evaluate = () => {}},
exports: undefined
}
class Super {
constructor() {
this.log = new.target.DI('log');
}
static DI(name) {
if (name === 'log') return console.log;
}
}
class Sub extends Super {
static DI(name) {

Integration with ES6 modules

While ES6 defines how to parse, link and execute a module, ES6 does not define when this parsing/linking/execution occurs. An additional extension to the HTML spec is required to say when a script is parsed as a module instead of normal global code. This work is ongoing. Currently, the following entry points for modules are being considered:

  • <script type="module">;
  • an overload to the Worker constructor;
  • an overload to the importScripts Worker API;

original:

let i = 0;
export let decrement = () => i--;
export let increment = () => i++;

sharded: