Skip to content

Commit 13c5074

Browse files
committed
feat: implement request deduplication mechanism in createRequestDeduplicator
1 parent a530e85 commit 13c5074

4 files changed

Lines changed: 235 additions & 207 deletions

File tree

App.tsx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,17 @@ import {bms, BMSProvider, useBMS} from './src/BMSContext';
55
import nodejs from 'nodejs-mobile-react-native';
66
import WebView from 'react-native-webview';
77
import {NetworkInfo} from 'react-native-network-info';
8+
import {createRequestDeduplicator} from './src/createRequestDeduplicator';
89

910
nodejs.start('main.js');
1011

12+
const {request} = createRequestDeduplicator(async () => {
13+
// console.log('request', new Date().toUTCString());
14+
const info = await bms.fetchBatteryInfo();
15+
const cellVolts = await bms.fetchCellVoltages();
16+
return {info, cellVolts};
17+
});
18+
1119
const sendResponse = (id: number, json: any, code = 200) => {
1220
nodejs.channel.send(
1321
JSON.stringify({
@@ -25,8 +33,7 @@ nodejs.channel.addListener('message', async msg => {
2533
sendResponse(json.id, {code: 'DEVICE_NOT_SELECTED'}, 400);
2634
return;
2735
}
28-
const info = await bms.fetchBatteryInfo();
29-
const cellVolts = await bms.fetchCellVoltages();
36+
const {info, cellVolts} = await request();
3037
sendResponse(json.id, {...info, cellVolts});
3138
}
3239
});
@@ -62,7 +69,7 @@ const PageHandler = () => {
6269
Hosting on {localIp}:12345
6370
</Text>
6471
<WebView
65-
clear
72+
clear
6673
bounces={false}
6774
style={{backgroundColor: 'black', width: '100%', height: '100%'}}
6875
overScrollMode="never"

nodejs-assets/nodejs-project/index.html

Lines changed: 48 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -354,38 +354,41 @@
354354
// only plot after 60 seconds
355355
if (Date.now() - this.lastPlottedAt < 60 * 1000) return;
356356

357-
// check how long it has been since the last plot
358-
const lastPlotDuration = Date.now() - this.lastPlottedAt;
359-
// add nulls to array if it has been more than 60 seconds since the last plot. plot every 60 seconds
360-
if (this.lastPlottedAt !== -1 && lastPlotDuration > 61 * 1000) {
361-
for (let i = 0; i < lastPlotDuration / 70 / 1000 - 1; i++) {
362-
const date = new Date(this.lastPlottedAt + 60 * 1000 * (i + 1));
363-
this.labels.push(
364-
Intl.DateTimeFormat('en', {
365-
timeStyle: 'short',
366-
hour12: false,
367-
}).format(date),
368-
);
369-
this.data.capacity.push(null);
370-
this.data.current.push(null);
371-
372-
this.data.voltage.push(null);
373-
this.data.cellDiff.push(null);
374-
375-
this.data.cells.forEach(cell => {
376-
cell.push(null);
377-
});
378-
}
379-
}
357+
// // check how long it has been since the last plot
358+
// const lastPlotDuration = Date.now() - this.lastPlottedAt;
359+
// // add nulls to array if it has been more than 60 seconds since the last plot. plot every 60 seconds
360+
// if (this.lastPlottedAt !== -1 && lastPlotDuration > 61 * 1000) {
361+
// for (let i = 0; i < lastPlotDuration / 70 / 1000 - 1; i++) {
362+
// const date = new Date(this.lastPlottedAt + 60 * 1000 * (i + 1));
363+
// this.labels.push(
364+
// Intl.DateTimeFormat('en', {
365+
// timeStyle: 'short',
366+
// hour12: false,
367+
// }).format(date),
368+
// );
369+
// this.data.capacity.push(null);
370+
// this.data.current.push(null);
371+
372+
// this.data.voltage.push(null);
373+
// this.data.cellDiff.push(null);
374+
375+
// this.data.cells.forEach(cell => {
376+
// cell.push(null);
377+
// });
378+
// }
379+
// }
380380

381381
this.lastPlottedAt = Date.now();
382382
}
383383

384384
const date = new Date();
385385
this.labels.push(
386-
Intl.DateTimeFormat('en', {timeStyle: 'short', hour12: false}).format(
387-
date,
388-
),
386+
date.getDate() +
387+
' ' +
388+
Intl.DateTimeFormat('en', {
389+
timeStyle: 'short',
390+
hour12: false,
391+
}).format(date),
389392
);
390393

391394
this.data.capacity.push(value.capacity);
@@ -431,19 +434,21 @@
431434
}
432435

433436
const plotter = new Plotter();
434-
const [initialValues, setInitialValues] = cachedValue("initialValues", {});
435-
const [cellStartValues, setCellStartValues] = cachedValue('cellStartValues', []);
437+
const [initialValues, setInitialValues] = cachedValue('initialValues', {});
438+
const [cellStartValues, setCellStartValues] = cachedValue(
439+
'cellStartValues',
440+
[],
441+
);
436442

437443
const resetGraphBtn = document.getElementById('resetGraph');
438444
resetGraphBtn.addEventListener('click', () => {
439445
plotter.resetValues();
440-
})
446+
});
441447
const resetOffsetBtn = document.getElementById('resetOffset');
442448
resetOffsetBtn.addEventListener('click', () => {
443449
setCellStartValues([]);
444450
setInitialValues({});
445-
})
446-
451+
});
447452

448453
function getIndicesOfLargestNumbers(arr) {
449454
if (!arr || arr.length === 0) {
@@ -544,9 +549,6 @@
544549
}
545550
}
546551

547-
548-
549-
550552
const createOrUpdateSection = (id, title, description, value, rawValue) => {
551553
let initVal = initialValues()[id];
552554
if (initVal === undefined) {
@@ -602,7 +604,6 @@
602604
sections.innerHTML += element;
603605
};
604606

605-
606607
const estimateBalanceCells = [];
607608
function createCellsSection(balanceStatus, cellVolts) {
608609
if (!balanceStatus || !cellVolts) return;
@@ -772,7 +773,6 @@
772773
sections.innerHTML += element;
773774
}
774775

775-
776776
function convertSecondsToHHMM(totalSeconds) {
777777
const hours = Math.floor(totalSeconds / 3600);
778778
const minutes = Math.floor((totalSeconds % 3600) / 60);
@@ -781,8 +781,6 @@
781781
return `${hours}h ${minutes}m`;
782782
}
783783

784-
785-
786784
monitorStarted = false;
787785
function fetchSchedule() {
788786
fetchBatteryInfo().then(info => {
@@ -792,7 +790,7 @@
792790
}, 1500);
793791

794792
const header = document.getElementById('header');
795-
header.style.color = "red"
793+
header.style.color = 'red';
796794
if (info?.code) {
797795
header.innerText = info.code;
798796
} else {
@@ -841,24 +839,25 @@
841839

842840
if (charging) {
843841
secondsRemaining =
844-
((nominalCapacityAh - remainingCapacityAh) / Math.abs(current)) * 3600;
842+
((nominalCapacityAh - remainingCapacityAh) / Math.abs(current)) *
843+
3600;
845844
} else if (discharging) {
846845
secondsRemaining = (remainingCapacityAh / Math.abs(current)) * 3600;
847846
}
848847
const HHMMRemaining = convertSecondsToHHMM(secondsRemaining);
849848

850849
let estTitle = '';
851850
if (charging || discharging) {
852-
estTitle = `Est ${
853-
charging ? 'Charging' : 'Discharging'
854-
}: ${HHMMRemaining}`;
851+
estTitle = `${
852+
charging ? 'Charged in' : 'Discharging in'
853+
} ${HHMMRemaining}`;
855854
}
856855
const estTime = document.getElementById('estimateTime');
857856
estTime.innerText = !charging && !discharging ? '' : estTitle;
858857

859858
const header = document.getElementById('header');
860859
header.innerText = `${info.name}`;
861-
header.style.color = "white"
860+
header.style.color = 'white';
862861

863862
const batteryPercentText = `${info.remainingPercentSoc}%`;
864863
const batteryPercent = document.getElementById('batteryPercent');
@@ -902,7 +901,7 @@
902901

903902
setTimeout(() => {
904903
fetchSchedule();
905-
}, 1500);
904+
}, 1000);
906905
});
907906
}
908907
fetchSchedule();
@@ -1078,9 +1077,11 @@
10781077
});
10791078

10801079
function cachedValue(key, initialValue) {
1081-
let value = JSON.parse(localStorage.getItem(key) || JSON.stringify(initialValue));
1080+
let value = JSON.parse(
1081+
localStorage.getItem(key) || JSON.stringify(initialValue),
1082+
);
10821083

1083-
const set = (val) => {
1084+
const set = val => {
10841085
value = val;
10851086
localStorage.setItem(key, JSON.stringify(val));
10861087
};

0 commit comments

Comments
 (0)