Skip to content

Instantly share code, notes, and snippets.

@dimitarcl
Last active December 16, 2015 20:39
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/5494500 to your computer and use it in GitHub Desktop.
Save dimitarcl/5494500 to your computer and use it in GitHub Desktop.
Using Coherent UI with typescript
interface SingleArgumentCallback {
(result: any);
}
interface ArbitraryCallback {
(...args: any[]);
}
interface EventHandle {
/**
* Detach this handler from the event
*/
clear(): void;
}
interface Promise {
/**
* 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, error?: SingleArgumentCallback): Promise;
/**
* 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, context?: any): Promise;
/**
* 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, context?: any): Promise;
/**
* 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, context?: any): Promise;
}
interface Deferred extends Promise {
/**
* 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: any): 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: any): void;
}
declare var 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;
}
/// <reference path="coherent.d.ts" />
/// <reference path="Player.d.ts" />
class Inventory {
constructor(player: Player) {
this._player = player;
}
public show(): void {
this._player.GetInventory().then(items => this.render(items));
}
private render(items: Item[]): void {
}
private _player: Player;
}
engine.on('start', (player: Player) => {
var playerInventory = new Inventory(player);
playerInventory.show();
});
namespace Game
{
public struct Item
{
int Id;
string Name;
}
public class Player
{
List<Item> GetInventory()
{
}
}
}
/// <reference path="coherent.d.ts" />
interface Item {
id: number;
name: string;
}
class Player {
public GetInventory(): Promise;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment