mirror of https://github.com/Squidex/squidex.git
16 changed files with 217 additions and 130 deletions
@ -0,0 +1,42 @@ |
|||
/* |
|||
* Squidex Headless CMS |
|||
* |
|||
* @license |
|||
* Copyright (c) Sebastian Stehle. All rights reserved |
|||
*/ |
|||
|
|||
import { MessageBus, MessageBusFactory } from './../'; |
|||
|
|||
class Event1 { } |
|||
class Event2 { } |
|||
|
|||
describe('MessageBus', () => { |
|||
it('should instantiate from factory', () => { |
|||
const messageBus = MessageBusFactory(); |
|||
|
|||
expect(messageBus).toBeDefined(); |
|||
}); |
|||
|
|||
it('should instantiate', () => { |
|||
const messageBus = new MessageBus(); |
|||
|
|||
expect(messageBus).toBeDefined(); |
|||
}); |
|||
|
|||
it('should publish events and subscribe', () => { |
|||
const messageBus = new MessageBus(); |
|||
const event1 = new Event1(); |
|||
const event2 = new Event2(); |
|||
|
|||
let lastEvent: any = null; |
|||
|
|||
messageBus.of(Event1).subscribe(event => { |
|||
lastEvent = event; |
|||
}); |
|||
|
|||
messageBus.publish(event1); |
|||
messageBus.publish(event2); |
|||
|
|||
expect(lastEvent).toBe(event1); |
|||
}); |
|||
}); |
|||
@ -0,0 +1,35 @@ |
|||
/* |
|||
* Squidex Headless CMS |
|||
* |
|||
* @license |
|||
* Copyright (c) Sebastian Stehle. All rights reserved |
|||
*/ |
|||
|
|||
import { Injectable } from '@angular/core'; |
|||
import { Observable, Subject } from 'rxjs'; |
|||
|
|||
interface Message { |
|||
channel: string; |
|||
data: any; |
|||
} |
|||
|
|||
export const MessageBusFactory = () => { |
|||
return new MessageBus(); |
|||
}; |
|||
|
|||
@Injectable() |
|||
export class MessageBus { |
|||
private message$ = new Subject<Message>(); |
|||
|
|||
public publish<T>(message: T): void { |
|||
const channel = (<any>message.constructor).name; |
|||
|
|||
this.message$.next({ channel: channel, data: message }); |
|||
} |
|||
|
|||
public of<T>(messageType: { new(...args: any[]): T }): Observable<T> { |
|||
const channel = (<any>messageType).name; |
|||
|
|||
return this.message$.filter(m => m.channel === channel).map(m => m.data); |
|||
} |
|||
} |
|||
@ -0,0 +1,8 @@ |
|||
/* |
|||
* Squidex Headless CMS |
|||
* |
|||
* @license |
|||
* Copyright (c) Sebastian Stehle. All rights reserved |
|||
*/ |
|||
|
|||
export class HistoryChannelUpdated { } |
|||
Loading…
Reference in new issue