RxPromise


Source link: https://github.com/Q42/RxPromise

RxPromise  

A Promise wrapper around RxJava's Observable. A promise represents a future value (usually of an asynchronous operation).

JavaDoc

Why?

Why not just use Observables you ask? Well a Promise is easier to use when working with a single value (instead of a stream of values). Additionally, it has a consitent behaviour in terms of caching already fullfilled values.

Semantics

This promise is eager and is always cached:

  • Eager: The promise will start fulfilling it's value as soon as it's created.
  • Cached: The value will be redeemed from it's origin only once. All calls will wait until the promise's value is successfully fulfilled or the promise is rejected with an exception. All subsequent calls will immediately get the already fulfilled value (or exception).

For example, if the promise is a future value from a HTTP API call, the API call will always be called once, regardless of the complexity of the promise chain.

Compatibility

This library is compatible with Java 6

Installation

See https://jitpack.io/#Q42/RxPromise for instructions to include RxPromise as a gradle or maven dependency.

Usage

Promise.async(Test::longRunningOperation)

.map(integer -> integer * 10)

.flatMap(Test::anotherLongRunningOperation)

.onError(SomeException.class, someException -> log(someException))

.onSuccess(o -> doSomeThingWith(o));

Subscriptions

You can unsubscribe callbacks and subscribe again later on, resubscribing to the promise will not execute the longRunningOperation again, instead it will use the already fullfilled value or wait until the promise is fullfilled.

Promise<String> promise = Promise.async(Test::longRunningOperation);
 Subscription subscription = promise.then(System.out::println);
  // Unsubscribe, the callback 'System.out::println' will never be invoked subscription.unsubscribe();
  // Resubscribe promise.then(System.err::println);

Multiple promises

Wait for all promises to be fullfilled.

Promise<String> a = ...; Promise<String> b = ...; Promise<String> c = ...; Promise<String> d = ...;  Promise.all(a, b, c, d).onSuccess(System.out::println);
 // prints:  [a, b, c, d]

Wait for any promises to be fullfilled, promises that are rejected are ignored.

Promise<String> a = ...; Promise<String> b = ...; // error Promise<String> c = ...; Promise<String> d = ...; // error  Promise.any(a, b, c, d).onSuccess(System.out::println);
 // prints:  [a, c]

Wait for a specific amount of promises to be fullfilled, promises that are rejected are ignored. If there are not enough fullfilled promises to be returned, the somePromise is rejected.

Promise<String> a = ...; Promise<String> b = ...; // error Promise<String> c = ...; Promise<String> d = ...;  List<String> somePromise = Promise.some(2, a, b, c, d);
 somePromise.onSuccess(System.out::println) // prints:  [a, c]

Combine values of promises with different types

Promise<String> p1 = ...; Promise<Integer> p2 = ...; Promise<Boolean> p3 = ...; Promise.join(p1, p2, p3, (s, i, b) -> System.out.printf("%s, %d, %s%n", s, i, b));
 // prints: a, 1, true

Threads

You can (globally) specify the thread callback should be scheduled on.

// Set callbacks scheduler for all promises Promise.DEFAULT_CALLBACKS_SCHEDULER = AndroidSchedulers.mainThread();
  Promise.async(Test::longRunningOperation).then(o -> {

  // This is always called on the Android main Thread
  // Update the view here 
}
);

or

Promise.async(Test::longRunningOperation).callbacksOn(AndroidSchedulers.mainThread()).then(o -> {

  // This is called on the Android main Thread
  // Update the view here 
}
);

Resources

A way to MVVM using RxJava with new Android databinding.

Android Library to execute pieces of code that only need to run once on version updates.

Animated Expanding ListView provides a fancy animation on expanding or collapsing the content of a listview item.

Show a Snackbar from the top.

A simple to use, survey library for collecting feedback from your users.

This is a simple start-template to save you a little time.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes