From f4c69bcc5ee5c5c82df4a0b023ec63551406e4c7 Mon Sep 17 00:00:00 2001 From: Cees-Jan Kiewiet Date: Sun, 18 Oct 2020 21:27:11 +0200 Subject: [PATCH] Defer calls --- src/AbstractGeneratedProxy.php | 29 +++++++++++++++++++++++++++++ src/Composer/InterfaceProxier.php | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/AbstractGeneratedProxy.php b/src/AbstractGeneratedProxy.php index c5f24fb..afe3845 100644 --- a/src/AbstractGeneratedProxy.php +++ b/src/AbstractGeneratedProxy.php @@ -55,6 +55,35 @@ final protected function proxyCallToMainThread(string $method, array $args) return $result; } + /** + * @param mixed[] $args + * + * @return mixed|void + */ + final protected function deferCallToMainThread(string $method, array $args) + { + $input = new Channel(1); + $call = new Call( + $input, + $this->hash, + spl_object_hash($this), + $method, + $args, + ); + + if ($this->deferredCallHandler instanceof DeferredCallHandler) { + $this->deferredCallHandler->call($call); + $this->deferredCallHandler->commit($this->out); + } else { + $this->out->send($call); + } + + $result = $input->recv(); + $input->close(); + + return $result; + } + /** * @param mixed[] $args */ diff --git a/src/Composer/InterfaceProxier.php b/src/Composer/InterfaceProxier.php index 9360a27..40ba6cc 100644 --- a/src/Composer/InterfaceProxier.php +++ b/src/Composer/InterfaceProxier.php @@ -190,7 +190,7 @@ private function populateMethod(Node\Stmt\ClassMethod $method): Node\Stmt\ClassM $methodBody = new Node\Expr\MethodCall( new Node\Expr\Variable('this'), - $this->isMethodVoid($method) ? ($this->isDeferrable($method) ? 'deferNotifyMainThread' : 'proxyNotifyMainThread') : 'proxyCallToMainThread', + $this->isMethodVoid($method) ? ($this->isDeferrable($method) ? 'deferNotifyMainThread' : 'proxyNotifyMainThread') : ($this->isDeferrable($method) ? 'deferCallToMainThread' : 'proxyCallToMainThread'), iterator_to_array($this->methodCallArguments($method)) );