Skip to content

Instantly share code, notes, and snippets.

@dimitarcl
Last active December 19, 2015 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dimitarcl/9656cf463960818ecb99 to your computer and use it in GitHub Desktop.
Save dimitarcl/9656cf463960818ecb99 to your computer and use it in GitHub Desktop.
Coherent UI TypeScript 0.9 declaration
interface SingleArgumentCallback<T> {
(result: T):any;
}
interface ArbitraryCallback {
(...args: any[]);
}
interface EventHandle {
/**
* Detach this handler from the event
*/
clear(): void;
}
interface Promise<T> {
/**
* Add callbacks for the result of the promise
* @param success callback to be executed when the promise is resolved
* @param error callback to be executed when the promise is rejected
* @return chained promise for the result of the success callback
*/
then(success: SingleArgumentCallback<T>, error?: SingleArgumentCallback<T>): Promise<any>;
/**
* Add callback for the successful result of the promise
* @param callback the function to be executed when the promise is resolved
* @param context *this* context for the function
* @return the same instance
*/
success(callback: SingleArgumentCallback<T>, context?: any): Promise<any>;
/**
* Add callback for all results of the promise
* @param callback the function to be executed when the promise is resolved or rejected
* @param context *this* context for the function
* @return the same instance
*/
always(callback: SingleArgumentCallback<T>, context?: any): Promise<any>;
/**
* Add callback for the failure result of the promise
* @param callback the function to be executed when the promise is resolved
* @param context *this* context for the function
* @return the same instance
*/
otherwise(callback: SingleArgumentCallback<T>, context?: any): Promise<any>;
}
interface Deferred<T> extends Promise<T> {
/**
* Resolve the promise with the specified value. All success handlers will be called with value
* @param value the success value of the promise
*/
resolve(value: T): void;
/**
* Reject the promise with the specified value. All failure handlers will be called with value
* @param value the failure value of the promise
*/
reject(value: T): void;
}
interface Engine {
/**
* Register handler for an event
* @param name name of the event
* @param callback function to be executed when the event has been triggered
* @param context *this* context for the function, by default the engine object
* @return handle for unsubscribing this callback to the event
*/
on(name: string, callback: ArbitraryCallback, context?: any): EventHandle;
/**
* Remove handler for an event
* @param name name of the event, by default removes all events
* @param callback the function to be removed, by default removes all callbacks for a given event
* @param context *this* context for the function, by default all removes all callbacks, regardless of context
*/
off(name: string, callback?: ArbitraryCallback, context?: any): void;
/**
* @function engine.trigger
* Trigger an event
* This function will trigger any C++ handler registered for this event with `Coherent::UI::View::RegisterForEvent`
* @param name name of the event
* @param args any extra arguments to be passed to the event handlers
*/
trigger(name: string, ...args: any[]): void;
/**
* Call asynchronously a C++ handler and retrieve the result
* The C++ handler must have been registered with `Coherent::UI::View::BindCall`
* @param name name of the C++ handler to be called
* @param args any extra parameters to be passed to the C++ handler
* @return promise for the result of the C++ function
*/
call(name: string, ...args: any[]): Promise;
/**
* Create a new deferred object.
* Use this to create deferred / promises that can be used together with `engine.call`.
* @return a new deferred object
*/
createDeferred(): Deferred<any>;
}
declare var engine: Engine;
inteface Document {
createElement(elementName: string): HTMLElement;
createElement(elementName: 'audio'): HTMLAudioElement;
createElement(elementName: 'canvas'): HTMLCanvasElement;
// ...
}
function Show(x:number) {
}
engine.on('showUnit', Show);
var audio = <HTMLAudioElement>document.createElement('audio');
// becomes
var audio = document.createElement('audio');
interface Callback<T> {
(result: T):any;
}
interface Promise<T> {
then(success: Callback<T>, error?: Callback<T>): Promise<any>;
success(callback: Callback<T>, context?: any): Promise<any>;
always(callback: Callback<T>, context?: any): Promise<any>;
otherwise(callback: Callback<T>, context?: any): Promise<any>;
}
var audio:HTMLAudioElement = document.createElement('adio');
///<reference path="coherent.d.ts" />
// using declaration merging to add overloads for 'on' and 'call'
interface Engine {
on(name: 'showUnit', callback: SingleArgumentCallback<Unit>): EventHandle;
call(name: 'getAbilities', unitId: number): Promise<Ability[]>;
}
interface Unit {
id: number;
}
interface Ability {
name: string;
level: number;
}
class UnitAbilities {
constructor(unit: Unit) {
this._unit = unit;
}
public show(): void {
engine.call('getAbilities', this._unit.id).then(abilities => {
// abilities are array of Ability instances
this.render(abilities);
});
}
private render(abilities: Ability[]): void {
}
private _unit: Unit;
}
engine.on('showUnit', unit => {
// unit implements the Unit interface
var abilitities = new UnitAbilities(unit);
abilitities.show();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment