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
interface IDeferred<T> { | |
promise: () => Promise<T>, | |
resolve: (result: T) => void, | |
reject: (error: Error) => void | |
} | |
function createDeferred<T>(): Promise<IDeferred<T>> { | |
return new Promise<IDeferred<T>>(resolveCreate => { | |
const promise = new Promise<T>(function (resolve, reject) { | |
resolveCreate({ promise: () => promise, resolve, reject }); |
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
function asyncToDone(task: () => Promise<any>) { | |
return function (done: (err?: Error) => void) { | |
task().then(() => done(), done); | |
} | |
} |
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 http2 = require('http2'); | |
function main() { | |
const server = http2.createSecureServer({ | |
cert: CERTIFICATE, | |
key: PRIVATE_KEY | |
}); | |
server.on('error', err => console.error(err)); |
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
// executeAsyncScript is not running a Promise function and is not able to deal with errors. | |
// https://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/index_exports_WebDriver.html#executeAsyncScript | |
// This function will use executeAsyncScript to run a Promise function in an async fashion. | |
export default async function executePromiseScript(driver, fn, ...args) { | |
const { error, result } = await driver.executeAsyncScript((fn, args, callback) => { | |
eval(`(${fn})`).apply(null, args).then(result => callback({ result }), error => callback({ error })); | |
}, fn + '', args); | |
if (error) { |
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
<!DOCTYPE html> | |
<!-- Very unpolished, just for the very short purpose --> | |
<html lang="en-US"> | |
<head> | |
<title>Render logo</title> | |
<script type="text/javascript"> | |
(async function () { | |
'use strict'; | |
const SIZE = 192; |
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
function useLayoutEffectDebug(fn, deps) { | |
const [lastDeps, setLastDeps] = useState([]); | |
useLayoutEffect(() => { | |
console.log({ lastDeps, deps }); | |
lastDeps && console.log(deps.findIndex((value, index) => value !== lastDeps[index])); | |
setLastDeps(deps); |
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 searchParams = Array.from(new URLSearchParams(location.search).entries()).reduce((result, [key, value]) => ({ ...result, [key]: value }), {}); |
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 TOKEN_EXPIRE_AFTER = 300000; | |
// This function will create a memoized fetchCredentials function and reduce the call to the function. | |
// The memoized result is time-sensitive and will be invalidated after 5 minutes (specified in TOKEN_EXPIRE_AFTER). | |
const createMemoizedFetchCredentials = () => { | |
let lastFetch = 0; | |
let lastPromise; | |
return () => { | |
const now = Date.now(); |
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
export default function useDetectDepsChange(deps) { | |
const prevDeps = useRef([]); | |
deps.forEach((value, index) => { | |
if (prevDeps.current[index] !== value) { | |
console.log('Dep changed', { index, dep, prevDep: prevDeps.current[index] }); | |
} | |
}); | |
prevDeps.current = [...deps]; |
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 { useRef } from 'react'; | |
export default function useDebugDeps(depsMap, name) { | |
const lastDepsMapRef = useRef({}); | |
const { current: lastDepsMap } = lastDepsMapRef; | |
const keys = new Set([...Object.keys(depsMap), ...Object.keys(lastDepsMap)]); | |
const keysChanged = Array.from(keys).filter(key => !Object.is(depsMap[key], lastDepsMap[key])); | |
if (keysChanged.length) { |
OlderNewer