-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
37 changed files
with
444 additions
and
410 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import 'package:rxdart/rxdart.dart'; | ||
import 'package:stock/src/stock_response.dart'; | ||
|
||
extension StreamExtensions<T> on Stream<T> { | ||
Stream<StockResponse<T>> mapToResponse(ResponseOrigin origin) => | ||
map((data) => StockResponse.data(origin, data)) | ||
.onErrorReturnWith((error, stacktrace) { | ||
return StockResponse.error(origin, error, stacktrace); | ||
}); | ||
} | ||
|
||
extension FutureExtensions<T> on Future<T> { | ||
Future<StockResponse<T>> mapToResponse(ResponseOrigin origin) async { | ||
try { | ||
return StockResponse.data(origin, await this); | ||
} catch (error, stacktrace) { | ||
return StockResponse.error(origin, error, stacktrace); | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
lib/src/extensions/stock_response_internal_extensions.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import 'package:stock/src/errors.dart'; | ||
import 'package:stock/src/stock_response.dart'; | ||
import 'package:stock/src/stock_response_extensions.dart'; | ||
|
||
extension StockResponseExtensions<T> on StockResponse<T> { | ||
StockResponse<R> swapType<R>() { | ||
if (this is StockResponseData<T>) { | ||
return StockResponse.data(origin, requireData() as R); | ||
} else if (isError) { | ||
var errorResponse = this as StockResponseError<T>; | ||
return StockResponse.error( | ||
origin, | ||
errorResponse.error, | ||
errorResponse.stackTrace, | ||
); | ||
} else if (isLoading) { | ||
return StockResponse.loading(origin); | ||
} else { | ||
throw StockError('Unknown type'); | ||
} | ||
} | ||
} | ||
|
||
extension StockResponseStreamExtensions<T> on Stream<StockResponse<T?>> { | ||
Stream<StockResponse<T>> whereDataNotNull() => where( | ||
(event) => | ||
event is StockResponseData<T?> ? event.requireData() != null : true, | ||
).map((event) => event.swapType<T>()); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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]. | ||
abstract class Fetcher<Key, T> { | ||
Fetcher._(); | ||
|
||
/// "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); | ||
} |
2 changes: 1 addition & 1 deletion
2
lib/src/factory_fetcher.dart → lib/src/implementations/factory_fetcher.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
lib/src/source_of_truth_impl.dart → ...implementations/source_of_truth_impl.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.