| 1 | | | import '../converters/context_aware_converter.dart'; |
| 2 | | | import '../converters/converter.dart'; |
| 3 | | | import '../converters/serialization_context.dart'; |
| 4 | | | import '../squadron_singleton.dart'; |
| 5 | | | import '../typedefs.dart'; |
| 6 | | | |
| 7 | | | /// Marshaling context. Context-aware marshalers can register marshaled / |
| 8 | | | /// unmarshaled instances; if the same input is encountered, the same instance |
| 9 | | | /// can be fetched from the context instead of marshaling/unmarshaling a fresh |
| 10 | | | /// instance. Context-aware marshalers can be used where object identities |
| 11 | | | /// matter. Additionally, they also help support serialization of instances |
| 12 | | | /// that bear cyclical dependencies. |
| 13 | | | class MarshalingContext { |
| 14 | | 2 | MarshalingContext({bool contextAware = true}) |
| 15 | | | : _converter = |
| 16 | | 3 | contextAware ? ContextAwareConverter() : Squadron.converter, |
| 17 | | 2 | _objects = contextAware ? SerializationContext(identical) : null; |
| 18 | | | |
| 19 | | | /// [none] can be used as a context-unaware converter. |
| 20 | | | static const MarshalingContext? none = null; |
| 21 | | | |
| 22 | | | /// [Converter] to convert data before marshaling/unmarshaling. In a context- |
| 23 | | | /// aware marshaling context, the converter also keeps track of raw data |
| 24 | | | /// received from / sent to the worker |
| 25 | | | final Converter _converter; |
| 26 | | | |
| 27 | | | final SerializationContext? _objects; |
| 28 | | | |
| 29 | | | /// Whether this context is context-aware. |
| 30 | | 2 | bool get isContextAware => _objects != null; |
| 31 | | | |
| 32 | | | /// Get the instance that was registered for the same [data]. |
| 33 | | 2 | T? getReference<T extends Object>(dynamic data) => |
| 34 | | 4 | _objects?.getReference(data); |
| 35 | | | |
| 36 | | | /// Register [instance] for this [data]. |
| 37 | | 2 | void setReference<T extends Object>(dynamic data, T instance) => |
| 38 | | 4 | _objects?.setReference(data, instance); |
| 39 | | | } |
| 40 | | | |
| 41 | | | /// Make MarshalingContext? a converter |
| 42 | | | extension ConverterExt on MarshalingContext? { |
| 43 | | 4 | Converter get converter => this?._converter ?? Squadron.converter; |
| 44 | | | |
| 45 | | 3 | Cast<T> value<T extends Object>() => converter.value<T>(); |
| 46 | | | |
| 47 | | 2 | Cast<List<T>> list<T extends Object>([Cast<T>? cast]) => |
| 48 | | 4 | converter.list<T>(cast); |
| 49 | | | |
| 50 | | 0 | Cast<List<T?>> nlist<T extends Object>([Cast<T>? cast]) => |
| 51 | | 0 | converter.nlist(cast); |
| 52 | | | |
| 53 | | 0 | Cast<Set<T>> set<T extends Object>([Cast<T>? cast]) => converter.set<T>(cast); |
| 54 | | | |
| 55 | | 0 | Cast<Set<T?>> nset<T extends Object>([Cast<T>? cast]) => converter.nset(cast); |
| 56 | | | |
| 57 | | 0 | Cast<Map<K, V>> map<K extends Object, V extends Object>( |
| 58 | | | {Cast<K>? kcast, Cast<V>? vcast}) => |
| 59 | | 0 | converter.map<K, V>(kcast: kcast, vcast: vcast); |
| 60 | | | |
| 61 | | 0 | Cast<Map<K, V?>> nmap<K extends Object, V extends Object>( |
| 62 | | | {Cast<K>? kcast, Cast<V>? vcast}) => |
| 63 | | 0 | converter.nmap<K, V>(kcast: kcast, vcast: vcast); |
| 64 | | | } |