Angular components | Display code results | Cell 4 | Search

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.

Run example

npm run import -- "Sockify search notebook provider"

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;

What the code could have been:

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:

  1. Initialization:

  2. searchAll() Function:

  3. searchHandler() Function:

  4. Export:

Let me know if you have any other code snippets you'd like me to explain!