1 | | | import '_impl/xplat/_platform.dart' |
2 | | | if (dart.library.io) '_impl/native/_platform.dart' |
3 | | | if (dart.library.html) '_impl/web/_platform.dart' |
4 | | | if (dart.library.js_interop) '_impl/web/_platform.dart' as impl; |
5 | | | import 'converters/converter.dart'; |
6 | | |
|
7 | | | class Squadron { |
8 | | 0 | Squadron._(); |
9 | | |
|
10 | | | /// Gets the current platform type. |
11 | | 8 | static final platformType = impl.getPlatformType(); |
12 | | |
|
13 | | | /// Parse [url] and returns the corresponding [Uri]. |
14 | | | /// |
15 | | | /// On Web platforms, a leading '~' character will be replaced with the |
16 | | | /// current page's root URL. E.g. '~/workers' from '/path/to/index.html' |
17 | | | /// will return '/path/to/workers'. |
18 | | 12 | static Uri uri(String url) => impl.mapUrl(url); |
19 | | |
|
20 | | 36 | static final _platformConverter = impl.getPlatformConverter(); |
21 | | 36 | static Converter _converter = _platformConverter; |
22 | | |
|
23 | | | /// Gets the current converter. |
24 | | 27 | static Converter get converter => _converter; |
25 | | |
|
26 | | | /// Sets the current converter. If [value] is `null`, the default converter |
27 | | | /// will be restored. If the converter was updated, handlers registered with |
28 | | | /// [onConverterChanged] will be notified of the change. |
29 | | 2 | static set converter(Converter? value) { |
30 | | 2 | value ??= _platformConverter; |
31 | | 3 | if (_converter != value) { |
32 | | | _converter = value; |
33 | | 4 | for (var handler in _converterChangeHandlers.values) { |
34 | | | try { |
35 | | 2 | handler(); |
36 | | | } catch (_) { |
37 | | | // ignore |
38 | | | } |
39 | | | } |
40 | | | } |
41 | | 1 | } |
42 | | |
|
43 | | 3 | static final _converterChangeHandlers = <Object, void Function()>{}; |
44 | | |
|
45 | | | /// Registers a callback that will be called whenever `converter` is changed. |
46 | | | /// Returns an object that can be used to unregister the callback by passing |
47 | | | /// it to [unregisterConverterChanged]. |
48 | | 1 | static Object onConverterChanged(void Function() handler) { |
49 | | 1 | final key = Object(); |
50 | | 3 | _converterChangeHandlers[key] = handler; |
51 | | | return key; |
52 | | | } |
53 | | |
|
54 | | | /// Unregisters a callback that was registered with [onConverterChanged]. |
55 | | 1 | static void unregisterConverterChanged(Object key) { |
56 | | 3 | _converterChangeHandlers.remove(key); |
57 | | | } |
58 | | | } |