schickling/chromeless

Add support for flatMap

garbles opened this issue · 0 comments

This is a Feature Proposal

If I have to evaluate something on the page, it's inconvenient to have to assign the instance to a variable, run my evaluation and then continue on with the assigned variable. e.g.

const chromeless = new Chromeless();

const c1 = chromeless
  .goto('url')
  .click('a#feed_page');

const itemLinks = await c1.evaluate(...); // get a list of urls or something
const itemLink = _.sample(items);

return c1
  .click(itemLink)
  .type('Hi', '#comment_input')
  .click('#submit')
  .end();

Instead, I would like to be able to do something like below where I can simply keep chaining operations together.

const chromeless = new Chromeless();

return chromeless
  .goto('url')
  .click('a#feed_page')
  .flatMap(async c => {
    const itemLinks = await c.evaluate(...); // get a list of urls or something
    const itemLink = _.sample(items);

    return c.click(itemLink);
  })
  .type('Hi', '#comment_input')
  .click('#submit')
  .end()

The type signature would be something like

flatMap<U>(c: Chromeless<T>): Chromeless<U>;

I don't think implementing something like would require much effort and I can do it if you're digging this idea.