Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | 4x 9x 4x 7x 7x 5x 4x 3x 4x 5x 5x 4x 4x 5x 5x 1x 1x 1x 4x | import { Message } from '../model/message';
import { HasSubscribers, Publisher, Subscriber, Subscription } from './publisher';
/**
* A Publisher that handle all the data in memory. It is a very simple implementation that should be used
* only for development and test purposes.
*/
export class InMemoryPublisher implements Publisher, HasSubscribers {
private listeners: Map<string, Array<Subscriber>> = new Map();
public async publish(message: Message) {
const aggregationListeners = this.listeners.get(message.stream.aggregation);
if (aggregationListeners != null && aggregationListeners.length) {
aggregationListeners.forEach(subscriber => subscriber(message));
return new Date().getTime().toString();
}
return null;
}
public async subscribe(aggregation: string, subscriber: Subscriber): Promise<Subscription> {
let aggregateListeners = this.listeners.get(aggregation);
if (!aggregateListeners) {
aggregateListeners = new Array<Subscriber>();
this.listeners.set(aggregation, aggregateListeners);
}
aggregateListeners.push(subscriber);
return {
remove: async () => {
const index = aggregateListeners.indexOf(subscriber);
aggregateListeners.splice(index, 1);
}
};
}
}
|