Skip to content

Instantly share code, notes, and snippets.

View WebReflection's full-sized avatar
🎯
Focusing

Andrea Giammarchi WebReflection

🎯
Focusing
View GitHub Profile
@WebReflection
WebReflection / todo.js
Last active May 22, 2025 07:32
Web Components, the React way, without Shadow DOM
// https://medium.com/@bdc/web-components-the-react-way-8ed5b6f4f942
const store = (() => {
let state;
return todos => {
if (todos) {
state = todos;
render("todo-list");
}
return state;
};
@WebReflection
WebReflection / has-external-reference.md
Created January 15, 2017 10:35
Idea: Reflect.hasExternalReference

I am not sure how feasible this concept is, and I'll start just with the pattern it's useful for.

someRef.setup({
  one: 'or more',
  runtime: 'definition'
});

In JS user-land, the amount of unnecessary duplicated objects is embarrassing.

const Class = require('classtrophobic');
const List = Class({
extends: Array,
static: {import:(o) => List.from(o)},
constructor(...args) {
this.super().push(...args);
},
push(...args) {
this.super.push(...args);
const MyDiv = Class({
extends: HTMLElement,
constructor(...args) {
this.super(...args);
this.addEventListener('click', console.log);
},
connectedCallback() {
this.textContent = 'Hello World';
}
});
class MyDiv extends HTMLElement {
constructor(...args) {
super(...args);
this.addEventListener('click', console.log);
}
connectedCallback() {
this.textContent = 'Hello World';
}
}
class NoSpecies extends Array {}
(new NoSpecies).slice() instanceof NoSpecies;
// it should be true on native environment
@WebReflection
WebReflection / list.js
Last active January 14, 2017 18:07
Native JS Class
class List extends Array {
constructor(...args) {
super().push(...args);
}
// chainable push
push(...args) {
super.push(...args);
return this;
}
}
@WebReflection
WebReflection / cancelable.js
Created December 20, 2016 00:06
Essential Cancelable Promise Module
var Cancelable = (function ($Promise) {
var setPrototypeOf = Object.setPrototypeOf ||
function (o, p) { o.__proto__ = p; };
function Promise(fn) {
var
cancel,
promise = new $Promise(function (resolve, reject) {
cancel = function (info) {
@WebReflection
WebReflection / try.md
Created November 24, 2016 13:31
Move away deoptimized try/catch

Accordingly to this Writing efficient JavaScript post, I wonder if the following utility could anyhow help to improve performance without removing entirely the ability to try/catch

const Try = (fn) => {
  let e, attempt = {
    catch: (fn) => {
      if (e) fn(e);
      return attempt;
 },

Most simple little Map (using native WeakMap) shim.

// ES3 compatible syntax example
var wm = typeof WeakMap === typeof wm ?
    (function (m, a, p) {
        var indexOf = m.indexOf || function (v) {
            p = this.length;
            while (p-- && this[p] !== v) {}
            return p;