import User from './lib/user' ; import ArtalkConfig from '~/types/artalk-config' ; import { EventPayloadMap , Event , EventScopeType , Handler } from '~/types/event' ; /**
 * Artalk Context
 */ export default class Context { public cid : number ; public $root : HTMLElement ; public conf : ArtalkConfig ; public user : User ; private eventList : Event [ ] = [ ] ; public constructor ( rootEl , conf ) { this . cid = + new Date ( ) ; this . $root = rootEl ; this . conf = conf ; this . user = new User ( this . conf ) ; this . $root . setAttribute ( 'atk-run-id' , this . cid . toString ( ) ) ; } public on < K extends keyof EventPayloadMap > ( name , handler , scope = 'internal' ) { this . eventList . push ( { name , handler : handler as any , scope } ) ; } public off < K extends keyof EventPayloadMap > ( name , handler ? , scope = 'internal' ) { this . eventList = this . eventList . filter ( ( evt ) => { if ( handler ) return ! ( evt . name === name && evt . handler === handler && evt . scope === scope ) ; return ! ( evt . name === name && evt . scope === scope ) ; } ) ; } public trigger < K extends keyof EventPayloadMap > ( name , payload ? , scope ? ) { this . eventList . filter ( ( evt ) => evt . name === name && ( scope ? ( evt . scope === scope ) : true ) ) . map ( ( evt ) => evt . handler ) . forEach ( ( handler ) => handler ( payload as any ) ) ; } }