-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat!: automatic reloading of application when config is updated (#86)
- Loading branch information
Showing
14 changed files
with
781 additions
and
968 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,4 +53,4 @@ | |
"lint-staged": { | ||
"*.ts": "yarn run lint" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import type { UseWebSocketReturn } from '@vueuse/core' | ||
import type { ReceivedMessage, ReceivedMessageMap, SendMessage, SendMessageMap } from '~/types' | ||
|
||
export type WsClientReturn = Pick<UseWebSocketReturn<string>, 'open' | 'close' | 'status'> & { | ||
on: SendHook | ||
off: SendHook | ||
send: ReceiveHook | ||
} | ||
export type SendHook = <E extends SendMessage['event']>(event: E, handler: (data: Omit<SendMessageMap[E], 'event'>) => any) => void | ||
export type ReceiveHook = <E extends ReceivedMessage['event']>(event: E, handler: (data: Omit<ReceivedMessageMap[E], 'event'>) => any) => void | ||
|
||
function createEventMessage<E extends SendMessage['event'] | ReceivedMessage['event']>(event: E, data: Record<string, any> = {}): string { | ||
return JSON.stringify({ event, ...data }) | ||
} | ||
|
||
export function wsClient(): WsClientReturn { | ||
const url = useRequestURL() | ||
const isSecure = url.protocol === 'https:' | ||
const endpoint = `${(isSecure ? 'wss://' : 'ws://') + url.host}/api/websocket` | ||
const sendHandlers = new Map<SendMessage['event'], Set<Function>>() | ||
const { status, open, close, send: _send } = useWebSocket(endpoint, { | ||
heartbeat: { | ||
message: createEventMessage('ping'), | ||
interval: 30 * 1000, | ||
}, | ||
autoReconnect: { | ||
delay: 5 * 1000, | ||
}, | ||
onMessage(_, e) { | ||
const { event, ...data } = JSON.parse(e.data) as SendMessage | ||
|
||
sendHandlers.get(event)?.forEach((handler) => handler(data)) | ||
}, | ||
}) | ||
|
||
const off: SendHook = (event, handler) => { | ||
if (sendHandlers.has(event)) { | ||
sendHandlers.get(event)?.delete(handler) | ||
} | ||
} | ||
|
||
const on: SendHook = (event, handler) => { | ||
if (!sendHandlers.has(event)) { | ||
sendHandlers.set(event, new Set()) | ||
} | ||
|
||
sendHandlers.get(event)!.add(handler) | ||
tryOnScopeDispose(() => off(event, handler)) | ||
} | ||
|
||
const send: ReceiveHook = (event, data) => { | ||
_send(createEventMessage(event, data)) | ||
} | ||
|
||
return { | ||
status, | ||
open, | ||
close, | ||
send, | ||
on, | ||
off, | ||
} | ||
} | ||
|
||
export const useWebsocket = createSharedComposable(wsClient) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const logger = useLogger('websocket') | ||
|
||
export default defineWebSocketHandler({ | ||
async open(peer) { | ||
logger.info('New peer', peer) | ||
|
||
const storage = useStorage('data') | ||
await storage.watch(async (_, key) => { | ||
if (key !== `data:${configFileName}`) { | ||
return | ||
} | ||
|
||
await runTask('config:update') | ||
peer.send({ event: 'config:update' }) | ||
}) | ||
}, | ||
async message(peer, message) { | ||
const { event } = JSON.parse(message as unknown as string) | ||
|
||
if (event === 'ping') { | ||
peer.send({ event: 'pong' }) | ||
} | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/** | ||
* @todo remove plugin when hooks appear in nitro task | ||
*/ | ||
export default defineNitroPlugin(async () => { | ||
runTask('config:update') | ||
}) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export default defineTask({ | ||
meta: { | ||
name: 'config:update', | ||
}, | ||
async run() { | ||
const config = await loadConfig() | ||
await setConfig(config) | ||
|
||
return { | ||
result: {}, | ||
} | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './config' | ||
export * from './services' | ||
export * from './websocket' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
export interface PingEvent { | ||
event: 'ping' | ||
} | ||
|
||
export interface PongEvent { | ||
event: 'pong' | ||
} | ||
|
||
export interface ConfigUpdateEvent { | ||
event: 'config:update' | ||
} | ||
|
||
export type ReceivedMessage = PingEvent | ||
|
||
export interface ReceivedMessageMap { | ||
ping: PingEvent | ||
} | ||
|
||
export type SendMessage = PongEvent | ConfigUpdateEvent | ||
|
||
export interface SendMessageMap { | ||
'pong': PongEvent | ||
'config:update': ConfigUpdateEvent | ||
} |
Oops, something went wrong.