laya-coreui-es
Version:
laya核心库、ui库es版本,剔除了媒体、TTF等功能。基于 LayaAir-release_2.12.0 修改
58 lines (57 loc) • 1.82 kB
JavaScript
export class Handler {
constructor(caller = null, method = null, args = null, once = false) {
this.once = false;
this._id = 0;
this.setTo(caller, method, args, once);
}
setTo(caller, method, args, once = false) {
this._id = Handler._gid++;
this.caller = caller;
this.method = method;
this.args = args;
this.once = once;
return this;
}
run() {
if (this.method == null)
return null;
var id = this._id;
var result = this.method.apply(this.caller, this.args);
this._id === id && this.once && this.recover();
return result;
}
runWith(data) {
if (this.method == null)
return null;
var id = this._id;
if (data == null)
var result = this.method.apply(this.caller, this.args);
else if (!this.args && !data.unshift)
result = this.method.call(this.caller, data);
else if (this.args)
result = this.method.apply(this.caller, this.args.concat(data));
else
result = this.method.apply(this.caller, data);
this._id === id && this.once && this.recover();
return result;
}
clear() {
this.caller = null;
this.method = null;
this.args = null;
return this;
}
recover() {
if (this._id > 0) {
this._id = 0;
Handler._pool.push(this.clear());
}
}
static create(caller, method, args = null, once = true) {
if (Handler._pool.length)
return Handler._pool.pop().setTo(caller, method, args, once);
return new Handler(caller, method, args, once);
}
}
Handler._pool = [];
Handler._gid = 1;