@internetarchive/ia-book-actions 1.1.0-alpha7 → 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/demo/index.html CHANGED
@@ -131,7 +131,7 @@
131
131
  iaBookActions.returnUrl = '';
132
132
  iaBookActions.localCache = localCache;
133
133
 
134
- let loanTotalTime2Min = { loanTotalTime: 80, loanRenewAtLast: 55, pageChangedInLast: 15 };
134
+ let loanTotalTime2Min = { loanTotalTime: 170, loanRenewAtLast: 80, pageChangedInLast: 15 };
135
135
  let loanTotalTime5Min = { loanTotalTime: 300, loanRenewAtLast: 180, pageChangedInLast: 15 };
136
136
  let loanTotalTime1Hour = { loanTotalTime: 3600, loanRenewAtLast: 660, pageChangedInLast: 900 };
137
137
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@internetarchive/ia-book-actions",
3
- "version": "1.1.0-alpha7",
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",
@@ -118,7 +118,7 @@ export const canBrowseAndBorrow = {
118
118
  loanStartDate: '2021-07-30 09:57:40',
119
119
  loansUrl: '/details/@neeraj-archive?tab=loans#loans-on-loan',
120
120
  maxLoans: 10,
121
- secondsLeftOnLoan: 155,
121
+ secondsLeftOnLoan: 170,
122
122
  shouldProtectImages: true,
123
123
  totalWaitlistCount: 0,
124
124
  userHasBorrowed: false,
@@ -47,7 +47,7 @@ export default class TimerCountdown extends LitElement {
47
47
  }
48
48
 
49
49
  render() {
50
- const viewClass = this.displayTime ? 'view' : 'hide';
50
+ const viewClass = this.displayTime ? 'view' : 'hide1';
51
51
  return html`
52
52
  <button
53
53
  id="timer-counter"
@@ -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 = 3;
92
+ this.timerExecutionSeconds = 30;
94
93
 
95
94
  /** @deprecated */
96
95
  this.toast = undefined;
@@ -283,6 +282,21 @@ export default class IABookActions extends LitElement {
283
282
  this.autoLoanRenewChecker(true);
284
283
  }
285
284
  });
285
+
286
+ /**
287
+ * auto-renew interval may stale when tab is in background,
288
+ * so [visibilitychange] event can trigger timer to show relavent messages
289
+ */
290
+ document.addEventListener("visibilitychange", async () => {
291
+ if (!document.hidden) {
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));
298
+ }
299
+ });
286
300
  }
287
301
 
288
302
  /**
@@ -487,36 +501,6 @@ export default class IABookActions extends LitElement {
487
501
  this.toast?.remove();
488
502
  }
489
503
 
490
- /**
491
- * @deprecated
492
- * Show toast messages on some specific loan renew features. e.g.
493
- * - show message when book is auto renewed
494
- * - show message when book is expired
495
- * - show message having remaining time when book is about to auto returned
496
- */
497
- // async showToastMessage() {
498
- // await this.useToastManager();
499
-
500
- // // if secondsLeft < 60, consider it 1 minute
501
- // let { secondsLeft } = this.loanRenewResult;
502
- // if (secondsLeft === undefined) {
503
- // secondsLeft = this.lendingStatus.secondsLeftOnLoan;
504
- // } else {
505
- // secondsLeft = secondsLeft > 60 ? secondsLeft : 60;
506
- // }
507
-
508
- // const config = new ToastConfig();
509
- // config.dismisOnClick = true;
510
- // config.texts = this.loanRenewHelper?.getMessageTexts(
511
- // this.loanRenewResult.texts,
512
- // secondsLeft
513
- // );
514
-
515
- // this.toast?.showToast({
516
- // config,
517
- // });
518
- // }
519
-
520
504
  /**
521
505
  * Execute when loan is expired
522
506
  */
@@ -677,44 +661,62 @@ export default class IABookActions extends LitElement {
677
661
  startTimerCountdown() {
678
662
  window?.IALendingIntervals?.clearTimerCountdown();
679
663
 
680
- let secondsLeft = Number(this.lendingStatus.secondsLeftOnLoan);
664
+ const secondsLeft = Number(this.lendingStatus.secondsLeftOnLoan);
681
665
  this.timeWhenTimerStart = new Date();
682
666
 
683
667
  window.IALendingIntervals.timerCountdown = setInterval(async () => {
684
- secondsLeft -= this.timerExecutionSeconds;
685
- secondsLeft = Math.round(secondsLeft); // round number
668
+ // interval execution block
669
+ await this.setIntervalExecutionBlock(secondsLeft);
670
+ }, this.timerExecutionSeconds * 1000);
671
+ }
686
672
 
687
- // re-sync timer if gone off because of background window
688
- // side effect: updates this.lendingStatus & kicks off lifecycle,
689
- // if really updated, escape from here
690
- const resync = await 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
691
685
 
692
- if (resync.resyncd) {
693
- secondsLeft = resync.whatShouldLeft;
694
- }
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);
695
690
 
696
- log('startTimerCountdown --- countdown still valid. continue...', {
697
- resync,
691
+ if (resyncd?.hasSynced) {
692
+ secondsLeft = resyncd.whatShouldLeft;
693
+
694
+ log('startTimerCountdown --- stale, timer has resyncd', {
698
695
  secondsLeft,
699
- loanRenewAtLast: this.loanRenewTimeConfig.loanRenewAtLast,
700
696
  });
697
+ }
701
698
 
702
- /**
703
- * execute from last 10th minute to 0th minute
704
- * - 10th - to check if user has viewed
705
- * - till 0th - to show warning msg with remaining time to auto expired
706
- * @see IABookActions::bindLoanRenewEvents
707
- */
708
- if (secondsLeft <= this.loanRenewTimeConfig.loanRenewAtLast) {
709
- await this.loanRenewAttempt(secondsLeft);
710
- }
699
+ log('startTimerCountdown --- countdown still valid. continue...', {
700
+ resyncd,
701
+ secondsLeft,
702
+ loanRenewAtLast: this.loanRenewTimeConfig.loanRenewAtLast,
703
+ }, new Date().getMinutes(), new Date().getSeconds());
711
704
 
712
- // clear interval in secondsLeft if less
713
- if (secondsLeft <= this.timerExecutionSeconds) {
714
- this.disconnectedCallback();
715
- window?.Sentry?.captureMessage(sentryLogs.clearOneHourTimer);
716
- }
717
- }, 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
+ }
718
720
  }
719
721
 
720
722
  /**
@@ -739,26 +741,28 @@ export default class IABookActions extends LitElement {
739
741
  const whatShouldLeft = Math.round(secondsShouldLeft);
740
742
  const timerElSeconds = this.timerCountdownEl.secondsLeftOnLoan || 0;
741
743
 
742
- if (whatIsleft !== whatShouldLeft) {
744
+ log(`reSyncTimerIfGoneOff?`, {
745
+ whatIsleft,
746
+ whatShouldLeft,
747
+ timerElSeconds,
748
+ timeLeftInMin: Math.ceil(timerSecondsLeft / 60),
749
+ });
750
+
751
+ if (timerElSeconds !== whatShouldLeft || whatIsleft !== whatShouldLeft) {
743
752
  // set lending status with new time to update decrementor
744
753
  const currStatus = {
745
754
  ...this.lendingStatus,
746
755
  secondsLeftOnLoan: whatShouldLeft,
747
756
  };
748
757
  this.lendingStatus = currStatus;
758
+ }
749
759
 
750
- log(`reSyncTimerIfGoneOff,\nlet's re-sync --- `, {
751
- whatIsleft,
752
- whatShouldLeft,
753
- timerElSeconds,
754
- timeLeftInMin: Math.ceil(timerSecondsLeft / 60),
755
- }, '\ntimer resyncd --- ', {
756
- whatShouldLeft,
757
- });
760
+ if (whatIsleft !== whatShouldLeft) {
761
+ log(`reSyncTimerIfGoneOff --- let's re-sync.`);
758
762
 
759
- return { resyncd: true, whatShouldLeft };
763
+ return { hasSynced: true, whatShouldLeft };
760
764
  }
761
- return { resyncd: false, whatShouldLeft };
765
+ return false;
762
766
  }
763
767
 
764
768
  /**