amphp / amp

A non-blocking concurrency framework for PHP applications. 🐘
https://amphp.org/amp
MIT License
4.25k stars 257 forks source link

Add Interval #441

Open trowski opened 3 months ago

trowski commented 3 months ago

This adds a utility class Interval which controls a repeating timer, automatically cancelling the timer when the object is destroyed.

The main use-case of this object is to store an instance of Interval as an object property. When the holding object is destroyed, the repeating timer will be cancelled. This avoids a bit of boilerplate in the class, such as needing to declare a destructor to manually cancel the event loop callback.

The value here isn't necessarily huge, but this is a pattern we frequently use for classes with self-cleanup mechanisms such as LocalCache.

xpader commented 3 months ago

It's just a auto destruct for EventLoop::repeat()?? I think it's not very necessary for a base library.

trowski commented 3 months ago

How about moving this to amphp/sync? That library has sort of become a utility library.

kelunik commented 3 months ago

I'd rather keep this here, as it's very basic, no?

trowski commented 3 months ago

@kelunik Yeah, I can't see this needing changing, so seems basic enough to go here. Should we add this then?