public synchronized Promise<T> then(UniCallback<T> onFulfilled, UniCallback<Throwable> onRejected) { if (next != null) return next.then(onFulfilled, onRejected); next = new Promise<>(onFulfilled, onRejected); switch (state) { case REJECTED: next.reject(throwable); break; case FULFILLED: next.resolve(value); break; } return next; }
@Override public Promise<Void> setIcon(FilePath filePath) { return new Promise<>((resolve, reject) -> resolve.invoke(null)); }
private static void imageLoadHelper(FilePath path, ISubmitter<Image> submitter) { SilenceEngine.io.getImageReader() .readImage(path) .then(img -> submitter.submit(img, path)) .whenThrown(SilenceException::reThrow); }
public synchronized Promise<T> then(UniCallback<T> onFulfilled) { return then(onFulfilled, t -> { }); }
public Promise(TriCallback<Promise<T>, UniCallback<T>, UniCallback<Throwable>> function) { try { function.invoke(this, this::resolve, this::reject); } catch (Throwable throwable) { reject(throwable); } }
public synchronized void resolve(T value) { if (state == State.FULFILLED) throw new PromiseException("Cannot resolve more than once"); if (state == State.REJECTED) throw new PromiseException("Cannot resolve an already rejected promise"); this.value = value; this.state = State.FULFILLED; if (onFulfilled != null) onFulfilled.invoke(value); if (next != null) next.resolve(value); }
@Override public Promise<Void> copyTo(FilePath path) { return new Promise<>((resolve, reject) -> reject.invoke(new IOException("Cannot copy files in HTML5 platform."))); }
.then(data -> SilenceEngine.audio.readToALBuffer(finalFormat, data) .then(buffer -> submitter.submit(buffer, path)) .whenThrown(SilenceException::reThrow)) .whenThrown(SilenceException::reThrow);
public synchronized Promise<T> whenThrown(UniCallback<Throwable> onThrown) { return then(v -> { }, onThrown); }
public synchronized void reject(Throwable throwable) { if (state == State.REJECTED) throw new PromiseException("Cannot reject more than once"); if (state == State.FULFILLED) throw new PromiseException("Cannot resolve an already fulfilled promise"); this.value = null; this.state = State.REJECTED; this.throwable = throwable; if (onRejected != null) onRejected.invoke(throwable); if (next != null) next.reject(throwable); }
@Override public Promise<List<FilePath>> listFiles() { return new Promise<>((resolve, reject) -> resolve.invoke(EMPTY_LIST)); }
private static void textureLoadHelper(FilePath path, ISubmitter<Texture> submitter) { SilenceEngine.io.getImageReader() .readImage(path) .then(img -> { submitter.submit(Texture.fromImage(img), path); img.dispose(); }) .whenThrown(SilenceException::reThrow); }
@Override public Promise<Void> createFile() { return new Promise<>((resolve, reject) -> reject.invoke(new IOException("Cannot create a file inside jar"))); }
@Override public Promise<Boolean> delete() { return new Promise<>((resolve, reject) -> reject.invoke(new IOException("Cannot delete an in-jar resource"))); }
@Override public Promise<Void> mkdirs() { return new Promise<>((resolve, reject) -> reject.invoke(new IOException("Cannot create directories in HTML5 platform."))); }