Skip to content

Instantly share code, notes, and snippets.

@luo3house
luo3house / promisifychannel.ts
Last active April 29, 2022 06:21
promisify an event-based channel, easy to build context-isolated apis
/**
* @name promisifychannel
* @brief Promisify an event-based channel
* @version 2
* @license MIT
* @copyright luo3house
* @requires JSON, ES2015 or later (Promise lib)
*/
// util types
@luo3house
luo3house / wx-navigation-channel.ts
Last active April 29, 2022 08:12
小程序页面跳转库
// promisifychannel 库
import {
ChannelCreateOptions,
createChannel,
} from '../../utils/promisifychannel'
// 小程序可能没有带 btoa 方法,请自行寻找 base64 实现库
import { btoa, atob } from '../../utils/base64'
// protocol types
@luo3house
luo3house / server.js
Created August 2, 2022 09:26
HTTPS 反代本地开发环境,避免线上后端 CORS
// Step 1: 修改 /etc/hosts,把 CORS 合法域名指向本机
// Step 2: 当前目录准备该域名的证书 tls.crt
// Step 3: 当前目录准备该域名的证书 tls.key
// Step 4: 修改 TARGET 为本地开发环境的 URL
// Step 5: node server.js
var http = require('http'), https = require('https'), fs = require('fs')
var TARGET = 'http://localhost:8080'
var PORT = 443
@luo3house
luo3house / useObserver.tsx
Created October 10, 2022 03:36
mobx-react-lite: useObserver completely follow component effects
import { getDependencyTree, Reaction } from "mobx"
import React, { useRef, useState, useLayoutEffect } from "react"
import { isUsingStaticRendering } from "./staticRendering"
import { printDebugValue } from "./utils/printDebugValue"
function observerComponentNameFor(baseComponentName: string) {
return `observer${baseComponentName}`
}
@luo3house
luo3house / avd.yaml
Created March 21, 2023 14:03
Clash Proxy within AVD
port: 7890
socks-port: 7891
allow-lan: true
mode: Global
log-level: info
external-controller: 127.0.0.1:9090
# https://developer.android.com/studio/run/emulator-networking?hl=zh-cn#networkaddresses
proxies:
- {name: "AVD Host", desc: "(10.0.2.2)", server: 10.0.2.2, port: 7890, type: socks5 }
@luo3house
luo3house / TouchOpacity.dart
Last active April 12, 2023 08:17
Flutter Widget: TouchOpacity
import 'package:flutter/widgets.dart';
class TouchOpacity extends StatefulWidget {
final Widget child;
final void Function()? onTap;
const TouchOpacity({
super.key,
this.child = const SizedBox(),
this.onTap,
@luo3house
luo3house / i18n-translation.dart
Last active April 26, 2023 06:45
Simple i18n strings mapper
class Translation {
// "zh-a-b-c" -> m"-c"
static final _localeSuffixRE = RegExp(r"-[^-]+$");
final bool localeCaseSensitive;
var _currentLocale = "en";
var _fallbackLocale = "en";
Map<String, Map<String, String>> dict = Map();
Translation({
@luo3house
luo3house / lru_linked_map.dart
Last active April 20, 2023 10:19
LRU cache: Circular linked map
/// if true, will pop the last
typedef LRUPolicyTest<K, V> = bool Function(LRULinkedMap lru, _Node<K, V> item);
class _Node<K, V> {
late K key;
late V value;
_Node<K, V>? prev;
_Node<K, V>? next;
}
@luo3house
luo3house / interceptor.dart
Last active February 21, 2024 01:48
Interceptor Pattern
typedef Middleware<C> = dynamic Function(C context, Function() next);
class Interceptor<C> {
static _noop() {}
final List<Middleware<C>> middlewares = List.empty(growable: true);
Future<void> call<T>(C context, [Function() fn = _noop]) async {
if (middlewares.isEmpty) return fn();
return middlewares.reduce((pre, cur) {
@luo3house
luo3house / token_store.dart
Created April 26, 2023 13:01
Easy to manage access tokens and refresh tokens
class VariableTokenStore<Token> {
/// validate token is valid
/// - true = ok
/// - false = not ok
/// - neverthrow
final Future<bool> Function(Token token) checkToken;
/// fetch a new token
/// - null = require authenticate
/// - not null = ok