xmartlabs / stock

Dart package for Async Data Loading and Caching. Combine local (DB, cache) and network data simply and safely.
https://pub.dev/packages/stock
Apache License 2.0
74 stars 6 forks source link

non-stream method stock.get() never finishes when object not existing #44

Open jkropf opened 3 months ago

jkropf commented 3 months ago

First of all, many thanks for the package! It satisfies my needs in caching media files (images and audio) in a app which needs to run on multiple platforms including web.

However, when I request a file from the cache using an indexed DB, the code

Uint8List data = await stock.get(path);

never finishes, even the data gets fetched and stored in the database successfully. The writer method for the database finishes and returns the data:

 Future<Uint8List> writeFile(path, data) async {
    var txn = db.transaction(storeName, "readwrite");
    var store = txn.objectStore(storeName);
    await store.put(data, path);
    log.d("writing finished. waiting for completion.");
    await txn.completed;
    log.d("writing data to database completed");
    return data;
}

When the data is already cached and existing in the database, everything works as expected.

Here is the initialization of the stock:

 final futureFetcher = Fetcher.ofFuture<String, Uint8List>(
           (path) => _apiService.getFile(path),
     );

final sourceOfTruth = SourceOfTruth<String, Uint8List>(
         reader: (path) => _database!.getFile(path),
         writer: (path, data) => _database!.writeFile(path, data!),
        );

     // Create Stock
     stock = Stock<String, Uint8List>(
         fetcher: futureFetcher,
        sourceOfTruth: sourceOfTruth,
     );

Kind regards, Johannes