Skip to content

Commit

Permalink
πŸ”€ #2632 from LSS-Manager/perf/status-updates
Browse files Browse the repository at this point in the history
πŸ”€βš‘ [store/api] improve radioMessage performance
  • Loading branch information
jxn-30 authored Oct 8, 2023
2 parents a57d71e + 8c53837 commit 38b2ff1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 26 deletions.
32 changes: 12 additions & 20 deletions src/stores/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const defineAPIStore = defineStore('api', {
debounce: {
vehicles: {
timeout: null,
updates: [],
updates: new Map(),
},
},
initialBroadcastUpdateFinished: false,
Expand Down Expand Up @@ -346,32 +346,24 @@ export const defineAPIStore = defineStore('api', {
},
radioMessage(radioMessage: RadioMessage) {
if (radioMessage.type !== 'vehicle_fms') return;
this.debounce.vehicles.updates.push({
vehicleId: radioMessage.id,
this.debounce.vehicles.updates.set(radioMessage.id, {
caption: radioMessage.caption,
fms_show: radioMessage.fms,
fms_real: radioMessage.fms_real,
});
if (this.debounce.vehicles.timeout)
window.clearTimeout(this.debounce.vehicles.timeout);
this.debounce.vehicles.timeout = window.setTimeout(() => {
const vehicles = this.vehicles;
const updatedIds: number[] = [];
let vehicle = this.debounce.vehicles.updates.pop();
while (vehicle) {
const { vehicleId, caption, fms_show, fms_real } = vehicle;
if (!updatedIds.includes(vehicleId)) {
const index = vehicles.findIndex(
({ id }) => id === vehicleId
);
updatedIds.push(vehicleId);
if (index >= 0) {
vehicles[index].caption = caption;
vehicles[index].fms_show = fms_show;
vehicles[index].fms_real = fms_real;
}
}
vehicle = this.debounce.vehicles.updates.pop();
for (const vehicle of this.vehicles) {
const update = this.debounce.vehicles.updates.get(
vehicle.id
);
if (!update) continue;
vehicle.caption = update.caption;
vehicle.fms_show = update.fms_show;
vehicle.fms_real = update.fms_real;
this.debounce.vehicles.updates.delete(vehicle.id);
if (this.debounce.vehicles.updates.size === 0) break;
}
useBroadcastStore()
.apiBroadcast('vehicles', {
Expand Down
14 changes: 8 additions & 6 deletions typings/store/api/State.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ export interface APIState extends StorageAPIs {
debounce: {
vehicles: {
timeout: number | null;
updates: {
vehicleId: number;
caption: string;
fms_show: number;
fms_real: number;
}[];
updates: Map<
number, // represents the vehicleID
{
caption: string;
fms_show: number;
fms_real: number;
}
>;
};
};
initialBroadcastUpdateFinished: boolean;
Expand Down

0 comments on commit 38b2ff1

Please sign in to comment.