rx-player 3.30.0-dev.2023020100 → 3.30.0-dev.2023022200

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (167) hide show
  1. package/CHANGELOG.md +2 -1
  2. package/VERSION +1 -1
  3. package/dist/_esm5.processed/compat/eme/close_session.js +2 -2
  4. package/dist/_esm5.processed/compat/event_listeners.js +1 -1
  5. package/dist/_esm5.processed/core/adaptive/adaptive_representation_selector.js +4 -2
  6. package/dist/_esm5.processed/core/api/debug/buffer_graph.d.ts +28 -0
  7. package/dist/_esm5.processed/core/api/debug/buffer_graph.js +175 -0
  8. package/dist/_esm5.processed/core/api/debug/buffer_size_graph.d.ts +10 -0
  9. package/dist/_esm5.processed/core/api/debug/buffer_size_graph.js +105 -0
  10. package/dist/_esm5.processed/core/api/debug/constants.d.ts +2 -0
  11. package/dist/_esm5.processed/core/api/debug/constants.js +2 -0
  12. package/dist/_esm5.processed/core/api/debug/index.d.ts +2 -0
  13. package/dist/_esm5.processed/core/api/debug/index.js +2 -0
  14. package/dist/_esm5.processed/core/api/debug/modules/general_info.d.ts +3 -0
  15. package/dist/_esm5.processed/core/api/debug/modules/general_info.js +199 -0
  16. package/dist/_esm5.processed/core/api/debug/modules/segment_buffer_content.d.ts +4 -0
  17. package/dist/_esm5.processed/core/api/debug/modules/segment_buffer_content.js +121 -0
  18. package/dist/_esm5.processed/core/api/debug/modules/segment_buffer_size.d.ts +3 -0
  19. package/dist/_esm5.processed/core/api/debug/modules/segment_buffer_size.js +35 -0
  20. package/dist/_esm5.processed/core/api/debug/render.d.ts +3 -0
  21. package/dist/_esm5.processed/core/api/debug/render.js +32 -0
  22. package/dist/_esm5.processed/core/api/debug/utils.d.ts +39 -0
  23. package/dist/_esm5.processed/core/api/debug/utils.js +57 -0
  24. package/dist/_esm5.processed/core/api/playback_observer.js +3 -2
  25. package/dist/_esm5.processed/core/api/public_api.d.ts +3 -0
  26. package/dist/_esm5.processed/core/api/public_api.js +24 -13
  27. package/dist/_esm5.processed/core/api/utils.js +3 -3
  28. package/dist/_esm5.processed/core/decrypt/__tests__/__global__/utils.d.ts +5 -5
  29. package/dist/_esm5.processed/core/decrypt/attach_media_keys.js +1 -1
  30. package/dist/_esm5.processed/core/decrypt/content_decryptor.js +1 -1
  31. package/dist/_esm5.processed/core/decrypt/session_events_listener.js +37 -47
  32. package/dist/_esm5.processed/core/decrypt/utils/check_key_statuses.js +4 -0
  33. package/dist/_esm5.processed/core/fetchers/cdn_prioritizer.d.ts +17 -8
  34. package/dist/_esm5.processed/core/fetchers/cdn_prioritizer.js +10 -6
  35. package/dist/_esm5.processed/core/fetchers/manifest/manifest_fetcher.js +5 -4
  36. package/dist/_esm5.processed/core/fetchers/segment/segment_fetcher.d.ts +22 -5
  37. package/dist/_esm5.processed/core/fetchers/segment/segment_fetcher.js +37 -21
  38. package/dist/_esm5.processed/core/fetchers/segment/task_prioritizer.js +21 -23
  39. package/dist/_esm5.processed/core/fetchers/utils/schedule_request.js +6 -5
  40. package/dist/_esm5.processed/core/init/directfile_content_initializer.js +1 -1
  41. package/dist/_esm5.processed/core/init/media_source_content_initializer.js +27 -31
  42. package/dist/_esm5.processed/core/init/utils/content_time_boundaries_observer.js +2 -2
  43. package/dist/_esm5.processed/core/init/utils/create_media_source.js +3 -12
  44. package/dist/_esm5.processed/core/init/utils/end_of_stream.js +6 -3
  45. package/dist/_esm5.processed/core/init/utils/get_loaded_reference.js +2 -1
  46. package/dist/_esm5.processed/core/init/utils/initial_seek_and_play.js +9 -5
  47. package/dist/_esm5.processed/core/init/utils/initialize_content_decryption.js +2 -1
  48. package/dist/_esm5.processed/core/init/utils/media_duration_updater.js +23 -19
  49. package/dist/_esm5.processed/core/init/utils/stream_events_emitter/stream_events_emitter.js +6 -4
  50. package/dist/_esm5.processed/core/init/utils/throw_on_media_error.js +1 -1
  51. package/dist/_esm5.processed/core/segment_buffers/implementations/audio_video/audio_video_segment_buffer.js +7 -10
  52. package/dist/_esm5.processed/core/segment_buffers/implementations/text/html/html_text_segment_buffer.js +4 -2
  53. package/dist/_esm5.processed/core/segment_buffers/segment_buffers_store.js +13 -9
  54. package/dist/_esm5.processed/core/stream/adaptation/adaptation_stream.js +10 -11
  55. package/dist/_esm5.processed/core/stream/orchestrator/stream_orchestrator.js +26 -12
  56. package/dist/_esm5.processed/core/stream/period/period_stream.js +11 -10
  57. package/dist/_esm5.processed/core/stream/representation/representation_stream.js +15 -15
  58. package/dist/_esm5.processed/core/stream/representation/utils/append_segment_to_buffer.js +1 -1
  59. package/dist/_esm5.processed/core/stream/representation/utils/downloading_queue.js +16 -6
  60. package/dist/_esm5.processed/experimental/features/debug_element.d.ts +8 -0
  61. package/dist/_esm5.processed/experimental/features/debug_element.js +10 -0
  62. package/dist/_esm5.processed/experimental/features/index.d.ts +1 -0
  63. package/dist/_esm5.processed/experimental/features/index.js +1 -0
  64. package/dist/_esm5.processed/experimental/tools/VideoThumbnailLoader/prepare_source_buffer.js +7 -4
  65. package/dist/_esm5.processed/experimental/tools/VideoThumbnailLoader/video_thumbnail_loader.js +5 -4
  66. package/dist/_esm5.processed/features/features_object.js +1 -0
  67. package/dist/_esm5.processed/features/initialize_features.js +13 -10
  68. package/dist/_esm5.processed/features/types.d.ts +3 -0
  69. package/dist/_esm5.processed/manifest/adaptation.js +4 -0
  70. package/dist/_esm5.processed/manifest/manifest.js +2 -0
  71. package/dist/_esm5.processed/manifest/representation.js +11 -4
  72. package/dist/_esm5.processed/parsers/manifest/dash/common/parse_adaptation_sets.js +105 -137
  73. package/dist/_esm5.processed/parsers/manifest/dash/common/parse_representations.js +25 -5
  74. package/dist/_esm5.processed/public_types.d.ts +1 -0
  75. package/dist/_esm5.processed/transports/dash/add_segment_integrity_checks_to_loader.js +13 -12
  76. package/dist/_esm5.processed/transports/dash/low_latency_segment_loader.js +2 -2
  77. package/dist/_esm5.processed/transports/dash/manifest_parser.js +1 -1
  78. package/dist/_esm5.processed/transports/dash/segment_loader.js +4 -4
  79. package/dist/_esm5.processed/transports/local/segment_loader.js +13 -26
  80. package/dist/_esm5.processed/transports/smooth/segment_loader.js +4 -4
  81. package/dist/_esm5.processed/transports/utils/call_custom_manifest_loader.js +3 -3
  82. package/dist/_esm5.processed/utils/cancellable_sleep.js +4 -10
  83. package/dist/_esm5.processed/utils/create_cancellable_promise.d.ts +26 -0
  84. package/dist/_esm5.processed/utils/create_cancellable_promise.js +52 -0
  85. package/dist/_esm5.processed/utils/reference.js +6 -0
  86. package/dist/_esm5.processed/utils/request/xhr.js +1 -1
  87. package/dist/_esm5.processed/utils/task_canceller.d.ts +34 -15
  88. package/dist/_esm5.processed/utils/task_canceller.js +55 -22
  89. package/dist/rx-player.js +739 -626
  90. package/dist/rx-player.min.js +1 -1
  91. package/jest.config.js +1 -0
  92. package/package.json +18 -18
  93. package/scripts/build/constants.d.ts +1 -0
  94. package/scripts/report_build_sizes +4 -0
  95. package/sonar-project.properties +1 -1
  96. package/src/compat/eme/close_session.ts +2 -2
  97. package/src/compat/event_listeners.ts +1 -1
  98. package/src/core/adaptive/adaptive_representation_selector.ts +4 -2
  99. package/src/core/api/debug/buffer_graph.ts +247 -0
  100. package/src/core/api/debug/buffer_size_graph.ts +131 -0
  101. package/src/core/api/debug/constants.ts +2 -0
  102. package/src/core/api/debug/index.ts +3 -0
  103. package/src/core/api/debug/modules/general_info.ts +204 -0
  104. package/src/core/api/debug/modules/segment_buffer_content.ts +155 -0
  105. package/src/core/api/debug/modules/segment_buffer_size.ts +48 -0
  106. package/src/core/api/debug/render.ts +40 -0
  107. package/src/core/api/debug/utils.ts +103 -0
  108. package/src/core/api/playback_observer.ts +4 -2
  109. package/src/core/api/public_api.ts +27 -13
  110. package/src/core/api/utils.ts +3 -3
  111. package/src/core/decrypt/attach_media_keys.ts +1 -1
  112. package/src/core/decrypt/content_decryptor.ts +1 -1
  113. package/src/core/decrypt/session_events_listener.ts +37 -49
  114. package/src/core/decrypt/utils/check_key_statuses.ts +6 -0
  115. package/src/core/fetchers/cdn_prioritizer.ts +18 -9
  116. package/src/core/fetchers/manifest/manifest_fetcher.ts +5 -4
  117. package/src/core/fetchers/segment/segment_fetcher.ts +36 -14
  118. package/src/core/fetchers/segment/task_prioritizer.ts +25 -30
  119. package/src/core/fetchers/utils/schedule_request.ts +6 -5
  120. package/src/core/init/directfile_content_initializer.ts +1 -1
  121. package/src/core/init/media_source_content_initializer.ts +37 -41
  122. package/src/core/init/utils/content_time_boundaries_observer.ts +2 -2
  123. package/src/core/init/utils/create_media_source.ts +4 -16
  124. package/src/core/init/utils/end_of_stream.ts +6 -3
  125. package/src/core/init/utils/get_loaded_reference.ts +2 -1
  126. package/src/core/init/utils/initial_seek_and_play.ts +9 -5
  127. package/src/core/init/utils/initialize_content_decryption.ts +2 -1
  128. package/src/core/init/utils/media_duration_updater.ts +25 -20
  129. package/src/core/init/utils/stream_events_emitter/stream_events_emitter.ts +6 -4
  130. package/src/core/init/utils/throw_on_media_error.ts +1 -1
  131. package/src/core/segment_buffers/implementations/audio_video/audio_video_segment_buffer.ts +7 -11
  132. package/src/core/segment_buffers/implementations/text/html/html_text_segment_buffer.ts +4 -2
  133. package/src/core/segment_buffers/segment_buffers_store.ts +16 -13
  134. package/src/core/stream/adaptation/adaptation_stream.ts +10 -11
  135. package/src/core/stream/orchestrator/stream_orchestrator.ts +26 -12
  136. package/src/core/stream/period/period_stream.ts +11 -10
  137. package/src/core/stream/representation/representation_stream.ts +15 -15
  138. package/src/core/stream/representation/utils/append_segment_to_buffer.ts +1 -1
  139. package/src/core/stream/representation/utils/downloading_queue.ts +16 -4
  140. package/src/experimental/features/__tests__/debug_element.test.ts +26 -0
  141. package/src/experimental/features/debug_element.ts +13 -0
  142. package/src/experimental/features/index.ts +1 -0
  143. package/src/experimental/tools/VideoThumbnailLoader/prepare_source_buffer.ts +7 -4
  144. package/src/experimental/tools/VideoThumbnailLoader/video_thumbnail_loader.ts +5 -4
  145. package/src/features/__tests__/initialize_features.test.ts +11 -0
  146. package/src/features/features_object.ts +1 -0
  147. package/src/features/initialize_features.ts +15 -10
  148. package/src/features/types.ts +9 -0
  149. package/src/manifest/adaptation.ts +7 -0
  150. package/src/manifest/manifest.ts +4 -0
  151. package/src/manifest/representation.ts +10 -4
  152. package/src/parsers/manifest/dash/common/parse_adaptation_sets.ts +116 -151
  153. package/src/parsers/manifest/dash/common/parse_representations.ts +21 -4
  154. package/src/public_types.ts +1 -0
  155. package/src/transports/dash/add_segment_integrity_checks_to_loader.ts +30 -24
  156. package/src/transports/dash/low_latency_segment_loader.ts +2 -2
  157. package/src/transports/dash/manifest_parser.ts +1 -1
  158. package/src/transports/dash/segment_loader.ts +4 -4
  159. package/src/transports/local/segment_loader.ts +14 -30
  160. package/src/transports/smooth/segment_loader.ts +4 -4
  161. package/src/transports/utils/call_custom_manifest_loader.ts +3 -3
  162. package/src/typings/globals.d.ts +2 -0
  163. package/src/utils/cancellable_sleep.ts +5 -14
  164. package/src/utils/create_cancellable_promise.ts +69 -0
  165. package/src/utils/reference.ts +6 -0
  166. package/src/utils/request/xhr.ts +1 -1
  167. package/src/utils/task_canceller.ts +63 -34
@@ -25,7 +25,7 @@ export default function callCustomManifestLoader(customManifestLoader, fallbackM
25
25
  * @param {Object} args
26
26
  */
27
27
  var resolve = function (_args) {
28
- if (hasFinished || cancelSignal.isCancelled) {
28
+ if (hasFinished || cancelSignal.isCancelled()) {
29
29
  return;
30
30
  }
31
31
  hasFinished = true;
@@ -45,7 +45,7 @@ export default function callCustomManifestLoader(customManifestLoader, fallbackM
45
45
  */
46
46
  var reject = function (err) {
47
47
  var _a, _b, _c;
48
- if (hasFinished || cancelSignal.isCancelled) {
48
+ if (hasFinished || cancelSignal.isCancelled()) {
49
49
  return;
50
50
  }
51
51
  hasFinished = true;
@@ -62,7 +62,7 @@ export default function callCustomManifestLoader(customManifestLoader, fallbackM
62
62
  * the "regular" implementation
63
63
  */
64
64
  var fallback = function () {
65
- if (hasFinished || cancelSignal.isCancelled) {
65
+ if (hasFinished || cancelSignal.isCancelled()) {
66
66
  return;
67
67
  }
68
68
  hasFinished = true;
@@ -13,6 +13,7 @@
13
13
  * See the License for the specific language governing permissions and
14
14
  * limitations under the License.
15
15
  */
16
+ import createCancellablePromise from "./create_cancellable_promise";
16
17
  /**
17
18
  * Wait the given `delay`, resolving the Promise when finished.
18
19
  *
@@ -27,15 +28,8 @@
27
28
  * cancellation with the corresponding `CancellationError`.
28
29
  */
29
30
  export default function cancellableSleep(delay, cancellationSignal) {
30
- return new Promise(function (res, rej) {
31
- var timeout = setTimeout(function () {
32
- unregisterCancelSignal();
33
- res();
34
- }, delay);
35
- var unregisterCancelSignal = cancellationSignal
36
- .register(function onCancel(cancellationError) {
37
- clearTimeout(timeout);
38
- rej(cancellationError);
39
- });
31
+ return createCancellablePromise(cancellationSignal, function (res) {
32
+ var timeout = setTimeout(function () { return res(); }, delay);
33
+ return function () { return clearTimeout(timeout); };
40
34
  });
41
35
  }
@@ -0,0 +1,26 @@
1
+ import { CancellationSignal } from "./task_canceller";
2
+ /**
3
+ * Returns a Promise linked to a `CancellationSignal`, which will reject the
4
+ * corresponding `CancellationError` if that signal emits before the wanted
5
+ * task finishes (either on success or on error).
6
+ *
7
+ * The given callback mimicks the Promise interface with the added possibility
8
+ * of returning a callback which will be called when and if the task is
9
+ * cancelled before being either resolved or rejected.
10
+ * In that case, that logic will be called just before the Promise is rejected
11
+ * with the corresponding `CancellationError`.
12
+ * The point of this callback is to implement aborting logic, such as for
13
+ * example aborting a request.
14
+ *
15
+ * @param {Object} cancellationSignal - The `CancellationSignal` the returned
16
+ * Promise will be linked to.
17
+ * @param {Function} cb - The function implementing the cancellable Promise. Its
18
+ * arguments follow Promise's semantics but it can also return a function which
19
+ * will be called when and if `cancellationSignal` emits before either arguments
20
+ * are called.
21
+ * @returns {Promise} - The created Promise, which will resolve when and if the
22
+ * first argument to `cb` is called first and reject either if the second
23
+ * argument to `cb` is called first or if the given `CancellationSignal` emits
24
+ * before either of the two previous conditions.
25
+ */
26
+ export default function createCancellablePromise<T>(cancellationSignal: CancellationSignal, cb: (resolve: (val: T) => void, reject: (err: unknown) => void) => (() => void) | void): Promise<T>;
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Returns a Promise linked to a `CancellationSignal`, which will reject the
3
+ * corresponding `CancellationError` if that signal emits before the wanted
4
+ * task finishes (either on success or on error).
5
+ *
6
+ * The given callback mimicks the Promise interface with the added possibility
7
+ * of returning a callback which will be called when and if the task is
8
+ * cancelled before being either resolved or rejected.
9
+ * In that case, that logic will be called just before the Promise is rejected
10
+ * with the corresponding `CancellationError`.
11
+ * The point of this callback is to implement aborting logic, such as for
12
+ * example aborting a request.
13
+ *
14
+ * @param {Object} cancellationSignal - The `CancellationSignal` the returned
15
+ * Promise will be linked to.
16
+ * @param {Function} cb - The function implementing the cancellable Promise. Its
17
+ * arguments follow Promise's semantics but it can also return a function which
18
+ * will be called when and if `cancellationSignal` emits before either arguments
19
+ * are called.
20
+ * @returns {Promise} - The created Promise, which will resolve when and if the
21
+ * first argument to `cb` is called first and reject either if the second
22
+ * argument to `cb` is called first or if the given `CancellationSignal` emits
23
+ * before either of the two previous conditions.
24
+ */
25
+ export default function createCancellablePromise(cancellationSignal, cb) {
26
+ var abortingLogic;
27
+ return new Promise(function (res, rej) {
28
+ if (cancellationSignal.cancellationError !== null) {
29
+ // If the signal was already triggered before, do not even call `cb`
30
+ return rej(cancellationSignal.cancellationError);
31
+ }
32
+ var hasUnregistered = false;
33
+ abortingLogic = cb(function onCancellablePromiseSuccess(val) {
34
+ cancellationSignal.deregister(onCancellablePromiseCancellation);
35
+ hasUnregistered = true;
36
+ res(val);
37
+ }, function onCancellablePromiseFailure(err) {
38
+ cancellationSignal.deregister(onCancellablePromiseCancellation);
39
+ hasUnregistered = true;
40
+ rej(err);
41
+ });
42
+ if (!hasUnregistered) {
43
+ cancellationSignal.register(onCancellablePromiseCancellation);
44
+ }
45
+ function onCancellablePromiseCancellation(error) {
46
+ if (abortingLogic !== undefined) {
47
+ abortingLogic();
48
+ }
49
+ rej(error);
50
+ }
51
+ });
52
+ }
@@ -126,6 +126,9 @@ export default function createSharedReference(initialValue, cancelSignal) {
126
126
  }
127
127
  options.clearSignal.register(unlisten);
128
128
  function unlisten() {
129
+ if ((options === null || options === void 0 ? void 0 : options.clearSignal) !== undefined) {
130
+ options.clearSignal.deregister(unlisten);
131
+ }
129
132
  if (cbObj.hasBeenCleared) {
130
133
  return;
131
134
  }
@@ -165,6 +168,9 @@ export default function createSharedReference(initialValue, cancelSignal) {
165
168
  finish: finish,
166
169
  };
167
170
  function finish() {
171
+ if (cancelSignal !== undefined) {
172
+ cancelSignal.deregister(finish);
173
+ }
168
174
  isFinished = true;
169
175
  var clonedCbs = cbs.slice();
170
176
  for (var _i = 0, clonedCbs_2 = clonedCbs; _i < clonedCbs_2.length; _i++) {
@@ -68,7 +68,7 @@ export default function request(options) {
68
68
  }
69
69
  reject(err);
70
70
  });
71
- if (cancelSignal.isCancelled) {
71
+ if (cancelSignal.isCancelled()) {
72
72
  return;
73
73
  }
74
74
  }
@@ -116,12 +116,12 @@ export default class TaskCanceller {
116
116
  * notified that it should be aborted when this `TaskCanceller` is triggered
117
117
  * (through its `cancel` method).
118
118
  */
119
- signal: CancellationSignal;
119
+ readonly signal: CancellationSignal;
120
120
  /**
121
121
  * `true` if this `TaskCanceller` has already been triggered.
122
122
  * `false` otherwise.
123
123
  */
124
- isUsed: boolean;
124
+ private _isUsed;
125
125
  /**
126
126
  * @private
127
127
  * Internal function called when the `TaskCanceller` is triggered`.
@@ -131,15 +131,28 @@ export default class TaskCanceller {
131
131
  * Creates a new `TaskCanceller`, with its own `CancellationSignal` created
132
132
  * as its `signal` provide.
133
133
  * You can then pass this property to async task you wish to be cancellable.
134
- * @param {Object|undefined} options
135
134
  */
136
- constructor(options?: {
137
- /**
138
- * If set the TaskCanceller created here will automatically be triggered
139
- * when that signal emits.
140
- */
141
- cancelOn?: CancellationSignal | undefined;
142
- } | undefined);
135
+ constructor();
136
+ /**
137
+ * Returns `true` if this `TaskCanceller` has already been triggered.
138
+ * `false` otherwise.
139
+ */
140
+ isUsed(): boolean;
141
+ /**
142
+ * Bind this `TaskCanceller` to a `CancellationSignal`, so the former
143
+ * is automatically cancelled when the latter is triggered.
144
+ *
145
+ * Note that this call registers a callback on the given signal, until either
146
+ * the current `TaskCanceller` is cancelled or until this given
147
+ * `CancellationSignal` is triggered.
148
+ * To avoid leaking memory, the returned callback allow to undo this link.
149
+ * It should be called if/when that link is not needed anymore, such as when
150
+ * there is no need for this `TaskCanceller` anymore.
151
+ *
152
+ * @param {Object} signal
153
+ * @returns {Function}
154
+ */
155
+ linkToSignal(signal: CancellationSignal): () => void;
143
156
  /**
144
157
  * "Trigger" the `TaskCanceller`, notify through its associated
145
158
  * `CancellationSignal` (its `signal` property) that a task should be aborted.
@@ -166,11 +179,6 @@ export default class TaskCanceller {
166
179
  * @class
167
180
  */
168
181
  export declare class CancellationSignal {
169
- /**
170
- * True when the cancellation order was already triggered, meaning that the
171
- * linked task needs to be aborted.
172
- */
173
- isCancelled: boolean;
174
182
  /**
175
183
  * Error associated to the cancellation, only set if the `CancellationSignal`
176
184
  * has been used (which means that the task has been cancelled).
@@ -188,6 +196,11 @@ export declare class CancellationSignal {
188
196
  * which depend on this CancellationSignal.
189
197
  */
190
198
  private _listeners;
199
+ /**
200
+ * True when the cancellation order was already triggered, meaning that the
201
+ * linked task needs to be aborted.
202
+ */
203
+ private _isCancelled;
191
204
  /**
192
205
  * Creates a new CancellationSignal.
193
206
  * /!\ Note: Only a `TaskCanceller` is supposed to be able to create one.
@@ -195,6 +208,12 @@ export declare class CancellationSignal {
195
208
  * cancelled.
196
209
  */
197
210
  constructor(registerToSource: (listener: ICancellationListener) => void);
211
+ /**
212
+ * Returns `true` when the cancellation order was already triggered, meaning
213
+ * that the linked task needs to be aborted.
214
+ * @returns boolean
215
+ */
216
+ isCancelled(): boolean;
198
217
  /**
199
218
  * Registers a function that will be called when/if the current task is
200
219
  * cancelled.
@@ -28,6 +28,7 @@ var __extends = (this && this.__extends) || (function () {
28
28
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
29
29
  };
30
30
  })();
31
+ import log from "../log";
31
32
  import assert from "./assert";
32
33
  import noop from "./noop";
33
34
  /**
@@ -132,21 +133,42 @@ var TaskCanceller = /** @class */ (function () {
132
133
  * Creates a new `TaskCanceller`, with its own `CancellationSignal` created
133
134
  * as its `signal` provide.
134
135
  * You can then pass this property to async task you wish to be cancellable.
135
- * @param {Object|undefined} options
136
136
  */
137
- function TaskCanceller(options) {
138
- var _this = this;
137
+ function TaskCanceller() {
139
138
  var _a = createCancellationFunctions(), trigger = _a[0], register = _a[1];
140
- this.isUsed = false;
139
+ this._isUsed = false;
141
140
  this._trigger = trigger;
142
141
  this.signal = new CancellationSignal(register);
143
- if ((options === null || options === void 0 ? void 0 : options.cancelOn) !== undefined) {
144
- var unregisterParent = options.cancelOn.register(function () {
145
- _this.cancel();
146
- });
147
- this.signal.register(unregisterParent);
148
- }
149
142
  }
143
+ /**
144
+ * Returns `true` if this `TaskCanceller` has already been triggered.
145
+ * `false` otherwise.
146
+ */
147
+ TaskCanceller.prototype.isUsed = function () {
148
+ return this._isUsed;
149
+ };
150
+ /**
151
+ * Bind this `TaskCanceller` to a `CancellationSignal`, so the former
152
+ * is automatically cancelled when the latter is triggered.
153
+ *
154
+ * Note that this call registers a callback on the given signal, until either
155
+ * the current `TaskCanceller` is cancelled or until this given
156
+ * `CancellationSignal` is triggered.
157
+ * To avoid leaking memory, the returned callback allow to undo this link.
158
+ * It should be called if/when that link is not needed anymore, such as when
159
+ * there is no need for this `TaskCanceller` anymore.
160
+ *
161
+ * @param {Object} signal
162
+ * @returns {Function}
163
+ */
164
+ TaskCanceller.prototype.linkToSignal = function (signal) {
165
+ var _this = this;
166
+ var unregister = signal.register(function () {
167
+ _this.cancel();
168
+ });
169
+ this.signal.register(unregister);
170
+ return unregister;
171
+ };
150
172
  /**
151
173
  * "Trigger" the `TaskCanceller`, notify through its associated
152
174
  * `CancellationSignal` (its `signal` property) that a task should be aborted.
@@ -159,10 +181,10 @@ var TaskCanceller = /** @class */ (function () {
159
181
  * @param {Error} [srcError]
160
182
  */
161
183
  TaskCanceller.prototype.cancel = function (srcError) {
162
- if (this.isUsed) {
184
+ if (this._isUsed) {
163
185
  return;
164
186
  }
165
- this.isUsed = true;
187
+ this._isUsed = true;
166
188
  var cancellationError = srcError !== null && srcError !== void 0 ? srcError : new CancellationError();
167
189
  this._trigger(cancellationError);
168
190
  };
@@ -192,18 +214,32 @@ var CancellationSignal = /** @class */ (function () {
192
214
  */
193
215
  function CancellationSignal(registerToSource) {
194
216
  var _this = this;
195
- this.isCancelled = false;
217
+ this._isCancelled = false;
196
218
  this.cancellationError = null;
197
219
  this._listeners = [];
198
220
  registerToSource(function (cancellationError) {
199
221
  _this.cancellationError = cancellationError;
200
- _this.isCancelled = true;
222
+ _this._isCancelled = true;
201
223
  while (_this._listeners.length > 0) {
202
- var listener = _this._listeners.splice(_this._listeners.length - 1, 1)[0];
203
- listener(cancellationError);
224
+ try {
225
+ var listener = _this._listeners.pop();
226
+ listener === null || listener === void 0 ? void 0 : listener(cancellationError);
227
+ }
228
+ catch (err) {
229
+ log.error("Error while calling clean up listener", err instanceof Error ? err.toString() :
230
+ "Unknown error");
231
+ }
204
232
  }
205
233
  });
206
234
  }
235
+ /**
236
+ * Returns `true` when the cancellation order was already triggered, meaning
237
+ * that the linked task needs to be aborted.
238
+ * @returns boolean
239
+ */
240
+ CancellationSignal.prototype.isCancelled = function () {
241
+ return this._isCancelled;
242
+ };
207
243
  /**
208
244
  * Registers a function that will be called when/if the current task is
209
245
  * cancelled.
@@ -227,9 +263,10 @@ var CancellationSignal = /** @class */ (function () {
227
263
  */
228
264
  CancellationSignal.prototype.register = function (fn) {
229
265
  var _this = this;
230
- if (this.isCancelled) {
266
+ if (this._isCancelled) {
231
267
  assert(this.cancellationError !== null);
232
268
  fn(this.cancellationError);
269
+ return noop;
233
270
  }
234
271
  this._listeners.push(fn);
235
272
  return function () { return _this.deregister(fn); };
@@ -243,13 +280,9 @@ var CancellationSignal = /** @class */ (function () {
243
280
  * @param {Function} fn
244
281
  */
245
282
  CancellationSignal.prototype.deregister = function (fn) {
246
- if (this.isCancelled) {
247
- return;
248
- }
249
- for (var i = 0; i < this._listeners.length; i++) {
283
+ for (var i = this._listeners.length - 1; i >= 0; i--) {
250
284
  if (this._listeners[i] === fn) {
251
285
  this._listeners.splice(i, 1);
252
- return;
253
286
  }
254
287
  }
255
288
  };