1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-12-12 09:59:41 +01:00
MadelineProto/src/loop.php

77 lines
2.3 KiB
PHP
Raw Normal View History

2023-01-26 15:34:16 +01:00
<?php
declare(strict_types=1);
namespace danog\Loop\Generic {
2023-02-06 20:17:35 +01:00
if (\defined('MADELINE_POLYFILLED_LOOP')) {
return;
}
\define('MADELINE_POLYFILLED_LOOP', true);
2023-01-26 15:34:16 +01:00
use danog\Loop\GenericLoop as LoopGenericLoop;
use danog\Loop\PeriodicLoop as LoopPeriodicLoop;
use danog\MadelineProto\Tools;
use Generator;
/**
* @deprecated Please use danog\Loop\PeriodicLoop instead
*/
class PeriodicLoop extends LoopPeriodicLoop
{
2023-01-26 15:34:16 +01:00
public function __construct(callable $callback, string $name, ?int $interval)
{
if ($callback instanceof \Closure) {
try {
$callback = $callback->bindTo($this);
} catch (\Throwable) {
// Might cause an error for wrapped object methods
}
}
2023-01-28 16:39:03 +01:00
/** @psalm-suppress InvalidArgument */
2023-01-26 15:34:16 +01:00
parent::__construct(
function ($_) use ($callback) {
$result = $callback();
if ($result instanceof Generator) {
$result = Tools::consumeGenerator($result);
}
return $result;
},
$name,
$interval ? $interval/1000 : null
);
}
}
/**
* @deprecated Please use danog\Loop\GenericLoop instead
*/
class GenericLoop extends LoopGenericLoop
{
2023-01-26 15:34:16 +01:00
public function __construct(callable $callback, string $name)
{
if ($callback instanceof \Closure) {
try {
$callback = $callback->bindTo($this);
} catch (\Throwable) {
// Might cause an error for wrapped object methods
}
}
2023-01-28 16:39:03 +01:00
/** @psalm-suppress InvalidArgument */
2023-01-26 15:34:16 +01:00
parent::__construct(
function ($_) use ($callback) {
$result = $callback();
if ($result instanceof Generator) {
$result = Tools::consumeGenerator($result);
}
if (\is_int($result) || \is_float($result)) {
2023-01-26 15:34:16 +01:00
$result /= 1000;
}
return $result;
},
$name
);
}
}
}