/** * To be used inside {@link Promise#catch_} blocks. Decides whether to process a RuntimeException that * caused a prior Promise rejection or to process an expected object rejected by a prior Promise. To proceed * with default error handlers, reject the untreated exception inside the catchBlock function. */ @SuppressWarnings("unchecked") public <V, T> Promise<T> catchOrExecute(final Object o, final Function<RuntimeException, Promise<T>> catchBlock, final Function<V, Promise<T>> expectedRejectionHandler) { if (o instanceof JavaScriptObject) { // A RuntimeException occurred inside a promise and was transformed in a JavaScriptObject return resolve() .then(i -> catchBlock.apply(new RuntimeException("Client-side exception inside Promise: " + o.toString()))) .catch_(this::handleCatchBlockExceptions); } if (o instanceof RuntimeException) { return resolve() .then(i -> catchBlock.apply((RuntimeException) o)) .catch_(this::handleCatchBlockExceptions); } if (o instanceof Promises.Error) { return resolve() .then(i -> catchBlock.apply((RuntimeException) ((Error) o).getThrowable())) .catch_(i -> handleCatchBlockExceptions(o)); } return expectedRejectionHandler.apply((V) o); }
public <T> Promise<T> create(final Promise.PromiseExecutorCallbackFn<T> executor) { return new Promise<>(executor); }
/** * Returns a resolved Promise when every Promise in the list is resolved. * If any Promise is rejected, the resulting Promise will be rejected. */ @SafeVarargs public final <O> Promise<O> all(final Promise<O>... promises) { return Arrays.stream(promises).reduce(resolve(), (p1, p2) -> p1.then(ignore -> p2)); }
void save(final String comment) { promises.reduceLazilyChaining(getSavingSteps(comment), this::executeSavingStep) .catch_(o -> promises.catchOrExecute(o, promises::reject, sectionManager::goTo)); }
@JsOverlay public static final <V> Promise<V> resolve(V value) { return resolve(Js.<ResolveValueUnionType<V>>uncheckedCast(value)); }
/** * Maps the objects to Promises using the provided function then behaves just like {@link org.uberfire.client.promise.Promises#all}. */ public <T, O> Promise<O> all(final List<T> objects, final Function<T, Promise<O>> f) { return objects.stream().map(f).reduce(resolve(), (p1, p2) -> p1.then(ignore -> p2)); }
@Test public void testSave() { final KModuleModel kModuleModel = spy(new KModuleModel()); knowledgeBasesPresenter.kModuleModel = kModuleModel; final Map<String, KBaseModel> kBasesMap = spy(new HashMap<>()); doReturn(kBasesMap).when(kModuleModel).getKBases(); knowledgeBasesPresenter.save("Test comment", null).catch_(i -> { Assert.fail("Promise should've been resolved!"); return promises.resolve(); }); verify(kBasesMap).clear(); verify(kBasesMap).putAll(any()); }
@JsOverlay public static final <V> Promise<V> resolve(V value) { return resolve(Js.<ResolveValueUnionType<V>>uncheckedCast(value)); }
/** * To be used inside {@link Promise#catch_} blocks. Decides whether to process a RuntimeException that * caused a prior Promise rejection or to process an expected object rejected by a prior Promise. To proceed * with default error handlers, reject the untreated exception inside the catchBlock function. */ @SuppressWarnings("unchecked") public <V, T> Promise<T> catchOrExecute(final Object o, final Function<RuntimeException, Promise<T>> catchBlock, final Function<V, Promise<T>> expectedRejectionHandler) { if (o instanceof JavaScriptObject) { // A RuntimeException occurred inside a promise and was transformed in a JavaScriptObject return resolve() .then(i -> catchBlock.apply(new RuntimeException("Client-side exception inside Promise: " + o.toString()))) .catch_(this::handleCatchBlockExceptions); } if (o instanceof RuntimeException) { return resolve() .then(i -> catchBlock.apply((RuntimeException) o)) .catch_(this::handleCatchBlockExceptions); } if (o instanceof Promises.Error) { return resolve() .then(i -> catchBlock.apply((RuntimeException) ((Error) o).getThrowable())) .catch_(i -> handleCatchBlockExceptions(o)); } return expectedRejectionHandler.apply((V) o); }
/** * Maps the objects to Promises using the provided function but only execute the Promises when the * previous Promise is resolved. If a rejection occurs in the middle of the chain, the remaining * Promises are not executed and the resulting Promise is rejected. */ public <T, O> Promise<O> reduceLazily(final List<T> objects, final Function<T, Promise<O>> f) { return objects.stream() .<Supplier<Promise<O>>> map(o -> () -> f.apply(o)) .<Supplier<Promise<O>>> reduce(this::resolve, (p1, p2) -> () -> p1.get().then(ignore -> p2.get()) ) .get(); }
@Override public Disposable schedule(Runnable action, long delayTime, TimeUnit unit) { action = RxJavaPlugins.onSchedule(action); ScheduledAction scheduledAction = new ScheduledAction(action); if (incremental && (delayTime <= 0 || unit == null)) { Promise.resolve(0).then(o -> { scheduledAction.run(); return null; }); } else { DomGlobal.setTimeout(args -> scheduledAction.run(), (int) unit.toMillis(delayTime)); } return scheduledAction; }
@Test public void testLoadDeploymentDescriptor() { doReturn(mock(Path.class)).when(ddEditorService).load(any()); presenter.loadDeploymentDescriptor().catch_(i -> { Assert.fail("Promise should've been resolved!"); return promises.resolve(); }); verify(ddEditorService).load(any()); }
public static native <V> Promise<V> resolve(ResolveValueUnionType<V> value);
public <T> Promise<T> create(final Promise.PromiseExecutorCallbackFn<T> executor) { return new Promise<>(executor); }
Promise<Object> setupSection(final ProjectScreenModel model, final Section<ProjectScreenModel> section) { return section.setup(model) .then(i -> { sectionManager.resetDirtyIndicator(section); return promises.resolve(); }).catch_(e -> { sectionManager.remove(section); notificationEvent.fire(new NotificationEvent(getSectionSetupErrorMessage(section), WARNING)); return promises.resolve(); }); }
/** * Maps the objects to Promises using the provided function then behaves just like {@link org.uberfire.client.promise.Promises#all}. */ public <T, O> Promise<O> all(final List<T> objects, final Function<T, Promise<O>> f) { return objects.stream().map(f).reduce(resolve(), (p1, p2) -> p1.then(ignore -> p2)); }
@Override public Disposable schedule(Runnable action, long delayTime, TimeUnit unit) { action = RxJavaPlugins.onSchedule(action); ScheduledAction scheduledAction = new ScheduledAction(action); if (incremental && (delayTime <= 0 || unit == null)) { Promise.resolve(0).then(o -> { scheduledAction.run(); return null; }); } else { DomGlobal.setTimeout(args -> scheduledAction.run(), (int) unit.toMillis(delayTime)); } return scheduledAction; }
@Test public void testSaveModel() { persistencePresenter.persistenceDescriptorEditorContent = newPersistenceDescriptorEditorContent(); doReturn(mock(Path.class)).when(editorService).save(any(), any(), any(), eq("Test comment")); persistencePresenter.save("Test comment").catch_(i -> { Assert.fail("Promise should've been resolved!"); return promises.resolve(); }); verify(editorService).save(any(), any(), any(), eq("Test comment")); }
public static native <V> Promise<V> resolve(ResolveValueUnionType<V> value);