-
Notifications
You must be signed in to change notification settings - Fork 8
/
fetcher.dart
41 lines (39 loc) · 1.77 KB
/
fetcher.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import 'package:stock/src/implementations/factory_fetcher.dart';
import 'package:stock/src/stock.dart';
import 'package:stock/src/stock_response.dart';
/// Fetcher is used by [Stock] to fetch network records of the type [T]
/// for a given key of the type [Key]. The return type is [Stream] to
/// allow for multiple results per request.
///
/// Note: [Stock] does not catch exceptions thrown by a [Fetcher].
/// Use [StockResponseError] to communicate expected errors.
///
/// See [ofFuture] for easily translating from a regular `Future` function.
/// See [ofStream], for easily translating to [StockResponse] (and
/// automatically transforming exceptions into [StockResponseError]).
base class Fetcher<Key, T> {
/// "Creates" a [Fetcher] from a [futureFactory] and translates the results
/// into a [StockResponse].
///
/// Emitted values will be wrapped in [StockResponseData]. If an exception
/// disrupts the stream then it will be wrapped in [StockResponseError]
///
/// Use when creating a [Stock] that fetches objects in a single response per
/// request network protocol (e.g Http).
static Fetcher<Key, Output> ofFuture<Key, Output>(
Future<Output> Function(Key key) futureFactory,
) =>
FutureFetcher(futureFactory);
/// "Creates" a [Fetcher] from a [streamFactory] and translates the results
/// into a [StockResponse].
///
/// Emitted values will be wrapped in [StockResponseData]. If an exception
/// disrupts the flow then it will be wrapped in [StockResponseError].
///
/// Use when creating a [Stock] that fetches objects in a multiple responses
/// per request network protocol (e.g Web Sockets).
static Fetcher<Key, Output> ofStream<Key, Output>(
Stream<Output> Function(Key key) streamFactory,
) =>
StreamFetcher(streamFactory);
}