@internetarchive/ia-book-actions 0.1.4-alpha1 → 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,7 +131,7 @@
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
 
@@ -150,23 +150,6 @@
150
150
  });
151
151
  });
152
152
 
153
- window.addEventListener('BookReader:userAction', () => {
154
- if (this.borrowType === 'browsed') {
155
- this.suppressToast = false;
156
- this.autoLoanRenewChecker(true);
157
- }
158
-
159
- return nothing;
160
- });
161
-
162
- document.querySelector('.brControlClick').addEventListener('click', () => {
163
- window.dispatchEvent(new CustomEvent('BookReader:userAction', {
164
- bubbles: true,
165
- composed: true,
166
- detail: { props },
167
- }));
168
- });
169
-
170
153
  document.querySelectorAll('.errorEnable input[type=checkbox]').forEach((item) => {
171
154
  if (window.location.href.indexOf('?error=true') !== -1) {
172
155
  item.checked = true;
@@ -328,9 +311,6 @@
328
311
  <td>
329
312
  <button class="pageChangedEvent">Page Change Event</button>
330
313
  </td>
331
- <td>
332
- <button class="brControlClick">Br Controls</button>
333
- </td>
334
314
  </tr>
335
315
  </table>
336
316
  </fieldset>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@internetarchive/ia-book-actions",
3
- "version": "0.1.4-alpha1",
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",
@@ -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,25 +48,33 @@ 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
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(
66
80
  new Date().toLocaleTimeString(),
@@ -70,6 +84,9 @@ export default class TimerCountdown extends LitElement {
70
84
  Math.ceil(secondsLeft / 60)
71
85
  );
72
86
 
87
+ // re-sync timer is gone off
88
+ await this.reSyncTimerIfGoneOff(secondsLeft);
89
+
73
90
  /**
74
91
  * execute from last 10th minute to 0th minute
75
92
  * - 10th - to check if user has viewed
@@ -77,26 +94,63 @@ export default class TimerCountdown extends LitElement {
77
94
  * @see IABookActions::bindLoanRenewEvents
78
95
  */
79
96
  if (secondsLeft <= this.loanRenewAtLast) {
80
- this.dispatchEvent(
81
- new CustomEvent('IABookActions:loanRenew', {
82
- detail: {
83
- hasPageChanged: false,
84
- secondsLeft,
85
- },
86
- bubbles: true,
87
- composed: true,
88
- })
89
- );
97
+ this.dispatchLoanRenewEvent(secondsLeft);
90
98
  }
91
99
 
92
100
  // clear interval
93
- if (secondsLeft <= 60) {
101
+ if (secondsLeft <= 1) {
94
102
  this.disconnectedCallback();
95
103
  window?.Sentry?.captureMessage(sentryLogs.clearOneHourTimer);
96
104
  }
97
105
  }, this.timerExecutionSeconds * 1000);
98
106
  }
99
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
+
100
154
  /**
101
155
  * get remaining time with timeunit
102
156
  *
@@ -272,9 +272,10 @@ export default class IABookActions extends LitElement {
272
272
  * detect click-event on document to close toast-template
273
273
  */
274
274
  window.addEventListener('click', e => {
275
- if (this.loanRenewHelper && this.loanRenewResult.secondsLeft > 0) {
276
- this.suppressToast = true;
277
- }
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 =