This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const EEStoEventMaps = new WeakMap(); | |
export default class EventEmitter{ | |
constructor(publisher){ | |
const eventMap = Object.create(null); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**generates async tasks */ | |
function asyncTask() { | |
return new Promise((resolve, reject) => { | |
let val = Math.floor(Math.random() * 10); | |
if (val > 5) { | |
resolve(val); | |
} else { | |
reject(val); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let cache = []; | |
function fib(num) { | |
if (cache[num] == undefined) { | |
if (num == 0) { | |
cache[0] = 0; | |
} else if (num == 1) { | |
cache[1] = 1; | |
} else { | |
cache[num] = fib(num - 1) + fib(num - 2); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Array map polyfill | |
* @param {*} callback | |
* @returns | |
*/ | |
function myMap(callback) { | |
let newArr = []; | |
for (let i = 0; i < this.length; i++) { | |
newArr.push(callback(this[i])); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Problem Statement | |
//Implement a function in JavaScript that caches the API response for the given amount of time. | |
//If a new call is made between that time, the response from the cache will be returned, | |
//else a fresh API call will be made. | |
/** | |
* | |
* @param {*} expiryTime in ms | |
*/ | |
function cachedApi(expiryTime) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Problem Statement | |
//Implement a function in JavaScript that caches the API response for the given amount of time. | |
//If a new call is made between that time, the response from the cache will be returned, | |
//else a fresh API call will be made. | |
/** | |
* | |
* @param {*} expiryTime in ms | |
*/ | |
function cachedApi(expiryTime) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useState, useEfftect, useRef } from "react"; | |
const useStateWithcallback = (initialState) => { | |
const [state, setState] = useState(initialState); | |
const callbackRef = useRef(null); | |
const setStateWithCallback = (newState, callback) => { | |
callbackRef.current = callback; | |
setState(newState); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class EventBus { | |
static handlers = {}; | |
static on(eventName, handler, scope) { | |
if (!EventBus.handlers[eventName]) { | |
EventBus.handlers[eventName] = []; | |
} | |
EventBus.handlers[eventName].push({ | |
subscription: handler, | |
scope, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useCallback, useRef } from "react"; | |
const useDebounceHook = (fn, delay, immediate = false) => { | |
const timerId = useRef(); | |
const debounce = useCallback( | |
function () { | |
let context = this; | |
let args = arguments; | |
//check if immediate flag is true so invoke the debounce immediately | |
let callNow = immediate && !timerId.current; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect, useRef } from "react"; | |
const usePrevious = (value) => { | |
const ref = useRef(); | |
useEffect(() => { | |
ref.current = value; | |
}, [value]); | |
return ref.current; | |
}; |
OlderNewer