Skip to content

Instantly share code, notes, and snippets.

View DanishSiddiq's full-sized avatar
🎯
Focusing

DanishSiddiq

🎯
Focusing
View GitHub Profile
@DanishSiddiq
DanishSiddiq / bouncerPattern.js
Last active March 5, 2021 04:51
Bouncer pattern explaining early return
function calculateSomeFormula(param1, param2) {
validateParams(param1, param2);
return (param1 * 2) * (param2 * 3);
}
// although return value is not used in our example but this method can also return value which can be used by the caller
function validateParams(param1, param2) {
if (!param1) {
@DanishSiddiq
DanishSiddiq / earlyReturn.js
Last active March 5, 2021 04:49
Early Return Sample
function calculateSomeFormula(param1, param2) {
if (!param1) {
throw new Error('Param1 is not defined');
}
if (isNaN(param1)) {
throw new Error('Param1 is not a number');
}
if (!param2) {
@DanishSiddiq
DanishSiddiq / complicatedCode.js
Last active March 5, 2021 04:44
Complicated Code Structure
function calculateSomeFormula(param1, param2) {
if (param1) {
if (!isNaN(param1)) {
if (param2) {
if (!isNaN(param2)) {
return (param1 * 2) * (param2 * 3);
} else {
throw new Error('Param2 is not a number');
}
} else {
@DanishSiddiq
DanishSiddiq / student.graphql.variables.json
Last active May 10, 2020 18:40
GraphQL Student Query Variables
{
"studentId": "5e727fa7b9f23a1c643e671f",
"firstName": "Danish",
"lastName": "Siddiq",
"registrationNumber": 543678,
"email": "[email protected]"
}
@DanishSiddiq
DanishSiddiq / student.graphql.query.json
Last active May 10, 2020 12:32
GraphQL Student Client Queries
query getStudent($studentId: String, $firstName: String) {
byId: findOne(input: {_id: $studentId}) {
...studentFields
}
byName: findOne(input: {firstName: $firstName}) {
...studentFields
}
}
mutation createStudent($firstName: String!, $lastName: String!, $registrationNumber: Int!, $email: String!) {
@DanishSiddiq
DanishSiddiq / student.route.js
Last active May 11, 2020 20:50
Defining CRUD operations using GraphQL and Express route
const { Router } = require('express');
const express_graphql = require('express-graphql');
const { buildSchema } = require('graphql');
const studentController = require('./student.ctrl');
// GraphQL schema
const schema = buildSchema(`
scalar Date
, input SearchInput {
@DanishSiddiq
DanishSiddiq / ecosystem.config.js
Last active September 29, 2018 19:19
Configuration file for setting up PM2
module.exports = {
apps : [
{
name : 'Custering',
script : 'Clustering/src/app.babel-register.js',
exec_mode : 'cluster_mode',
instances : 'max'
},
{
name : 'Clustering_Replcia',
@DanishSiddiq
DanishSiddiq / workerThread.js
Created September 23, 2018 15:16
Worker thread code
// parentPort for registering to events from main thread
// workerData for receiving data clone
const { parentPort, workerData } = require('worker_threads');
let interval;
let index = -1;
registerForEventListening();
function registerForEventListening () {
@DanishSiddiq
DanishSiddiq / index.js
Created September 23, 2018 15:14
Main Threading Code
// module included to create worker threads
const { Worker } = require('worker_threads');
// main attributes
let lst; // list will be populated from 0 to n
let index = -1; // index will be used to traverse list
let myWorker; // worker reference
let interval;
mainBody();
@DanishSiddiq
DanishSiddiq / workerCode.js
Created September 23, 2018 10:47
worker code for a long operation
export default class AsyncListController {
createList = async(req, res) => {
await this.populateHugeList();
res.json({ ProcessId: 'Worker Process Id' + process.pid });
};
/* populate list with million elements
*/
populateHugeList = async() => {