@internetarchive/ia-book-actions 0.1.3-alpha2 → 0.1.4-alpha2

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,11 +131,11 @@
131
131
  iaBookActions.returnUrl = 'https://openlibrary.org';
132
132
  iaBookActions.localCache = localCache;
133
133
 
134
- let loanTotalTime2Min = { loanTotalTime: 20, loanRenewAtLast: 15, pageChangedInLast: 15 };
134
+ let loanTotalTime2Min = { loanTotalTime: 20, loanRenewAtLast: 15, pageChangedInLast: 8 };
135
135
  let loanTotalTime5Min = { loanTotalTime: 300, loanRenewAtLast: 180, pageChangedInLast: 15 };
136
136
  let loanTotalTime1Hour = { loanTotalTime: 3600, loanRenewAtLast: 600, pageChangedInLast: 900 };
137
137
 
138
- iaBookActions.loanRenewTimeConfig = loanTotalTime1Hour;
138
+ iaBookActions.loanRenewTimeConfig = loanTotalTime2Min;
139
139
 
140
140
  let lendingStatus1 = iaBookActions.lendingStatus;
141
141
  let secondsLeftOnLoan = iaBookActions.loanRenewTimeConfig.loanTotalTime;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@internetarchive/ia-book-actions",
3
- "version": "0.1.3-alpha2",
3
+ "version": "0.1.4-alpha2",
4
4
  "description": "Bookreader actions for borrowable book",
5
5
  "author": "Internet Archive",
6
6
  "license": "AGPL-3.0-only",
@@ -36,7 +36,7 @@ export default css`
36
36
  align-items: center;
37
37
  transition: all 0.1s ease 0s;
38
38
  vertical-align: middle;
39
- padding: 0 1rem;
39
+ padding: 0 3rem;
40
40
  outline-color: ${whiteColor};
41
41
  outline-offset: -4px;
42
42
  user-select: none;
@@ -31,6 +31,12 @@ export default class TimerCountdown extends LitElement {
31
31
  */
32
32
  this.loanRenewAtLast = 0;
33
33
 
34
+ /**
35
+ * timer which is not reduced along with timer
36
+ * @type {date}
37
+ */
38
+ this.timeWhenTimerStart = '';
39
+
34
40
  /**
35
41
  * delay seconds in setInterval function
36
42
  * @type {number}
@@ -42,33 +48,45 @@ export default class TimerCountdown extends LitElement {
42
48
  window?.IALendingIntervals?.clearTimerCountdown();
43
49
  }
44
50
 
51
+ firstUpdated() {
52
+ this.timerCountdown();
53
+ }
54
+
45
55
  updated(changed) {
46
56
  if (changed.has('secondsLeftOnLoan') && this.secondsLeftOnLoan > 0) {
47
- this.disconnectedCallback();
48
-
49
57
  this.timerCountdown();
50
58
  }
51
59
  }
52
60
 
53
61
  timerCountdown() {
62
+ this.disconnectedCallback();
63
+
54
64
  // - let just execute setInterval in every 1 minute
55
- this.timerExecutionSeconds = 60;
65
+ this.timerExecutionSeconds = 1;
66
+ let secondsLeft = this.secondsLeftOnLoan;
67
+
68
+ // store current time
69
+ this.timeWhenTimerStart = new Date();
56
70
 
57
71
  /**
58
72
  * set interval in window object
59
73
  * @see ia-lending-intervals.js
60
74
  */
61
- window.IALendingIntervals.timerCountdown = setInterval(() => {
62
- this.secondsLeftOnLoan -= this.timerExecutionSeconds;
63
- const secondsLeft = Math.round(this.secondsLeftOnLoan);
75
+ window.IALendingIntervals.timerCountdown = setInterval(async () => {
76
+ secondsLeft -= this.timerExecutionSeconds;
77
+ secondsLeft = Math.round(secondsLeft); // round number
64
78
 
65
79
  console.log(
80
+ new Date().toLocaleTimeString(),
66
81
  'timeLeftInSec',
67
82
  secondsLeft,
68
83
  ' ||| timeLeftInMin',
69
84
  Math.ceil(secondsLeft / 60)
70
85
  );
71
86
 
87
+ // re-sync timer is gone off
88
+ await this.reSyncTimerIfGoneOff(secondsLeft);
89
+
72
90
  /**
73
91
  * execute from last 10th minute to 0th minute
74
92
  * - 10th - to check if user has viewed
@@ -76,26 +94,63 @@ export default class TimerCountdown extends LitElement {
76
94
  * @see IABookActions::bindLoanRenewEvents
77
95
  */
78
96
  if (secondsLeft <= this.loanRenewAtLast) {
79
- this.dispatchEvent(
80
- new CustomEvent('IABookActions:loanRenew', {
81
- detail: {
82
- hasPageChanged: false,
83
- secondsLeft,
84
- },
85
- bubbles: true,
86
- composed: true,
87
- })
88
- );
97
+ this.dispatchLoanRenewEvent(secondsLeft);
89
98
  }
90
99
 
91
100
  // clear interval
92
- if (secondsLeft <= 60) {
101
+ if (secondsLeft <= 1) {
93
102
  this.disconnectedCallback();
94
103
  window?.Sentry?.captureMessage(sentryLogs.clearOneHourTimer);
95
104
  }
96
105
  }, this.timerExecutionSeconds * 1000);
97
106
  }
98
107
 
108
+ /**
109
+ * dispatch loan renew attempt event
110
+ *
111
+ * @param {number} secondsLeft
112
+ * @memberof TimerCountdown
113
+ */
114
+ dispatchLoanRenewEvent(secondsLeft) {
115
+ this.dispatchEvent(
116
+ new CustomEvent('IABookActions:loanRenew', {
117
+ detail: {
118
+ hasPageChanged: false,
119
+ secondsLeft,
120
+ },
121
+ bubbles: true,
122
+ composed: true,
123
+ })
124
+ );
125
+ }
126
+
127
+ /**
128
+ * helper function to determine if timer is not in sync properly
129
+ *
130
+ * @param {number} timerSecondsLeft - actual seconds left get from setInterval
131
+ *
132
+ * @memberof TimerCountdown
133
+ */
134
+ async reSyncTimerIfGoneOff(timerSecondsLeft) {
135
+ const currentTime = new Date();
136
+
137
+ // current time - loan time
138
+ const diffInSeconds =
139
+ currentTime.getTime() / 1000 - this.timeWhenTimerStart.getTime() / 1000;
140
+
141
+ const secondsShouldLeft = this.secondsLeftOnLoan - diffInSeconds;
142
+
143
+ // convert in minutes
144
+ const whatIsleft = Math.round(timerSecondsLeft);
145
+ const whatShouldLeft = Math.round(secondsShouldLeft);
146
+
147
+ if (whatIsleft !== whatShouldLeft) {
148
+ console.log("timer is gone off, let's re-sync");
149
+ console.log('whatIsleft', whatIsleft, 'whatShouldLeft', whatShouldLeft);
150
+ this.secondsLeftOnLoan = secondsShouldLeft;
151
+ }
152
+ }
153
+
99
154
  /**
100
155
  * get remaining time with timeunit
101
156
  *
@@ -166,7 +166,7 @@ export default class ActionsConfig {
166
166
 
167
167
  return {
168
168
  id: 'purchaseBook',
169
- text: 'Purchase at',
169
+ text: 'Purchase at ',
170
170
  subText: 'Better World Books',
171
171
  title: 'Purchase',
172
172
  url: this.bwbPurchaseUrl,
@@ -271,10 +271,11 @@ export default class IABookActions extends LitElement {
271
271
  /**
272
272
  * detect click-event on document to close toast-template
273
273
  */
274
- document.addEventListener('click', e => {
275
- if (this.loanRenewHelper && this.loanRenewResult.secondsLeft > 0) {
276
- this.suppressToast = true;
277
- }
274
+ window.addEventListener('click', e => {
275
+ console.log('detech click event');
276
+ // if (this.loanRenewHelper && this.loanRenewResult.secondsLeft > 0) {
277
+ this.suppressToast = true;
278
+ // }
278
279
 
279
280
  // reload when user click on book-page and book is EXPIRED
280
281
  const hasExpired =