Skip to content

Instantly share code, notes, and snippets.

View coodoo's full-sized avatar

Jeremy Lu coodoo

View GitHub Profile
@coodoo
coodoo / book_summary.txt
Created April 26, 2024 22:23
《黑暗巨塔》書摘
# BOOK INFO
- 黑暗巨塔
- <no_subtitle>
- 大衛.恩里奇
# OVERVIEW
@coodoo
coodoo / fatch.js
Last active February 5, 2024 05:31
替 fetch 加上 retry 與 timeout 功能
/*
# goal
- 在不修改 node-fetch 原始參數格式下新增兩功能
1. 新增 timeout 設定且逾時自動重試
2. 新增 retry 模式可指定重試次數
- 所有 config{} 皆有預設值因此使用上與原始 fetch() 相同
$$('*').forEach(e => {e.style.outline = "1px solid #" + (~~(Math.random()*(1<<24))).toString(16);e.style.outlineOffset='-1px'} )
// sample code copied from the blog:
// https://blog.axlight.com/posts/you-might-not-need-react-query-for-jotai/
const idAtom = atom(1)
const dataAtom = atom(
// read
async (get) => {
const id = get(idAtom)
const res = await fetch(`https://reqres.in/api/posts/${id}`)
const data = await res.json()
return data
@coodoo
coodoo / statechart-xstate.md
Last active May 26, 2022 20:00
Extensive research of statecharts with a focus on front-end development.

The case for statechart and xstate -- why it matters and how we can benefit from it

Bottom line up front

  • redux is a global data management tool, not a proper state management tool, hence causing a lot of troubles

  • statecharts is an extension to Finite State Machine (FSM) which provides explicit and safe state management capabilities, perfectly fit for front-end development

  • statecharts had been used intensivelly in all industries (be it embedded systems, hardware, electronics, aeronautics, automotive, game development and more), it's us front-end developers late to the party

@coodoo
coodoo / a.js
Last active February 21, 2022 06:54
async function foo() {
// throw new Error('bb')
return new Promise((res, rej) => {
setTimeout(_ => {
// throw new Error('bb')
rej('cc')
}, 1000)
})
}
@coodoo
coodoo / a1.js
Last active January 14, 2022 07:11
async sample
// 前情提要:async/await 只是 Promise 的語法糖
// 只要精熟 Promise 則使用 async/await 就心無罣礙
// 範例 1 - 錯誤示範
// main 為同步執行,不會等待 forEach 內三件 async 工作完成即先結束
async function async_log(time) {
// 模擬這支 fn 經過 300ms 後才回應,因此是非同步函式
// 想像成是 fetch() 就行
return new Promise((resolve, reject) => {
setTimeout(_ => {
@coodoo
coodoo / permutation.js
Created August 17, 2021 22:32
permutation in js with reducer
let arr = [
['a1', 'a2'],
['b1', 'b2', 'b3']
]
let result = arr.reduce(
// -----------
// 最外層 reduce 會依序遍歷
// ['a1', 'a2'] 與 ['b1', 'b2', 'b3']
@coodoo
coodoo / example.js
Created March 8, 2021 07:11
一個十分鐘寫的土砲 hotkey manager
useHotkey(['Meta', 'c'], () => {
console.log( `要跑 1`, )
})
useHotkey(['ctrl', 'c'], () => {
console.log( `要跑 2`, )
})
@coodoo
coodoo / starling.js
Created May 6, 2013 02:25
Starling.js source code, slightly annotated. Starling.js is a canvas based drawing framework, ported from iOS to AS3 to Javascript. Demo: http://gamua.com/area-51/
/*
RequireJS 2.1.4 Copyright (c) 2010-2012, The Dojo Foundation All Rights Reserved.
Available via the MIT or new BSD license.
see: http://github.com/jrburke/requirejs for details
*/
/*! jQuery v1.9.1 | (c) 2005, 2012 jQuery Foundation, Inc. | jquery.org/license
//@ sourceMappingURL=jquery.min.map
*/