Skip to content

Instantly share code, notes, and snippets.

View hotreload.js
(function (){
const moduleDef = {
"./a.tsx": {
code: function (module, exports, require){
module.exports = 1
},
parent: ["./b.tsx"]
},
"./b.tsx": {
@buhichan
buhichan / base.ts
Created May 20, 2021
deep clone when there's circlular reference
View base.ts
class BaseModel {
private deepCloneImpl(oldObject: object, intrinsicTypes: string[], clonedMap: WeakMap<object, unknown>){
if (!oldObject || typeof oldObject !== "object") return oldObject;
if(clonedMap.has(oldObject)){
return clonedMap.get(oldObject)
}
@buhichan
buhichan / rxjs-materialize-response.tsx
Last active Apr 15, 2021
rxjs-materialize-response.tsx
View rxjs-materialize-response.tsx
import { Observable, SubscribableOrPromise, OperatorFunction } from "rxjs"
import { switchMap, tap } from "rxjs/operators"
export type MaterializedResponseState<T> = { loading: true, error: null, value: null | T } | { loading: false, error: Error } | { loading:false, value: T }
export function materializeResponse<T1, T2>(performRequest: (t1: T1) => SubscribableOrPromise<T2>): OperatorFunction<T1, MaterializedResponseState<T2>> {
return (observer: Observable<T1>) => {
let lastResponse = null as null | T2
return new Observable<MaterializedResponseState<T2>>(subscriber => {
return observer
@buhichan
buhichan / suspendable-use-observables.ts
Last active Feb 5, 2021
suspendable use-observables
View suspendable-use-observables.ts
type ObservedValueOfWithoutDefaultValue<T> = T extends Observable<infer U2> ? U2 : T extends Promise<infer U1> ? U1 : never
export type ObservedValueTupleFromArrayWithoutDefaultValue<X> = X extends readonly (Observable<unknown> | Promise<unknown> | null | undefined)[]
? { [K in keyof X]: ObservedValueOfWithoutDefaultValue<X[K]> }
: never
const mapValue = new WeakMap<
object,
{
@buhichan
buhichan / bubu-form-antd.tsx
Last active Dec 30, 2020
model (rxjs) based form
View bubu-form-antd.tsx
import { Form, Col, Row, Button } from "antd"
import { FormItemProps } from "antd/lib/form"
import { useObservables } from "./use-observables"
import * as React from "react"
import { AbstractControl, ValidationInfo, FormControls, FormControlList } from "./model"
import { CloseOutlined, PlusOutlined, MinusOutlined } from "@ant-design/icons"
type FormItemRenderChildren<T, Meta> = (inputProps: { value?: T; onChange?: (v: T) => void }, behavior: Meta | null, err: ValidationInfo) => React.ReactNode
export function FormItem<T, Meta>({
@buhichan
buhichan / parser.ts
Created Nov 30, 2020
An attempt to implement antlr-like functionality in typescript.
View parser.ts
const EOF = Symbol("EOF")
type TokenStream = {
token: Token
start: number
end: number
}[]
type Token = {
kind: "token"
@buhichan
buhichan / obuservable.ts
Last active Nov 13, 2020
简化的observable, 帮助理解observable.
View obuservable.ts
type Unsubscribe = () => void
const NOOP: Unsubscribe = () => {}
interface Observer<T> {
(t: T): void
}
interface Observable<T> {
(ob: Observer<T>): Unsubscribe
@buhichan
buhichan / babylonjs-builder-pattern.ts
Last active Oct 21, 2020
babylonjs-builder-pattern.ts
View babylonjs-builder-pattern.ts
import { Animation, CubicEase, EasingFunction, Mesh, MeshBuilder, MultiMaterial, Scene, Vector3, ActionManager, InterpolateValueAction } from "@babylonjs/core"
import { RACK_HEIGHT, RACK_WIDTH, UNIT_HEIGHT } from "config/canvas-constants"
import { genMultiMaterialMesh } from "utils/canvas-utils"
export class DoorBuilder {
constructor(private scene: Scene, material: MultiMaterial) {
const doorCube = MeshBuilder.CreateBox("door", { width: RACK_WIDTH, height: RACK_HEIGHT, depth: UNIT_HEIGHT })
genMultiMaterialMesh(doorCube, material)
@buhichan
buhichan / typescript-di.tsx
Last active Oct 19, 2020
A typescript DI solution with no need of reflect-metadata (and can also need no decorators, if you prefer it)
View typescript-di.tsx
//Idea is borrowed from https://github.com/gnaeus/react-ioc
//Differences are:
//1. Does not require reflect-metadata
//2. Has an additional "useProvider" method
import * as React from "react"
export interface Disposable {
dispose?(): void
}
View use-observables.ts
import { BehaviorSubject, Observable, Subscription, SubscribableOrPromise, from } from "rxjs"
import { skip } from "rxjs/operators"
import * as React from "react"
import { useSubscription } from "use-subscription"
type ObservedValueOf<T> = T extends BehaviorSubject<infer U1> ? U1 : T extends Observable<infer U2> ? U2 | null : never
export function useObservables<T>(ob: T | undefined | null): [ObservedValueOf<T>]
export function useObservables<T1, T2>(ob1: T1 | undefined | null, ob2: T2 | undefined | null): [ObservedValueOf<T1>, ObservedValueOf<T2>]
export function useObservables<T1, T2, T3>(