Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
add: experimental persist data
Browse files Browse the repository at this point in the history
  • Loading branch information
saeidex committed Nov 22, 2024
1 parent 6f4fb59 commit b976cde
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 21 deletions.
20 changes: 6 additions & 14 deletions apps/next/src/app/dashboard/test/[busId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,20 @@ const BusLocationPage = (props: BusLocationPageProps) => {
};

export const Location = (props: { busId: string }) => {
const {
data: location,
isLoading,
isError,
error,
} = useBusLocationQuery(props.busId);
const { data: location, isError, error } = useBusLocationQuery(props.busId);

if (isLoading) return <p>Waiting for bus location...</p>;
if (isError)
return (
<p className="grid h-dvh place-content-center place-items-center gap-4 text-destructive">
An error has occurred: {error.message}
</p>
);

if (location) {
return (
<div className="max-w-[90dvw] overflow-x-scroll rounded-sm bg-destructive p-4 text-destructive-foreground">
<pre>{JSON.stringify(JSON.parse(location), null, 2)}</pre>
</div>
);
}
return (
<div className="max-w-[90dvw] overflow-x-scroll rounded-sm bg-destructive p-4 text-destructive-foreground">
<pre>{JSON.stringify(location)}</pre>
</div>
);
};

export default BusLocationPage;
4 changes: 3 additions & 1 deletion packages/mqtt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
"dependencies": {
"@tanstack/react-query": "^5.59.16",
"mqtt": "^5.10.1",
"react": "^18.3.1"
"react": "^18.3.1",
"superjson": "2.2.1",
"zustand": "^5.0.1"
}
}
1 change: 1 addition & 0 deletions packages/mqtt/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from "./client";
export * from "./provider";
export * from "./service";
export * from "./topics";
export type * from "./types";
11 changes: 9 additions & 2 deletions packages/mqtt/src/service.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
import { useQuery } from "@tanstack/react-query";
import SuperJSON from "superjson";

import type { BusData } from "./types";
import { mqttClient as client } from "./client";
import { useMqttStore } from "./store";
import { getBusLocationTopic } from "./topics";

export const useBusLocationQuery = (busId: string) => {
const topic = getBusLocationTopic(busId);
const data = useMqttStore.getState().data;
const setData = useMqttStore.getState().setData;

return useQuery({
queryKey: [topic],
queryFn: () => {
return new Promise<string>((resolve, reject) => {
return new Promise<BusData>((resolve, reject) => {
client.on("message", (receivedTopic, payload) => {
if (receivedTopic === topic) {
resolve(payload.toString());
setData(topic, SuperJSON.parse<BusData>(payload.toString()));
resolve(SuperJSON.parse<BusData>(payload.toString()));
}
});

Expand All @@ -27,6 +33,7 @@ export const useBusLocationQuery = (busId: string) => {
});
});
},
initialData: data,
refetchInterval: 1000 * 3,
staleTime: Infinity,
});
Expand Down
31 changes: 31 additions & 0 deletions packages/mqtt/src/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { persist } from "zustand/middleware";
import { createStore } from "zustand/vanilla";

import type { BusData } from "./types";

interface MqttStore {
data: BusData;
setData: (topic: string, data: BusData) => void;
}

export const useMqttStore = createStore<MqttStore>()(
persist(
(set) => ({
data: {
bus_id: "",
latitude: "",
longitude: "",
timestamp: "",
heading: "",
speed: "",
},
setData: (topic, data) =>
set((state) => ({
data: { ...state.data, [topic]: data },
})),
}),
{
name: "bus-data",
},
),
);
8 changes: 8 additions & 0 deletions packages/mqtt/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface BusData {
bus_id: string;
latitude: string;
longitude: string;
timestamp: string;
speed: string;
heading: string;
}
37 changes: 33 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b976cde

Please sign in to comment.