Popular
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
import { Evt } from "../lib/index.ts";import { assert } from "https://deno.land/x/tsafe@v1.4.1/assert.ts";import { getPromiseAssertionApi } from "../tools/testing/getPromiseAssertionApi.ts";
(async () => {
{
const { mustResolve } = getPromiseAssertionApi();
const evtText = new Evt<string>();
const evtAge = new Evt<number>();
const ctx = Evt.newCtx();
const evtUnion = Evt.merge(ctx, [evtText, evtAge]);
assert( ctx.evtAttach.postCount ===2 );
const text = "ok";
const age = 88;
{
const pr = mustResolve({ "promise": evtUnion.waitFor(), "expectedData": text });
evtText.post(text);
await pr;
}
{
const pr = mustResolve({ "promise": evtUnion.waitFor(), "expectedData": age });
evtAge.post(age);
await pr;
}
assert(evtText.getHandlers().length === 1); assert(evtAge.getHandlers().length === 1);
ctx.getHandlers() .filter(({ evt }) => evt === evtText) .forEach(({ handler }) => handler.detach()) ;
assert(evtText.getHandlers().length === 0); assert(evtAge.getHandlers().length === 1);
ctx.getHandlers() .filter(({ evt }) => evt === evtAge) .forEach(({ handler }) => handler.detach()) ;
assert( ctx.evtDetach.postCount ===2 );
assert(evtAge.getHandlers().length === 0);
}
{
const { mustResolve } = getPromiseAssertionApi();
const evtText = new Evt<string>();
const evtAge = new Evt<number>();
const evtUnion = Evt.merge([evtText, evtAge]);
const text = "ok";
const age = 88;
{
const pr = mustResolve({ "promise": evtUnion.waitFor(), "expectedData": text });
evtText.post(text);
await pr;
}
{
const pr = mustResolve({ "promise": evtUnion.waitFor(), "expectedData": age });
evtAge.post(age);
await pr;
}
assert(evtText.getHandlers().length === 1); assert(evtAge.getHandlers().length === 1);
}
console.log("PASS");
})();