Class Watcher<T>

Watches a matcher function, and trigger whenever it changes from matching to not.

Remarks

Whenever wait resolves, it runs match, and triggers enter with the match result, or leave without, then wait again.

By default, wait is a function that resolves on every page navigation or mutation.

Example

async function step(ctx: Context) {
const spec = {
wait(): Promise<void> {
return new Promise((resolve, reject) => {
setTimeout(() => { reject() }, 5000)
})
},
async match() {
return await ctx.page.waitForSelector('#input:focus');
},
async enter(match): Promise<void> {
return await match.evaluate(el => {
alert(`you have just focused ${el.id}`);
});
},
leave(match): Promise<void> {
return new Promise(resolve => {
alert(`you are left the input so gonna be redirected to google, just in case`);
ctx.page.goto('https://google.com');
resolve();
})
}
};
const watcher = new Watcher(ctx, spec);
await watcher.start();
}

Param: ctx

Context object.

Param: spec

Options for running the watcher.

Type Parameters

  • T

Methods

  • Stops the watcher.

    Returns Promise<void>

    Remarks

    If matching when stopped, will also trigger leave.