Skip to content

Instantly share code, notes, and snippets.

@buhichan
buhichan / infinite-scroll-ag-grid.tsx
Created July 12, 2017 08:20
Create a redux-driven server side pagination, filtering and sorting data grid with the power of ag-grid and my component ag-grid-material-preset
/**
* Created by buhi on 2017/7/11.
*/
import * as React from "react"
import {Grid,GridFieldSchema,GridProps} from "ag-grid-material-preset";
import {IGetRowsParams} from "ag-grid/dist/lib/rowModels/iDatasource";
import {GridApi} from "ag-grid/dist/lib/gridApi";
import {List,Repeat} from "immutable";
type InfiniteScrollGridProps = {
(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 08:31
deep clone when there's circlular reference
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 April 15, 2021 07:58
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 February 5, 2021 07:22
suspendable use-observables
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 December 30, 2020 09:04
model (rxjs) based form
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 November 30, 2020 11:31
An attempt to implement antlr-like functionality in typescript.
const EOF = Symbol("EOF")
type TokenStream = {
token: Token
start: number
end: number
}[]
type Token = {
kind: "token"
@buhichan
buhichan / obuservable.ts
Last active November 13, 2020 02:56
简化的observable, 帮助理解observable.
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 October 21, 2020 12:07
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 October 19, 2020 11:11
A typescript DI solution with no need of reflect-metadata (and can also need no decorators, if you prefer it)
//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
}