Coder Social home page Coder Social logo

Comments (4)

DanielAsher avatar DanielAsher commented on May 30, 2024

I ran into this requirement today. I couldn't figure out whether there was any way to iterate over all keys in a store.

from store.

ychescale9 avatar ychescale9 commented on May 30, 2024

@digitalbuddha is this something we want to support? I can send a PR if it is 😃

New Store API might look like this:

interface Store<Key : Any, Output : Any> {

    /**
     * Return a flow for the given key
     * @param request - see [StoreRequest] for configurations
     */
    fun stream(request: StoreRequest<Key>): Flow<StoreResponse<Output>>

    /**
     * Purge a particular entry from memory and disk cache.
     * Persistent storage will only be cleared if a delete function was passed to
     * [StoreBuilder.persister] or [StoreBuilder.nonFlowingPersister] when creating the [Store].
     */
    suspend fun clear(key: Key)

    /**
     * Purge all entries from memory and disk cache.
     * Persistent storage will only be cleared if a deleteAll function was passed to
     * [StoreBuilder.persister] or [StoreBuilder.nonFlowingPersister] when creating the [Store].
     */
    suspend fun clearAll()
}

New persister APIs for deleting disk cache:

    /**
     * Connects a ([kotlinx.coroutines.flow.Flow]) source of truth that is accessed via [reader], [writer] and [delete].
     *
     * For maximal flexibility, [writer]'s record type ([Output]] and [reader]'s record type
     * ([NewOutput]) are not identical. This allows us to read one type of objects from network and
     * transform them to another type when placing them in local storage.
     *
     * A source of truth is usually backed by local storage. It's purpose is to eliminate the need
     * for waiting on network update before local modifications are available (via [Store.stream]).
     *
     * @param reader reads records from the source of truth
     * @param writer writes records **coming in from the fetcher (network)** to the source of truth.
     * Writing local user updates to the source of truth via [Store] is currently not supported.
     * @param delete deletes records in the source of truth for the give key
     * @param deleteAll deletes all records in the source of truth
     *
     */
    fun <NewOutput : Any> persister(
        reader: (Key) -> Flow<NewOutput?>,
        writer: suspend (Key, Output) -> Unit,
        delete: (suspend (Key) -> Unit)? = null,
        deleteAll: (suspend () -> Unit)? = null
    ): StoreBuilder<Key, NewOutput>

    /**
     * Connects a (non-[Flow]) source of truth that is accessible via [reader], [writer],
     * [delete], and [deleteAll].
     *
     * @see persister
     */
    fun <NewOutput : Any> nonFlowingPersister(
        reader: suspend (Key) -> NewOutput?,
        writer: suspend (Key, Output) -> Unit,
        delete: (suspend (Key) -> Unit)? = null,
        deleteAll: (suspend () -> Unit)? = null
    ): StoreBuilder<Key, NewOutput>

from store.

digitalbuddha avatar digitalbuddha commented on May 30, 2024

Off the top of my head I can't think of any reason not to have this. Let's give it a go and create a @experimental annotation allowing consumers to know that this is less hardened than other apis.

from store.

ychescale9 avatar ychescale9 commented on May 30, 2024

Cool. Will give it a go next week.

from store.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.