Lists
A list is the ids a part of a page draws, in order, with what the browser needs to draw more of it and to place a new one. It lives in the page state by its key (state.lists['rooms:1']), beside the rows it names (state.rows.room[12]). The server puts it there on a whole load. The browser asks it as JSON after a move from a page file, after an event that changes it, and for its next page. One kind of list is registered once, and every list of it is named by a key.
For the whole path of a part drawn from data, see Building for instant pages and live updates.
Registering a kind
function my_rooms_lists( $live ) {
$live->register_list( 'rooms', array(
'args' => array(
'space' => array( 'type' => 'integer', 'minimum' => 0, 'default' => 0 ),
'page' => array( 'type' => 'integer', 'minimum' => 1, 'maximum' => 50, 'default' => 1 ),
),
'list' => 'my_rooms_list',
'key' => 'my_rooms_key',
'rows' => 'my_rooms_rows',
) );
}
add_action( 'poly_community_register_lists', 'my_rooms_lists' );
function my_rooms_key( $rest ) {
$bits = explode( ':', (string) $rest );
return array( 'space' => absint( $bits[0] ?? 0 ), 'page' => max( 1, absint( $bits[1] ?? 1 ) ) );
}
function my_rooms_rows( $ids, $request ) {
foreach ( $ids as $id ) {
my_rooms_build_row( my_rooms_get( (int) $id ) ); // the row type builder, as Live updates in code has it
}
}
list( $args )puts the rows of the list for its reader and returns its shape (below), null for nothing there, or aWP_Error. It is the same function the block calls on a whole load, so both draw alike.argsare its query arguments as REST schemas. What is not in them is not passed on.key( $rest )turns what followsrooms:in a key into its arguments, or null for a key it does not know. A page file names its lists by key, and the browser asks them by key.rows( $ids, $request )puts the rows of those ids, for a row read again after an event or a press. A kind may haverowsalone, as the corememberskind does.
The kind is read at GET /wp-json/poly-community/v1/lists/rooms with its arguments, GET lists/rooms?ids=1,2 for rows, and many lists at once with GET lists?keys=rooms:0:1,rooms:12:1, as a page from its file asks them in its one request. Each answer holds the list and its rows (data), in the shape of the page state.
The shape of a list
array(
'ids' => array( 12, 7, 3 ), // in the order drawn
'type' => 'room', // the row type its ids are of
'more' => true, // whether a next page exists
'page' => 1,
'args' => $args, // what it was read with, asked again with
// Optional:
'events' => 'room.created room.deleted:space=12',
'reads' => array( 'room.updated' ),
'min' => 30,
)
eventsnames the events the list follows (a room added, one gone). An event about the reader asks the whole list again: their own change, made on this page or in another tab, or a kind told to them alone. A change by anyone else asks nothing. The list stays as drawn under the reader, so it never shifts while they read it, and shows the change on their next move. A.deletedevent of a row the list shows is the one exception: the row leaves at once.readsnames the events that change one of its rows, the one whose id the event names asid. When the event carries that row in the type of the list (the row typeevents, see Instant and live), the row is written in place and nothing is asked. Otherwise an event about the reader reads the row again through the kindrows, batched, one request at a time for a page, and a change by anyone else shows on the next move of the reader.minis how often at most, in seconds, an event about the reader asks the list again.
A list follows the realtime connection where the site has one and the poll where it has not. Either way, only the events of the reader make it ask, so a page open for hours costs the server nothing for the changes of others.
How events are named
A declaration is tokens separated by spaces. A token is an event type (room.created), or a type and what its payload must hold: key=value pairs separated by commas (room.deleted:space=12), or a word alone for the payload change or kind (item.changed:pinned). Where the payload holds a list, a value matches when one of its entries is that value.
Drawing a list
On a whole load the block puts the list. While a page file is drawn, it names it alone. The same markup draws it either way, from the state:
$lists = Poly_Community_Address_Lists();
$slot = $lists->new_slot();
$key = 'rooms:' . (int) $space . ':1';
if ( ! $lists->is_skeleton() ) {
$list = my_rooms_list( array( 'space' => (int) $space, 'page' => 1 ) );
if ( $list ) {
Poly_Community()->live_data->put_list( $key, $list );
}
}
$lists->state();
wp_interactivity_state( 'poly-community', array( 'tileKeys' => array( $slot => $key ) ) );
A list is named by its slot in tileKeys, which a page file and its one request read. A list whose every row is one part of trusted HTML (html.row in its row) draws with the core getters, before any script of yours has loaded:
<div data-poly-tiles="<?php echo esc_attr( $slot ); ?>" data-poly-from-data
data-wp-context="<?php echo esc_attr( wp_json_encode( array( 'tileSlot' => $slot ) ) ); ?>"
data-wp-bind--data-poly-list="state.tileKey">
<template data-wp-each--item="state.partRows" data-wp-each-key="context.item.id">
<div data-wp-bind--data-poly-tile="context.item.id">
<div class="poly-tile-part" data-wp-bind--data-poly-html="state.rowPartKey" data-wp-html="state.rowPart"></div>
</div>
</template>
</div>
Read nothing else of the moment while a file is drawn: a row, a part or a list put then refuses the drawing, and the page moves by its HTML. /?poly_moves_made=1 says what it read.
A list of posts through a component card
A list of posts drawn through a component own card (a roadmap column, an events list) does not register a kind: it goes through the core item list, which names itself in a page file. The component answers which posts it holds:
function my_rooms_news_list( $found, $source, $args ) {
if ( 'my-rooms-news' !== $source ) {
return $found;
}
return array(
'ids' => my_news_ids( $args ),
'more' => false,
'total' => my_news_total( $args ),
'events' => 'item.created:room_news item.deleted',
);
}
add_filter( 'poly_community_item_list', 'my_rooms_news_list', 10, 3 );
It may also say ids_only (of its events, those that count only for a post it shows), min, words (its words by name, drawn with it) and heads (a heading before a card, by its id).
What is stable
poly_community_register_lists and register_list() are on the stable list (What is stable). The shape of a list, its event declarations, tileKeys, the core getters and poly_community_item_list are not on it yet.
See also Live updates in code, Moving between pages and Built on the Interactivity API.