emitter 支持 context
This commit is contained in:
Vendored
+6
-1
@@ -1044,10 +1044,15 @@ declare class DrawUtils {
|
|||||||
declare class Emitter<T> {
|
declare class Emitter<T> {
|
||||||
private _messageTable;
|
private _messageTable;
|
||||||
constructor();
|
constructor();
|
||||||
addObserver(eventType: T, handler: Function): void;
|
addObserver(eventType: T, handler: Function, context: any): void;
|
||||||
removeObserver(eventType: T, handler: Function): void;
|
removeObserver(eventType: T, handler: Function): void;
|
||||||
emit(eventType: T, data?: any): void;
|
emit(eventType: T, data?: any): void;
|
||||||
}
|
}
|
||||||
|
declare class FuncPack {
|
||||||
|
func: Function;
|
||||||
|
context: any;
|
||||||
|
constructor(func: Function, context: any);
|
||||||
|
}
|
||||||
declare class GlobalManager {
|
declare class GlobalManager {
|
||||||
static globalManagers: GlobalManager[];
|
static globalManagers: GlobalManager[];
|
||||||
private _enabled;
|
private _enabled;
|
||||||
|
|||||||
@@ -4880,7 +4880,7 @@ var Emitter = (function () {
|
|||||||
function Emitter() {
|
function Emitter() {
|
||||||
this._messageTable = new Map();
|
this._messageTable = new Map();
|
||||||
}
|
}
|
||||||
Emitter.prototype.addObserver = function (eventType, handler) {
|
Emitter.prototype.addObserver = function (eventType, handler, context) {
|
||||||
var list = this._messageTable.get(eventType);
|
var list = this._messageTable.get(eventType);
|
||||||
if (!list) {
|
if (!list) {
|
||||||
list = [];
|
list = [];
|
||||||
@@ -4888,7 +4888,7 @@ var Emitter = (function () {
|
|||||||
}
|
}
|
||||||
if (list.contains(handler))
|
if (list.contains(handler))
|
||||||
console.warn("您试图添加相同的观察者两次");
|
console.warn("您试图添加相同的观察者两次");
|
||||||
list.push(handler);
|
list.push(new FuncPack(handler, context));
|
||||||
};
|
};
|
||||||
Emitter.prototype.removeObserver = function (eventType, handler) {
|
Emitter.prototype.removeObserver = function (eventType, handler) {
|
||||||
this._messageTable.get(eventType).remove(handler);
|
this._messageTable.get(eventType).remove(handler);
|
||||||
@@ -4897,11 +4897,18 @@ var Emitter = (function () {
|
|||||||
var list = this._messageTable.get(eventType);
|
var list = this._messageTable.get(eventType);
|
||||||
if (list) {
|
if (list) {
|
||||||
for (var i = list.length - 1; i >= 0; i--)
|
for (var i = list.length - 1; i >= 0; i--)
|
||||||
list[i](data);
|
list[i].func.call(list[i].context, data);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
return Emitter;
|
return Emitter;
|
||||||
}());
|
}());
|
||||||
|
var FuncPack = (function () {
|
||||||
|
function FuncPack(func, context) {
|
||||||
|
this.func = func;
|
||||||
|
this.context = context;
|
||||||
|
}
|
||||||
|
return FuncPack;
|
||||||
|
}());
|
||||||
var GlobalManager = (function () {
|
var GlobalManager = (function () {
|
||||||
function GlobalManager() {
|
function GlobalManager() {
|
||||||
}
|
}
|
||||||
|
|||||||
Vendored
+1
-1
File diff suppressed because one or more lines are too long
Vendored
+6
-1
@@ -1044,10 +1044,15 @@ declare class DrawUtils {
|
|||||||
declare class Emitter<T> {
|
declare class Emitter<T> {
|
||||||
private _messageTable;
|
private _messageTable;
|
||||||
constructor();
|
constructor();
|
||||||
addObserver(eventType: T, handler: Function): void;
|
addObserver(eventType: T, handler: Function, context: any): void;
|
||||||
removeObserver(eventType: T, handler: Function): void;
|
removeObserver(eventType: T, handler: Function): void;
|
||||||
emit(eventType: T, data?: any): void;
|
emit(eventType: T, data?: any): void;
|
||||||
}
|
}
|
||||||
|
declare class FuncPack {
|
||||||
|
func: Function;
|
||||||
|
context: any;
|
||||||
|
constructor(func: Function, context: any);
|
||||||
|
}
|
||||||
declare class GlobalManager {
|
declare class GlobalManager {
|
||||||
static globalManagers: GlobalManager[];
|
static globalManagers: GlobalManager[];
|
||||||
private _enabled;
|
private _enabled;
|
||||||
|
|||||||
+10
-3
@@ -4880,7 +4880,7 @@ var Emitter = (function () {
|
|||||||
function Emitter() {
|
function Emitter() {
|
||||||
this._messageTable = new Map();
|
this._messageTable = new Map();
|
||||||
}
|
}
|
||||||
Emitter.prototype.addObserver = function (eventType, handler) {
|
Emitter.prototype.addObserver = function (eventType, handler, context) {
|
||||||
var list = this._messageTable.get(eventType);
|
var list = this._messageTable.get(eventType);
|
||||||
if (!list) {
|
if (!list) {
|
||||||
list = [];
|
list = [];
|
||||||
@@ -4888,7 +4888,7 @@ var Emitter = (function () {
|
|||||||
}
|
}
|
||||||
if (list.contains(handler))
|
if (list.contains(handler))
|
||||||
console.warn("您试图添加相同的观察者两次");
|
console.warn("您试图添加相同的观察者两次");
|
||||||
list.push(handler);
|
list.push(new FuncPack(handler, context));
|
||||||
};
|
};
|
||||||
Emitter.prototype.removeObserver = function (eventType, handler) {
|
Emitter.prototype.removeObserver = function (eventType, handler) {
|
||||||
this._messageTable.get(eventType).remove(handler);
|
this._messageTable.get(eventType).remove(handler);
|
||||||
@@ -4897,11 +4897,18 @@ var Emitter = (function () {
|
|||||||
var list = this._messageTable.get(eventType);
|
var list = this._messageTable.get(eventType);
|
||||||
if (list) {
|
if (list) {
|
||||||
for (var i = list.length - 1; i >= 0; i--)
|
for (var i = list.length - 1; i >= 0; i--)
|
||||||
list[i](data);
|
list[i].func.call(list[i].context, data);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
return Emitter;
|
return Emitter;
|
||||||
}());
|
}());
|
||||||
|
var FuncPack = (function () {
|
||||||
|
function FuncPack(func, context) {
|
||||||
|
this.func = func;
|
||||||
|
this.context = context;
|
||||||
|
}
|
||||||
|
return FuncPack;
|
||||||
|
}());
|
||||||
var GlobalManager = (function () {
|
var GlobalManager = (function () {
|
||||||
function GlobalManager() {
|
function GlobalManager() {
|
||||||
}
|
}
|
||||||
|
|||||||
Vendored
+1
-1
File diff suppressed because one or more lines are too long
@@ -1,12 +1,12 @@
|
|||||||
class Emitter<T> {
|
class Emitter<T> {
|
||||||
private _messageTable: Map<T, Function[]>;
|
private _messageTable: Map<T, FuncPack[]>;
|
||||||
|
|
||||||
constructor(){
|
constructor(){
|
||||||
this._messageTable = new Map<T, Function[]>();
|
this._messageTable = new Map<T, FuncPack[]>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public addObserver(eventType: T, handler: Function){
|
public addObserver(eventType: T, handler: Function, context: any){
|
||||||
let list: Function[] = this._messageTable.get(eventType);
|
let list: FuncPack[] = this._messageTable.get(eventType);
|
||||||
if (!list){
|
if (!list){
|
||||||
list = [];
|
list = [];
|
||||||
this._messageTable.set(eventType, list);
|
this._messageTable.set(eventType, list);
|
||||||
@@ -14,7 +14,7 @@ class Emitter<T> {
|
|||||||
|
|
||||||
if (list.contains(handler))
|
if (list.contains(handler))
|
||||||
console.warn("您试图添加相同的观察者两次");
|
console.warn("您试图添加相同的观察者两次");
|
||||||
list.push(handler);
|
list.push(new FuncPack(handler, context));
|
||||||
}
|
}
|
||||||
|
|
||||||
public removeObserver(eventType: T, handler: Function){
|
public removeObserver(eventType: T, handler: Function){
|
||||||
@@ -22,10 +22,20 @@ class Emitter<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public emit(eventType: T, data?: any){
|
public emit(eventType: T, data?: any){
|
||||||
let list: Function[] = this._messageTable.get(eventType);
|
let list: FuncPack[] = this._messageTable.get(eventType);
|
||||||
if (list){
|
if (list){
|
||||||
for (let i = list.length - 1; i >= 0; i --)
|
for (let i = list.length - 1; i >= 0; i --)
|
||||||
list[i](data);
|
list[i].func.call(list[i].context, data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class FuncPack {
|
||||||
|
public func: Function;
|
||||||
|
public context: any;
|
||||||
|
|
||||||
|
constructor(func: Function, context: any){
|
||||||
|
this.func = func;
|
||||||
|
this.context = context;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user