Skip to content
This repository was archived by the owner on Oct 14, 2022. It is now read-only.
This repository was archived by the owner on Oct 14, 2022. It is now read-only.

Add lazy/eager instructions #13

@nesk

Description

@nesk

The problem

Currently, it's up to the implementation to determine if the instruction should be awaited or not. For example, PuPHPeteer always await the instructions.

However, awaiting all the instructions is a problem. The waitForNavigation() method returns a promise that will be resolved only once the goto() method is called. Since PuPHPeteer is already awaiting for the first method, the second method cannot be called and Node will throw a Navigation Timeout Exceeded error. See rialto-php/puphpeteer#4 for more information about this specific bug.

How this can be solved

Instead of letting the implementation choose to use await or not for all of its instructions, Rialto could let the implementation choose a default resolving strategy (await or not) and the user will be able to override this behaviour for some instructions.

For example, Rialto could provide a useAwaitByDefault() method to let the implementation define the preferred resolving strategy:

async handleInstruction(instruction, responseHandler, errorHandler)
{
    instruction.useAwaitByDefault(true);

    // ...
}

Then the user could simply execute an instruction:

$resource->someMethodReturningAPromise(); // This will return the value of the resolved Promise

Or he could override the resolve strategy:

$resource->lazy->someMethodReturningAPromise(); // This will return the Promise

Of course, the implementation could also choose to not await by default ( instruction.useAwaitByDefault(false)), but the user could override this too:

$resource->someMethodReturningAPromise(); // This will return the Promise
$resource->eager->someMethodReturningAPromise(); // This will return the value of the resolved Promise

Promises

Since it would be possible for an instruction to return a Promise, then we should provide some tools to use them.

A promise in PHP should be a BasicResource with a then() method which accepts a PHP callback with the resolved value as the first argument.

A PHP equivalent to Promise.all() should be provided, this would allow to wait for multiple promises. Typically, it would enable parallel calls (see #9):

$browser = (new Puppeteer)->launch();

$page1 = $browser->newPage();
$page2 = $browser->newPage();

$request1 = $page1->lazy->goto('https://github.com/nesk/');
$request2 = $page2->lazy->goto('https://github.com/-not--a--real--profile-/');

Promise::all([$request1, $request2]).then(function ($responses) {
    echo $responses[0]->status(); // 200
    echo $responses[1]->status(); // 404
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions