Skip to content

Instantly share code, notes, and snippets.

View teal-front's full-sized avatar
🚀
Learning

Aaron Sprinkle teal-front

🚀
Learning
View GitHub Profile
@teal-front
teal-front / fetch.js
Last active March 28, 2024 02:47
project-snippets
/**
* fetch api
* 1. 确保返回 fulfilled promise,没有rejected promise,所以也不用处理错误
* 带错误的Promise<[null, data]>
* 正常结果的Promise<[{code: number, msg: string}]>
* 2. 使用了signal来处理超时情况
*/
// 如果是sdk项目,使用ponyfill,如果是自己的项目,使用polyfill(isomorphic-fetch)
import fetchPonyfill from "fetch-ponyfill";
import pickBy from "lodash/pickBy";
@teal-front
teal-front / gallery-list-dyamic.scss
Last active December 12, 2023 05:27
css snippets
@teal-front
teal-front / scroll-to-view.jsx
Last active December 17, 2022 14:14
common interaction
import React from 'react';
import classNames from 'classnames';
/**
* 点菜单,滚动到内容区的固定位置
*/
export function ScrollToView() {
const header = React.useRef(null);
const anchor1 = React.useRef(null);
@teal-front
teal-front / base.md
Last active August 9, 2023 02:23
Vue
@teal-front
teal-front / Array.js
Last active March 30, 2023 15:05
编程基本能力
/// flatten array
// 1. absolute flatten, no depth param
// [[1,[2,[[3]]]],4,[5,[[[6]]]]] => [1,2,3,4,5,6]
const flatten = list => list.reduce(
(a, b) => a.concat(Array.isArray(b) ? flatten(b) : b), []
)
// recursive
function flatten(arr, result = []) {
arr.forEach(item => {
// region Unknown vs. any
// unknown: unknown provide safer typing,
// you should use type assertion or narrow to a special type
// if you want to perform operations on unknown.
function f1(a: any) {
a(); // OK
}
function invokeAnything(callback: unknown) {
@teal-front
teal-front / useEffect-with-async.js
Last active January 27, 2022 08:22
React hooks best use
// https://devtrium.com/posts/async-functions-useeffect
/**
* 当依赖改变导致useEffect再次执行时,两次async函数的到达时间顺序并不能保证
* 所以在cleanup函数里,去掉前一次请求的状态码,即使前一次请求后到达,也不能执行`setData(json)`
*/
useEffect(() => {
// flag
let isSubscribed = true;
@teal-front
teal-front / rpc.js
Last active November 8, 2021 15:53
threads
/**
* rpc mini impl
* @author 神说要有光
*/
let id = 0
function getId() {
return id++
}
const callMap = new Map()
@teal-front
teal-front / Promise.all.js
Last active January 24, 2024 12:09
promise
// https://brunoscopelliti.com/blog/lets-write-a-promise-polyfill/
function isEmptyIterable(iterable) {
for(const _ of iterable) {
return false
}
return true
}
Promise.all = function (iterable) {