This code defines a Socket.IO server-side handler that receives search requests, performs searches across multiple sources, and sends the combined results back to the client.
npm run import -- "Sockify search notebook provider"
var client = require('socket.io-client')('https://localhost:8000', {
secure: true,
rejectUnauthorized: false
});
var importer = require('../Core');
var {
fuseSearch,
interpretObject,
} = importer.import()
var searchNotebooks = importer.import("search notebooks using gulp");
var fuseSearch, interpretObject;
function searchAll(queries, root = '') {
return (typeof queries === 'string'
? fuseSearch(queries)
: Promise.all(queries.map(fuseSearch)))
.then(r => interpretObject(r))
};
function searchHandler() {
return new Promise(resolve => {
console.log('Search handler');
client.on('resolve', (name, search) => {
console.log('searching...');
if (name === 'SearchService.prototype.search') {
client.emit('result', 'SearchService.prototype.search',
'Searching notebooks for ' + search);
// TODO: include gulp notebook search, .bash_sessions,
// selenium scripts, stack overflow, github,
Promise.all([
searchAll(search),
searchNotebooks(search)
])
.then(r => client.emit('result', 'SearchService.prototype.results', [].concat(...r)))
.catch(e => console.log(e))
}
});
client.emit('handle', 'SearchService', () => resolve());
client.on('error', e => console.log(e))
})
};
module.exports = searchHandler;
import * as io from'socket.io-client';
import { importModule } from '../Core';
import { fuseSearch, interpretObject } from './interprateAllNotebooks';
import { searchNotebooks } from './searchNotebooksUsingGulp';
interface Queries {
type: string;
value: string;
}
async function searchAll(queries: Queries[], root: string = ''): Promise {
try {
const results = await Promise.all(queries.map(query => {
if (typeof query.value ==='string') {
return fuseSearch(query.value);
} else {
return Promise.resolve(query.value);
}
}));
return interpretObject(results);
} catch (error) {
console.error(error);
}
}
const searchHandler = async () => {
try {
console.log('Search handler');
const client = io('https://localhost:8000', {
secure: true,
rejectUnauthorized: false,
});
client.on('resolve', (name, search: string) => {
console.log('searching...');
if (name === 'SearchService.prototype.search') {
client.emit('result', 'SearchService.prototype.search', `Searching notebooks for ${search}`);
Promise.all([
searchAll([{ type:'string', value: search }]),
searchNotebooks(search),
])
.then((results: string[]) => {
const combinedResults = results.flat();
client.emit('result', 'SearchService.prototype.results', combinedResults);
})
.catch((error: Error) => console.error(error));
}
});
client.on('error', (error: Error) => console.error(error));
client.emit('handle', 'SearchService', () => {
console.log('Search service initiated');
});
} catch (error) {
console.error(error);
}
};
export default searchHandler;
This code sets up a Socket.IO server-side handler for search requests.
Here's a breakdown:
Initialization:
https://localhost:8000
.fuseSearch
and interpretObject
for searching and interpreting search results, and searchNotebooks
for searching notebooks.searchAll()
Function:
fuseSearch
to search for matches.interpretObject
.searchHandler()
Function:
searchAll()
and searchNotebooks()
concurrently to search various sources.Export:
searchHandler
function, making it available for use in other parts of the application.Let me know if you have any other code snippets you'd like me to explain!