1 | | | /// Singleton class used to generate unique IDs for cancellation tokens. |
2 | | | /// |
3 | | | /// The class implements a simple counter that is incremented each time a new ID will be requested. Because of Dart's |
4 | | | /// architecture, this guarantees unicity per event loop (thread) only. If the [SequenceId] is used from an Isolate |
5 | | | /// or a Web Worker, IDs may overlap across threads. This may be a problem in complex scenarios where workers invoke |
6 | | | /// other workers while creating their own cancellation tokens. Ideally, workers should forward the cancellation token |
7 | | | /// they received when calling other workers. |
8 | | | class SequenceId { |
9 | | 2 | SequenceId._(); |
10 | | |
|
11 | | 4 | static final instance = SequenceId._(); |
12 | | |
|
13 | | | int _id = 0; |
14 | | |
|
15 | | 3 | int next() => ++_id; |
16 | | | } |