import { Evt, Handler, Ctx } from "../lib"; import { assert } from "../tools/typeSafety"; import { getPromiseAssertionApi } from "../tools/testing"; import { assertRepresentsSameDataFactory } from "../tools/inDepthComparison"; const { assertRepresentsSameData } = assertRepresentsSameDataFactory({ "takeIntoAccountArraysOrdering": false }); const { mustResolve } = getPromiseAssertionApi(); const evtText = new Evt(); const evtAge = new Evt(); const ctx = Evt.newCtx(); const prText = evtText.attach(ctx, () => assert(false)); const prAge = evtAge.attach(ctx, () => assert(false)); const handlers_ = [ ...(evtText.getHandlers() as Handler[]).map(handler => ({ handler, "evt": evtText })), ...(evtAge.getHandlers() as Handler[]).map(handler => ({ handler, "evt": evtAge })) ]; mustResolve({ "promise": ctx.getEvtDone().attachOnce( ([,,handlers]) => assertRepresentsSameData({ "got": handlers, "expected": handlers_ }) ) }); mustResolve({ "promise": evtAge.getEvtDetach().attachOnce(handler => assert(handler.ctx === ctx)), "delay": 0 }); const prTest= Promise.all([ mustResolve({ "promise": ctx.getEvtDetach().waitFor( ({ handler, evt }) => ( evt === evtText && handler.ctx === ctx && handler.timeout === undefined && handler.promise === prText ) ) }), mustResolve({ "promise": ctx.getEvtDetach().waitFor( ({ handler, evt }) => ( evt === evtAge && handler.ctx === ctx && handler.timeout === undefined && handler.promise === prAge ) ) }) ]); ctx.done(); assert(evtText.getHandlers().length === 0); assert(evtAge.getHandlers().length === 0); evtText.post("nothing"); evtAge.post(0); prTest.then(()=> console.log("PASS".green));