Skip to content

Instantly share code, notes, and snippets.

@disco0
disco0 / 1.md
Created October 15, 2020 01:44 — forked from getify/1.md
BetterPromise: a strawman experiment in subclassing Promise and "fixing" a bunch of its awkward/bad parts

Some things that are "better" with this BetterPromise implementation:

  • BetterPromise # then(..) accepts a BetterPromise (or Promise) instance passed directly, instead of requiring a function to return it, so that the promise is linked into the chain.

    var p = BetterPromise.resolve(42);
    
    var q = Promise.resolve(10);
    
    p.then(console.log).then(q).then(console.log);
@disco0
disco0 / primitive-types-proxy-and-decorator.ts
Created October 15, 2020 01:59 — forked from mmichlin66/primitive-types-proxy-and-decorator.ts
Virtualizing TypeScript Properties with Proxy and Decorator - listing 6
// @virtual decorator function
function virtual( target: any, name: string)
{
// symbol to keep the proxy handler value
let sym = Symbol( name + "_proxy_handler");
Object.defineProperty( target, name, {
enumerable: true,
get()
@disco0
disco0 / virtualizing-numbers.ts
Created October 15, 2020 01:59 — forked from mmichlin66/virtualizing-numbers.ts
Virtualizing TypeScript Properties with Proxy and Decorator - listing 5
class Base
{
@virtual a = 1;
b = this.a;
}
class Derived extends Base
{
a = 2;
}
@disco0
disco0 / proxy-and-decorator.ts
Created October 15, 2020 01:59 — forked from mmichlin66/proxy-and-decorator.ts
Virtualizing TypeScript Properties with Proxy and Decorator - listing 4
export function virtual( target: any, name: string)
{
// symbol to keep the proxy handler value
let sym = Symbol( name + "_proxy_handler");
Object.defineProperty( target, name, {
enumerable: true,
get()
{
@disco0
disco0 / virtualizing-with-proxy.ts
Created October 15, 2020 02:00 — forked from mmichlin66/virtualizing-with-proxy.ts
Virtualizing TypeScript Properties with Proxy and Decorator - listing 3
class VirtHandler
{
target: any;
get( t: any, p: PropertyKey, r: any): any
{
return this.target[p];
}
}
@disco0
disco0 / virtualizing-with-function.ts
Created October 15, 2020 02:00 — forked from mmichlin66/virtualizing-with-function.ts
Virtualizing TypesScript Class Properties with Proxy and Decorator - listing 2
class Base
{
first = { v: 1 };
second = () => this.first;
}
class Derived extends Base
{
first = { v: 2 };
}
@disco0
disco0 / virtualizing-problem.ts
Created October 15, 2020 02:00 — forked from mmichlin66/virtualizing-problem.ts
Virtualizing TypeScript Properties with Proxy and Decorator - listing 1
class Base
{
first = { v: 1 };
second = this.first;
}
class Derived extends Base
{
first = { v: 2 };
}
@disco0
disco0 / function-iterator.js
Created October 15, 2020 02:01 — forked from nicholasguyett/function-iterator.js
Functional Iterator
/* This class wraps an iterator/iterable and allows the map/filter/reduce methods
* to be used without being forced to make intermediate temporary arrays.
* I've also implemented the flatten and flatmap functions for convenience
*/
class FunctionalIterator {
constructor(source) {
// Get iterator from source, if present. Otherwise, assume that the source is an iterator
this.source = source[Symbol.iterator] ? source[Symbol.iterator]() : source;
}
@disco0
disco0 / index.ts
Created October 18, 2020 11:35
TypeScript - Typed MutationObserver
/**
* Extended version of MutationObserver that allows control of MutationRecord typing via type
* parameter on (but not limited to) constructor.
*
* I have used this to soothe the type checker on existing(/working) javascript code (using
* HTMLElement for type parameter to cast mutation records as HTMLElements), however, similar to
* ParentNode#querySelector without template literal type inferrence parsing the selector, I'd
* imagine you can easily set an incorrect type for mutation records in a callback (until an
* inference point is added).
*/
@disco0
disco0 / OldReddit.user.js
Last active October 21, 2020 12:12 — forked from simshaun/Reddit.user.js
Userscript - Redirect all reddit pages through old.reddit.com
// ==UserScript==
// @name Old Reddit, Forever
// @namespace gitlab.com/disk0/userscript/reddit/old
// @version 0.1.0
// @author disk0
// @include /^https?:\/\/((?!old)[a-z][a-z\d_]*\.)?reddit\.com.*/
// @grant none
// @run-at document-start
// ==/UserScript==