Skip to content

Instantly share code, notes, and snippets.

View qkreltms's full-sized avatar
💖
Happy coding

JungHoonPark qkreltms

💖
Happy coding
View GitHub Profile
@qkreltms
qkreltms / throttle.ts
Created January 3, 2023 08:15
throttle
export const throttle = (() => {
let isWaiting = false;
return (callback: () => void, delay = 100) => {
if (isWaiting) {
return;
}
isWaiting = true;
setTimeout(() => {
callback();
isWaiting = false;
const CountButton = React.memo(function CountButton({ onClick, count }: any) {
return <button onClick={onClick}>{count}</button>;
});
function DualCounter() {
const [count1, setCount1] = React.useState(0)
const [count2, setCount2] = React.useState(0)
const increment1 = React.useCallback(()=>setCount1(c => c + 1),[])
const increment2 = React.useCallback(()=>setCount2(c => c + 1),[])
const h=[]
for(let i=0;i<100;i++){
function CountButton({onClick, count}:{onClick:()=>void, count: number}) {
  return <button onClick={onClick}>{count}</button>
}
function DualCounter() {
  const [count1, setCount1] = React.useState(0)
  const increment1 = () => setCount1(c => c + 1)
  const [count2, setCount2] = React.useState(0)
  const increment2 = () => setCount2(c => c + 1)
 return (
function CandyDispenser() {
const initialCandies = ['snickers', 'skittles', 'twix', 'milky way']
const [candies, setCandies] = React.useState(initialCandies)
const dispense = React.useCallback(candy => {
setCandies(allCandies => allCandies.filter(c => c !== candy))
}, [])
return (
<div>
<h1>Candy Dispenser</h1>
<div>
1. Let x be ? thisNumberValue(this value).
2. Let f be ? ToIntegerOrInfinity(fractionDigits).
3. Assert: If fractionDigits is undefined, then f is 0.
4. If f is not finite, throw a RangeError exception.
5. If f < 0 or f > 100, throw a RangeError exception.
6. If x is not finite, return ! Number::toString(x).
7. Set x to (x).
8. Let s be the empty String.
9. If x < 0, then
  // https://github.com/rt2zz/redux-persist/blob/master/src/createPersistoid.js#L110-L115 
  function passWhitelistBlacklist(key) {
    // whitelist에 적은 특정 키 값만 저장을 허용한다.
    if (whitelist && whitelist.indexOf(key) === -1 && key !== '_persist')
      return false
    // blacklist에 적은 키 값은 저장을 허용하지 않는다.
    if (blacklist && blacklist.indexOf(key) !== -1) return false
    return true
 }
const update = (state: Object) => {
    // add any changed keys to the queue
    Object.keys(state).forEach(key => {
      if (!passWhitelistBlacklist(key)) return // is keyspace ignored? noop
      if (lastState[key] === state[key]) return // value unchanged? noop
      if (keysToProcess.indexOf(key) !== -1) return // is key already queued? noop
      keysToProcess.push(key) // add key to queue
    })
  let persistor: Persistor = {
    ..._pStore,
    purge: () => {
      let results = []
      store.dispatch({
        type: PURGE,
        result: purgeResult => {
          results.push(purgeResult)
 },
 } else if (action.type === REHYDRATE) {
      // noop on restState if purging
      if (_purge)
        return {
          ...restState,
          _persist: { ..._persist, rehydrated: true },
        }

 // @NOTE if key does not match, will continue to default else below
if (action.type === PERSIST) {
  //...

  action.register(config.key)

  getStoredState(config).then(
        restoredState => {
          //...
 _rehydrate(migratedState)