com.google.android.agera.Observable

Here are the examples of the java api class com.google.android.agera.Observable taken from open source projects.

1. NotesStore#notesStore()

Project: agera
Source File: NotesStore.java
View license
@NonNull
public static synchronized NotesStore notesStore(@NonNull final Context applicationContext) {
    if (notesStore != null) {
        return notesStore;
    }
    // Create a thread executor to execute all database operations on.
    final Executor executor = newSingleThreadExecutor();
    // Create a database supplier that initializes the database. This is also used to supply the
    // database in all database operations.
    final NotesSqlDatabaseSupplier databaseSupplier = databaseSupplier(applicationContext);
    // Create a function that processes database write operations.
    final Function<SqlInsertRequest, Result<Long>> insertNoteFunction = databaseInsertFunction(databaseSupplier);
    final Function<SqlUpdateRequest, Result<Integer>> updateNoteFunction = databaseUpdateFunction(databaseSupplier);
    final Function<SqlDeleteRequest, Result<Integer>> deleteNoteFunction = databaseDeleteFunction(databaseSupplier);
    // Create a reservoir of database write requests. This will be used as the receiver of write
    // requests submitted to the NotesStore, and the event/data source of the reacting repository.
    final Reservoir<Object> writeRequestReservoir = reservoir();
    // Create a reacting repository that processes all write requests. The value of the repository
    // is unimportant, but it must be able to notify the notes repository on completing each write
    // operation. The database thread executor is single-threaded to optimize for disk I/O, but if
    // the executor can be multi-threaded, then this is the ideal place to multiply the reacting
    // repository to achieve parallelism. The notes repository should observe all these instances.
    final Number unimportantValue = 0;
    final Merger<Number, Number, Boolean> alwaysNotify = staticMerger(true);
    final Observable writeReaction = repositoryWithInitialValue(unimportantValue).observe(writeRequestReservoir).onUpdatesPerLoop().goTo(executor).attemptGetFrom(writeRequestReservoir).orSkip().thenAttemptTransform( input -> {
        if (input instanceof SqlInsertRequest) {
            return insertNoteFunction.apply((SqlInsertRequest) input);
        }
        if (input instanceof SqlUpdateRequest) {
            return updateNoteFunction.apply((SqlUpdateRequest) input);
        }
        if (input instanceof SqlDeleteRequest) {
            return deleteNoteFunction.apply((SqlDeleteRequest) input);
        }
        return failure();
    }).orSkip().notifyIf(alwaysNotify).compile();
    // Keep the reacting repository in this lazy singleton activated for the full app life cycle.
    // This is optional -- it allows the write requests submitted when the notes repository is not
    // active to still be processed asap.
    writeReaction.addUpdatable(() -> {
    });
    // Create the repository of notes, wire it up to update on each database write, set it to fetch
    // notes from the database on the database thread executor.
    // Create the wired up notes store
    notesStore = new NotesStore(repositoryWithInitialValue(INITIAL_VALUE).observe(writeReaction).onUpdatesPerLoop().goTo(executor).getFrom(() -> sqlRequest().sql(GET_NOTES_FROM_TABLE).compile()).thenAttemptTransform(databaseQueryFunction(databaseSupplier,  cursor -> note(cursor.getInt(ID_COLUMN_INDEX), cursor.getString(NOTE_COLUMN_INDEX)))).orEnd(staticFunction(INITIAL_VALUE)).onConcurrentUpdate(SEND_INTERRUPT).onDeactivation(SEND_INTERRUPT).compile(), writeRequestReservoir);
    return notesStore;
}

2. MockUpdatable#removeFromObservables()

Project: agera
Source File: MockUpdatable.java
View license
public void removeFromObservables() {
    for (final Observable observable : observables) {
        observable.removeUpdatable(this);
    }
    observables.clear();
}

3. MockUpdatable#removeFromObservables()

Project: agera
Source File: MockUpdatable.java
View license
public void removeFromObservables() {
    for (final Observable observable : observables) {
        observable.removeUpdatable(this);
    }
    observables.clear();
    runUiThreadTasksIncludingDelayedTasks();
}