rtk.Future
¶
Represents the state of an asynchronous action that will be completed some time in the future.
In rtk, asynchronous tasks, such as animations, will return an rtk.Future
where
you have the opportunity to register callbacks to optionally perform subsequent
actions (after()
), or callbacks to be invoked when the Future is fully resolved
(done()
).
If the asynchronous task supports it, it can also be cancelled (cancel()
).
-- Kick off an animation sequence. rtk.Widget:animate() returns an rtk.Future.
widget:animate{'bg', dst='red'}
:after(function()
-- Once the background color animates to red, now animate it to
-- turquoise with a different easing.
return widget:animate{'bg', dst='turquoise', easing='in-out-elastic'}
end)
:after(function()
-- And now once the animation to turquoise completes, animate the
-- width to 60% of the widget's parent's width.
return widget:animate{'w', dst=0.6}
end)
:done(function()
-- Finally all 'after' callbacks have completed.
log.info('animation sequence complete')
end)
:cancelled(function()
-- If cancel() was called on the Future then we'll log this message.
log.warning('animation sequence was cancelled')
end)
state | futurestateconst | read-only |
Represents the current state of the Future |
result | any | read-only |
The result returned by the asynchronous task as passed to either |
cancellable | boolean | read-only |
If true, the owner of the asynchronous task has registered a |
rtk.Future() | Create a new Future object |
after() | Register a callback to be invoked after the current task completes, but
before |
done() | Register a callback to be invoked when the Future completes (not cancelled) and
after all |
cancelled() | Register a callback to be invoked when the Future is cancelled |
cancel() | Cancels the Future and invokes all previously registered |
resolve() | Resolves the Future, causing all |
Represents the current state of the Future.
The result returned by the asynchronous task as passed to either resolve()
or cancel()
.
If true, the owner of the asynchronous task has registered a cancelled
callback
to cancel the operation, which allows cancel()
to be called.
Create a new Future object.
(rtk.Future) | the new Future object |
Register a callback to be invoked after the current task completes, but
before done
callbacks are invoked.
Multiple callbacks can be registered and will be invoked in the order they were added.
If the callback returns another rtk.Future
then that Future is chained
to this one, such that the new Future must complete before any subsequent
after
callbacks registered against this Future will be invoked (and likewise
for done
). If a dependent Future is in-flight and this one is cancelled,
then the dependent Future will also be cancelled. The reverse is not true,
however: if a dependent Future is cancelled,
If the rtk.Future
has already been resolved before registering
func
, it will still be executed at the beginning of the next
{rtk.Window.onupdate|update cycle}.
func | (function) | the function that's invoked after the asynchronous task completes, and which receives as an argument the return value from the asynchronous task if it's the first |
(rtk.Future) | returns self for method chaining |
Register a callback to be invoked when the Future completes (not cancelled) and
after all after()
callbacks have been invoked.
Multiple callbacks can be registered and will be invoked in the order they were added.
If the rtk.Future
has already been resolved before registering
func
, it will still be executed at the beginning of the next
{rtk.Window.onupdate|update cycle}.
func | (function) | the function that's invoked after the asynchronous task completes and all |
(rtk.Future) | returns self for method chaining |
Register a callback to be invoked when the Future is cancelled.
Multiple callbacks can be registered and will be invoked in the order they were added.
If the rtk.Future
has already been cancelled before registering
func
, then it will be immediately invoked.
func | (function) | the function that's invoked when |
(rtk.Future) | returns self for method chaining |
Cancels the Future and invokes all previously registered cancelled
callbacks.
The cancelled
callbacks are immediately invoked in order.
It is a runtime error to cancel()
an rtk.Future
that has no cancelled
callbacks. You can check the cancellable
attribute first to determine if
the Future can be cancelled.
v | (any) | the arbitrary value to be passed to the registered |
(rtk.Future) | returns self for method chaining |
Resolves the Future, causing all after
and done
callbacks to be invoked.
Usually this method will only be called by the originator of the asynchronous task.
If there have been any after
or done
callbacks registered, they are
immediately invoked. But if no callbacks have been attached, then they will be
rechecked after one {rtk.Window.onupdate|update cycle}, allowing the opportunity
for callbacks to be registered even after the rtk.Future
is resolved.
The Future may not transition to the DONE
state
after calling this method:
if any after
callbacks return a Future, they will be chained, and this Future
will not transition to DONE
until all dependent Futures also complete.
value | (any) | the arbitrary value returned by the asynchronous task, and that will be passed into the |
(rtk.Future) | returns self for method chaining |