-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: consolidate search query modes into one event
- Loading branch information
1 parent
d455064
commit 8922c96
Showing
5 changed files
with
30 additions
and
19 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 |
---|---|---|
@@ -1,20 +1,31 @@ | ||
import { SearchQuery } from '@presenter/contract' | ||
|
||
import { firstLetterSearch, fullWordSearch } from '~/services/database' | ||
import { SocketServer } from '~/services/websocket-server' | ||
|
||
const searchHandlers = [ | ||
[ 'first-letter', firstLetterSearch ], | ||
[ 'full-word', fullWordSearch ], | ||
] as const | ||
const searchHandlers = { | ||
'first-letter': firstLetterSearch, | ||
'full-word': fullWordSearch, | ||
} satisfies Record< | ||
//! This typing is a little silly | ||
SearchQuery['type'], | ||
( params: Parameters<typeof firstLetterSearch>[0] ) => ReturnType<typeof firstLetterSearch> | ||
> | ||
|
||
type SearchModuleOptions = { | ||
socketServer: SocketServer, | ||
} | ||
|
||
const createSearchModule = ( { socketServer }: SearchModuleOptions ) => { | ||
searchHandlers.forEach( ( [ name, searchFn ] ) => socketServer.on( | ||
`search:${name}`, | ||
( { query, options }, { json } ) => void searchFn( query, options ).then( ( results ) => json( 'search:results', results ) ) | ||
) ) | ||
socketServer.on( | ||
'search:query', | ||
( { type, query, options }, { json } ) => { | ||
const searchFn = searchHandlers[ type ] | ||
if ( !searchFn ) return | ||
|
||
void searchFn( query, options ).then( ( results ) => json( 'search:results', results ) ) | ||
} | ||
) | ||
} | ||
|
||
export default createSearchModule |
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,4 +1,5 @@ | ||
export * from './about' | ||
export * from './data' | ||
export * from './search' | ||
export * from './settings' | ||
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
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