|
| 1 | +import 'dart:async'; |
| 2 | +import 'package:meta/meta.dart'; |
| 3 | +import 'package:ferry_exec/ferry_exec.dart'; |
| 4 | +import 'package:gql/ast.dart'; |
| 5 | +import 'package:built_value/serializer.dart'; |
| 6 | +import 'package:hive_flutter/hive_flutter.dart'; |
| 7 | +import 'package:hive/hive.dart'; |
| 8 | +import 'package:ferry/typed_links.dart'; |
| 9 | +export 'package:ferry_cache/ferry_cache.dart'; |
| 10 | +export 'package:gql_link/gql_link.dart'; |
| 11 | +export 'package:normalize/policies.dart'; |
| 12 | +export 'package:ferry/src/update_cache_typed_link.dart' show UpdateCacheHandler; |
| 13 | +export 'package:ferry_exec/ferry_exec.dart'; |
| 14 | +export 'package:gql/ast.dart' show OperationType; |
| 15 | +import 'package:ferry_hive_store/ferry_hive_store.dart'; |
| 16 | + |
| 17 | +import './src/offline_mutation_typed_link.dart'; |
| 18 | + |
| 19 | +class OfflineClientConfig { |
| 20 | + final LinkExceptionHandler linkExceptionHandler; |
| 21 | + |
| 22 | + OfflineClientConfig({ |
| 23 | + this.linkExceptionHandler, |
| 24 | + }); |
| 25 | +} |
| 26 | + |
| 27 | +class OfflineClient extends TypedLink { |
| 28 | + final Link link; |
| 29 | + final StreamController<OperationRequest> requestController; |
| 30 | + final Map<String, TypePolicy> typePolicies; |
| 31 | + final Map<String, Function> updateCacheHandlers; |
| 32 | + final Map<OperationType, FetchPolicy> defaultFetchPolicies; |
| 33 | + final bool addTypename; |
| 34 | + final Box storeBox; |
| 35 | + final Box mutationQueueBox; |
| 36 | + final Cache cache; |
| 37 | + final OfflineClientConfig offlineConfig; |
| 38 | + final Serializers serializers; |
| 39 | + |
| 40 | + TypedLink _typedLink; |
| 41 | + |
| 42 | + OfflineClient({ |
| 43 | + @required this.link, |
| 44 | + @required this.storeBox, |
| 45 | + @required this.mutationQueueBox, |
| 46 | + @required this.serializers, |
| 47 | + this.offlineConfig, |
| 48 | + StreamController<OperationRequest> requestController, |
| 49 | + this.typePolicies = const {}, |
| 50 | + this.updateCacheHandlers = const {}, |
| 51 | + this.defaultFetchPolicies = const {}, |
| 52 | + this.addTypename = true, |
| 53 | + }) : cache = Cache( |
| 54 | + store: HiveStore(storeBox), |
| 55 | + typePolicies: typePolicies, |
| 56 | + addTypename: addTypename, |
| 57 | + ), |
| 58 | + requestController = requestController ?? StreamController.broadcast() { |
| 59 | + _typedLink = TypedLink.from([ |
| 60 | + RequestControllerTypedLink(this.requestController), |
| 61 | + OfflineMutationTypedLink( |
| 62 | + cache: cache, |
| 63 | + mutationQueueBox: mutationQueueBox, |
| 64 | + serializers: serializers, |
| 65 | + requestController: this.requestController, |
| 66 | + linkExceptionHandler: offlineConfig?.linkExceptionHandler, |
| 67 | + ), |
| 68 | + if (addTypename) AddTypenameTypedLink(), |
| 69 | + if (updateCacheHandlers.isNotEmpty) |
| 70 | + UpdateCacheTypedLink( |
| 71 | + cache: cache, |
| 72 | + updateCacheHandlers: updateCacheHandlers, |
| 73 | + ), |
| 74 | + FetchPolicyTypedLink( |
| 75 | + link: link, |
| 76 | + cache: cache, |
| 77 | + defaultFetchPolicies: defaultFetchPolicies, |
| 78 | + ) |
| 79 | + ]); |
| 80 | + } |
| 81 | + |
| 82 | + @override |
| 83 | + Stream<OperationResponse<TData, TVars>> request<TData, TVars>( |
| 84 | + OperationRequest<TData, TVars> request, [ |
| 85 | + forward, |
| 86 | + ]) => |
| 87 | + _typedLink.request(request, forward); |
| 88 | + |
| 89 | + /// Initializes an [OfflineClient] with default hive boxes |
| 90 | + static Future<OfflineClient> init({ |
| 91 | + @required Link link, |
| 92 | + @required Serializers serializers, |
| 93 | + OfflineClientConfig offlineConfig, |
| 94 | + StreamController<OperationRequest> requestController, |
| 95 | + Map<String, TypePolicy> typePolicies, |
| 96 | + Map<String, Function> updateCacheHandlers, |
| 97 | + Map<OperationType, FetchPolicy> defaultFetchPolicies, |
| 98 | + bool addTypename, |
| 99 | + }) async { |
| 100 | + await Hive.initFlutter(); |
| 101 | + final storeBox = await Hive.openBox<Map<String, dynamic>>('ferry_store'); |
| 102 | + final mutationQueueBox = |
| 103 | + await Hive.openBox<Map<String, dynamic>>('ferry_mutation_queue'); |
| 104 | + return OfflineClient( |
| 105 | + link: link, |
| 106 | + storeBox: storeBox, |
| 107 | + mutationQueueBox: mutationQueueBox, |
| 108 | + serializers: serializers, |
| 109 | + offlineConfig: offlineConfig, |
| 110 | + requestController: requestController, |
| 111 | + typePolicies: typePolicies, |
| 112 | + updateCacheHandlers: updateCacheHandlers, |
| 113 | + defaultFetchPolicies: defaultFetchPolicies, |
| 114 | + addTypename: addTypename, |
| 115 | + ); |
| 116 | + } |
| 117 | +} |
0 commit comments