/**
 * Copyright (c) 2018-present, tarant
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */ import Actor from '../actor-system/actor' ; import ActorSystem from '../actor-system/actor-system' ; import uuid from '../helper/uuid' ; type TopicSeenAs < T > = T & Topic < T > ; export default class Topic < T > extends Actor { param system param name param consumerClass public static for < T > ( system , name , consumerClass ) { return system . actorOf ( Topic , [ name , consumerClass ] ) as any ; } private readonly subscriptions : Map < string , T > ; public constructor ( topicName , consumerClass ) { super ( 'topics/' + topicName ) ; this . subscriptions = new Map ( ) ; const props = Object . getOwnPropertyNames ( consumerClass . prototype ) ; props . filter ( ( k ) => k !== 'constructor' ) . forEach ( ( k ) => { ; ( this as any ) . constructor . prototype [ k ] = ( ... args ) => { this . subscriptions . forEach ( ( actor ) => actor [ k ] . apply ( actor , args ) ) ; } ; } ) ; } public subscribe ( t ) { const id = uuid ( ) ; this . subscriptions . set ( id , t ) ; return id ; } public unsubscribe ( id ) { this . subscriptions . delete ( id ) ; } }