Skip to content

Instantly share code, notes, and snippets.

@nisanharamati
Created October 27, 2017 18:56
Show Gist options
  • Save nisanharamati/9f1726072906e5f0edf4894d888faeb2 to your computer and use it in GitHub Desktop.
Save nisanharamati/9f1726072906e5f0edf4894d888faeb2 to your computer and use it in GitHub Desktop.
Pony Timers cancel example
use "time"
actor Main
new create(env: Env) =>
// print a number every 100 ms
let timers = Timers
// Get Timer iso
let notify = Timer(Notify(env), 0, 500_000_000)
// We'll need a tag to the same Timer to cancel it later
let notify_tag: Timer tag = notify
timers(consume notify)
// wait 5 seconds then cancel the first timer
let cancel = Timer(CancelNotify(env, notify_tag, timers), 5_000_000_000, 0)
timers(consume cancel)
class Notify is TimerNotify
let _env: Env
var _counter: U32 = 0
new iso create(env: Env) =>
_env = env
fun ref apply(timer: Timer, count: U64): Bool =>
_env.out.print(_counter.string())
_counter = _counter + 1
true
class CancelNotify is TimerNotify
let _env: Env
let _notify: Timer tag
let _timers: Timers tag
new iso create(env: Env, notify: Timer tag, timers: Timers tag) =>
_env = env
_notify = notify
_timers = timers
fun ref apply(timer: Timer, count: U64): Bool =>
_env.out.print("Cancelling Notify!")
_timers.cancel(_notify)
false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment