1: <?php
2:
3: namespace Alchemy\util\promise;
4:
5:
6: /**
7: * Signals are Waitables with the concept of a 'source', a dynamic value
8: * represented by a callable that returns either an unresolved NULL, an
9: * Exception, or a non-NULL value.
10: */
11: class Signal extends Waitable {
12: protected $fnSource = null;
13:
14:
15: public function __construct($source = null, $type = null) {
16: parent::__construct($type);
17:
18: if (is_callable($source)) {
19: $this->fnSource = $source;
20: } else {
21: $this->resolve($source);
22: }
23: }
24:
25:
26: /**
27: * Queries the Signal's source for its current value. If this is
28: * itself a Waitable, that Waitable becomes the new source.
29: */
30: protected function precheck() {
31: if ($this->fnSource !== null) {
32: $this->resolve(call_user_func($this->fnSource));
33: }
34:
35: if ($this->result instanceof Waitable) {
36: $this->fnSource = $this->result;
37: $this->result = null;
38: $this->check();
39: }
40: }
41: }