declare namespace lime.app { /** * `Future` is an implementation of Futures and Promises, with the exception that * in addition to "success" and "failure" states (represented as "complete" and "error"), * Lime `Future` introduces "progress" feedback as well to increase the value of * `Future` values. * * ```haxe * var future = Image.loadFromFile ("image.png"); * future.onComplete (function (image) { trace ("Image loaded"); }); * future.onProgress (function (loaded, total) { trace ("Loading: " + loaded + ", " + total); }); * future.onError (function (error) { trace (error); }); * * Image.loadFromFile ("image.png").then (function (image) { * * return Future.withValue (image.width); * * }).onComplete (function (width) { trace (width); }) * ``` * * `Future` values can be chained together for asynchronous processing of values. * * If an error occurs earlier in the chain, the error is propagated to all `onError` callbacks. * * `Future` will call `onComplete` callbacks, even if completion occurred before registering the * callback. This resolves race conditions, so even functions that return immediately can return * values using `Future`. * * `Future` values are meant to be immutable, if you wish to update a `Future`, you should create one * using a `Promise`, and use the `Promise` interface to influence the error, complete or progress state * of a `Future`. * */ export class Future { /** * @param work Optional: a function to compute this future's value. * @param useThreads Whether to run `work` on a background thread, where supported. * If false or if this isn't a system target, it will run immediately on the main thread. * */ constructor(work?: () => T, useThreads?: boolean); /** * Create a new `Future` instance based on complete and (optionally) error and/or progress `Event` instances * */ static ofEvents(onComplete: any, onError?: any, onProgress?: any): Future; /** * Creates a `Future` instance which has finished with an error value * @param error The error value to set * @return A new `Future` instance * */ static withError(error: any): Future; /** * Creates a `Future` instance which has finished with a completion value * @param value The completion value to set * @return A new `Future` instance * */ static withValue(value: T): Future; /** * If the `Future` has finished with an error state, the `error` value * */ error: any; /** * Whether the `Future` finished with a completion state * */ isComplete: boolean; /** * Whether the `Future` finished with an error state * */ isError: boolean; /** * If the `Future` has finished with a completion state, the completion `value` * */ value: T; /** * Register a listener for when the `Future` completes. * * If the `Future` has already completed, this is called immediately with the result * @param listener A callback method to receive the result value * @return The current `Future` * */ onComplete(listener: (arg0: T) => void): Future; /** * Register a listener for when the `Future` ends with an error state. * * If the `Future` has already ended with an error, this is called immediately with the error value * @param listener A callback method to receive the error value * @return The current `Future` * */ onError(listener: (arg0: any) => void): Future; /** * Register a listener for when the `Future` updates progress. * * If the `Future` is already completed, this will not be called. * @param listener A callback method to receive the progress value * @return The current `Future` * */ onProgress(listener: (arg0: number, arg1: number) => void): Future; /** * Attempts to block on an asynchronous `Future`, returning when it is completed. * @param waitTime (Optional) A timeout before this call will stop blocking * @return This current `Future` * */ ready(waitTime?: number): Future; /** * Attempts to block on an asynchronous `Future`, returning the completion value when it is finished. * @param waitTime (Optional) A timeout before this call will stop blocking * @return The completion value, or `null` if the request timed out or blocking is not possible * */ result(waitTime?: number): T; /** * Chains two `Future` instances together, passing the result from the first * as input for creating/returning a new `Future` instance of a new or the same type * */ then(next: (arg0: T) => Future): Future; } } export default lime.app.Future;