sdk.watcher.watcher
Home > @matterway/sdk > Watcher > Watcher
Watcher.Watcher class
Watches a matcher function, and trigger whenever it changes from matching to not.
Signature:
export declare class Watcher<T>
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();
}
Constructors
Constructor | Modifiers | Description |
---|---|---|
(constructor)(ctx, spec) | Constructs a new instance of the Watcher class |
Properties
Property | Modifiers | Type | Description |
---|---|---|---|
lastMatch? | T | (Optional) |
Methods
Method | Modifiers | Description |
---|---|---|
getLastMatch() | Returns the last stored match, if any. | |
isMatching() | Checks if matcher is matching. | |
isRunning() | Checks if matcher is running. | |
start() | Starts the watcher | |
stop() | Stops the watcher. |