Skip to content

Instantly share code, notes, and snippets.

//Implement a throttler that executes an array of tasks. When the throttler is passed a number, only executes that number of the tasks and passes the other tasks into a queue.
//Throttling is a way/technique to restrict the number of function execution/calls.
/**
* normal throttle function
* @param {*} fn
* @param {*} delay
* @returns
*/
@satyam4p
satyam4p / useResponsive.jsx
Created August 6, 2024 15:44
provide hook for handling screen size
import { useEffect, useState } from "react";
import useDebounceHook from "./useDebounceHook";
const useResponsive = () => {
const debounce = useDebounceHook(onResizeHandler, 500, false);
const [state, setState] = useState({
isMobile: false,
isTablet: false,
isDesktop: false,
//We will now look at pattern that will execute set of given tasks in parallel with given concurrency.
function makeSimpleTask(name) {
return (cb) => {
console.log("started");
setTimeout(() => {
console.log(name, "completed");
cb();
}, Math.random() * 2000);
};
@satyam4p
satyam4p / ASyncTaskQueue.js
Last active August 12, 2024 16:17
Taskqueue with given concurrency
class AsyncTaskQueue {
constructor(concurrency) {
this.concurrency = concurrency;
this.running = 0;
this.queue = [];
}
pushtask(task) {
this.queue.push(task);
process.nextTick(this.next.bind(this));
@satyam4p
satyam4p / FileExplorereUsingReact.js
Created November 14, 2024 06:42
file explorere like ui
import { useState } from "react";
import "./styles.css";
const data = [
{
id: 1,
name: "README.md",
},
{
id: 2,