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 / 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.
/**
* A source for cancellation
*/
export declare class CancellationTokenSource {
/**
* @param links Other `CancellationToken` instances that will cancel this source if the tokens are canceled.
*/
constructor(links?: CancellationToken[]);
/**
* Gets the `CancellationToken` for this source.
@rbuckton
rbuckton / mirrors.ts
Last active May 13, 2016 21:55
Comprehensive Mirrors API for ECMAScript
declare namespace Reflect {
/**
* Gets a mirror for the target.
*
* @param target The target of the mirror.
* @param usage The intended usage for the mirror, either a read-only introspection mirror or a writable mutation mirror. Default "mutation".
* @returns A mirror for the provided target.
*/
function mirror(target: any, usage?: "introspection" | "mutation"): ObjectMirror | FunctionMirror | ClassMirror | undefined | null;
}
// operators proposal
// partial application (? and ...)
f(?, 1) // (_a = f, _b = 1, (_c) => _a(_c, _b))
f(?, 1, ?) // (_a = f, _b = 1, (_c, _d) => _a(_c, _b, _d))
f(?1, ?0) // (_a = f, (_b, _c) => _a(_c, _b))
f(...) // (_a = f, (..._b) => _a(..._b))
f(?, 1, ...) // (_a = f, _b = 1, (_c, ..._d) => _a(_c, _b, ..._d))
o.f(?, 1) // (_a = o.f, _b = 1, (_c) => _a(_c, _b))
@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 / 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: