Skip to content

Commit

Permalink
refactor: consolidate search query modes into one event
Browse files Browse the repository at this point in the history
  • Loading branch information
Harjot1Singh committed Nov 5, 2024
1 parent d455064 commit 8922c96
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 19 deletions.
14 changes: 7 additions & 7 deletions apps/backend/src/features/search/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe( 'Search', () => {
it( 'should return no results given an unreasonable query', async () => {
const { client } = setup()

await client.sendEvent( 'search:first-letter', { query: 'pppppppppppppppp' } )
await client.sendEvent( 'search:query', { type: 'first-letter', query: 'pppppppppppppppp' } )

const results = await client.waitForEvent( 'search:results' )
expect( results ).toHaveLength( 0 )
Expand All @@ -28,7 +28,7 @@ describe( 'Search', () => {
it( 'should return first letter results', async () => {
const { client } = setup()

await client.sendEvent( 'search:first-letter', { query: 'hhhg' } )
await client.sendEvent( 'search:query', { type: 'first-letter', query: 'hhhg' } )

const results = await client.waitForEvent( 'search:results' )
expect( results.length ).toBeGreaterThan( 0 )
Expand All @@ -37,7 +37,7 @@ describe( 'Search', () => {
it( 'should return full word results', async () => {
const { client } = setup()

await client.sendEvent( 'search:full-word', { query: 'hir gun gwvY' } )
await client.sendEvent( 'search:query', { type: 'full-word', query: 'hir gun gwvY' } )

const results = await client.waitForEvent( 'search:results' )
expect( results.length ).toBeGreaterThan( 0 )
Expand All @@ -46,7 +46,7 @@ describe( 'Search', () => {
it( 'should not return extra fields determined by options', async () => {
const { client } = setup()

await client.sendEvent( 'search:first-letter', { query: 'hggmssp' } )
await client.sendEvent( 'search:query', { type: 'first-letter', query: 'hggmssp' } )

const results = await client.waitForEvent( 'search:results' )
expect( results[ 0 ].shabad ).toBeUndefined()
Expand All @@ -58,7 +58,7 @@ describe( 'Search', () => {
it( 'should return citations with results', async () => {
const { client } = setup()

await client.sendEvent( 'search:first-letter', { query: 'hggmssp', options: { citations: true } } )
await client.sendEvent( 'search:query', { type: 'first-letter', query: 'hggmssp', options: { citations: true } } )

const results = await client.waitForEvent( 'search:results' )
expect( results[ 0 ].shabad!.section ).toBeDefined()
Expand All @@ -67,7 +67,7 @@ describe( 'Search', () => {
it( 'should return translations with results', async () => {
const { client } = setup()

await client.sendEvent( 'search:first-letter', { query: 'hggmssp', options: { translations: true } } )
await client.sendEvent( 'search:query', { type: 'first-letter', query: 'hggmssp', options: { translations: true } } )

const results = await client.waitForEvent( 'search:results' )
expect( results[ 0 ].translations ).toBeDefined()
Expand All @@ -76,7 +76,7 @@ describe( 'Search', () => {
it( 'should return transliterations with results', async () => {
const { client } = setup()

await client.sendEvent( 'search:first-letter', { query: 'hggmssp', options: { transliterations: true } } )
await client.sendEvent( 'search:query', { type: 'first-letter', query: 'hggmssp', options: { transliterations: true } } )

const results = await client.waitForEvent( 'search:results' )
expect( results[ 0 ].transliterations ).toBeDefined()
Expand Down
27 changes: 19 additions & 8 deletions apps/backend/src/features/search/index.ts
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
1 change: 1 addition & 0 deletions packages/contract/src/index.ts
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'
1 change: 1 addition & 0 deletions packages/contract/src/search.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export type SearchQuery = {
type: 'first-letter' | 'full-word',
query: string,
options?: {
translations?: boolean,
Expand Down
6 changes: 2 additions & 4 deletions packages/contract/src/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ export const serverEvents = [
'content:tracker:autojump',
'history:clear',
'settings:all',
'search:first-letter',
'search:full-word',
'search:query',
'action:open-overlay-folder',
'action:open-logs-folder',
'action:open-external-url',
Expand All @@ -37,8 +36,7 @@ export type ServerEventParameters = DefineParameters<ServerEvent, {
'content:tracker:set-main-line': string,
'content:tracker:set-next-line': string,
'settings:all': Partial<Settings>,
'search:full-word': SearchQuery,
'search:first-letter': SearchQuery,
'search:query': SearchQuery,
'action:open-external-url': string,
}>

Expand Down

0 comments on commit 8922c96

Please sign in to comment.