11import type { StorageMounts } from 'nitropack'
22import type { Storage , StorageValue } from 'unstorage'
33import type { NuxtDevtoolsServerContext , ServerFunctions } from '../types'
4+ import { watchStorageMount } from './storage-watch'
45
56const IGNORE_STORAGE_MOUNTS = [ 'root' , 'build' , 'src' , 'cache' ]
67function shouldIgnoreStorageKey ( key : string ) {
@@ -15,16 +16,26 @@ export function setupStorageRPC({
1516 const storageMounts : StorageMounts = { }
1617
1718 let storage : Storage | undefined
19+ let unwatchStorageMounts : Array < ( ) => Promise < void > | void > = [ ]
1820
1921 nuxt . hook ( 'nitro:init' , ( nitro ) => {
2022 storage = nitro . storage
2123
22- nuxt . hook ( 'ready' , ( ) => {
23- storage ! . watch ( ( event , key ) => {
24- if ( shouldIgnoreStorageKey ( key ) )
25- return
26- rpc . broadcast . callHook . asEvent ( 'storage:key:update' , key , event )
27- } )
24+ nuxt . hook ( 'close' , async ( ) => {
25+ await Promise . all ( unwatchStorageMounts . map ( unwatch => unwatch ( ) ) )
26+ unwatchStorageMounts = [ ]
27+ } )
28+
29+ nuxt . hook ( 'ready' , async ( ) => {
30+ if ( ! storage )
31+ return
32+ await Promise . all ( unwatchStorageMounts . map ( unwatch => unwatch ( ) ) )
33+ unwatchStorageMounts = await Promise . all ( Object . keys ( storageMounts ) . map ( mountName =>
34+ watchStorageMount ( storage , mountName , ( event , key ) => {
35+ if ( shouldIgnoreStorageKey ( key ) )
36+ return
37+ rpc . broadcast . callHook . asEvent ( 'storage:key:update' , key , event )
38+ } ) ) )
2839 } )
2940
3041 // Taken from https://github.com/unjs/nitro/blob/d83f2b65165d7ba996e7ef129ea99ff5b551dccc/src/storage.ts#L7-L10
0 commit comments