@internetarchive/ia-book-actions 1.1.0-alpha8 → 1.1.0-alpha9

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@internetarchive/ia-book-actions",
3
- "version": "1.1.0-alpha8",
3
+ "version": "1.1.0-alpha9",
4
4
  "description": "Bookreader actions for borrowable book",
5
5
  "author": "Internet Archive",
6
6
  "license": "AGPL-3.0-only",
@@ -4,7 +4,6 @@ import { html, css, LitElement, nothing } from 'lit';
4
4
 
5
5
  import { SharedResizeObserver } from '@internetarchive/shared-resize-observer';
6
6
  import { ModalConfig } from '@internetarchive/modal-manager';
7
- // import { ToastConfig } from '@internetarchive/toast-manager';
8
7
  import { LocalCache } from '@internetarchive/local-cache';
9
8
 
10
9
  import './components/collapsible-action-group.js';
@@ -90,7 +89,7 @@ export default class IABookActions extends LitElement {
90
89
  this.lendingOptions = {};
91
90
  this.borrowType = null; // 'browsed'|'borrowed'
92
91
  this.browseTimer = undefined; // timeout
93
- this.timerExecutionSeconds = 5;
92
+ this.timerExecutionSeconds = 30;
94
93
 
95
94
  /** @deprecated */
96
95
  this.toast = undefined;
@@ -286,12 +285,16 @@ export default class IABookActions extends LitElement {
286
285
 
287
286
  /**
288
287
  * auto-renew interval may stale when tab is in background,
289
- * so [visibilitychange] event can trigger to check timer to show relavent messages
288
+ * so [visibilitychange] event can trigger timer to show relavent messages
290
289
  */
291
- document.addEventListener("visibilitychange", () => {
292
- log('visibilitychange');
290
+ document.addEventListener("visibilitychange", async () => {
293
291
  if (!document.hidden) {
294
- this.startTimerCountdown();
292
+ log('visibilitychange', new Date().getMinutes, new Date().getSeconds());
293
+
294
+ const loanTime = await this.localCache.get(`${this.identifier}-loanTime`);
295
+ const secondsLeft = Math.round((loanTime - new Date()) / 1000); // different in seconds
296
+
297
+ this.setIntervalExecutionBlock(Number(secondsLeft));
295
298
  }
296
299
  });
297
300
  }
@@ -498,36 +501,6 @@ export default class IABookActions extends LitElement {
498
501
  this.toast?.remove();
499
502
  }
500
503
 
501
- /**
502
- * @deprecated
503
- * Show toast messages on some specific loan renew features. e.g.
504
- * - show message when book is auto renewed
505
- * - show message when book is expired
506
- * - show message having remaining time when book is about to auto returned
507
- */
508
- // async showToastMessage() {
509
- // await this.useToastManager();
510
-
511
- // // if secondsLeft < 60, consider it 1 minute
512
- // let { secondsLeft } = this.loanRenewResult;
513
- // if (secondsLeft === undefined) {
514
- // secondsLeft = this.lendingStatus.secondsLeftOnLoan;
515
- // } else {
516
- // secondsLeft = secondsLeft > 60 ? secondsLeft : 60;
517
- // }
518
-
519
- // const config = new ToastConfig();
520
- // config.dismisOnClick = true;
521
- // config.texts = this.loanRenewHelper?.getMessageTexts(
522
- // this.loanRenewResult.texts,
523
- // secondsLeft
524
- // );
525
-
526
- // this.toast?.showToast({
527
- // config,
528
- // });
529
- // }
530
-
531
504
  /**
532
505
  * Execute when loan is expired
533
506
  */
@@ -688,48 +661,62 @@ export default class IABookActions extends LitElement {
688
661
  startTimerCountdown() {
689
662
  window?.IALendingIntervals?.clearTimerCountdown();
690
663
 
691
- let secondsLeft = Number(this.lendingStatus.secondsLeftOnLoan);
664
+ const secondsLeft = Number(this.lendingStatus.secondsLeftOnLoan);
692
665
  this.timeWhenTimerStart = new Date();
693
666
 
694
667
  window.IALendingIntervals.timerCountdown = setInterval(async () => {
695
- secondsLeft -= this.timerExecutionSeconds;
696
- secondsLeft = Math.round(secondsLeft); // round number
668
+ // interval execution block
669
+ await this.setIntervalExecutionBlock(secondsLeft);
670
+ }, this.timerExecutionSeconds * 1000);
671
+ }
697
672
 
698
- // re-sync timer if gone off because of background window
699
- // side effect: updates this.lendingStatus & kicks off lifecycle,
700
- // if really updated, escape from here
701
- const resyncd = this.reSyncTimerIfGoneOff(secondsLeft);
673
+ /**
674
+ * setInterval execution function do following things
675
+ * - if setInterval timer gone off, let's resync
676
+ * - loan-renew attempt to renew the loan OR show relavant message
677
+ * - if loan has expired, clear interval timers
678
+ *
679
+ * @param {Number} secondsLeftOnLoan
680
+ */
681
+ async setIntervalExecutionBlock(secondsLeftOnLoan) {
682
+ let secondsLeft = secondsLeftOnLoan;
683
+ secondsLeft -= this.timerExecutionSeconds;
684
+ secondsLeft = Math.round(secondsLeft); // round number
702
685
 
703
- if (resyncd.hasSynced) {
704
- secondsLeft = resyncd.whatShouldLeft;
686
+ // re-sync timer if gone off because of background window
687
+ // side effect: updates this.lendingStatus & kicks off lifecycle,
688
+ // if really updated, escape from here
689
+ const resyncd = await this.reSyncTimerIfGoneOff(secondsLeft);
705
690
 
706
- log('startTimerCountdown --- stale, timer has resyncd', {
707
- secondsLeft,
708
- });
709
- }
691
+ if (resyncd?.hasSynced) {
692
+ secondsLeft = resyncd.whatShouldLeft;
710
693
 
711
- log('startTimerCountdown --- countdown still valid. continue...', {
712
- resyncd,
694
+ log('startTimerCountdown --- stale, timer has resyncd', {
713
695
  secondsLeft,
714
- loanRenewAtLast: this.loanRenewTimeConfig.loanRenewAtLast,
715
- }, new Date().getMinutes(), new Date().getSeconds());
696
+ });
697
+ }
716
698
 
717
- /**
718
- * execute from last 10th minute to 0th minute
719
- * - 10th - to check if user has viewed
720
- * - till 0th - to show warning msg with remaining time to auto expired
721
- * @see IABookActions::bindLoanRenewEvents
722
- */
723
- if (secondsLeft <= this.loanRenewTimeConfig.loanRenewAtLast) {
724
- await this.loanRenewAttempt(secondsLeft);
725
- }
699
+ log('startTimerCountdown --- countdown still valid. continue...', {
700
+ resyncd,
701
+ secondsLeft,
702
+ loanRenewAtLast: this.loanRenewTimeConfig.loanRenewAtLast,
703
+ }, new Date().getMinutes(), new Date().getSeconds());
726
704
 
727
- // clear interval in secondsLeft if less
728
- if (secondsLeft <= this.timerExecutionSeconds) {
729
- this.disconnectedCallback();
730
- window?.Sentry?.captureMessage(sentryLogs.clearOneHourTimer);
731
- }
732
- }, this.timerExecutionSeconds * 1000);
705
+ /**
706
+ * execute from last 10th minute to 0th minute
707
+ * - 10th - to check if user has viewed
708
+ * - till 0th - to show warning msg with remaining time to auto expired
709
+ * @see IABookActions::bindLoanRenewEvents
710
+ */
711
+ if (secondsLeft <= this.loanRenewTimeConfig.loanRenewAtLast) {
712
+ await this.loanRenewAttempt(secondsLeft);
713
+ }
714
+
715
+ // clear interval in secondsLeft if less
716
+ if (secondsLeft <= this.timerExecutionSeconds) {
717
+ this.disconnectedCallback();
718
+ window?.Sentry?.captureMessage(sentryLogs.clearOneHourTimer);
719
+ }
733
720
  }
734
721
 
735
722
  /**
@@ -775,7 +762,7 @@ export default class IABookActions extends LitElement {
775
762
 
776
763
  return { hasSynced: true, whatShouldLeft };
777
764
  }
778
- return { hasSynced: false, whatShouldLeft };
765
+ return false;
779
766
  }
780
767
 
781
768
  /**