@reactor-team/js-sdk 2.10.1 → 2.11.1

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.
package/dist/index.js CHANGED
@@ -153,7 +153,7 @@ var import_zod = require("zod");
153
153
  // package.json
154
154
  var package_default = {
155
155
  name: "@reactor-team/js-sdk",
156
- version: "2.10.1",
156
+ version: "2.11.1",
157
157
  description: "Reactor JavaScript frontend SDK \u2014 connect React and TypeScript apps to real-time AI video models on Reactor.",
158
158
  license: "Apache-2.0",
159
159
  repository: {
@@ -325,6 +325,16 @@ var WebRTCSdpOfferRequestSchema = import_zod.z.object({
325
325
  var WebRTCSdpAnswerResponseSchema = import_zod.z.object({
326
326
  sdp_answer: import_zod.z.string()
327
327
  });
328
+ var IceCandidateSchema = import_zod.z.object({
329
+ candidate: import_zod.z.string(),
330
+ sdp_mid: import_zod.z.string().optional(),
331
+ sdp_mline_index: import_zod.z.number().optional()
332
+ });
333
+ var IceCandidatesRequestSchema = import_zod.z.object({
334
+ candidates: import_zod.z.array(IceCandidateSchema),
335
+ is_final: import_zod.z.boolean(),
336
+ client_info: ClientInfoSchema.optional()
337
+ });
328
338
 
329
339
  // src/core/CoordinatorClient.ts
330
340
  var SESSION_POLL_INITIAL_BACKOFF_MS = 200;
@@ -1179,7 +1189,6 @@ function createOffer(pc) {
1179
1189
  yield pc.setLocalDescription(
1180
1190
  new RTCSessionDescription({ type: initial.type, sdp })
1181
1191
  );
1182
- yield waitForIceGathering(pc);
1183
1192
  const localDescription = pc.localDescription;
1184
1193
  if (!localDescription) {
1185
1194
  throw new Error("Failed to create local description");
@@ -1208,28 +1217,6 @@ function transformIceServers(response) {
1208
1217
  return rtcServer;
1209
1218
  });
1210
1219
  }
1211
- function waitForIceGathering(pc, timeoutMs = 5e3) {
1212
- return new Promise((resolve) => {
1213
- if (pc.iceGatheringState === "complete") {
1214
- resolve();
1215
- return;
1216
- }
1217
- const onGatheringStateChange = () => {
1218
- if (pc.iceGatheringState === "complete") {
1219
- pc.removeEventListener(
1220
- "icegatheringstatechange",
1221
- onGatheringStateChange
1222
- );
1223
- resolve();
1224
- }
1225
- };
1226
- pc.addEventListener("icegatheringstatechange", onGatheringStateChange);
1227
- setTimeout(() => {
1228
- pc.removeEventListener("icegatheringstatechange", onGatheringStateChange);
1229
- resolve();
1230
- }, timeoutMs);
1231
- });
1232
- }
1233
1220
  function sendMessage(channel, command, data, scope = "application", maxBytes = DEFAULT_MAX_MESSAGE_BYTES, uploads) {
1234
1221
  if (channel.readyState !== "open") {
1235
1222
  throw new Error(`Data channel not open: ${channel.readyState}`);
@@ -1344,6 +1331,7 @@ var INITIAL_BACKOFF_MS = 200;
1344
1331
  var MAX_BACKOFF_MS = 15e3;
1345
1332
  var BACKOFF_MULTIPLIER = 2;
1346
1333
  var DEFAULT_MAX_POLL_ATTEMPTS = 6;
1334
+ var ICE_CANDIDATE_BATCH_WINDOW_MS = 25;
1347
1335
  var WebRTCTransportClient = class {
1348
1336
  constructor(config) {
1349
1337
  this.eventListeners = /* @__PURE__ */ new Map();
@@ -1352,6 +1340,8 @@ var WebRTCTransportClient = class {
1352
1340
  this.publishedTracks = /* @__PURE__ */ new Map();
1353
1341
  this.peerConnected = false;
1354
1342
  this.dataChannelOpen = false;
1343
+ // Trickle ICE batching
1344
+ this.pendingIceCandidates = [];
1355
1345
  var _a, _b;
1356
1346
  this.baseUrl = config.baseUrl;
1357
1347
  this.sessionId = config.sessionId;
@@ -1486,6 +1476,70 @@ var WebRTCTransportClient = class {
1486
1476
  console.debug("[WebRTCTransport] SDP offer accepted (202)");
1487
1477
  });
1488
1478
  }
1479
+ sendIceCandidates(candidates, is_final) {
1480
+ return __async(this, null, function* () {
1481
+ console.debug(
1482
+ `[WebRTCTransport] Sending ICE candidates (count=${candidates.length}, is_final=${is_final})`
1483
+ );
1484
+ const requestBody = {
1485
+ candidates,
1486
+ is_final,
1487
+ client_info: {
1488
+ sdk_version: REACTOR_SDK_VERSION,
1489
+ sdk_type: REACTOR_SDK_TYPE
1490
+ }
1491
+ };
1492
+ const response = yield fetch(`${this.transportBaseUrl}/ice_candidates`, {
1493
+ method: "POST",
1494
+ headers: __spreadProps(__spreadValues({}, yield this.getHeaders()), {
1495
+ "Content-Type": "application/json"
1496
+ }),
1497
+ body: JSON.stringify(requestBody),
1498
+ signal: this.signal
1499
+ });
1500
+ yield this.checkVersionMismatch(response);
1501
+ if (response.status !== 202) {
1502
+ const errorText = yield response.text();
1503
+ throw new Error(
1504
+ `Failed to send ICE candidates: ${response.status} ${errorText}`
1505
+ );
1506
+ }
1507
+ console.debug("[WebRTCTransport] ICE candidates accepted (202)");
1508
+ return;
1509
+ });
1510
+ }
1511
+ /**
1512
+ * Drain the pending ICE candidates buffer and POST them as a single
1513
+ * batch. Called either by the debounce timer (collected candidates so
1514
+ * far) or directly by the gathering-complete handler (with
1515
+ * ``isFinal=true``).
1516
+ *
1517
+ * Errors are logged at debug level — trickle ICE is fire-and-forget,
1518
+ * and the connection can still succeed even if some candidate batches
1519
+ * are lost (host/srflx candidates from the other side are usually
1520
+ * sufficient).
1521
+ */
1522
+ flushPendingIceCandidates(isFinal) {
1523
+ if (this.iceCandidateFlushTimer !== void 0) {
1524
+ clearTimeout(this.iceCandidateFlushTimer);
1525
+ this.iceCandidateFlushTimer = void 0;
1526
+ }
1527
+ const batch = this.pendingIceCandidates;
1528
+ this.pendingIceCandidates = [];
1529
+ if (batch.length === 0 && !isFinal) {
1530
+ return;
1531
+ }
1532
+ this.sendIceCandidates(batch, isFinal).catch((err) => {
1533
+ console.debug("[WebRTCTransport] ICE candidate flush failed:", err);
1534
+ });
1535
+ }
1536
+ cancelPendingIceCandidates() {
1537
+ if (this.iceCandidateFlushTimer !== void 0) {
1538
+ clearTimeout(this.iceCandidateFlushTimer);
1539
+ this.iceCandidateFlushTimer = void 0;
1540
+ }
1541
+ this.pendingIceCandidates = [];
1542
+ }
1489
1543
  pollSdpAnswer() {
1490
1544
  return __async(this, null, function* () {
1491
1545
  console.debug("[WebRTCTransport] Polling for SDP answer...");
@@ -1555,6 +1609,7 @@ var WebRTCTransportClient = class {
1555
1609
  this.resetTransportTimings();
1556
1610
  this.stopPing();
1557
1611
  this.stopStatsPolling();
1612
+ this.cancelPendingIceCandidates();
1558
1613
  if (this.dataChannel) {
1559
1614
  this.dataChannel.close();
1560
1615
  this.dataChannel = void 0;
@@ -1617,6 +1672,7 @@ var WebRTCTransportClient = class {
1617
1672
  return __async(this, null, function* () {
1618
1673
  this.stopPing();
1619
1674
  this.stopStatsPolling();
1675
+ this.cancelPendingIceCandidates();
1620
1676
  for (const name of Array.from(this.publishedTracks.keys())) {
1621
1677
  yield this.unpublishTrack(name);
1622
1678
  }
@@ -1870,8 +1926,21 @@ var WebRTCTransportClient = class {
1870
1926
  this.emit("trackReceived", trackName, event.track, stream);
1871
1927
  };
1872
1928
  this.peerConnection.onicecandidate = (event) => {
1929
+ var _a, _b;
1873
1930
  if (event.candidate) {
1874
- console.debug("[WebRTCTransport] ICE candidate:", event.candidate);
1931
+ this.pendingIceCandidates.push({
1932
+ candidate: event.candidate.candidate,
1933
+ sdp_mid: (_a = event.candidate.sdpMid) != null ? _a : void 0,
1934
+ sdp_mline_index: (_b = event.candidate.sdpMLineIndex) != null ? _b : void 0
1935
+ });
1936
+ if (this.iceCandidateFlushTimer === void 0) {
1937
+ this.iceCandidateFlushTimer = setTimeout(
1938
+ () => this.flushPendingIceCandidates(false),
1939
+ ICE_CANDIDATE_BATCH_WINDOW_MS
1940
+ );
1941
+ }
1942
+ } else {
1943
+ this.flushPendingIceCandidates(true);
1875
1944
  }
1876
1945
  };
1877
1946
  this.peerConnection.onicecandidateerror = (event) => {
@@ -2666,9 +2735,13 @@ var Reactor = class {
2666
2735
  sendCommand(command, data, scope = "application") {
2667
2736
  return __async(this, null, function* () {
2668
2737
  var _a;
2669
- if (process.env.NODE_ENV !== "development" && this.status !== "ready") {
2670
- const errorMessage = `Cannot send message, status is ${this.status}`;
2671
- console.warn("[Reactor]", errorMessage);
2738
+ if (this.status !== "ready") {
2739
+ this.createError(
2740
+ "NOT_READY",
2741
+ `Cannot send command "${command}" while status is "${this.status}". Must be "ready".`,
2742
+ "api",
2743
+ true
2744
+ );
2672
2745
  return;
2673
2746
  }
2674
2747
  try {
@@ -3355,7 +3428,10 @@ var createReactorStore = (initProps, publicState = defaultInitState) => {
3355
3428
  console.error("[ReactorStore] Failed to upload file:", error);
3356
3429
  throw error;
3357
3430
  }
3358
- })
3431
+ }),
3432
+ requestClip: (durationSeconds) => get().internal.reactor.requestClip(durationSeconds),
3433
+ requestRecording: () => get().internal.reactor.requestRecording(),
3434
+ downloadClipAsFile: (clip, filename, options) => get().internal.reactor.downloadClipAsFile(clip, filename, options)
3359
3435
  });
3360
3436
  });
3361
3437
  };
@@ -3483,7 +3559,7 @@ function ReactorProvider(_a) {
3483
3559
  console.error("[ReactorProvider] Failed to disconnect:", error);
3484
3560
  });
3485
3561
  };
3486
- }, [apiUrl, modelName, autoConnect, local, jwtSource, maxAttempts]);
3562
+ }, [apiUrl, modelName, local, jwtSource]);
3487
3563
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ReactorContext.Provider, { value: storeRef.current, children });
3488
3564
  }
3489
3565
  function useReactorStore(selector) {
@@ -3559,7 +3635,7 @@ function ReactorView({
3559
3635
  className,
3560
3636
  style,
3561
3637
  videoObjectFit = "contain",
3562
- muted = true
3638
+ muted = audioTrack === void 0
3563
3639
  }) {
3564
3640
  const { videoMediaTrack, audioMediaTrack } = useReactor((state) => {
3565
3641
  var _a, _b;
@@ -4202,6 +4278,8 @@ var import_react7 = require("react");
4202
4278
  var import_jsx_runtime4 = require("react/jsx-runtime");
4203
4279
  function WebcamStream({
4204
4280
  track,
4281
+ audio = false,
4282
+ audioTrack,
4205
4283
  className,
4206
4284
  style,
4207
4285
  videoConstraints = {
@@ -4209,7 +4287,10 @@ function WebcamStream({
4209
4287
  height: { ideal: 720 }
4210
4288
  },
4211
4289
  showWebcam = true,
4212
- videoObjectFit = "contain"
4290
+ videoObjectFit = "contain",
4291
+ onPermissionDenied,
4292
+ onPublished,
4293
+ onError
4213
4294
  }) {
4214
4295
  const [stream, setStream] = (0, import_react7.useState)(null);
4215
4296
  const [isPublishing, setIsPublishing] = (0, import_react7.useState)(false);
@@ -4221,12 +4302,28 @@ function WebcamStream({
4221
4302
  reactor: state.internal.reactor
4222
4303
  }));
4223
4304
  const videoRef = (0, import_react7.useRef)(null);
4305
+ const onPermissionDeniedRef = (0, import_react7.useRef)(onPermissionDenied);
4306
+ const onPublishedRef = (0, import_react7.useRef)(onPublished);
4307
+ const onErrorRef = (0, import_react7.useRef)(onError);
4308
+ (0, import_react7.useEffect)(() => {
4309
+ onPermissionDeniedRef.current = onPermissionDenied;
4310
+ onPublishedRef.current = onPublished;
4311
+ onErrorRef.current = onError;
4312
+ });
4313
+ const audioRequested = audio !== false && audio !== void 0;
4314
+ const audioEnabled = audioRequested && !!audioTrack;
4315
+ if (audioRequested && !audioTrack) {
4316
+ console.warn(
4317
+ "[WebcamStream] `audio` was set but `audioTrack` is missing; capturing video-only."
4318
+ );
4319
+ }
4224
4320
  const startWebcam = () => __async(null, null, function* () {
4321
+ var _a, _b;
4225
4322
  console.debug("[WebcamPublisher] Starting webcam");
4226
4323
  try {
4227
4324
  const mediaStream = yield navigator.mediaDevices.getUserMedia({
4228
4325
  video: videoConstraints,
4229
- audio: false
4326
+ audio: audioEnabled ? audio === true ? true : audio : false
4230
4327
  });
4231
4328
  console.debug("[WebcamPublisher] Webcam started successfully");
4232
4329
  setStream(mediaStream);
@@ -4236,16 +4333,34 @@ function WebcamStream({
4236
4333
  if (err instanceof DOMException && (err.name === "NotAllowedError" || err.name === "PermissionDeniedError")) {
4237
4334
  console.debug("[WebcamPublisher] Camera permission denied");
4238
4335
  setPermissionDenied(true);
4336
+ (_a = onPermissionDeniedRef.current) == null ? void 0 : _a.call(onPermissionDeniedRef);
4337
+ } else {
4338
+ (_b = onErrorRef.current) == null ? void 0 : _b.call(
4339
+ onErrorRef,
4340
+ err instanceof Error ? err : new Error(String(err))
4341
+ );
4239
4342
  }
4240
4343
  }
4241
4344
  });
4242
4345
  const stopWebcam = () => __async(null, null, function* () {
4346
+ var _a;
4243
4347
  console.debug("[WebcamPublisher] Stopping webcam");
4244
- try {
4245
- yield unpublish(track);
4246
- console.debug("[WebcamPublisher] Unpublished before stopping");
4247
- } catch (err) {
4248
- console.error("[WebcamPublisher] Error unpublishing before stop:", err);
4348
+ const unpublishTasks = [unpublish(track)];
4349
+ if (audioEnabled && audioTrack) {
4350
+ unpublishTasks.push(unpublish(audioTrack));
4351
+ }
4352
+ const results = yield Promise.allSettled(unpublishTasks);
4353
+ for (const r of results) {
4354
+ if (r.status === "rejected") {
4355
+ console.error(
4356
+ "[WebcamPublisher] Error unpublishing before stop:",
4357
+ r.reason
4358
+ );
4359
+ (_a = onErrorRef.current) == null ? void 0 : _a.call(
4360
+ onErrorRef,
4361
+ r.reason instanceof Error ? r.reason : new Error(String(r.reason))
4362
+ );
4363
+ }
4249
4364
  }
4250
4365
  setIsPublishing(false);
4251
4366
  stream == null ? void 0 : stream.getTracks().forEach((t) => {
@@ -4280,25 +4395,54 @@ function WebcamStream({
4280
4395
  console.debug(
4281
4396
  "[WebcamPublisher] Reactor ready, auto-publishing webcam stream"
4282
4397
  );
4283
- const videoTrack = stream.getVideoTracks()[0];
4284
- if (videoTrack) {
4285
- publish(track, videoTrack).then(() => {
4286
- console.debug("[WebcamPublisher] Auto-publish successful");
4287
- setIsPublishing(true);
4288
- }).catch((err) => {
4289
- console.error("[WebcamPublisher] Auto-publish failed:", err);
4290
- });
4291
- }
4398
+ const videoMediaTrack = stream.getVideoTracks()[0];
4399
+ const audioMediaTrack = audioEnabled && audioTrack ? stream.getAudioTracks()[0] : null;
4400
+ const tasks = [];
4401
+ if (videoMediaTrack) tasks.push(publish(track, videoMediaTrack));
4402
+ if (audioMediaTrack && audioTrack)
4403
+ tasks.push(publish(audioTrack, audioMediaTrack));
4404
+ if (tasks.length === 0) return;
4405
+ Promise.all(tasks).then(() => {
4406
+ var _a;
4407
+ console.debug("[WebcamPublisher] Auto-publish successful");
4408
+ setIsPublishing(true);
4409
+ (_a = onPublishedRef.current) == null ? void 0 : _a.call(onPublishedRef);
4410
+ }).catch((err) => {
4411
+ var _a;
4412
+ console.error("[WebcamPublisher] Auto-publish failed:", err);
4413
+ (_a = onErrorRef.current) == null ? void 0 : _a.call(
4414
+ onErrorRef,
4415
+ err instanceof Error ? err : new Error(String(err))
4416
+ );
4417
+ });
4292
4418
  } else if (status !== "ready" && isPublishing) {
4293
4419
  console.debug("[WebcamPublisher] Reactor not ready, auto-unpublishing");
4294
- unpublish(track).then(() => {
4295
- console.debug("[WebcamPublisher] Auto-unpublish successful");
4420
+ const tasks = [unpublish(track)];
4421
+ if (audioEnabled && audioTrack) tasks.push(unpublish(audioTrack));
4422
+ Promise.allSettled(tasks).then((results) => {
4423
+ var _a;
4424
+ for (const r of results) {
4425
+ if (r.status === "rejected") {
4426
+ console.error("[WebcamPublisher] Auto-unpublish failed:", r.reason);
4427
+ (_a = onErrorRef.current) == null ? void 0 : _a.call(
4428
+ onErrorRef,
4429
+ r.reason instanceof Error ? r.reason : new Error(String(r.reason))
4430
+ );
4431
+ }
4432
+ }
4296
4433
  setIsPublishing(false);
4297
- }).catch((err) => {
4298
- console.error("[WebcamPublisher] Auto-unpublish failed:", err);
4299
4434
  });
4300
4435
  }
4301
- }, [status, stream, isPublishing, publish, unpublish, track]);
4436
+ }, [
4437
+ status,
4438
+ stream,
4439
+ isPublishing,
4440
+ publish,
4441
+ unpublish,
4442
+ track,
4443
+ audioEnabled,
4444
+ audioTrack
4445
+ ]);
4302
4446
  (0, import_react7.useEffect)(() => {
4303
4447
  const handleError = (error) => {
4304
4448
  console.debug("[WebcamPublisher] Received error event:", error);
@@ -4401,7 +4545,8 @@ function ClipPlayer({
4401
4545
  autoPlay = true,
4402
4546
  muted = true,
4403
4547
  className,
4404
- style
4548
+ style,
4549
+ onError
4405
4550
  }) {
4406
4551
  const videoRef = (0, import_react8.useRef)(null);
4407
4552
  const [phase, setPhase] = (0, import_react8.useState)({ kind: "waiting" });
@@ -4409,13 +4554,21 @@ function ClipPlayer({
4409
4554
  const getJwtRef = (0, import_react8.useRef)(getJwt);
4410
4555
  const autoPlayRef = (0, import_react8.useRef)(autoPlay);
4411
4556
  const slackMsRef = (0, import_react8.useRef)(slackMs);
4557
+ const onErrorRef = (0, import_react8.useRef)(onError);
4412
4558
  const storeRef = (0, import_react8.useRef)(store);
4413
4559
  (0, import_react8.useEffect)(() => {
4414
4560
  getJwtRef.current = getJwt;
4415
4561
  autoPlayRef.current = autoPlay;
4416
4562
  slackMsRef.current = slackMs;
4563
+ onErrorRef.current = onError;
4417
4564
  storeRef.current = store;
4418
4565
  });
4566
+ (0, import_react8.useEffect)(() => {
4567
+ var _a;
4568
+ if (phase.kind === "error") {
4569
+ (_a = onErrorRef.current) == null ? void 0 : _a.call(onErrorRef, phase.error);
4570
+ }
4571
+ }, [phase]);
4419
4572
  (0, import_react8.useEffect)(() => {
4420
4573
  const video = videoRef.current;
4421
4574
  if (!video) return;
@@ -4423,9 +4576,10 @@ function ClipPlayer({
4423
4576
  let cancelled = false;
4424
4577
  let hlsInstance = null;
4425
4578
  let manifestBlobUrl = null;
4426
- const fail = (message) => {
4579
+ const fail = (error) => {
4427
4580
  if (cancelled) return;
4428
- setPhase({ kind: "error", message });
4581
+ const message = error instanceof RecordingError ? `${error.code}: ${error.reason}` : error.message;
4582
+ setPhase({ kind: "error", message, error });
4429
4583
  };
4430
4584
  const attachPlayer = (manifestUrl) => __async(null, null, function* () {
4431
4585
  const canPlayNative = video.canPlayType("application/vnd.apple.mpegurl") !== "";
@@ -4444,13 +4598,17 @@ function ClipPlayer({
4444
4598
  HlsCtor = mod.default;
4445
4599
  } catch (e) {
4446
4600
  fail(
4447
- "HLS playback unavailable in this browser. Install `hls.js` as a peer dependency, or use Download."
4601
+ new Error(
4602
+ "HLS playback unavailable in this browser. Install `hls.js` as a peer dependency, or use Download."
4603
+ )
4448
4604
  );
4449
4605
  return;
4450
4606
  }
4451
4607
  if (cancelled) return;
4452
4608
  if (!HlsCtor.isSupported()) {
4453
- fail("This browser cannot play HLS clips. Use Download instead.");
4609
+ fail(
4610
+ new Error("This browser cannot play HLS clips. Use Download instead.")
4611
+ );
4454
4612
  return;
4455
4613
  }
4456
4614
  const hls = new HlsCtor();
@@ -4468,7 +4626,7 @@ function ClipPlayer({
4468
4626
  var _a;
4469
4627
  if (cancelled) return;
4470
4628
  if (data.fatal) {
4471
- fail(`Playback error: ${(_a = data.details) != null ? _a : "unknown"}`);
4629
+ fail(new Error(`Playback error: ${(_a = data.details) != null ? _a : "unknown"}`));
4472
4630
  return;
4473
4631
  }
4474
4632
  console.warn("[Reactor.ClipPlayer] hls.js non-fatal error", data);
@@ -4499,8 +4657,7 @@ function ClipPlayer({
4499
4657
  if (err instanceof DOMException && err.name === "AbortError") {
4500
4658
  return;
4501
4659
  }
4502
- const message = err instanceof RecordingError ? `${err.code}: ${err.reason}` : err instanceof Error ? err.message : String(err);
4503
- fail(message);
4660
+ fail(err instanceof Error ? err : new Error(String(err)));
4504
4661
  }
4505
4662
  });
4506
4663
  setup();
@@ -4584,6 +4741,9 @@ function ClipPlayer({
4584
4741
  );
4585
4742
  }
4586
4743
 
4744
+ // src/react/ClipDownloadButton.tsx
4745
+ var import_react10 = require("react");
4746
+
4587
4747
  // src/react/useClipDownload.ts
4588
4748
  var import_react9 = require("react");
4589
4749
  function useClipDownload(clip, options = {}) {
@@ -4641,18 +4801,35 @@ function ClipDownloadButton({
4641
4801
  children,
4642
4802
  className,
4643
4803
  style,
4644
- disabled
4804
+ disabled,
4805
+ onSuccess,
4806
+ onError
4645
4807
  }) {
4646
4808
  const { state, download } = useClipDownload(clip, { filename, getJwt });
4647
4809
  const downloading = state.kind === "downloading";
4648
4810
  const isDisabled = downloading || !!disabled;
4811
+ const onSuccessRef = (0, import_react10.useRef)(onSuccess);
4812
+ const onErrorRef = (0, import_react10.useRef)(onError);
4813
+ (0, import_react10.useEffect)(() => {
4814
+ onSuccessRef.current = onSuccess;
4815
+ onErrorRef.current = onError;
4816
+ });
4817
+ (0, import_react10.useEffect)(() => {
4818
+ var _a;
4819
+ if (state.kind === "error") {
4820
+ (_a = onErrorRef.current) == null ? void 0 : _a.call(onErrorRef, new Error(state.message));
4821
+ }
4822
+ }, [state]);
4649
4823
  const content = typeof children === "function" ? children(state) : children !== void 0 ? children : defaultLabel(state);
4650
4824
  return /* @__PURE__ */ (0, import_jsx_runtime6.jsx)(
4651
4825
  "button",
4652
4826
  {
4653
4827
  type: "button",
4654
4828
  onClick: () => {
4655
- void download();
4829
+ void download().then((blob) => {
4830
+ var _a;
4831
+ if (blob) (_a = onSuccessRef.current) == null ? void 0 : _a.call(onSuccessRef, blob);
4832
+ });
4656
4833
  },
4657
4834
  disabled: isDisabled,
4658
4835
  title: state.kind === "error" ? state.message : void 0,