Skip to content

Instantly share code, notes, and snippets.

const musicNotify = () => {
const m = '/storage/emulated/0/Download/WeiXin/平凡之路.mp3'
media.playMusic(m);
sleep(media.getMusicDuration());
}
const to_mall_cart = () => {
shopping_cart_btn=id('img_shopping_cart').findOne()
@Misaka-0x447f
Misaka-0x447f / React 组件设计范式:调用子组件的方法.md
Last active February 17, 2022 08:12
React 组件设计范式:调用子组件的方法

本文不考虑各种写法的 CPU 时间开销,只考虑各种写法的开发人员时间开销。

可能是最糟糕的方法:使用 ref 保存指向组件实例的指针

ref 以及其衍生的 forwardRef 是 React 直接提供给组件设计人员的工具,但我个人并不推荐用 ref,也不是很推荐用 forwardRef。 不推荐 ref 的原因:

  • 函数式组件没有实例,不能通过 ref 访问其实例。
  • 需要新建一个变量,且其初始类型一定为 null,在使用时需要判断。 不太推荐在跨组件场景下用 forwardRef 的原因:
  • 函数式组件没有实例。
  • 虽然无论如何都要保留一个联系子组件的"指针",但可能有更好的方法,从而不需要将该"指针"存储于单独的位置(而不是组件内部),造成更多的多实例场景下的麻烦。当然,对于子组件联系亲代组件,仍需将该指针保存于单独的位置。

替代方案

@Misaka-0x447f
Misaka-0x447f / 错误处理语句编写方式讨论.md
Created February 15, 2022 11:20
错误处理语句编写方式讨论

想象以下场景。你在当地的办公室里写着一个页面,遇到了一个可能抛出错误的 api 需要处理。你使用了 try catch 语句:

const [loading, setLoading] = useState(false)
const [error, setError] = useState(false)
const [data, setData] = useState<number>([])
const refresh = async () => {
    setError(false)
    setLoading(true)
    try {
        const res = await api()
@Misaka-0x447f
Misaka-0x447f / scan-ip.js
Last active December 26, 2021 14:26
自动扫描中国电信精品网 ip
// ==UserScript==
// @name 自动扫描中国电信精品网
// @namespace http://tampermonkey.net/
// @version 0.1
// @description 自动扫描中国电信精品网 ip (58.32.xx.xx)
// @author misaka.org
// @match http://10.0.0.1/cgi-bin/luci/admin/network/network
// @icon https://www.google.com/s2/favicons?domain=0.1
// @grant none
// ==/UserScript==
@Misaka-0x447f
Misaka-0x447f / microsoft-interview-questions.md
Last active December 13, 2021 02:24
Microsoft Interview Questions

Initial round

  • implement lodash.debounce
  • implement a background task manager with max concurrency limit

2nd round

  • possible IP address. given a string like '25525512218', return an array like ['255.255.12.218', '255.255.122.18']

3rd round leader interview

  • design an undirected graph with no cycle, including method of add & delete, then print the longest path length in the graph.

4th round grand-leader interview

(scheduled)

@Misaka-0x447f
Misaka-0x447f / override.ts
Created November 19, 2021 10:16
Overriding arrow function
const m: {
(a: string): void;
(b: number): void
} = (c: string | number) => {}
// editor now knows first use name a, second use name b.
m('1')
m(1)
// 粘贴到 console 里就可以了
let r = () => {
const temp = Math.random; Math.random = () => 0; document.getElementById('random').click(); Math.random = temp;
}
let s = () => {
let t;
t = setInterval(() => {
let e = document.getElementById('lifeTrajectory')
if (!e) clearInterval(t)
else e.click()
@Misaka-0x447f
Misaka-0x447f / selectCase.ts
Last active October 27, 2023 03:12
lang tool: select case
/**
* @author [email protected]
* @desc 在多个分支中选择一个分支来执行。以函数式编程的方式呈现,且比原生的 switch 表达逻辑更清晰
* @example
* const result = selectCase(
* [isNull(alice), () => {
* alert('alice should not be null')
* return false
* }],
* [isNumeric(alice), () => true]
@Misaka-0x447f
Misaka-0x447f / index.tsx
Created August 18, 2021 03:48
react native 创建基于原生组件的透明组件
// 合并的方式不是给 style 传一个可能带 function 的 array,而是给 style 传一个返回值一定不带 function 的 (whatever: any) => unknown[]
export const PressableWithFeedback: React.FC<PressableProps> = props => {
return (
<Pressable
{...props}
style={args => [
isFunction(props.style) ? props.style(args) : props.style,
{
backgroundColor: args.pressed ? 'white' : 'transparent',
},