@Override public void subscribe(final SingleEmitter<T> emitter) { operation.enqueue(new ApolloStoreOperation.Callback<T>() { @Override public void onSuccess(T result) { emitter.onSuccess(result); } @Override public void onFailure(Throwable t) { emitter.onError(t); } }); } });
/** * Execute store operation * <p> * <b>NOTE: this is a sync operation, proceed with a caution as it may include SQLite instruction<b/> * </p> * * @throws {@link ApolloException} in case of any errors */ public final T execute() throws ApolloException { checkIfExecuted(); try { return perform(); } catch (Exception e) { throw new ApolloException("Failed to perform store operation", e); } }
@NotNull @Override public ApolloStoreOperation<Boolean> clearAll() { return ApolloStoreOperation.emptyOperation(Boolean.FALSE); }
@Override public void run() { T result; try { result = perform(); } catch (Exception e) { notifyFailure(new ApolloException("Failed to perform store operation", e)); return; } notifySuccess(result); } });
/** * Clear all entries from the normalized cache. This is synchronous operation and will be executed int the current * thread * * @return {@code true} if operation succeed, {@code false} otherwise */ public boolean clearNormalizedCache() { return apolloStore.clearAll().execute(); }
/** * Schedules operation to be executed in dispatcher * * @param callback to be notified about operation result */ public void enqueue(@Nullable final Callback<T> callback) { checkIfExecuted(); this.callback.set(callback); dispatcher.execute(new Runnable() { @Override public void run() { T result; try { result = perform(); } catch (Exception e) { notifyFailure(new ApolloException("Failed to perform store operation", e)); return; } notifySuccess(result); } }); }
Set<String> rollbackOptimisticUpdates(final InterceptorRequest request) { try { return apolloStore.rollbackOptimisticUpdates(request.uniqueId).execute(); } catch (Exception e) { logger.e(e, "failed to rollback operation optimistic updates, for: %s", request.operation); return Collections.emptySet(); } }
/** * Clear all entries from the normalized cache. This is asynchronous operation and will be scheduled on the * dispatcher * * @param callback to be notified when operation is completed */ public void clearNormalizedCache(@NotNull ApolloStoreOperation.Callback<Boolean> callback) { checkNotNull(callback, "callback == null"); apolloStore.clearAll().enqueue(callback); }
@NotNull @Override public <D extends Operation.Data, T, V extends Operation.Variables> ApolloStoreOperation<T> read( @NotNull Operation<D, T, V> operation) { return ApolloStoreOperation.emptyOperation(null); }
@Override public void run() { try { apolloStore.rollbackOptimisticUpdatesAndPublish(request.uniqueId).execute(); } catch (Exception e) { logger.e(e, "failed to rollback operation optimistic updates, for: %s", request.operation); } } });
/** * Converts an {@link ApolloPrefetch} to a Completable. * * @param prefetch the ApolloPrefetch to convert * @return the converted Completable */ @NotNull public static Completable from(@NotNull final ApolloPrefetch prefetch) { checkNotNull(prefetch, "prefetch == null"); return Completable.create(new Completable.OnSubscribe() { @Override public void call(final CompletableSubscriber subscriber) { final Subscription subscription = getSubscription(subscriber, prefetch); prefetch.enqueue(new ApolloPrefetch.Callback() { @Override public void onSuccess() { if (!subscription.isUnsubscribed()) { subscriber.onCompleted(); } } @Override public void onFailure(@NotNull ApolloException e) { Exceptions.throwIfFatal(e); if (!subscription.isUnsubscribed()) { subscriber.onError(e); } } }); } }); }
@NotNull @Override public ApolloStoreOperation<Boolean> remove(@NotNull CacheKey cacheKey, boolean cascade) { return ApolloStoreOperation.emptyOperation(Boolean.FALSE); }
@Override public void run() { try { if (request.optimisticUpdates.isPresent()) { Operation.Data optimisticUpdates = request.optimisticUpdates.get(); apolloStore.writeOptimisticUpdatesAndPublish(request.operation, optimisticUpdates, request.uniqueId) .execute(); } } catch (Exception e) { logger.e(e, "failed to write operation optimistic updates, for: %s", request.operation); } } });
@NotNull @Override public ApolloStoreOperation<Boolean> rollbackOptimisticUpdatesAndPublish(@NotNull UUID mutationId) { return ApolloStoreOperation.emptyOperation(Boolean.FALSE); }
InterceptorResponse resolveFromCache(InterceptorRequest request) throws ApolloException { ResponseNormalizer<Record> responseNormalizer = apolloStore.cacheResponseNormalizer(); //noinspection unchecked ApolloStoreOperation<Response> apolloStoreOperation = apolloStore.read(request.operation, responseFieldMapper, responseNormalizer, request.cacheHeaders); Response cachedResponse = apolloStoreOperation.execute(); if (cachedResponse.data() != null) { logger.d("Cache HIT for operation %s", request.operation); return new InterceptorResponse(null, cachedResponse, responseNormalizer.records()); } logger.d("Cache MISS for operation %s", request.operation); throw new ApolloException(String.format("Cache miss for operation %s", request.operation)); }
@NotNull @Override public <D extends Operation.Data, T, V extends Operation.Variables> ApolloStoreOperation<Boolean> writeAndPublish( @NotNull Operation<D, T, V> operation, @NotNull D operationData) { return ApolloStoreOperation.emptyOperation(Boolean.FALSE); }
@NotNull @Override public <F extends GraphqlFragment> ApolloStoreOperation<F> read(@NotNull ResponseFieldMapper<F> fieldMapper, @NotNull CacheKey cacheKey, @NotNull Operation.Variables variables) { return ApolloStoreOperation.emptyOperation(null); }
@NotNull @Override public ApolloStoreOperation<Set<String>> rollbackOptimisticUpdates(@NotNull UUID mutationId) { return ApolloStoreOperation.emptyOperation(Collections.<String>emptySet()); } }
@NotNull @Override public <D extends Operation.Data, T, V extends Operation.Variables> ApolloStoreOperation<Boolean> writeOptimisticUpdatesAndPublish(@NotNull Operation<D, T, V> operation, @NotNull D operationData, @NotNull UUID mutationId) { return ApolloStoreOperation.emptyOperation(Boolean.FALSE); }
@NotNull @Override public ApolloStoreOperation<Boolean> remove(@NotNull CacheKey cacheKey) { return ApolloStoreOperation.emptyOperation(Boolean.FALSE); }