Skip to content

Instantly share code, notes, and snippets.

View huynhducduy's full-sized avatar
😍
I'm in love with js <3

Huynh Duc Duy huynhducduy

😍
I'm in love with js <3
View GitHub Profile
@huynhducduy
huynhducduy / isolate.ts
Created July 12, 2024 06:20
Wrap a function in `isolate` to prevent it from create closure.
export default function isolate<T extends (...args: unknown[]) => unknown>(fn: T): T {
// eslint-disable-next-line @typescript-eslint/no-implied-eval -- workaround
return new Function(`
with (new Proxy({}, {
has () { return true; },
get (target, property) {
if (typeof property !== 'string') return target[property];
throw new ReferenceError(property + ' accessed from isolated scope');
},
set (target, property) {
@huynhducduy
huynhducduy / detangle.js
Created July 2, 2024 18:55
detangle.js
export function detangle() {
let blocked = false
return callback => (...args) => {
if (blocked) return
blocked = true
callback(...args)
#!/bin/sh
TARGET_DIR=node_modules
if [ ! -d $TARGET_DIR ]; then
echo "$TARGET_DIR" does not exist
exit 1
fi
PATTERNS="
function bigIntToFloatString(value: bigint, decimalPlaces: number) {
if (typeof decimalPlaces !== 'number' || decimalPlaces < 0 || !Number.isInteger(decimalPlaces)) {
throw new TypeError('The second argument must be a non-negative integer');
}
const divisor = BigInt(10 ** decimalPlaces); // 1e{decimalPlaces}
const integerPart = value / divisor;
const fractionalPart = value % divisor;
Đầu tiên là phải hiểu đúng web3 là gì. Web3 có phải là blockchain không, hay là crypto? Web3.0 là Internet 3.0. Định nghĩa thì dài lắm nhưng cách hiểu nhanh nhất có thể được thể hiện qua sự so sánh của internet ở các thời kỳ như sau:
Web1.0 = 1 bên đăng, 1 bên đọc
Web2.0 = 2 bên đều có thể đọc và đăng nhưng 1 bên sở hữu, quản lý, hưởng lợi
(google, fb hạn chế 1 số chủ đề, bài viết chẳng hạn; thanh toán qua trung gian như sàn thương mại điện tử hoặc tài khoản ngân hàng)
Web3.0 = tất cả các bên đều có thể đọc, đăng, sở hữu, quản lý và hưởng lợi
(tự do tạo nội dung, tạo ra các loại app, thanh toán trực tiếp vào ví crypto và/hoặc thông qua smart contract).
Để hiểu đầy đủ, tường tận, bạn nên đọc kĩ bài viết này nhé: https://ethereum.org/en/web3/
Có 1 số người tin rằng web3.0 là tương lai của internet, 1 số người thì nghĩ web3.0 là scam. 1 số còn lại nghĩ rằng web2.0 và web3.0 sẽ tồn tại song song.
Chúng ta đều có những nhận định của riêng mình. Vì vậy, sau khi hiểu rõ web3.0 là gì thì mình sẽ chuyển qua bước tiếp
@huynhducduy
huynhducduy / useGlobalMemo.js
Created May 19, 2024 12:51 — forked from tannerlinsley/useGlobalMemo.js
useGlobalMemo is a React hook that lets you share memoizations across an entire app using a unique key.
const cache = {}
export default function useGlobalMemo (key, fn, deps) {
if (!cache[key]) {
cache[key] = {
subs: 0,
deps,
value: fn(),
}
export default function blockSync(ms: number) {
const start = Date.now()
let now = start
while (now - start < ms) {
now = Date.now()
}
}
@huynhducduy
huynhducduy / useStateAndRef.ts
Created April 4, 2024 04:46
useStateAndRef
import {useReducer, useRef} from 'react'
import {useCallback} from '@/utils/hooks/useCallback'
/*
* This hook create a ref that mirror every change to the state
* This is useful when we have an effect or memo that read value from state, but dont want to react to state change => use the ref for reading
* Future react version will add a support for `useEffectEvent` https://react.dev/reference/react/experimental_useEffectEvent which can solve the case
* This hook have 2 modes:
* 1. sync: (default mode) ref value and react state value will be update by react scheduler at the sametime that determined by the scheduler (usually lag behind the method call)
@huynhducduy
huynhducduy / useCallback.ts
Last active April 4, 2024 04:44
useBetterCallback (with type-safe, make sure props and arguments passed always be memoized callbacks)
import {useCallback as useCallbackBase} from 'react'
/*
* Use this type to make sure that the callback passed will always be a memoized callback
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export interface MemoizedCallback<T extends (...args: any[]) => any> {
(...args: Parameters<T>): ReturnType<T>
memoized: true
}
import * as React from "react";
import { useMousePosition } from "~/hooks/useMousePosition";
/** Component to cover the area between the mouse cursor and the sub-menu, to allow moving cursor to lower parts of sub-menu without the sub-menu disappearing. */
export function MouseSafeArea(props: { parentRef: React.RefObject<HTMLDivElement> }) {
const { x = 0, y = 0, height: h = 0, width: w = 0 } = props.parentRef.current?.getBoundingClientRect() || {};
const [mouseX, mouseY] = useMousePosition();
const positions = { x, y, h, w, mouseX, mouseY };
return (
<div