Skip to content

Instantly share code, notes, and snippets.

View rbuckton's full-sized avatar

Ron Buckton rbuckton

View GitHub Profile
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promise, generator) {
return new Promise(function (resolve, reject) {
generator = generator.call(thisArg, _arguments);
function cast(value) { return value instanceof Promise && value.constructor === Promise ? value
: new Promise(function (resolve) { resolve(value); }); }
function onfulfill(value) { try { step("next", value); } catch (e) { reject(e); } }
function onreject(value) { try { step("throw", value); } catch (e) { reject(e); } }
function step(verb, value) {
var result = generator[verb](value);
result.done ? resolve(result.value) : cast(result.value).then(onfulfill, onreject);
// Debug/Checked compilation
const DEBUG = true;
function conditional(condition) {
return target => condition ? target : Function.prototype;
}
@conditional(DEBUG)
function assert(condition, message = "assertion failed.") {
if (!condition) throw new Error(message);
@rbuckton
rbuckton / mirrors.ts
Last active May 3, 2022 12:29
Prototype of possible mirroring API for decorators (and/or Object.mirror)
// extensions to global Reflect object
declare namespace Reflect {
function mirror(value: (...args: any[]) => any): FunctionMirror;
function mirror(value: new (...args: any[]) => any): ClassMirror;
function mirror(value: any): ObjectMirror;
function mirror(value: any, propertyKey: PropertyKey): MemberMirror;
}
// Extensions to the Object() constructor function object.
interface ObjectConstructor {
/**
* 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))
declare function binarySearch<T>(array: T[], value: T, comparer?: (v1: T, v2: T) => number, offset?: number): number;
declare function insertItemAt<T>(array: T[], offset: number, value: T): void;
declare function orderedRemoveItemAt<T>(array: T[], index: number): void;
declare function lastOrUndefined<T>(array: T[]): T;
interface TextRange { pos: number, end: number }
class IntervalTree<T extends TextRange> {
public count: number = 0;
@rbuckton
rbuckton / cancellation-strawman.md
Last active April 8, 2017 11:09
ECMAScript Cancellation Strawman

Overview

Cancellation follows a source -> sink model and consists of three components: Source, Sink, and Signal.

  • Source - Created by the caller of an asynchronous operation, a Source is a Signal producer.
    • Represented in this proposal as CancellationSource.
  • Sink - Provided by the caller to an asynchronous operation, a Sink is a Signal consumer.
    • A Source and its Sink are entangled.
    • A Sink can only be used to consume or observe a cancellation Signal.
  • Represented in this proposal as a CancellationToken.
var __construct = (this && this.__construct) || (typeof Reflect !== "undefined" && Reflect.construct
? function (self, target, args) { return self !== null && Reflect.construct(target, args, self.constructor) || self; }
: function (self, target, args) { return self !== null && target.apply(self, args) || self; });
var PatchedPromise = (function (_super) {
__extends(PatchedPromise, _super);
function PatchedPromise(executor) {
var _this = this;
_this = __construct(this, _super, [executor]);
return _this;