Skip to content

Instantly share code, notes, and snippets.

View rbuckton's full-sized avatar

Ron Buckton rbuckton

View GitHub Profile

Function Decorator

// definition
function functionDecorator(target /*: Function */) /*: Function | void */ { 
  // ...
}

// application
@functionDecorator 
function targetFunction() {}
@rbuckton
rbuckton / sourcemap-mediatypes.md
Last active August 29, 2015 14:03 — forked from fitzgen/gist:35d7e3905a915238aa14
Proposal for addition of MediaTypes to the Source Map spec.

Proposed Format

/* 1.*/  {
/* 2.*/    "version": 3,
/* 3.*/    "file": "out.js",
/* 4.*/    "sourceRoot": "",
/* 5.*/    "sources": ["foo.js", "bar.ts"],
/* 6.*/    "sourcesContent": [null, null],
/* 7.*/ "names": ["src", "maps", "are", "fun"],
@rbuckton
rbuckton / Promise.finally.md
Last active December 18, 2016 15:53
Proposal for addition of Promise.prototype.finally

NOTE I have yet to update this proposal, but I find @domenic's version here to be a much improved implementation:

Promise.prototype.finally = function (callback) {
    return this.then(
        value => this.constructor.resolve(callback()).then(() => value),
        reason => this.constructor.resolve(callback()).then(() => throw reason)
    );
};
@rbuckton
rbuckton / CancellationToken.md
Last active June 26, 2021 20:58
External Cancellation of Promises with CancellationTokenSource
@rbuckton
rbuckton / es7decoratorunification.md
Last active April 13, 2018 08:37
Unification of Decorators and Annotations for ES7

Summary

One issue introduced by decorators is that any attempt to use them on a function declaration would either necessitate a TDZ for the declaration, or would need to be an error. This limits the usefullness of decorators.

Another issue is that many use cases for annotations need a coherent runtime API that allows for imperative reflection for annotations of an object, its members, or the parameters of a function.

This document describes two parts to a solution for unifying decorators and annotations:

  1. Marking decorators as "early" with the @decorator decorator.
  2. Extending the Reflect API with a comprehensive set of functions for reflecting and managing metadata.
@rbuckton
rbuckton / es7decoratorunification.ts
Last active August 29, 2015 14:16
Example shim for es7decoratorunification API.
module Reflect {
"use strict";
const weakMetadata = new WeakMap<any, Map<any, any>>();
const weakPropertyMetadata = new WeakMap<any, Map<PropertyKey, Map<any, any>>>();
const weakParameterMetadata = new WeakMap<Function, Map<number, Map<any, any>>>();
/**
* Applies a set of decorators to a target object.
* @param target The target object.
interface ClassDecoratorDescriptor<TFunc extends Function> {
  target: TFunc;
  
  // TypeScript specific
  type?: Function;
  paramTypes?: Function[];
  returnType?: Function;
}
@rbuckton
rbuckton / es7decoratorunification2.md
Last active August 29, 2015 14:16
Modified version of es7decoratorunifaction.md using a single-argument decorator

Summary

One issue introduced by decorators is that any attempt to use them on a function declaration would either necessitate a TDZ for the declaration, or would need to be an error. This limits the usefullness of decorators.

Another issue is that many use cases for annotations need a coherent runtime API that allows for imperative reflection for annotations of an object, its members, or the parameters of a function.

This document describes two parts to a solution for unifying decorators and annotations:

  1. Marking decorators as "early" with the @decorator decorator.
  2. Extending the Reflect API with a comprehensive set of functions for reflecting and managing metadata.
@rbuckton
rbuckton / genericresttype.md
Created April 10, 2015 16:49
Proposal: Generic "rest" type

Proposal

Add a generic "rest" type parameter, for use with Tuples. The goal of this feature is to provide better type support for certain operations that either cannot be reliably typed today, or could be more easily written.

Syntax

A generic "rest" type parameter is written in the form ...T. When generic types are inferred, this would be expanded into T0, T1, ... Tn. This type parameter can only be used inside of a tuple type, using the form [...T], which would be expanded into [T0, T1, ... Tn].

Scenarios

bind

Currently, if you wanted to write something like bind with generics, you have two options:

/**
* Basic shape for a type.
*/
interface Type {
/**
* Describes the specific shape of the type.
* @remarks
* One of:
* "any" -> IntrinsicType
* "number" -> IntrinsicType