Skip to content

Instantly share code, notes, and snippets.

View InfiniteXyy's full-sized avatar
🐿️
Learn to be a indie game developer

InfiniteXyy InfiniteXyy

🐿️
Learn to be a indie game developer
View GitHub Profile
@InfiniteXyy
InfiniteXyy / read-stream.js
Created September 11, 2023 03:24
Read a stream with async generator
async function* readAllChunks(stream: ReadableStream<Uint8Array>) {
const reader = stream.getReader();
const decoder = new TextDecoder("utf-8");
while (true) {
const { value, done } = await reader.read();
yield decoder.decode(value);
if (done) return;
}
}
@InfiniteXyy
InfiniteXyy / Program.cs
Created July 14, 2023 09:53
Write C# in a JS like way
console.log("this is a simple log", 312);
console.info("what is info");
console.info(_.reverse(_.array(1, 2, 3)), _.array("Hello", "World"));
console.error(_.set("1", "2", "2"));
@InfiniteXyy
InfiniteXyy / .editorconfig
Last active April 18, 2024 05:38
C# personal editorconfig
root = true
[*]
indent_style = space
indent_size = 2
[*.cs]
csharp_new_line_before_open_brace = none
csharp_new_line_before_else = false
csharp_new_line_before_catch = false
@InfiniteXyy
InfiniteXyy / use.tsx
Created October 18, 2022 02:36
decouple use, and state, memo
function use<
T extends { type: 'state'; value: unknown } | { type: 'memo'; fn: unknown },
>(
parameters: T,
): T extends { type: 'state'; value: infer V }
? [V, React.SetStateAction<V>]
: T extends { type: 'memo'; fn: () => infer R }
? R
: never {
const { type } = parameters;
@InfiniteXyy
InfiniteXyy / hooks-in-class-component.tsx
Created September 9, 2022 09:55
Hooks in React Class Component
import React, { useState } from "react";
function defineHooks<T extends unknown>(hooks: () => T) {
type Params = ReturnType<typeof hooks>;
return (props: { children: (params: Params) => JSX.Element }) => {
const { children } = props;
return children(hooks());
};
}
@InfiniteXyy
InfiniteXyy / FrontendFrameworkIdea.js
Created July 28, 2022 07:58
Frontend Framework Idea
let context = { components: [] };
function element(type, props, children) {
return React.createElement(
type,
props,
typeof children === 'function' ? children() : children,
);
}
const _ = new Proxy(
@InfiniteXyy
InfiniteXyy / defineGlobalComponent.tsx
Last active April 15, 2022 17:39
use global stateful component everywhere
import {
Dispatch,
memo,
SetStateAction,
useEffect,
ComponentType,
useMemo,
useState,
useCallback,
} from "react";
@InfiniteXyy
InfiniteXyy / powertoy.json
Last active December 6, 2023 16:36
Mac keyboard adaptive Powertoy setting
{"remapKeys":{"inProcess":[{"originalKeys":"91","newRemapKeys":"162"},{"originalKeys":"164","newRemapKeys":"91"},{"originalKeys":"162","newRemapKeys":"164"}]},"remapShortcuts":{"global":[{"originalKeys":"162;32","newRemapKeys":"162;164;32"},{"originalKeys":"91;8","newRemapKeys":"163;8"},{"originalKeys":"91;37","newRemapKeys":"163;37"},{"originalKeys":"91;39","newRemapKeys":"163;39"},{"originalKeys":"91;160;37","newRemapKeys":"162;160;37"},{"originalKeys":"91;160;39","newRemapKeys":"162;160;39"},{"originalKeys":"91;162;37","newRemapKeys":"164;37"},{"originalKeys":"91;162;39","newRemapKeys":"164;39"}],"appSpecific":[{"originalKeys":"164;32","newRemapKeys":"162;32","targetApp":"code"},{"originalKeys":"162;8","newRemapKeys":"162;89","targetApp":"code"},{"originalKeys":"91;13","newRemapKeys":"164;13","targetApp":"code"},{"originalKeys":"91;162;76","newRemapKeys":"162;164;76","targetApp":"code"}]}}
@InfiniteXyy
InfiniteXyy / define-ayanami.tsx
Created June 18, 2021 08:28
use factory api for ayanami
import { Injectable } from '@asuka/di'
import { Ayanami, ImmerReducer, useAyanami, Effect, EffectAction } from 'ayanami'
import type { Draft } from 'immer'
import React from 'react'
import { Observable } from 'rxjs'
import { map, delay } from 'rxjs/operators'
type ActionBuilder<State extends Record<string, any>> = Record<string, (draft: Draft<State>, arg: any) => void>
type EffectBuilder<State extends Record<string, any>, Actions extends Record<string, (...args: any) => void>> = Record<
const whyChangeRef = React.useRef({})
React.useEffect(() => {
console.log('why am i rendered?')
Object.keys(props).forEach((key) => {
const before = whyChangeRef.current[key]
const current = props[key]
if (before !== current) {
console.log(key + ' changed', [before, current])