@bitsocial/bitsocial-react-hooks 0.3.3 → 0.3.5

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.
@@ -38,7 +38,8 @@ export function handlePublishErrorWhenAbandoned(activeRequestIdRef, requestId, e
38
38
  if (activeRequestIdRef.current !== requestId)
39
39
  return;
40
40
  setErrors((errors) => [...errors, error]);
41
- (onError !== null && onError !== void 0 ? onError : noop)(error);
41
+ // the publish action rejected, which ends the publish, before or without a publication
42
+ (onError !== null && onError !== void 0 ? onError : noop)(error, undefined, { final: true });
42
43
  }
43
44
  /** Clears what the previous publication left in a publish hook's result. */
44
45
  const clearPublicationResult = (...setters) => {
@@ -48,8 +49,70 @@ const clearPublicationResult = (...setters) => {
48
49
  /** For usePublishVote catch. Exported for coverage. */
49
50
  export function handlePublishVoteError(error, setErrors, onError) {
50
51
  setErrors((errors) => [...errors, error]);
51
- (onError !== null && onError !== void 0 ? onError : noop)(error);
52
+ // the publish action rejected, which ends the publish, before or without a publication
53
+ (onError !== null && onError !== void 0 ? onError : noop)(error, undefined, { final: true });
52
54
  }
55
+ /**
56
+ * Sets a publish hook's state from a publishing state the store reports. pkc-js also reports
57
+ * "failed" for an error it recovers from, while the publication keeps publishing, so the hook state
58
+ * only becomes "failed" once the publish is over. Once the publish failed, a publishing state pkc-js
59
+ * reports after the error that ended it, e.g. an RPC server's "stopped", does not replace "failed".
60
+ */
61
+ const setPublishingStateOfPublication = (setPublishingState, publishFailedRef, publishingState, publication) => {
62
+ if (publishFailedRef.current)
63
+ return;
64
+ if (publishingState === "failed" && (publication === null || publication === void 0 ? void 0 : publication.state) === "publishing")
65
+ return;
66
+ setPublishingState(publishingState);
67
+ };
68
+ /**
69
+ * A final error ends the hook state failed, whatever publishing state pkc-js reported last: an
70
+ * earlier "failed" that was ignored above, an RPC server's state, or none when publish() rejected
71
+ * before a challenge exchange existed.
72
+ */
73
+ const setFailedStateOnFinalError = (setPublishingState, publishFailedRef, info) => {
74
+ if (!(info === null || info === void 0 ? void 0 : info.final))
75
+ return;
76
+ publishFailedRef.current = true;
77
+ setPublishingState("failed");
78
+ };
79
+ /**
80
+ * The publish options of one usePublishCommentEdit, usePublishCommentModeration or
81
+ * usePublishCommunityEdit call. Only the latest call writes the hook result, so a late state or
82
+ * error of an earlier publication that is still running can't replace the newer one's; the app's
83
+ * callbacks still get the earlier publication's challenge, verification and errors.
84
+ */
85
+ const getPublishEditCallOptions = (options, isActiveRequest, setters) => {
86
+ // set by this call's final error, see setPublishingStateOfPublication
87
+ const publishFailed = { current: false };
88
+ return Object.assign(Object.assign({}, options), { onChallenge: (challenge, publication) => __awaiter(void 0, void 0, void 0, function* () {
89
+ var _a;
90
+ if (isActiveRequest()) {
91
+ // cannot set a function directly with setState
92
+ setters.setPublishChallengeAnswers(() => publication === null || publication === void 0 ? void 0 : publication.publishChallengeAnswers.bind(publication));
93
+ setters.setChallenge(challenge);
94
+ // an automatic republish's challenge replaces the previous attempt's failed verification
95
+ setters.setChallengeVerification(undefined);
96
+ }
97
+ (_a = options.onChallenge) === null || _a === void 0 ? void 0 : _a.call(options, challenge, publication);
98
+ }), onChallengeVerification: (challengeVerification, publication) => __awaiter(void 0, void 0, void 0, function* () {
99
+ var _a;
100
+ if (isActiveRequest())
101
+ setters.setChallengeVerification(challengeVerification);
102
+ (_a = options.onChallengeVerification) === null || _a === void 0 ? void 0 : _a.call(options, challengeVerification, publication);
103
+ }), onPublishingStateChange: (publishingState, publication) => {
104
+ if (isActiveRequest()) {
105
+ setPublishingStateOfPublication(setters.setPublishingState, publishFailed, publishingState, publication);
106
+ }
107
+ }, onError: (error, publication, info) => __awaiter(void 0, void 0, void 0, function* () {
108
+ var _a;
109
+ if (isActiveRequest()) {
110
+ setters.setErrors((errors) => [...errors, error]);
111
+ setFailedStateOnFinalError(setters.setPublishingState, publishFailed, info);
112
+ }
113
+ (_a = options.onError) === null || _a === void 0 ? void 0 : _a.call(options, error, publication, info);
114
+ }) });
115
+ };
53
116
  const publishChallengeAnswersNotReady = () => __awaiter(void 0, void 0, void 0, function* () {
54
117
  throw Error(`can't call publishChallengeAnswers() before result.challenge is defined (before the challenge message is received)`);
55
118
  });
@@ -203,6 +266,8 @@ export function usePublishComment(options) {
203
266
  const [challengeVerification, setChallengeVerification] = useState();
204
267
  const [publishChallengeAnswers, setPublishChallengeAnswers] = useState();
205
268
  const publishRequestIdRef = useRef(0);
269
+ // set by a final error of the latest publication, see setPublishingStateOfPublication
270
+ const publishFailedRef = useRef(false);
206
271
  // the latest publication, until it is abandoned or the store rejects it. Only its events write the
207
272
  // hook state, and abandonPublish() abandons it.
208
273
  const activePublishRequestIdRef = useRef(undefined);
@@ -224,6 +289,7 @@ export function usePublishComment(options) {
224
289
  activePublishRequestIdRef.current = requestId;
225
290
  const isActive = () => activePublishRequestIdRef.current === requestId;
226
291
  const updatesHookState = () => mountedRef.current && isActive();
292
+ publishFailedRef.current = false;
227
293
  if (mountedRef.current) {
228
294
  resetPublishState();
229
295
  setErrors([]);
@@ -261,13 +327,14 @@ export function usePublishComment(options) {
261
327
  });
262
328
  abandonActivePublishRef.current = abandon;
263
329
  const { onError: originalOnError, onChallenge: originalOnChallenge, onChallengeVerification: originalOnChallengeVerification, onPendingComment: originalOnPendingComment, } = publishCommentOptions;
264
- const activePublishCommentOptions = Object.assign(Object.assign({}, publishCommentOptions), { onError: (error) => __awaiter(this, void 0, void 0, function* () {
330
+ const activePublishCommentOptions = Object.assign(Object.assign({}, publishCommentOptions), { onError: (error, comment, info) => __awaiter(this, void 0, void 0, function* () {
265
331
  if (abandoned)
266
332
  return;
267
333
  if (updatesHookState()) {
268
334
  setErrors((errors) => [...errors, error]);
335
+ setFailedStateOnFinalError(setPublishingState, publishFailedRef, info);
269
336
  }
270
- (originalOnError !== null && originalOnError !== void 0 ? originalOnError : noop)(error);
337
+ (originalOnError !== null && originalOnError !== void 0 ? originalOnError : noop)(error, comment, info);
271
338
  }), onChallenge: (challenge, comment) => __awaiter(this, void 0, void 0, function* () {
272
339
  if (abandoned)
273
340
  return;
@@ -285,7 +352,7 @@ export function usePublishComment(options) {
285
352
  setChallengeVerification(challengeVerification);
286
353
  }
287
354
  (originalOnChallengeVerification !== null && originalOnChallengeVerification !== void 0 ? originalOnChallengeVerification : noop)(challengeVerification, comment);
288
- }), onPublishingStateChange: withGuardActive(updatesHookState, (publishingState) => setPublishingState(publishingState)),
355
+ }), onPublishingStateChange: withGuardActive(updatesHookState, (publishingState, comment) => setPublishingStateOfPublication(setPublishingState, publishFailedRef, publishingState, comment)),
289
356
  // called once the pending comment is stored, and again when a deletion shifts its index
290
357
  _onPendingCommentIndex: (pendingIndex, _pendingComment, abandonComment) => {
291
358
  abandonPendingComment = abandonComment;
@@ -310,7 +377,7 @@ export function usePublishComment(options) {
310
377
  resolveDeferredAbandon();
311
378
  try {
312
379
  if (!mountedRef.current && isActive()) {
313
- (originalOnError !== null && originalOnError !== void 0 ? originalOnError : noop)(e);
380
+ (originalOnError !== null && originalOnError !== void 0 ? originalOnError : noop)(e, undefined, { final: true });
314
381
  }
315
382
  else {
316
383
  if (isActive()) {
@@ -371,6 +438,8 @@ export function usePublishVote(options) {
371
438
  // the stopped publication's late events (like the "stopped" publishing state) neither overwrite
372
439
  // the cleared hook state nor leak into the next publication
373
440
  const publishVoteRequestIdRef = useRef(0);
441
+ // set by a final error of the latest vote, see setPublishingStateOfPublication
442
+ const publishFailedRef = useRef(false);
374
443
  const activePublishVoteRequestIdRef = useRef(undefined);
375
444
  // given by the store as publishVote() starts, it abandons the vote in the account that publishes it
376
445
  const abandonActiveVoteRef = useRef(undefined);
@@ -381,9 +450,10 @@ export function usePublishVote(options) {
381
450
  }
382
451
  // define onError if not defined
383
452
  const originalOnError = publishVoteOptions.onError;
384
- const onError = (error) => __awaiter(this, void 0, void 0, function* () {
453
+ const onError = (error, vote, info) => __awaiter(this, void 0, void 0, function* () {
385
454
  setErrors((errors) => [...errors, error]);
386
- originalOnError === null || originalOnError === void 0 ? void 0 : originalOnError(error);
455
+ setFailedStateOnFinalError(setPublishingState, publishFailedRef, info);
456
+ originalOnError === null || originalOnError === void 0 ? void 0 : originalOnError(error, vote, info);
387
457
  });
388
458
  publishVoteOptions.onError = onError;
389
459
  // define onChallenge if not defined
@@ -403,12 +473,13 @@ export function usePublishVote(options) {
403
473
  });
404
474
  publishVoteOptions.onChallengeVerification = onChallengeVerification;
405
475
  // change state on publishing state change
406
- publishVoteOptions.onPublishingStateChange = (publishingState) => {
407
- setPublishingState(publishingState);
476
+ publishVoteOptions.onPublishingStateChange = (publishingState, vote) => {
477
+ setPublishingStateOfPublication(setPublishingState, publishFailedRef, publishingState, vote);
408
478
  };
409
479
  const publishVote = () => __awaiter(this, void 0, void 0, function* () {
410
480
  const requestId = publishVoteRequestIdRef.current + 1;
411
481
  publishVoteRequestIdRef.current = requestId;
482
+ publishFailedRef.current = false;
412
483
  activePublishVoteRequestIdRef.current = requestId;
413
484
  abandonActiveVoteRef.current = undefined;
414
485
  const isActiveRequest = () => activePublishVoteRequestIdRef.current === requestId;
@@ -420,16 +491,17 @@ export function usePublishVote(options) {
420
491
  }, onChallengeVerification: (challengeVerification, vote) => {
421
492
  if (isActiveRequest())
422
493
  onChallengeVerification(challengeVerification, vote);
423
- }, onPublishingStateChange: (publishingState) => {
424
- if (isActiveRequest())
425
- setPublishingState(publishingState);
494
+ }, onPublishingStateChange: (publishingState, vote) => {
495
+ if (isActiveRequest()) {
496
+ setPublishingStateOfPublication(setPublishingState, publishFailedRef, publishingState, vote);
497
+ }
426
498
  },
427
499
  // an earlier or abandoned vote's error still reaches the app's onError, but not the result
428
- onError: (error) => {
500
+ onError: (error, vote, info) => {
429
501
  if (isActiveRequest())
430
- onError(error);
502
+ onError(error, vote, info);
431
503
  else
432
- originalOnError === null || originalOnError === void 0 ? void 0 : originalOnError(error);
504
+ originalOnError === null || originalOnError === void 0 ? void 0 : originalOnError(error, vote, info);
433
505
  }, _onVotePublishSession: (abandonVote) => {
434
506
  if (isActiveRequest())
435
507
  abandonActiveVoteRef.current = abandonVote;
@@ -443,7 +515,7 @@ export function usePublishVote(options) {
443
515
  handlePublishVoteError(e, setErrors, originalOnError);
444
516
  }
445
517
  else {
446
- originalOnError === null || originalOnError === void 0 ? void 0 : originalOnError(e);
518
+ originalOnError === null || originalOnError === void 0 ? void 0 : originalOnError(e, undefined, { final: true });
447
519
  }
448
520
  }
449
521
  });
@@ -494,48 +566,29 @@ export function usePublishCommentEdit(options) {
494
566
  }
495
567
  // the latest publishCommentEdit() call owns the result
496
568
  const publishRequestIdRef = useRef(0);
497
- // define onError if not defined
498
- const originalOnError = publishCommentEditOptions.onError;
499
- const onError = (error) => __awaiter(this, void 0, void 0, function* () {
500
- setErrors((errors) => [...errors, error]);
501
- originalOnError === null || originalOnError === void 0 ? void 0 : originalOnError(error);
502
- });
503
- publishCommentEditOptions.onError = onError;
504
- // define onChallenge if not defined
505
- const originalOnChallenge = publishCommentEditOptions.onChallenge;
506
- const onChallenge = (challenge, commentEdit) => __awaiter(this, void 0, void 0, function* () {
507
- // cannot set a function directly with setState
508
- setPublishChallengeAnswers(() => commentEdit === null || commentEdit === void 0 ? void 0 : commentEdit.publishChallengeAnswers.bind(commentEdit));
509
- setChallenge(challenge);
510
- // an automatic republish's challenge replaces the previous attempt's failed verification
511
- setChallengeVerification(undefined);
512
- originalOnChallenge === null || originalOnChallenge === void 0 ? void 0 : originalOnChallenge(challenge, commentEdit);
513
- });
514
- publishCommentEditOptions.onChallenge = onChallenge;
515
- // define onChallengeVerification if not defined
516
- const originalOnChallengeVerification = publishCommentEditOptions.onChallengeVerification;
517
- const onChallengeVerification = (challengeVerification, commentEdit) => __awaiter(this, void 0, void 0, function* () {
518
- setChallengeVerification(challengeVerification);
519
- originalOnChallengeVerification === null || originalOnChallengeVerification === void 0 ? void 0 : originalOnChallengeVerification(challengeVerification, commentEdit);
520
- });
521
- publishCommentEditOptions.onChallengeVerification = onChallengeVerification;
522
- // change state on publishing state change
523
- publishCommentEditOptions.onPublishingStateChange = (publishingState) => {
524
- setPublishingState(publishingState);
525
- };
526
569
  const publishCommentEdit = () => __awaiter(this, void 0, void 0, function* () {
570
+ var _a;
527
571
  const requestId = ++publishRequestIdRef.current;
572
+ const isActiveRequest = () => publishRequestIdRef.current === requestId;
528
573
  clearPublicationResult(setChallenge, setChallengeVerification, setPublishChallengeAnswers, setPublishingState);
529
574
  setErrors([]);
575
+ const callOptions = getPublishEditCallOptions(publishCommentEditOptions, isActiveRequest, {
576
+ setErrors,
577
+ setPublishingState,
578
+ setChallenge,
579
+ setChallengeVerification,
580
+ setPublishChallengeAnswers,
581
+ });
530
582
  try {
531
- yield accountsActions.publishCommentEdit(publishCommentEditOptions, accountName);
583
+ yield accountsActions.publishCommentEdit(callOptions, accountName);
532
584
  }
533
585
  catch (e) {
534
- if (publishRequestIdRef.current === requestId) {
586
+ if (isActiveRequest()) {
535
587
  setPublishingState("failed");
536
588
  setErrors((errors) => [...errors, e]);
537
589
  }
538
- originalOnError === null || originalOnError === void 0 ? void 0 : originalOnError(e);
590
+ // the publish action rejected, which ends the publish, before or without a publication
591
+ (_a = publishCommentEditOptions.onError) === null || _a === void 0 ? void 0 : _a.call(publishCommentEditOptions, e, undefined, { final: true });
539
592
  }
540
593
  });
541
594
  return useMemo(() => ({
@@ -574,48 +627,29 @@ export function usePublishCommentModeration(options) {
574
627
  }
575
628
  // the latest publishCommentModeration() call owns the result
576
629
  const publishRequestIdRef = useRef(0);
577
- // define onError if not defined
578
- const originalOnError = publishCommentModerationOptions.onError;
579
- const onError = (error) => __awaiter(this, void 0, void 0, function* () {
580
- setErrors((errors) => [...errors, error]);
581
- originalOnError === null || originalOnError === void 0 ? void 0 : originalOnError(error);
582
- });
583
- publishCommentModerationOptions.onError = onError;
584
- // define onChallenge if not defined
585
- const originalOnChallenge = publishCommentModerationOptions.onChallenge;
586
- const onChallenge = (challenge, commentModeration) => __awaiter(this, void 0, void 0, function* () {
587
- // cannot set a function directly with setState
588
- setPublishChallengeAnswers(() => commentModeration === null || commentModeration === void 0 ? void 0 : commentModeration.publishChallengeAnswers.bind(commentModeration));
589
- setChallenge(challenge);
590
- // an automatic republish's challenge replaces the previous attempt's failed verification
591
- setChallengeVerification(undefined);
592
- originalOnChallenge === null || originalOnChallenge === void 0 ? void 0 : originalOnChallenge(challenge, commentModeration);
593
- });
594
- publishCommentModerationOptions.onChallenge = onChallenge;
595
- // define onChallengeVerification if not defined
596
- const originalOnChallengeVerification = publishCommentModerationOptions.onChallengeVerification;
597
- const onChallengeVerification = (challengeVerification, commentModeration) => __awaiter(this, void 0, void 0, function* () {
598
- setChallengeVerification(challengeVerification);
599
- originalOnChallengeVerification === null || originalOnChallengeVerification === void 0 ? void 0 : originalOnChallengeVerification(challengeVerification, commentModeration);
600
- });
601
- publishCommentModerationOptions.onChallengeVerification = onChallengeVerification;
602
- // change state on publishing state change
603
- publishCommentModerationOptions.onPublishingStateChange = (publishingState) => {
604
- setPublishingState(publishingState);
605
- };
606
630
  const publishCommentModeration = () => __awaiter(this, void 0, void 0, function* () {
631
+ var _a;
607
632
  const requestId = ++publishRequestIdRef.current;
633
+ const isActiveRequest = () => publishRequestIdRef.current === requestId;
608
634
  clearPublicationResult(setChallenge, setChallengeVerification, setPublishChallengeAnswers, setPublishingState);
609
635
  setErrors([]);
636
+ const callOptions = getPublishEditCallOptions(publishCommentModerationOptions, isActiveRequest, {
637
+ setErrors,
638
+ setPublishingState,
639
+ setChallenge,
640
+ setChallengeVerification,
641
+ setPublishChallengeAnswers,
642
+ });
610
643
  try {
611
- yield accountsActions.publishCommentModeration(publishCommentModerationOptions, accountName);
644
+ yield accountsActions.publishCommentModeration(callOptions, accountName);
612
645
  }
613
646
  catch (e) {
614
- if (publishRequestIdRef.current === requestId) {
647
+ if (isActiveRequest()) {
615
648
  setPublishingState("failed");
616
649
  setErrors((errors) => [...errors, e]);
617
650
  }
618
- originalOnError === null || originalOnError === void 0 ? void 0 : originalOnError(e);
651
+ // the publish action rejected, which ends the publish, before or without a publication
652
+ (_a = publishCommentModerationOptions.onError) === null || _a === void 0 ? void 0 : _a.call(publishCommentModerationOptions, e, undefined, { final: true });
619
653
  }
620
654
  });
621
655
  return useMemo(() => ({
@@ -654,48 +688,29 @@ export function usePublishCommunityEdit(options) {
654
688
  }
655
689
  // the latest publishCommunityEdit() call owns the result
656
690
  const publishRequestIdRef = useRef(0);
657
- // define onError if not defined
658
- const originalOnError = publishCommunityEditOptions.onError;
659
- const onError = (error) => __awaiter(this, void 0, void 0, function* () {
660
- setErrors((errors) => [...errors, error]);
661
- originalOnError === null || originalOnError === void 0 ? void 0 : originalOnError(error);
662
- });
663
- publishCommunityEditOptions.onError = onError;
664
- // define onChallenge if not defined
665
- const originalOnChallenge = publishCommunityEditOptions.onChallenge;
666
- const onChallenge = (challenge, communityEdit) => __awaiter(this, void 0, void 0, function* () {
667
- // cannot set a function directly with setState
668
- setPublishChallengeAnswers(() => communityEdit === null || communityEdit === void 0 ? void 0 : communityEdit.publishChallengeAnswers.bind(communityEdit));
669
- setChallenge(challenge);
670
- // an automatic republish's challenge replaces the previous attempt's failed verification
671
- setChallengeVerification(undefined);
672
- originalOnChallenge === null || originalOnChallenge === void 0 ? void 0 : originalOnChallenge(challenge, communityEdit);
673
- });
674
- publishCommunityEditOptions.onChallenge = onChallenge;
675
- // define onChallengeVerification if not defined
676
- const originalOnChallengeVerification = publishCommunityEditOptions.onChallengeVerification;
677
- const onChallengeVerification = (challengeVerification, communityEdit) => __awaiter(this, void 0, void 0, function* () {
678
- setChallengeVerification(challengeVerification);
679
- originalOnChallengeVerification === null || originalOnChallengeVerification === void 0 ? void 0 : originalOnChallengeVerification(challengeVerification, communityEdit);
680
- });
681
- publishCommunityEditOptions.onChallengeVerification = onChallengeVerification;
682
- // change state on publishing state change
683
- publishCommunityEditOptions.onPublishingStateChange = (publishingState) => {
684
- setPublishingState(publishingState);
685
- };
686
691
  const publishCommunityEdit = () => __awaiter(this, void 0, void 0, function* () {
692
+ var _a;
687
693
  const requestId = ++publishRequestIdRef.current;
694
+ const isActiveRequest = () => publishRequestIdRef.current === requestId;
688
695
  clearPublicationResult(setChallenge, setChallengeVerification, setPublishChallengeAnswers, setPublishingState);
689
696
  setErrors([]);
697
+ const callOptions = getPublishEditCallOptions(publishCommunityEditOptions, isActiveRequest, {
698
+ setErrors,
699
+ setPublishingState,
700
+ setChallenge,
701
+ setChallengeVerification,
702
+ setPublishChallengeAnswers,
703
+ });
690
704
  try {
691
- yield accountsActions.publishCommunityEdit(communityAddress, publishCommunityEditOptions, accountName);
705
+ yield accountsActions.publishCommunityEdit(communityAddress, callOptions, accountName);
692
706
  }
693
707
  catch (e) {
694
- if (publishRequestIdRef.current === requestId) {
708
+ if (isActiveRequest()) {
695
709
  setPublishingState("failed");
696
710
  setErrors((errors) => [...errors, e]);
697
711
  }
698
- originalOnError === null || originalOnError === void 0 ? void 0 : originalOnError(e);
712
+ // the publish action rejected, which ends the publish, before or without a publication
713
+ (_a = publishCommunityEditOptions.onError) === null || _a === void 0 ? void 0 : _a.call(publishCommunityEditOptions, e, undefined, { final: true });
699
714
  }
700
715
  });
701
716
  return useMemo(() => ({