Jump to content

Change React loop addPeriodicTimer timer


NotionCommotion

Recommended Posts

After a React loop addPeriodicTimer timer is started, how do I change it?  For instance, how can I change interval1 timer within the socket on data callback?

 

Thanks

<?php

require 'vendor/autoload.php';

use React\EventLoop\Factory;
use React\Socket\Connection;

$interval1=20;
$interval2=50;

$loop = Factory::create();
$app = new \ClientApp;

$server = @stream_socket_client("tcp://123.421.23.41:123", $errno, $errstr, STREAM_CLIENT_ASYNC_CONNECT);

$socket = new Connection($server, $loop);

$socket->on('data', function($data) use ($app) {
    //Change $interval1
});

$loop->addPeriodicTimer($interval1, function() use ($app) {
    $app->do_something();
});

$loop->addPeriodicTimer($interval2, function() use ($app) {
    $app->some_other_non_related_loop();  //Just shown so that you know there is more than one loop happening, so one must be able to somehow select the correct one when changing the timer
});
Link to comment
Share on other sites

From digging around in the react code it looks like you just have to remove the timer then re-add it.

 

$timer1 = $loop->addPeriodicTimer($interval1, function() use ($app) {
    $app->do_something();
});

$socket->on('data', function($data) use ($app, $loop, &$timer1) {
    $loop->cancelTimer($timer1);
    $newInterval = 10;
    $timer1 = $loop->addPeriodicTimer($newInterval, function() use ($app){
        $app->do_something();
    });
});
  • Like 1
Link to comment
Share on other sites

When "digging around", where do you normally start?  Does one normally start on the interface code?

Depends a bit on what I'm looking for.

 

In this case I was looking for a method to either re-register the timer or a way to change the interval of the timer so I started by just looking at the LoopInterface and TimerInterface. Neither of those have a way to change just the interval but the loop interface does have a cancel method.

 

I didn't really spend time looking around at any implementations as I figured if there is no way on the official interface then there's probably just not a way at all.

 

If I were more interested in knowing how something actually worked then I'd go directly to the implementation code rather than the interfaces.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.