/** Creates a new, uncompleted, promise. */ public static <T> RPromise<T> create () { return new RPromise<T>(); }
/** Causes this promise to be completed with {@code result}. */ public void complete (Try<T> result) { if (_result != null) throw new IllegalStateException("Already completed"); _result = result; try { notify(COMPLETE, result, null, null); } finally { clearConnections(); } }
/** * Decodes and returns a library synchronously. */ protected static Library decodeLibrarySync (final Platform plat, LibraryData libData, String baseDir) { RPromise<Library> result = RPromise.create(); decodeLibrary(libData, baseDir, result, new ImageLoader() { @Override public Image load (String path) { return plat.assets().getImageSync(path); } }); // this blows, but I don't want to add RPromise.get() final Library[] out = new Library[1]; result.onSuccess(new Slot<Library>() { public void onEmit (Library library) { out[0] = library; } }); assert out[0] != null; return out[0]; }
public void handleEvent(Event event) { if (event.detail == SWT.TRAVERSE_ESCAPE) { result.succeed(null); } } });
/** Configures this sound with an error in lieu of its platform implementation. * This may be called from any thread. */ public synchronized void fail (Throwable error) { ((RPromise<Sound>)state).fail(error); }
/** Transforms this future by mapping its result upon arrival. */ public <R> RFuture<R> transform (final Function<Try<? super T>,Try<R>> func) { final RPromise<R> xf = RPromise.create(); onComplete(new SignalView.Listener<Try<T>>() { public void onEmit (Try<T> result) { Try<R> xfResult; try { xfResult = func.apply(result); } catch (Throwable t) { xf.fail(t); return; } xf.complete(xfResult); } }); return xf; }
public HtmlImage (Graphics gfx, Scale scale, ImageElement elem, String source) { super(gfx, RPromise.<Image>create(), scale, elem.getWidth(), elem.getHeight(), source); img = elem; // we know that in this case, our state is a promise final RPromise<Image> pstate = ((RPromise<Image>)state); if (isComplete(img)) pstate.succeed(this); else { final Scale fscale = scale; HtmlInput.addEventListener(img, "load", new EventHandler() { @Override public void handleEvent (NativeEvent evt) { succeed(new ImageImpl.Data(fscale, img, img.getWidth(), img.getHeight())); } }, false); HtmlInput.addEventListener(img, "error", new EventHandler() { @Override public void handleEvent(NativeEvent evt) { fail(new RuntimeException("Error loading image " + img.getSrc())); } }, false); } }
public void handleEvent(Event event) { if (event.detail == SWT.TRAVERSE_ESCAPE) { result.succeed(null); } } });
/** Configures this sound with an error in lieu of its platform implementation. * This may be called from any thread. */ public synchronized void fail (Throwable error) { ((RPromise<Sound>)state).fail(error); }
/** Transforms this future by mapping its result upon arrival. */ public <R> RFuture<R> transform (final Function<Try<? super T>,Try<R>> func) { final RPromise<R> xf = RPromise.create(); onComplete(new SignalView.Listener<Try<T>>() { public void onEmit (Try<T> result) { Try<R> xfResult; try { xfResult = func.apply(result); } catch (Throwable t) { xf.fail(t); return; } xf.complete(xfResult); } }); return xf; }
public HtmlImage (Graphics gfx, Scale scale, ImageElement elem, String source) { super(gfx, RPromise.<Image>create(), scale, elem.getWidth(), elem.getHeight(), source); img = elem; // we know that in this case, our state is a promise final RPromise<Image> pstate = ((RPromise<Image>)state); if (isComplete(img)) pstate.succeed(this); else { final Scale fscale = scale; HtmlInput.addEventListener(img, "load", new EventHandler() { @Override public void handleEvent (NativeEvent evt) { succeed(new ImageImpl.Data(fscale, img, img.getWidth(), img.getHeight())); } }, false); HtmlInput.addEventListener(img, "error", new EventHandler() { @Override public void handleEvent(NativeEvent evt) { fail(new RuntimeException("Error loading image " + img.getSrc())); } }, false); } }
/** Causes this promise to be completed with {@code result}. */ public void complete (Try<T> result) { if (_result != null) throw new IllegalStateException("Already completed"); _result = result; try { notify(COMPLETE, result, null, null); } finally { clearConnections(); } }
public void onEmit (T result) { succeed(result); } };
public void onEmit (Throwable cause) { fail(cause); } };
/** Maps a successful result to a new result using {@code func} when it arrives. Failure on the * original result or the mapped result are both dispatched to the mapped result. This is * useful for chaining asynchronous actions. It's also known as monadic bind. */ public <R> RFuture<R> flatMap (final Function<? super T, RFuture<R>> func) { final RPromise<R> mapped = RPromise.create(); onComplete(new SignalView.Listener<Try<T>>() { public void onEmit (Try<T> result) { if (result.isFailure()) mapped.fail(result.getFailure()); else { RFuture<R> mappedResult; try { mappedResult = func.apply(result.get()); } catch (Throwable t) { mapped.fail(t); return; } mappedResult.onComplete(mapped.completer()); } } }); return mapped; }