Skip to content

Instantly share code, notes, and snippets.

View vbfox's full-sized avatar
❄️
Winter is coming

Julien Roncaglia vbfox

❄️
Winter is coming
View GitHub Profile
@vbfox
vbfox / useSplitGrid.ts
Last active June 2, 2022 09:15
split-grid as react hooks
import { useRef, useEffect, useCallback } from 'react';
import Split, { SplitOptions, SplitInstance } from 'split-grid';
export function useSplitGrid(options: SplitOptions): SplitInstance | undefined {
const splitInstance = useRef<SplitInstance>();
useEffect(() => {
const split = Split(options);
splitInstance.current = split;
return () => split.destroy();
@vbfox
vbfox / runes.ts
Last active June 9, 2021 14:32
Javascript/Typescript Rune iterators
/**
* Iterate a string runes. Same as iterating [...str]
*
* Note: This assume that the string is well-formed
*
* WARNING: This is useless, use the string iterator.
*/
export function* runeIterator(s: string) {
const len = s.length;
let highSurrogate: number | undefined;
@vbfox
vbfox / useScrollStartEndEvents.ts
Created April 28, 2021 14:21
Simulate a 'scrollstart' and 'scrollend' event by listening to scroll and debouncing the calls
/**
* Simulate a 'scrollstart' and 'scrollend' event by listening to scroll and debouncing the calls.
*
* The proposal to standardize part of this is https://github.com/w3c/csswg-drafts/issues/3744
*/
export function useScrollStartEndEvents(
ref: RefObject<HTMLElement>,
onScrollStart?: () => void,
onScrollEnd?: () => void,
debounceTimeMs: number = 100
/* eslint-disable @typescript-eslint/no-unsafe-return */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
function deepReferentialEquality(from: any, to: any, path = ''): any {
if (to === undefined || from === undefined || to === null || from === null) {
return to;
}
const objectType = typeof from;
if (objectType !== typeof to) {
// The object changed type
@vbfox
vbfox / keyedPromiseQueue.ts
Created June 4, 2020 14:38
A queue of promises keyed by a value
import { Queue } from 'src/utils/queue';
import { assertDefined } from 'src/utils/asserts';
interface QueueItem {
readonly generator: () => PromiseLike<any>;
readonly resolve: (value: any) => void;
readonly reject: (error: any) => void;
}
interface PerKeyQueue<TKey> {
@vbfox
vbfox / modifyProxiedResponse.js
Created November 25, 2019 10:13
Small function for express.js http-proxy-middleware to change response text
/* eslint-disable @typescript-eslint/unbound-method */
/* eslint-disable no-param-reassign */
import zlib from 'zlib';
import concatStream from 'concat-stream';
import BufferHelper from 'bufferhelper';
import stream from 'stream';
import { IncomingMessage, ServerResponse } from 'http';
export type Modification = (original: string, proxyRes: IncomingMessage) => string | undefined;
@vbfox
vbfox / Immutable.ts
Created April 16, 2019 15:25
Simple type immutable.js Map based immutable type with strongly typed keys
import { Iterable, Map } from "immutable";
// tslint:disable:array-type
// tslint:disable:unified-signatures
export interface Immutable<T> {
set<TKey extends keyof T>(key: TKey, value: T[TKey]): Immutable<T>;
/**
* Returns a new Map which excludes this `key`.
*
@vbfox
vbfox / FableReactEquals.fs
Last active April 16, 2019 15:27
Prototype of Equals function for F# Fable React interop that does the same as the (=) operator except that all functions are equal
// Flawed try, I don't think a deep function ignoring compare is practical
//
// https://fable.io/repl/#?code=PYBwpgdgBAYghgIwDZgHQEkC2JgCcAuqAUgMoCwAUKJLIiqgMJ5pEDO6E+Yuol108ZGia4wlSgG0APFhwEAFACIwARwCucJAEFcuOAE9WAdQCW+ABaKANFEUAzOmAC0SEwj259AegCq+E0ioAFasigCUAHwAupQo+FAguCYAbnBcUKoa2roGxmbmUPIAHgBcUMAIQWGF+mUVVYUAxth1lVBOEeVtHVAIwMBIYWV9A1AAvJRQU1AhAHJpKWIUkjLYePhKjcDYcKIAImlgoTb2ji5uHt5+AcGhkTEUcQlJqelbO-uHrIWlXQ3ytT+QygJk440m0zmC2SSxWAFFMGYlAASAAM4zGUGRAEZwtFYmB4okUocZqw4epNN9ilBWv99LSgYyRkhxmT5v4YeIKNIEUjFGioABCTE4vEPJ7E15gdnAfAUrLUoqM+rVAEqyrVYb9VmYqGc2GPQnPEnpVhwTBgAAq+nAPzpasBqvBFGmsvllKQ3wZEDUSFZADIAxC3VN6qgAEpgOzcSCNMAKqnyEOht0AeUqYEahAA5oSAAo8fBy21gNN2YphAD8WwgrHwuDU2bwVhTqamGaCWdzBaLJfA5YB1dr9cbzdwYW5vMRG0U+FLwDsWNR4oJRJepJCNvAi8KAH0NQ0yqPQTm2frFlOpHzZ4LQfW4BB47uDlxV0b16aZSZWK+ZfIDwdZkdXPVgOUvZYPxBCBXAgGUpVJH8dD0BliiAiZXWmZCDFQJCcgZZUAB9CKgbD9AAITUOwY1wXDWAANRMMAAHcoCKblJQ3dJRC2XAABNCzlfsZQw9NM2zVA83wQTi3nAcKwAbxIzBdgAa24NkGzUGVCIAX0nSCnh4k1pRBVgo14vj7SZUTphMJcQkTL02KgX1-SgCxIDbaYHC9JY3TAPzvKmTih
@vbfox
vbfox / TraceChanges.ts
Created March 27, 2019 16:04
Trace props or state that changed and made a React PureComponent refresh
export class Test extends React.PureComponent<{}, {}> {
UNSAFE_componentWillUpdate(nextProps: any, nextState: any) {
const changedProps = _.reduce(
this.props,
(result: string[], value, key) => value === nextProps[key] ? result : result.concat(key),
[]);
console.log("changedProps: ", changedProps);
const changedStates = _.reduce(
this.state,
(result: string[], value, key) => value === nextState[key] ? result : result.concat(key),
@vbfox
vbfox / MemoReactHelpers.fs
Last active October 24, 2018 12:42
React 16.6 Fable 1
module MemoReactHelpers
/// A special ReactElement that tell React to not render anything
[<Emit("null")>]
let noElement = jsNative<ReactElement>
[<Erase>]
type ComponentClass<'props, 'state> = class end
[<RequireQualifiedAccess>]