import {IConnection} from "../execution"; import {InsertableLog, LogUtil} from "../log"; import {ITable} from "../table"; import {Row} from "../row"; export type Delegate = (args : T) => (void|Promise); export class EventImpl { private delegates : Delegate[] = []; public has (d : Delegate) : boolean { const index = this.delegates.indexOf(d); return index >= 0; } public add (d : Delegate) : void { if (this.has(d)) { return; } this.delegates.push(d); } public remove (d : Delegate) : void { const index = this.delegates.indexOf(d); if (index >= 0) { this.delegates.splice(index, 1); } } public async invoke (args : T) { //We make a copy because these delegates could remove themselves when called. //Or add other delegates. //Modifying arrays when iterating over them is a bad idea. const delegates = this.delegates.slice(); for (let d of delegates) { await d(args); } } } export class Event extends EventImpl<{ connection : IConnection, row : RowT, }> { } export class TrackEvent extends EventImpl<{ connection : IConnection, trackResult : LogUtil.TrackResult, }> { } export class TableEvent extends EventImpl<{ connection : IConnection, row : Row, }> { }