/** Creates the sound of silence. */ public Sound () { this.state = RFuture.success(this); }
/** * Sets the tile rendered by this layer to the asynchronous result of {@code tile}. When the * future completes, this layer's tile will be set. Until then, the current tile (if any) will * continue to be rendered. */ public ImageLayer setTile (RFuture<? extends Tile> tile) { tile.onSuccess(new Slot<Tile>() { public void onEmit (Tile tile) { setTile(tile); } }); return this; }
/** * Returns a future which will deliver the default texture for this image once its loading has * completed. Uses {@link #texture} to create the texture. */ public RFuture<Texture> textureAsync () { return state.map(new Function<Image,Texture>() { public Texture apply (Image image) { return texture(); } }); }
/** Returns a future containing the results of {@code a} and {@code b} if both futures complete * successfully, or a {@link MultiFailureException} aggregating all failures, if either of the * futures fails. */ public static <A,B> RFuture<T2<A,B>> sequence (RFuture<A> a, RFuture<B> b) { @SuppressWarnings("unchecked") RFuture<Object> oa = (RFuture<Object>)a; @SuppressWarnings("unchecked") RFuture<Object> ob = (RFuture<Object>)b; return sequence(Arrays.asList(oa, ob)).map(new Function<List<Object>,T2<A,B>>() { public T2<A,B> apply (List<Object> results) { @SuppressWarnings("unchecked") A a = (A)results.get(0); @SuppressWarnings("unchecked") B b = (B)results.get(1); return new T2<A,B>(a, b); } }); }
/** Causes {@code slot} to be notified when this future is completed. If it has already * completed, the slot will be notified immediately. * @return this future for chaining. */ public RFuture<T> onComplete (final SignalView.Listener<? super Try<T>> slot) { Try<T> result = result(); if (result != null) slot.onEmit(result); else addConnection(slot); return this; }
@Override public void init() { final Image tile = game.assets.getImage("images/tile.png"); final Image orange = game.assets.getImage("images/orange.png"); Slot<Throwable> onError = new Slot<Throwable>() { float errY = 0; public void onEmit (Throwable err) { addDescrip("Error: " + err.getMessage(), 10, errY, game.graphics.viewSize.width()-20); errY += 30; } }; tile.state.onFailure(onError); orange.state.onFailure(onError); RFuture.collect(Arrays.asList(tile.state, orange.state)). onSuccess(imgs -> addTests(orange, tile)); }
/** Returns a future containing a list of all success results from {@code futures}. Any failure * results are simply omitted from the list. The success results are also in no particular * order. If all of {@code futures} fail, the resulting list will be empty. */ public static <T> RFuture<Collection<T>> collect (Collection<? extends RFuture<T>> futures) { // if we're passed an empty list of futures, succeed immediately with an empty list if (futures.isEmpty()) return RFuture.<Collection<T>>success(Collections.<T>emptyList()); final RPromise<Collection<T>> pseq = RPromise.create(); final int count = futures.size(); SignalView.Listener<Try<T>> collector = new SignalView.Listener<Try<T>>() { public synchronized void onEmit (Try<T> result) { if (result.isSuccess()) _results.add(result.get()); if (--_remain == 0) pseq.succeed(_results); } protected final List<T> _results = new ArrayList<T>(); protected int _remain = count; }; for (RFuture<T> future : futures) future.onComplete(collector); return pseq; }
Image atlas = loader.load(baseDir + "/" + atlasJson.getString("file")); atlasImages.add(atlas.state); atlas.state.onSuccess(new Slot<Image>() { public void onEmit (Image image) { for (Json.Object tjson : atlasJson.getArray("textures", Json.Object.class)) { return RFuture.sequence(atlasImages).map(new Function<List<Image>,Library>() { public Library apply (List<Image> atlases) { return new Library(frameRate, movies, textures);
Image atlas = imageLoader.load(baseDir + "/" + atlasData.file); atlasImages.add(atlas.state); atlas.state.onSuccess(new Slot<Image>() { public void onEmit (Image image) { for (LibraryData.TextureData textureData : atlasData.textures) { RFuture.sequence(atlasImages).onSuccess(new Slot<List<Image>>() { public void onEmit (List<Image> atlases) { result.succeed(new Library(frameRate, movies, textures)); }).onFailure(result.failer());
/** Returns whether this future is complete right now. This is an unfortunate name, but I * foolishly defined {@link #isComplete} to return a reactive view of completeness. */ public boolean isCompleteNow () { return result() != null; }
@Override public RFuture<ByteBuffer> getBytes(final String path) { if (!TypedArrays.isSupported()) return RFuture.failure( new UnsupportedOperationException("TypedArrays not supported by this browser.")); return doXhr(pathPrefix + path, XMLHttpRequest.ResponseType.ArrayBuffer). map(new Function<XMLHttpRequest,ByteBuffer>() { public ByteBuffer apply (XMLHttpRequest xhr) { return TypedArrayHelper.wrap(xhr.getResponseArrayBuffer()); } }); }
public Error (Exception error) { super(RFuture.<Sound>failure(error)); } }
protected Sound loadSound(String path) { Sound sound = game.assets.getSound(path); sound.state.onFailure(logFailure("Sound loading error: " + path)); return sound; }
/** Syncs the supplied database with this session's server. */ public void sync (final SyncDB db) { final Map<String,Integer> mods = db.getMods(); _server.sendSync(db.version(), db.getDelta()).onSuccess(new Slot<Response>() { public void onEmit (Response rsp) { onSyncSuccess(db, mods, rsp); } }).onFailure(new Slot<Throwable>() { public void onEmit (Throwable cause) { onSyncFailure(db, cause); } }); }
/** Returns a future containing a list of all success results from {@code futures}. Any failure * results are simply omitted from the list. The success results are also in no particular * order. If all of {@code futures} fail, the resulting list will be empty. */ public static <T> RFuture<Collection<T>> collect (Collection<? extends RFuture<T>> futures) { // if we're passed an empty list of futures, succeed immediately with an empty list if (futures.isEmpty()) return RFuture.<Collection<T>>success(Collections.<T>emptyList()); final RPromise<Collection<T>> pseq = RPromise.create(); final int count = futures.size(); SignalView.Listener<Try<T>> collector = new SignalView.Listener<Try<T>>() { public synchronized void onEmit (Try<T> result) { if (result.isSuccess()) _results.add(result.get()); if (--_remain == 0) pseq.succeed(_results); } protected final List<T> _results = new ArrayList<T>(); protected int _remain = count; }; for (RFuture<T> future : futures) future.onComplete(collector); return pseq; }
/** Returns a future containing the results of {@code a} and {@code b} if both futures complete * successfully, or a {@link MultiFailureException} aggregating all failures, if either of the * futures fails. */ public static <A,B> RFuture<T2<A,B>> sequence (RFuture<A> a, RFuture<B> b) { @SuppressWarnings("unchecked") RFuture<Object> oa = (RFuture<Object>)a; @SuppressWarnings("unchecked") RFuture<Object> ob = (RFuture<Object>)b; return sequence(Arrays.asList(oa, ob)).map(new Function<List<Object>,T2<A,B>>() { public T2<A,B> apply (List<Object> results) { @SuppressWarnings("unchecked") A a = (A)results.get(0); @SuppressWarnings("unchecked") B b = (B)results.get(1); return new T2<A,B>(a, b); } }); }
/** Returns whether this future is complete right now. This is an unfortunate name, but I * foolishly defined {@link #isComplete} to return a reactive view of completeness. */ public boolean isCompleteNow () { return result() != null; }
@Override public RFuture<ByteBuffer> getBytes(final String path) { if (!TypedArrays.isSupported()) return RFuture.failure( new UnsupportedOperationException("TypedArrays not supported by this browser.")); return doXhr(pathPrefix + path, XMLHttpRequest.ResponseType.ArrayBuffer). map(new Function<XMLHttpRequest,ByteBuffer>() { public ByteBuffer apply (XMLHttpRequest xhr) { return TypedArrayHelper.wrap(xhr.getResponseArrayBuffer()); } }); }