Skip to content

Commit

Permalink
✨ Add require priority
Browse files Browse the repository at this point in the history
  • Loading branch information
richardDobron committed Nov 15, 2024
1 parent a0372e8 commit 4335e97
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 14 deletions.
34 changes: 20 additions & 14 deletions src/BigPipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class BigPipe
{
private const JAVASCRIPT_REQUIRE_REGEX = "/^require\(['\"\[]+(?<module>.+?)['\"\]]+\)(\.(?<method>\w+)\(\))?$/";

private static $priorities = [];
private static $jsmods = [
"require" => [],
];
Expand Down Expand Up @@ -60,41 +61,46 @@ public static function parseRequireCall($fragment): ?array
* @throws BigPipeInvalidArgumentException
* @throws \Throwable
*/
public function require($fragment, array $args = []): self
public function require($fragment, array $args = [], int $priority = null): self
{
if (!self::isValidRequireCall($fragment)) {
throw new BigPipeInvalidArgumentException("Invalid call.");
}

$fragmentParts = self::parseRequireCall($fragment);
$priorities = self::$priorities;
$requires = self::$jsmods['require'];

$require = [
$fragmentParts['module'],
$fragmentParts['method'] ?? null,
];

if (!empty($args)) {
try {
static::$jsmods[__FUNCTION__][] = [];
$lastIndex = count(static::$jsmods[__FUNCTION__]) - 1;
try {
static::$jsmods[__FUNCTION__][] = [];
$lastIndex = array_key_last(static::$jsmods[__FUNCTION__]);
static::$priorities[] = $priority ?? $lastIndex;

$require[] = self::transformObjectString($args);
static::$jsmods[__FUNCTION__][$lastIndex] = array_trim($require);
} catch (\Throwable $exception) {
unset(static::$jsmods[__FUNCTION__][$lastIndex]);
static::$jsmods[__FUNCTION__] = array_values(static::$jsmods[__FUNCTION__]);

throw $exception;
if (! empty($args)) {
$transformedArgs = self::transformObjectString($args);
$require[] = $transformedArgs;
}
} else {
static::$jsmods[__FUNCTION__][] = array_trim($require);

static::$jsmods[__FUNCTION__][$lastIndex] = array_trim($require ?? []);
} catch (\Throwable $exception) {
static::$jsmods[__FUNCTION__] = $requires;
static::$priorities = $priorities;

throw $exception;
}

return $this;
}

public static function jsmods(): array
{
array_multisort(static::$priorities, static::$jsmods['require']);

return static::$jsmods;
}

Expand Down
136 changes: 136 additions & 0 deletions tests/AsyncResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,22 @@
namespace AsyncResponseTest;

use dobron\BigPipe\AsyncResponse;
use dobron\BigPipe\DialogResponse;
use dobron\BigPipe\Exceptions\BigPipeInvalidArgumentException;
use PHPUnit\Framework\TestCase;

class TestClass
{
public function __toString()
{
$response = new AsyncResponse();

$response->bigPipe()->require(['second'], [123]);

return 'test';
}
}

/**
* @runTestsInSeparateProcesses
*/
Expand Down Expand Up @@ -101,6 +114,129 @@ public function testAsyncResponse(): void
]);
}

public function testRequireInRequire(): void
{
$response = new DialogResponse();

$response->bigPipe()->require("require('first').init()");

$response->setDialog(new TestClass())
->dialog([
'backdrop' => 'static',
'keyboard' => false,
]);

$response->bigPipe()->require("require('third')");

$this->assertEquals($response->getResponse(), [
'payload' => [],
'domops' => [],
'jsmods' => [
'require' => [
[
'first', 'init'
],
[
'bigpipe-util/src/core/Dialog', 'render', [
[
'backdrop' => 'static',
'keyboard' => false,
'content' => 'test',
'controller' => null,
],
[]
]
],
[
'second', null, [123]
],
[
'third'
],
]
],
'__ar' => 1,
]);
}

public function testPriorityInRequire(): void
{
$response = new AsyncResponse();

$response->bigPipe()->require("require('someLowPriorityFunction')", [20], 20);
$response->bigPipe()->require("require('someMediumPriorityFunction')", [3], 3);
$response->bigPipe()->require("require('somePriorityFunction')", [0], 0);
$response->bigPipe()->require("require('someSuperHighPriorityFunction')", [-1], -1);
$response->bigPipe()->require("require('someFunctionWithDefaultPriority')", [1]);
$response->bigPipe()->require("require('someFunctionWithDefaultPriority')", [2]);
$response->bigPipe()->require("require('someFunctionWithDefaultPriority')", [3]);
$response->bigPipe()->require("require('someFunctionWithDefaultPriority')", [4]);

$this->assertEquals($response->getResponse(), [
'payload' => [],
'domops' => [],
'jsmods' => [
'require' => [
[
'someSuperHighPriorityFunction',
null,
[
-1,
],
],
[
'somePriorityFunction',
null,
[
0,
],
],
[
'someMediumPriorityFunction',
null,
[
3,
],
],
[
'someFunctionWithDefaultPriority',
null,
[
1,
],
],
[
'someFunctionWithDefaultPriority',
null,
[
2,
],
],
[
'someFunctionWithDefaultPriority',
null,
[
3,
],
],
[
'someFunctionWithDefaultPriority',
null,
[
4,
],
],
[
'someLowPriorityFunction',
null,
[
20,
],
],],],
'__ar' => 1,
]);
}

public function testReplaceWithoutSelector(): void
{
$response = new AsyncResponse();
Expand Down

0 comments on commit 4335e97

Please sign in to comment.