Skip to content

Commit

Permalink
Fix bookstore events
Browse files Browse the repository at this point in the history
Fix connection to event namespace
  • Loading branch information
bperel committed Feb 4, 2024
1 parent 126c1d2 commit 9f040bf
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 26 deletions.
2 changes: 2 additions & 0 deletions apps/web/src/components/Bookstores.vue
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ import { MapboxMap, MapboxMarker, MapboxPopup } from "vue-mapbox-ts";
import { SimpleBookstore } from "~dm-types/SimpleBookstore";
import { bookstoreServices } from "../composables/useSocket";
const { fetchStats } = users();
const { stats: userStats } = storeToRefs(users());
Expand Down
47 changes: 23 additions & 24 deletions apps/web/src/composables/useSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,29 +48,7 @@ const useSocket = <Services extends EventsMap>(
namespaceName: string,
cacheOptions?: Required<SocketCacheOptions>,
) => {
const socket = io(import.meta.env.VITE_SOCKET_URL + namespaceName, {
auth: async (cb) => {
if (!session.value) {
return;
}
const token = await session.value.getToken();
console.log(token);
cb({
token,
});
},
});

socket.on("connect_error", (e) => {
if (session.value?.onConnectError) {
session.value.onConnectError();
console.debug(`Namespace ${namespaceName}: connect_error: ${e}`);
} else {
console.error(
`Namespace ${namespaceName}: onConnectError is not defined`,
);
}
});
let socket: Socket;

return new Proxy({} as EventCalls<Services> & { connect: () => void }, {
get:
Expand All @@ -80,6 +58,27 @@ const useSocket = <Services extends EventsMap>(
): Promise<
EventReturnTypeIncludingError<Services[EventName]> | undefined
> => {
if (!socket) {
socket = io(import.meta.env.VITE_SOCKET_URL + namespaceName, {
auth: async (cb) => {
if (!session.value) {
return;
}
cb({
token: await session.value.getToken(),
});
},
}).on("connect_error", (e) => {
if (session.value?.onConnectError) {
session.value.onConnectError();
console.debug(`Namespace ${namespaceName}: connect_error: ${e}`);
} else {
console.error(
`Namespace ${namespaceName}: onConnectError is not defined`,
);
}
});
}
if (event === "connect") {
socket.connect();
return;
Expand Down Expand Up @@ -168,7 +167,7 @@ export const eventsServices = useSocket<EventsServices>(
EventsServices.namespaceEndpoint,
);
export const bookstoreServices = useSocket<BookstoreServices>(
BookcaseServices.namespaceEndpoint,
BookstoreServices.namespaceEndpoint,
);
export const collectionServices = useSocket<CollectionServices>(
CollectionServices.namespaceEndpoint,
Expand Down
3 changes: 2 additions & 1 deletion packages/api/services/coa/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import stories from "./stories";
import Services from "./types";

export default (io: Server) => {
(io.of(Services.namespaceEndpoint) as Namespace<Services>).on("connection", (socket) => { console.log("connected to coa");
(io.of(Services.namespaceEndpoint) as Namespace<Services>).on("connection", (socket) => {
console.log("connected to coa");

countries(socket);
publications(socket);
Expand Down
1 change: 0 additions & 1 deletion packages/api/services/global-stats/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export default (io: Server) => {
socket.on(
"getUsersPointsAndStats",
async (userIds: number[], callback) => {
console.log('!')
if (userIds.length) {
const result = {
points: await getMedalPoints(userIds),
Expand Down

0 comments on commit 9f040bf

Please sign in to comment.