Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Tests

on:
push:
branches: [main]
pull_request:

jobs:
tests:
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
# 8.3 is the minimum composer.json allows.
php: [8.3, 8.4]

name: Tests - PHP ${{ matrix.php }}

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, simplexml, mbstring, openssl, sodium
coverage: none

# composer.lock is not committed, so this resolves fresh each run,
# including nativephp/mobile from mobile-air's idea/asgard branch.
- name: Install dependencies
run: composer update --no-interaction --prefer-dist

- name: Run tests
run: vendor/bin/pest

code-style:
runs-on: ubuntu-latest

name: Code Style

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.4
extensions: dom, simplexml, mbstring
coverage: none

- name: Install dependencies
run: composer update --no-interaction --prefer-dist

- name: Run Pint
run: vendor/bin/pint --test
67 changes: 55 additions & 12 deletions src/Asgard/ServerRegion.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ class ServerRegion
* state, and the server rendered each fragment for one state. A
* WeakMap also lets a fragment go when its component does.
*
* Each fragment is held prepared (see prepare()), not as the tree
* that arrived.
*
* @var \WeakMap<NativeComponent, array<string, array>>|null
*/
protected static ?\WeakMap $fragments = null;
Expand Down Expand Up @@ -388,14 +391,14 @@ public static function applyFragment(string $region, array|string $tree, ?Native
return false;
}

$nodes = static::checkCallbacks($nodes, $region);
$prepared = static::prepare(static::checkCallbacks($nodes, $region));

if ($owner === null) {
static::$detached[$region] = $nodes;
static::$detached[$region] = $prepared;
unset(static::$registries[$region]);
} else {
$held = static::fragments()[$owner] ?? [];
$held[$region] = $nodes;
$held[$region] = $prepared;
static::fragments()[$owner] = $held;
static::dropRegistry($region, $owner);

Expand Down Expand Up @@ -1554,7 +1557,7 @@ public static function checkContent(mixed $tree, ?string &$reason = null): ?arra
*/
public static function paint(array $nodes): void
{
static::emit($nodes);
static::emit(static::prepare($nodes));
}

/**
Expand Down Expand Up @@ -1898,25 +1901,65 @@ protected static function keyPath(string $name): string
}

/**
* Emit a normalized node list through the collector's public
* Turn a checked node list into the form a region holds: three lists
* in pre-order, index for index. `types` and `counts` give each
* node's type and how many children follow it, and `attrs` its
* collector attribute bag.
*
* A fragment is applied once and painted on every frame, so the bags
* are built here rather than in emit().
*
* @return array{roots: int, types: list<string>, counts: list<int>, attrs: list<array>}
*/
protected static function prepare(array $nodes): array
{
$prepared = ['roots' => count($nodes), 'types' => [], 'counts' => [], 'attrs' => []];

$walk = function (array $nodes) use (&$walk, &$prepared): void {
foreach ($nodes as $node) {
$children = static::normalize($node['children'] ?? []);

$prepared['types'][] = (string) $node['type'];
$prepared['counts'][] = count($children);
$prepared['attrs'][] = static::attrsFor($node);

$walk($children);
}
};

$walk($nodes);

return $prepared;
}

/**
* Emit a prepared fragment through the collector's public
* open()/leaf()/close(), which is what makes a fragment work
* identically in the Element-tree and streaming paths.
*/
protected static function emit(array $nodes): void
protected static function emit(array $prepared): void
{
foreach ($nodes as $node) {
$type = (string) $node['type'];
$attrs = static::attrsFor($node);
$children = static::normalize($node['children'] ?? []);
$i = 0;
static::emitSiblings($prepared, $prepared['roots'], $i);
}

/** Emit `$count` siblings starting at node `$i`, and their subtrees. */
protected static function emitSiblings(array $prepared, int $count, int &$i): void
{
for ($n = 0; $n < $count; $n++) {
$type = $prepared['types'][$i];
$children = $prepared['counts'][$i];
$attrs = $prepared['attrs'][$i];
$i++;

if ($children === []) {
if ($children === 0) {
NativeElementCollector::leaf($type, $attrs);

continue;
}

NativeElementCollector::open($type, $attrs);
static::emit($children);
static::emitSiblings($prepared, $children, $i);
NativeElementCollector::close();
}
}
Expand Down
47 changes: 47 additions & 0 deletions tests/Asgard/ServerRegionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,53 @@ function renderNativeView(string $name, string $template): Element
expect(json_encode($region))->not->toContain('Loading sales');
});

it('keeps the shape of a fragment with several roots, siblings and depths', function () {
$template = <<<'BLADE'
<native:column fill>
@server('feed')
<native:text>Loading…</native:text>
@endserver
</native:column>
BLADE;

// Held prepared (flat, in pre-order), so this checks the child counts
// put every node back under the right parent.
ServerRegion::applyFragment('feed', [
['type' => 'column', 'key' => 'a', 'children' => [
['type' => 'text', 'key' => 'a1', 'props' => ['text' => 'a1']],
['type' => 'row', 'key' => 'a2', 'children' => [
['type' => 'text', 'key' => 'a2x', 'props' => ['text' => 'a2x']],
['type' => 'text', 'key' => 'a2y', 'props' => ['text' => 'a2y']],
]],
['type' => 'text', 'key' => 'a3', 'props' => ['text' => 'a3']],
]],
['type' => 'spacer', 'key' => 'b'],
['type' => 'text', 'key' => 'c', 'props' => ['text' => 'c']],
]);

$shape = function (array $node) use (&$shape): array {
return [$node['type'], $node['props']['text'] ?? null, array_map($shape, $node['children'] ?? [])];
};

$first = renderNativeView('asgard-shape', $template)->toArray(new CallbackRegistry);

expect(array_map($shape, $first['children'][0]['children']))->toBe([
['column', null, [
['text', 'a1', []],
['row', null, [['text', 'a2x', []], ['text', 'a2y', []]]],
['text', 'a3', []],
]],
['spacer', null, []],
['text', 'c', []],
]);

expect($first['children'][0]['children'][0]['children'][1]['children'][1]['id'])
->toBe(Element::fnv1a32('/feed/a/a2/a2y'));

// A held fragment paints the same on every frame.
expect(renderNativeView('asgard-shape', $template)->toArray(new CallbackRegistry))->toBe($first);
});

it('derives fragment node ids inside the region key path, not at the root', function () {
ServerRegion::applyFragment('sales-summary', [
'type' => 'column',
Expand Down
Loading