Created
June 8, 2017 16:12
-
-
Save ivanpopelyshev/050e5cf8e6e859d23eade6794e119218 to your computer and use it in GitHub Desktop.
pixi mini runner in typescript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace gobi.core { | |
export class MiniRunner { | |
items: Array<object> = []; | |
private _name: string; | |
run: Function = null; | |
get name() { | |
return this._name; | |
} | |
constructor(name: string, argsLength?: number) { | |
this.run = MiniRunner.generateRun(name, argsLength || 0); | |
} | |
add(item: object) { | |
if (!item[this._name]) return; | |
this.remove(item); | |
this.items.push(item); | |
} | |
remove(item: object) { | |
const index = this.items.indexOf(item); | |
if (index !== -1) { | |
this.items.splice(index, 1) | |
} | |
} | |
contains(item: object) { | |
return this.items.indexOf(item) !== -1 | |
} | |
removeAll() { | |
this.items.length = 0; | |
} | |
static hash: { [key: string]: Function } = {}; | |
static generateRun(name: string, argsLength: number) { | |
const key = name + "|" + argsLength; | |
let func = MiniRunner.hash[key]; | |
if (!func) { | |
if (argsLength > 0) { | |
var args = "arg0"; | |
for (let i = 1; i < argsLength; i++) { | |
args += ",arg" + i | |
} | |
func = new Function(args, "var items = this.items; for(var i=0;i<items.length;i++){ items[i]." + name + "(" + args + "); }") | |
} else { | |
func = new Function("var items = this.items; for(var i=0;i<items.length;i++){ items[i]." + name + "(); }") | |
} | |
MiniRunner.hash[key] = func | |
} | |
return func; | |
} | |
} | |
export function create0(name: string) { | |
return new MiniRunner(name, 0) as Runner0; | |
} | |
export function create1<T>(name: string) { | |
return new MiniRunner(name, 1) as Runner1<T>; | |
} | |
export function create2<T, U>(name: string) { | |
return new MiniRunner(name, 2) as Runner2<T, U>; | |
} | |
export function create3<T, U>(name: string) { | |
return new MiniRunner(name, 3) as Runner3<T, U>; | |
} | |
export interface Runner0<T> extends MiniRunner { | |
run(arg: T) | |
} | |
export interface Runner1<T> extends MiniRunner { | |
run(arg: T) | |
} | |
export interface Runner2<T, U> extends MiniRunner { | |
run(arg1: T, arg2: U) | |
} | |
export interface Runner3<T, U, V> extends MiniRunner { | |
run(arg1: T, arg2: U, agr3: V) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment