-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass-callback-node.php
More file actions
79 lines (72 loc) · 2.56 KB
/
Copy pathclass-callback-node.php
File metadata and controls
79 lines (72 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
/**
* Callback: adapt a PHP closure into the node graph.
*
* Short-lived graphs need a terminal that runs arbitrary PHP once per message —
* a test asserting on what reached the end of a chain, `Job_Delay` sorting due
* entries from held ones, `Log_Sources::read_at()` capturing the one record a
* Consumer stepped to. Callback is that terminal, so none of them has to declare
* a Node subclass it would use once.
*
* @package Newspack_Nodes
*/
namespace Newspack_Nodes;
\defined( 'ABSPATH' ) || exit;
/**
* Invokes a callable on every message and stops there.
*
* Callback needs no sink, forwards nothing and stamps no `target` into TO; a
* closure that means to transform and pass on fills the next node itself. Its
* constructor takes a required argument, which is why `make_node` cannot build
* it — that sequence instantiates with `new $fqcn()` (ADR-11) — so callers
* construct it directly in PHP and no topology line ever names it. Tachikoma's
* `Callback.pm`, the model, guards the same ground by dying whenever
* `arguments()` is handed anything.
*/
class Callback_Node extends Node {
/**
* The callable every message is handed to.
*
* @var callable
*/
private $cb;
/**
* Take the callable directly. A closure is not a scalar token, so it cannot
* ride the `arguments()` path every configurable node takes its setup from.
*
* @param callable $cb Invoked once per message as `function ( array $message ): void`.
*/
public function __construct( callable $cb ) {
parent::__construct();
$this->cb = $cb;
}
/**
* Count the message and hand it to the callable.
*
* The parameter is by value, so a callback declaring `array &$message`
* mutates this node's copy and nothing the caller can see. Transforming a
* message means forwarding the changed copy yourself. The JS mirror,
* `src/runtime/callback-node.js`, hands the array over by reference, so a
* closure that writes into the message edits the caller's copy there.
*
* @param array<int,mixed> $message The 7-field positional message array.
*/
public function fill( array $message ): void {
++$this->counter;
( $this->cb )( $message );
}
/**
* Console-palette manifest. Hidden: a closure has no TSL spelling and no form
* the GUI could render, so the class stays out of the palette.
*
* @return array<string,mixed>
*/
public static function node_schema(): array {
return [
'category' => 'Hidden',
'description' => 'Inline PHP closure as a node — invokes a callable on each message.',
'arguments' => [],
'commands' => [],
];
}
}