Archived
Private
Public Access
1
0

Initial commit

This commit is contained in:
2022-09-04 12:45:01 +02:00
commit f4a01d6a69
11601 changed files with 4206660 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
namespace TSE {
export interface IMessageHanlder {
onMessage(message: Message): void;
}
}

View File

@@ -0,0 +1,40 @@
namespace TSE {
export enum MessagePriority {
NORMAL,
HIGH
}
export class Message {
public code: string;
public context: any;
public sender: any;
public priority: MessagePriority;
public constructor(code: string, sender: any, context?: any, priority: MessagePriority = MessagePriority.NORMAL) {
this.code = code;
this.context = context;
this.sender = sender;
this.priority = priority;
}
public static send(code: string, sender: any, context?: any): void {
MessageBus.post(new Message(code, sender, context));
}
public static sendPriority(code: string, sender: any, context?: any): void {
MessageBus.post(new Message(code, sender, context, MessagePriority.HIGH));
}
public static subscribe(code: string, handler: IMessageHanlder): void {
MessageBus.addSubscription(code, handler);
}
public static unsubscribe(code: string, handler: IMessageHanlder): void {
MessageBus.removeSubscription(code, handler);
}
}
}

View File

@@ -0,0 +1,56 @@
namespace TSE {
export class MessageBus {
private static _subscriptions: { [code: string]: IMessageHanlder[] } = {};
private static _normalQueueMessagePerUpdate: number = 10;
private static _normalMessageQueue: MessageSubscriptionNode[] = [];
private constructor() { }
public static addSubscription(code: string, handler: IMessageHanlder): void {
if (MessageBus._subscriptions[code] === undefined)
MessageBus._subscriptions[code] = [];
if (MessageBus._subscriptions[code].indexOf(handler) !== -1)
console.warn("Attempting to add a duplicate hanlder to code: '" + code + "'. Subscription not added.");
else
MessageBus._subscriptions[code].push(handler);
}
public static removeSubscription(code: string, handler: IMessageHanlder): void {
if (MessageBus._subscriptions[code] === undefined) {
console.warn("Cannot unsubscribe handler from code: '" + code + "'. Because that code is not subscribed to.");
return;
}
let nodeIndex = MessageBus._subscriptions[code].indexOf(handler);
if (nodeIndex !== -1)
MessageBus._subscriptions[code].splice(nodeIndex, 1);
}
public static post(message: Message): void {
console.log("Message posted:", message);
let handlers = MessageBus._subscriptions[message.code];
if (handlers === undefined) return;
for (let handler of handlers) {
if (message.priority === MessagePriority.HIGH) handler.onMessage(message);
else MessageBus._normalMessageQueue.push(new MessageSubscriptionNode(message, handler));
}
}
public static update(time: number): void {
if (MessageBus._normalMessageQueue.length === 0) return;
let limit = Math.min(MessageBus._normalQueueMessagePerUpdate, MessageBus._normalMessageQueue.length);
for (let i = 0; i < limit; i++) {
let node = MessageBus._normalMessageQueue.pop();
node.handler.onMessage(node.message);
}
}
}
}

View File

@@ -0,0 +1,15 @@
namespace TSE {
export class MessageSubscriptionNode {
public message: Message;
public handler: IMessageHanlder;
public constructor(message: Message, handler: IMessageHanlder) {
this.message = message;
this.handler = handler;
}
}
}