export default class Dictionary {
private items = {};
public length: number = 0;
constructor(key?:any[],value?:any[]){
if(!key || !value){
return;
}
if(key.length != value.length){
console.log("%c The length of the key and value do not match.", "color:#FF0000");
return;
}
for(var i = 0; i < key.length; ++i){
if(this.has(key[i])){
var last_value = this.get(key[i]);
console.log("%c The key already has a value( %c " + last_value +") and will be overwritten by the value( %c "+ value[i] +")", "color:#FF0000","color:#0000FF","color:#00FF00");
}
this.set(key[i],value[i]);
}
}
public has(key: any): boolean {
return key in this.items;
}
public set(key: any, value: any) {
this.items[key] = value;
this.length = this.get_length();
}
public get(key: any) {
return this.has(key) ? this.items[key] : undefined;
}
public push(key:any,item:any,create :boolean = true){
if(this.has(key)){
var items = this.get(key);
items.push(item);
this.set(key,items);
}else{
if(create){
var new_items = [item];
this.set(key,new_items);
console.log("%c key : " + key + " not found.will craete one", "color:#FF0000");
}else{
console.log("%c key : " + key + " not found.", "color:#FF0000");
}
}
}
public all_key(): string[] {
var values: string[] = [];
for (var key in this.items) {
values.push(key)
}
return values;
}
public remove(key: any): boolean {
if (this.has(key)) {
delete this.items[key];
this.length = this.get_length();
return true;
} else {
console.log("%c key : " + key + " not found.", "color:#FF0000");
}
return false;
}
public remove_item(key: any, item: any) {
if (this.has(key)) {
var items = this.get(key);
for (var i = 0; i < items.length; ++i) {
if (items[i] == item) {
items.splice(i, 1);
break;
}
}
this.set(key, items);
this.length = this.get_length();
return true;
} else {
console.log("%c key : " + key + " not found.", "color:#FF0000");
}
return false;
}
public clear() {
this.items = {};
this.length = 0;
}
public copy(): Dictionary {
var dic = new Dictionary();
var all = this.all_key();
for (var i = 0; i < all.length; ++i) {
dic.set(all[i], this.get(all[i]));
}
dic.length = this.length;
return dic;
}
private get_length(): number {
return Object.keys(this.items).length;
}
}
export default class MessageSystemHelper {
callback: Function = null;
target = null;
constructor(target, call) {
this.target = target;
this.callback = call;
}
same(target) {
return this.target == target;
}
}
import Dictionary from "./Dictionary";
import MessageSystemHelper from "./MessageSystemHelper";
export default class MessageSystem {
private static list: Dictionary = new Dictionary();
public static add_message(message: String, target?, callbacks?: Function) {
if (callbacks == null || target == null){
var msg = "add message '" + message + "' failed,"
msg += callbacks == null && target == null ?"target and callback is null":target == null ? "target is null" : "callback is null";
msg += ",please check it.";
console.log("%c "+ msg,"color:#FF0000");
return;
}
var call = [];
if (this.list.has(message)) {
call = this.list.get(message);
}
for (var i = 0; i < call.length; ++i) {
if (call[i].same(target)) {
console.log("%c '" + message + "' has same callback.","color:#FF0000");
return;
}
}
call.push(new MessageSystemHelper(target, callbacks));
this.list.set(message, call);
}
public static remove_message(message: String, target?) {
if (target == null){
console.log("%c remove '" + message +"' failed, target is null.","color:#FF0000");
return;
}
var call: MessageSystemHelper[] = this.list.get(message);
if (call) {
for (var i = 0; i < call.length; ++i) {
if (call[i].same(target)) {
call.splice(i, 1);
break;
}
}
if (call.length > 0) {
this.list.set(message, call);
} else {
this.list.remove(message);
}
}
}
public static clear_message() {
this.list.clear();
}
public static send_messasge(message, arg?) {
console.log("%c send message " + message,"color:#0000FF");
var call: MessageSystemHelper[] = this.list.get(message);
if (call) {
for (var i = 0; i < call.length; ++i) {
var fun: Function = call[i].callback;
fun && fun(arg);
}
} else {
console.log("%c send message '" + message + "' failed,'" + message + "' no function.","color:#FF0000");
}
}
}