1
0
Fork 0
mirror of https://github.com/immich-app/immich.git synced 2025-01-10 13:56:47 +01:00
immich/web/src/lib/stores/websocket.ts
martin 502495883d
fix(web): log out (#5706)
* fix: logging out

* fix: websocket

---------

Co-authored-by: Alex Tran <alex.tran1502@gmail.com>
2023-12-15 02:29:05 +00:00

61 lines
2 KiB
TypeScript

import type { AssetResponseDto, ServerVersionResponseDto } from '@api';
import { Socket, io } from 'socket.io-client';
import { writable } from 'svelte/store';
import { loadConfig } from './server-config.store';
export interface ReleaseEvent {
isAvailable: boolean;
checkedAt: Date;
serverVersion: ServerVersionResponseDto;
releaseVersion: ServerVersionResponseDto;
}
export const websocketStore = {
onUploadSuccess: writable<AssetResponseDto>(),
onAssetDelete: writable<string>(),
onAssetTrash: writable<string[]>(),
onAssetUpdate: writable<AssetResponseDto>(),
onPersonThumbnail: writable<string>(),
serverVersion: writable<ServerVersionResponseDto>(),
connected: writable<boolean>(false),
onRelease: writable<ReleaseEvent>(),
};
let websocket: Socket | null = null;
export const openWebsocketConnection = () => {
try {
if (websocket) {
return;
}
websocket = io('', {
path: '/api/socket.io',
reconnection: true,
forceNew: true,
autoConnect: true,
});
websocket
.on('connect', () => websocketStore.connected.set(true))
.on('disconnect', () => websocketStore.connected.set(false))
// .on('on_upload_success', (data) => websocketStore.onUploadSuccess.set(data))
.on('on_asset_delete', (data) => websocketStore.onAssetDelete.set(data))
.on('on_asset_trash', (data) => websocketStore.onAssetTrash.set(data))
.on('on_asset_update', (data) => websocketStore.onAssetUpdate.set(data))
.on('on_person_thumbnail', (data) => websocketStore.onPersonThumbnail.set(data))
.on('on_server_version', (data) => websocketStore.serverVersion.set(data))
.on('on_config_update', () => loadConfig())
.on('on_new_release', (data) => websocketStore.onRelease.set(data))
.on('error', (e) => console.log('Websocket Error', e));
} catch (e) {
console.log('Cannot connect to websocket ', e);
}
};
export const closeWebsocketConnection = () => {
if (websocket) {
websocket.close();
}
websocket = null;
};