| 1 |  |  | import 'dart:async'; | 
                
                
                    | 2 |  |  |  | 
                
                
                    | 3 |  |  | import 'package:cancelation_token/cancelation_token.dart'; | 
                
                
                    | 4 |  |  | import 'package:meta/meta.dart'; | 
                
                
                    | 5 |  |  |  | 
                
                
                    | 6 |  |  | import '../_impl/xplat/_token_id.dart'; | 
                
                
                    | 7 |  |  | import '../exceptions/squadron_canceled_exception.dart'; | 
                
                
                    | 8 |  |  |  | 
                
                
                    | 9 |  |  | class SquadronCancelationToken extends CancelationToken { | 
                
                
                    | 10 |  | 2 |   SquadronCancelationToken._(this.token, this.id) { | 
                
                
                    | 11 |  | 8 |     token?.onCanceled.then(_checkToken); | 
                
                
                    | 12 |  |  |   } | 
                
                
                    | 13 |  |  |  | 
                
                
                    | 14 |  | 2 |   List serialize() { | 
                
                
                    | 15 |  | 2 |     _checkToken(); | 
                
                
                    | 16 |  | 4 |     return List.unmodifiable([ | 
                
                
                    | 17 |  | 2 |       id, | 
                
                
                    | 18 |  | 4 |       exception?.serialize(), | 
                
                
                    | 19 |  |  |     ]); | 
                
                
                    | 20 |  |  |   } | 
                
                
                    | 21 |  |  |  | 
                
                
                    | 22 |  | 10 |   static SquadronCancelationToken? deserialize(List? props) { | 
                
                
                    | 23 |  |  |     if (props == null) return null; | 
                
                
                    | 24 |  | 2 |     final id = props[_$id]; | 
                
                
                    | 25 |  | 4 |     final ex = SquadronCanceledExceptionExt.deserialize(props[_$ex]); | 
                
                
                    | 26 |  | 2 |     final token = SquadronCancelationToken._(null, id); | 
                
                
                    | 27 |  |  |     if (ex != null) { | 
                
                
                    | 28 |  | 2 |       token._exception = ex; | 
                
                
                    | 29 |  | 4 |       token._completer.complete(ex); | 
                
                
                    | 30 |  |  |     } | 
                
                
                    | 31 |  |  |     return token; | 
                
                
                    | 32 |  |  |   } | 
                
                
                    | 33 |  |  |  | 
                
                
                    | 34 |  |  |   // cancelation token ID | 
                
                
                    | 35 |  |  |   final String id; | 
                
                
                    | 36 |  |  |  | 
                
                
                    | 37 |  |  |   // reference to the real cancelation token (only set on the caller side, will | 
                
                
                    | 38 |  |  |   // be `null` in worker threads) | 
                
                
                    | 39 |  |  |   final CancelationToken? token; | 
                
                
                    | 40 |  |  |  | 
                
                
                    | 41 |  | 2 |   @override | 
                
                
                    | 42 |  | 2 |   SquadronCanceledException? get exception => _exception; | 
                
                
                    | 43 |  |  |   SquadronCanceledException? _exception; | 
                
                
                    | 44 |  |  |  | 
                
                
                    | 45 |  | 2 |   @override | 
                
                
                    | 46 |  |  |   void ensureStarted() { | 
                
                
                    | 47 |  | 4 |     token?.ensureStarted(); | 
                
                
                    | 48 |  |  |   } | 
                
                
                    | 49 |  |  |  | 
                
                
                    | 50 |  | 2 |   @override | 
                
                
                    | 51 |  | 4 |   Future<CanceledException> get onCanceled => _completer.future; | 
                
                
                    | 52 |  |  |   final _completer = Completer<CanceledException>(); | 
                
                
                    | 53 |  |  |  | 
                
                
                    | 54 |  | 2 |   void _checkToken([dynamic _]) { | 
                
                
                    | 55 |  | 4 |     final ex = token?.exception; | 
                
                
                    | 56 |  |  |     if (ex != null) { | 
                
                
                    | 57 |  | 6 |       _exception ??= SquadronCanceledException.from(id, ex); | 
                
                
                    | 58 |  | 4 |       if (!_completer.isCompleted) { | 
                
                
                    | 59 |  | 6 |         _completer.complete(_exception); | 
                
                
                    | 60 |  |  |       } | 
                
                
                    | 61 |  |  |     } | 
                
                
                    | 62 |  |  |   } | 
                
                
                    | 63 |  |  | } | 
                
                
                    | 64 |  |  |  | 
                
                
                    | 65 |  |  | const _$id = 0; | 
                
                
                    | 66 |  |  | const _$ex = 1; | 
                
                
                    | 67 |  |  |  | 
                
                
                    | 68 |  |  | @internal | 
                
                
                    | 69 |  |  | extension SquadronCancelationTokenExt on CancelationToken { | 
                
                
                    | 70 |  | 2 |   SquadronCancelationToken wrap() { | 
                
                
                    | 71 |  |  |     final self = this; | 
                
                
                    | 72 |  | 2 |     if (self is SquadronCancelationToken) return self; | 
                
                
                    | 73 |  | 4 |     final token = SquadronCancelationToken._(self, TokenId.next()); | 
                
                
                    | 74 |  | 2 |     token._checkToken(); | 
                
                
                    | 75 |  |  |     return token; | 
                
                
                    | 76 |  |  |   } | 
                
                
                    | 77 |  |  | } |