Commit 6cc198bf authored by Sangjune Bae's avatar Sangjune Bae
Browse files

first commit

parent 81c6da4b
import { useMemo, useRef } from 'react';
import useMounted from './useMounted';
import useWillUnmount from './useWillUnmount';
/*
* Browsers including Internet Explorer, Chrome, Safari, and Firefox store the
* delay as a 32-bit signed integer internally. This causes an integer overflow
* when using delays larger than 2,147,483,647 ms (about 24.8 days),
* resulting in the timeout being executed immediately.
*
* via: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout
*/
var MAX_DELAY_MS = Math.pow(2, 31) - 1;
function setChainedTimeout(handleRef, fn, timeoutAtMs) {
var delayMs = timeoutAtMs - Date.now();
handleRef.current = delayMs <= MAX_DELAY_MS ? setTimeout(fn, delayMs) : setTimeout(function () {
return setChainedTimeout(handleRef, fn, timeoutAtMs);
}, MAX_DELAY_MS);
}
/**
* Returns a controller object for setting a timeout that is properly cleaned up
* once the component unmounts. New timeouts cancel and replace existing ones.
*/
export default function useTimeout() {
var isMounted = useMounted(); // types are confused between node and web here IDK
var handleRef = useRef();
useWillUnmount(function () {
return clearTimeout(handleRef.current);
});
return useMemo(function () {
var clear = function clear() {
return clearTimeout(handleRef.current);
};
function set(fn, delayMs) {
if (delayMs === void 0) {
delayMs = 0;
}
if (!isMounted()) return;
clear();
if (delayMs <= MAX_DELAY_MS) {
// For simplicity, if the timeout is short, just set a normal timeout.
handleRef.current = setTimeout(fn, delayMs);
} else {
setChainedTimeout(handleRef, fn, Date.now() + delayMs);
}
}
return {
set: set,
clear: clear
};
}, []);
}
\ No newline at end of file
import { EffectCallback, DependencyList } from 'react';
/**
* Runs an effect only when the dependencies have changed, skipping the
* initial "on mount" run. Caution, if the dependency list never changes,
* the effect is **never run**
*
* ```ts
* const ref = useRef<HTMLInput>(null);
*
* // focuses an element only if the focus changes, and not on mount
* useUpdateEffect(() => {
* const element = ref.current?.children[focusedIdx] as HTMLElement
*
* element?.focus()
*
* }, [focusedIndex])
* ```
* @param effect An effect to run on mount
*
* @category effects
*/
declare function useUpdateEffect(fn: EffectCallback, deps: DependencyList): void;
export default useUpdateEffect;
import { useEffect, useRef } from 'react';
/**
* Runs an effect only when the dependencies have changed, skipping the
* initial "on mount" run. Caution, if the dependency list never changes,
* the effect is **never run**
*
* ```ts
* const ref = useRef<HTMLInput>(null);
*
* // focuses an element only if the focus changes, and not on mount
* useUpdateEffect(() => {
* const element = ref.current?.children[focusedIdx] as HTMLElement
*
* element?.focus()
*
* }, [focusedIndex])
* ```
* @param effect An effect to run on mount
*
* @category effects
*/
function useUpdateEffect(fn, deps) {
var isFirst = useRef(true);
useEffect(function () {
if (isFirst.current) {
isFirst.current = false;
return;
}
return fn();
}, deps);
}
export default useUpdateEffect;
\ No newline at end of file
/// <reference types="react" />
/**
* Returns a ref that is immediately updated with the new value
*
* @param value The Ref value
* @category refs
*/
export default function useUpdatedRef<T>(value: T): import("react").MutableRefObject<T>;
import { useRef } from 'react';
/**
* Returns a ref that is immediately updated with the new value
*
* @param value The Ref value
* @category refs
*/
export default function useUpdatedRef(value) {
var valueRef = useRef(value);
valueRef.current = value;
return valueRef;
}
\ No newline at end of file
/**
* Attach a callback that fires when a component unmounts
*
* @param fn Handler to run when the component unmounts
* @category effects
*/
export default function useWillUnmount(fn: () => void): void;
import useUpdatedRef from './useUpdatedRef';
import { useEffect } from 'react';
/**
* Attach a callback that fires when a component unmounts
*
* @param fn Handler to run when the component unmounts
* @category effects
*/
export default function useWillUnmount(fn) {
var onUnmount = useUpdatedRef(fn);
useEffect(function () {
return function () {
return onUnmount.current();
};
}, []);
}
\ No newline at end of file
{
"_from": "@restart/hooks@^0.3.21",
"_id": "@restart/hooks@0.3.25",
"_inBundle": false,
"_integrity": "sha512-m2v3N5pxTsIiSH74/sb1yW8D9RxkJidGW+5Mfwn/lHb2QzhZNlaU1su7abSyT9EGf0xS/0waLjrf7/XxQHUk7w==",
"_location": "/@restart/hooks",
"_phantomChildren": {},
"_requested": {
"type": "range",
"registry": true,
"raw": "@restart/hooks@^0.3.21",
"name": "@restart/hooks",
"escapedName": "@restart%2fhooks",
"scope": "@restart",
"rawSpec": "^0.3.21",
"saveSpec": null,
"fetchSpec": "^0.3.21"
},
"_requiredBy": [
"/react-bootstrap",
"/react-overlays"
],
"_resolved": "https://registry.npmjs.org/@restart/hooks/-/hooks-0.3.25.tgz",
"_shasum": "11004139ad1c70d2f5965a8939dcb5aeb96aa652",
"_spec": "@restart/hooks@^0.3.21",
"_where": "C:\\Users\\reyel\\OneDrive\\바탕 화면\\2020winter\\search-page\\node_modules\\react-bootstrap",
"author": {
"name": "Jason Quense",
"email": "monastic.panic@gmail.com"
},
"bugs": {
"url": "https://github.com/react-restart/hooks/issues"
},
"bundleDependencies": false,
"dependencies": {
"lodash": "^4.17.15",
"lodash-es": "^4.17.15"
},
"deprecated": false,
"homepage": "https://github.com/react-restart/hooks#readme",
"jest": {
"preset": "@4c",
"rootDir": "./test",
"setupFilesAfterEnv": [
"./setup.js"
]
},
"license": "MIT",
"main": "cjs/index.js",
"module": "esm/index.js",
"name": "@restart/hooks",
"peerDependencies": {
"react": "^16.8.0"
},
"prettier": {
"singleQuote": true,
"semi": false,
"trailingComma": "all"
},
"publishConfig": {
"access": "public",
"directory": "lib"
},
"release": {
"conventionalCommits": true
},
"repository": {
"type": "git",
"url": "git+https://github.com/jquense/react-common-hooks.git"
},
"types": "cjs/index.d.ts",
"version": "0.3.25"
}
{
"name": "@restart/hooks/useAnimationFrame",
"private": true,
"main": "../cjs/useAnimationFrame.js",
"module": "../esm/useAnimationFrame.js",
"types": "../esm/useAnimationFrame.d.ts"
}
{
"name": "@restart/hooks/useBreakpoint",
"private": true,
"main": "../cjs/useBreakpoint.js",
"module": "../esm/useBreakpoint.js",
"types": "../esm/useBreakpoint.d.ts"
}
{
"name": "@restart/hooks/useCallbackRef",
"private": true,
"main": "../cjs/useCallbackRef.js",
"module": "../esm/useCallbackRef.js",
"types": "../esm/useCallbackRef.d.ts"
}
{
"name": "@restart/hooks/useCommittedRef",
"private": true,
"main": "../cjs/useCommittedRef.js",
"module": "../esm/useCommittedRef.js",
"types": "../esm/useCommittedRef.d.ts"
}
{
"name": "@restart/hooks/useCustomEffect",
"private": true,
"main": "../cjs/useCustomEffect.js",
"module": "../esm/useCustomEffect.js",
"types": "../esm/useCustomEffect.d.ts"
}
{
"name": "@restart/hooks/useEventCallback",
"private": true,
"main": "../cjs/useEventCallback.js",
"module": "../esm/useEventCallback.js",
"types": "../esm/useEventCallback.d.ts"
}
{
"name": "@restart/hooks/useEventListener",
"private": true,
"main": "../cjs/useEventListener.js",
"module": "../esm/useEventListener.js",
"types": "../esm/useEventListener.d.ts"
}
{
"name": "@restart/hooks/useFocusManager",
"private": true,
"main": "../cjs/useFocusManager.js",
"module": "../esm/useFocusManager.js",
"types": "../esm/useFocusManager.d.ts"
}
{
"name": "@restart/hooks/useForceUpdate",
"private": true,
"main": "../cjs/useForceUpdate.js",
"module": "../esm/useForceUpdate.js",
"types": "../esm/useForceUpdate.d.ts"
}
{
"name": "@restart/hooks/useGlobalListener",
"private": true,
"main": "../cjs/useGlobalListener.js",
"module": "../esm/useGlobalListener.js",
"types": "../esm/useGlobalListener.d.ts"
}
{
"name": "@restart/hooks/useImage",
"private": true,
"main": "../cjs/useImage.js",
"module": "../esm/useImage.js",
"types": "../esm/useImage.d.ts"
}
{
"name": "@restart/hooks/useImmediateUpdateEffect",
"private": true,
"main": "../cjs/useImmediateUpdateEffect.js",
"module": "../esm/useImmediateUpdateEffect.js",
"types": "../esm/useImmediateUpdateEffect.d.ts"
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment