delay() public static method

The delay is a minimum and approximate, accuracy is not guaranteed. Order of calls MUST be determined by which timers expire first, but timers with the same expiration time MAY be executed in any order.
public static delay ( $time, callable $callback, mixed $data = null ) : string
$callback callable
$data mixed Arbitrary data given to the callback function as the `$data` parameter.
return string An unique identifier that can be used to cancel, enable or disable the watcher.
コード例 #1
0
ファイル: FilesystemObserver.php プロジェクト: koolkode/async
 protected function pollFilesystem()
 {
     if (empty($this->watchers)) {
         return;
     }
     $watchers = [];
     foreach ($this->watchers as $id => list($path, $events, $callback)) {
         $sync = $this->pool->invokeStaticMethod(static::class, 'collectStats', $path);
         $sync->when(function ($e, $v = null) use($id, $path, $events, $callback) {
             if (!$e) {
                 try {
                     if (isset($this->state[$id])) {
                         $this->triggerEvents($id, $this->state[$id], $v, $events, $callback);
                     }
                 } finally {
                     $this->state[$id] = $v;
                 }
             }
         });
         $watchers[] = $sync;
     }
     $await = new AwaitPending($watchers);
     $await->when(function () {
         $this->timer = Loop::delay($this->interval, function () {
             $this->pollFilesystem();
         });
     });
     return $await;
 }
コード例 #2
0
ファイル: Timeout.php プロジェクト: koolkode/async
 public function __construct(float $seconds, Promise $promise)
 {
     $this->promise = $promise;
     $promise->when(function (\Throwable $e = null, ...$val) {
         if ($this->state === self::PENDING) {
             Loop::cancel($this->watcher);
             if ($e) {
                 $this->fail($e);
             } else {
                 $this->resolve(...$val);
             }
         }
     });
     if ($this->state === self::PENDING) {
         $this->watcher = Loop::delay((int) \floor($seconds * 1000), function () use($seconds) {
             $this->watcher = null;
             if ($this->state === self::PENDING) {
                 $e = new TimeoutException(\sprintf('Operation timed out after %.3f seconds', $seconds));
                 if ($this->promise instanceof Awaitable) {
                     $this->state = self::FAILED;
                     try {
                         $this->promise->cancel($e);
                     } finally {
                         $this->state = self::PENDING;
                     }
                 }
                 $this->fail($e);
             }
         });
     }
 }
コード例 #3
0
ファイル: Pause.php プロジェクト: koolkode/async
 public function __construct(float $seconds, $value = null)
 {
     $this->watcher = Loop::delay((int) \floor($seconds * 1000), function () use($value) {
         if ($this->state === self::PENDING) {
             $this->resolve($value);
         }
     });
 }
コード例 #4
0
ファイル: AwaitWrite.php プロジェクト: koolkode/async
 public function __construct($stream, float $timeout = 0)
 {
     if (!\is_resource($stream)) {
         throw new \InvalidArgumentException(\sprintf('Expecting stream resource, given %s', \is_object($stream) ? \get_class($stream) : \gettype($stream)));
     }
     $this->watcher = Loop::onWritable($stream, function () use($stream) {
         if ($this->state === self::PENDING) {
             Loop::cancel($this->watcher);
             if ($this->timer !== null) {
                 Loop::cancel($this->timer);
             }
             $this->resolve($stream);
         }
     });
     if ($timeout > 0) {
         $this->timer = Loop::delay($timeout * 1000, function () use($timeout) {
             if ($this->state === self::PENDING) {
                 Loop::cancel($this->watcher);
                 $this->fail(new TimeoutException(\sprintf('Await Write timed out after %.3f seconds', $timeout)));
             }
         });
     }
 }