Skip to content

Instantly share code, notes, and snippets.

View srebalaji's full-sized avatar
🎯
Focusing

Srebalaji Thirumalai srebalaji

🎯
Focusing
View GitHub Profile
@srebalaji
srebalaji / checkout.sh
Created April 10, 2023 08:15
Bash script to checkout to new branch if you have any files with updated index
#!/bin/bash
# Set the branch name as the first argument passed to the script
branch_name=$1
# Cancel the skip-worktree changes for all files
git ls-files -v | grep ^S | cut -c 3- | xargs git update-index --no-skip-worktree
# Stash all files
git stash save --keep-index --include-untracked "Stashing skip-worktree files"
@srebalaji
srebalaji / git-hard-delete
Last active October 10, 2023 13:10
Examples of git custom command
#!/bin/sh
branch=$1
if [ ! -z "$1" ]
then
git branch -D $branch
git push -d origin $branch
else
echo "Branch name is not specified"
@srebalaji
srebalaji / readme.md
Created April 11, 2020 07:25
How to fix React router redirection in webpack production build - Netlify
@srebalaji
srebalaji / workerPool.js
Last active April 8, 2020 08:04
Worker thread pool - Manage worker threads pool in NodeJS
// workerPool.js
// Worker Thread pool
const {Worker, parentPort, MessageChannel} = require('worker_threads')
class WorkerPool {
constructor(size, worker) {
this.size = size
this.worker = worker
this.pool = []
@srebalaji
srebalaji / main.js
Created April 7, 2020 14:25
Message channels between workers
// main.js
const {Worker, parentPort, MessageChannel} = require('worker_threads')
const worker = new Worker('./worker.js')
const messageChannel = new MessageChannel()
// As you can see there are two parameters passed in `postMessage`
worker.postMessage({yourPort: messageChannel.port1}, [messageChannel.port1])
@srebalaji
srebalaji / main.js
Created April 7, 2020 06:12
Message channel in Worker threads
const { MessageChannel } = require('worker_threads')
const {port1, port2} = new MessageChannel()
port1.once('message', (msg) => {
console.log(msg)
})
port2.postMessage({msg: "hello world"})
@srebalaji
srebalaji / main.js
Last active April 7, 2020 07:47
NodeJS Worker thread example
const {Worker, isMainThread, parentPort, workerData} = require('worker_threads')
// Returns if the script runs in main thread of in worker
if (isMainThread) {
const arr = [[1, 10],[2, 15],[3, 21],[4, 25],[5, 86]]
for (const ele of arr) {
const worker = new Worker(__filename, {workerData: {a: ele[0], b: ele[1]}})
worker.on('message', (result) => {
console.log(`The sum of ${ele[0]} and ${ele[1]} is ${result}`)
@srebalaji
srebalaji / example.js
Last active April 6, 2020 09:49
NodeJS Worker thread example
// index.js
const {Worker, isMainThread, parentPort, workerData} = require('worker_threads')
const worker = new Worker("./worker.js", {workerData: {a: 5, b: 10}})
worker.once('message', (result) => {
console.log('The sum is', result)
})
@srebalaji
srebalaji / promise-all-map.js
Last active February 9, 2024 17:14
A sample Promise.all in map
// Function to fetch Github info of a user.
const fetchGithubInfo = async (url) => {
console.log(`Fetching ${url}`)
const githubInfo = await axios(url) // API call to get user info from Github.
return {
name: githubInfo.data.name,
bio: githubInfo.data.bio,
repos: githubInfo.data.public_repos
}
}
@srebalaji
srebalaji / Promise all example to send email.js
Last active April 11, 2019 05:07
A simple Promise all example to send emails
// Async function to send mail to a list of users.
const sendMailForUsers = async (users) => {
const usersLength = users.length
for (let i = 0; i < usersLength; i += 100) {
const requests = users.slice(i, i + 100).map((user) => { // The batch size is 100. We are processing in a set of 100 users.
return triggerMailForUser(user) // Async function to send the mail.
.catch(e => console.log(`Error in sending email for ${user} - ${e}`)) // Catch the error if something goes wrong. So that it won't block the loop.
})