Skip to content

Instantly share code, notes, and snippets.

View gvergnaud's full-sized avatar
🚀

Gabriel Vergnaud gvergnaud

🚀
View GitHub Profile
@gvergnaud
gvergnaud / PromiseQueue.ts
Last active March 31, 2024 14:19
A simple implementation of a promise queue with concurrency
export class PromiseQueue<T> {
readonly concurrency: number;
#ongoingPromisesCount: number;
#toRun: Array<() => Promise<T>>;
constructor({ concurrency = 1 } = {}) {
this.concurrency = concurrency;
this.#ongoingPromisesCount = 0;
this.#toRun = [];
}
@gvergnaud
gvergnaud / 0_types.ts
Created February 27, 2024 23:30
prose-mirror-json-schema
type TextMarks =
| { type: "bold" }
| { type: "italic" }
| { type: "code" }
| { type: "strike" }
| { type: "textStyle"; attrs: Partial<CSSStyleDeclaration> }
| {
type: "link";
attrs: {
href: string;
@gvergnaud
gvergnaud / Promises-under-the-hood.md
Last active December 24, 2023 18:35
Promises, under the hood.

Promises, under the hood

You all know that to create a new Promise you need to define it this way:

  new Promise((resolve, reject) => {
    ...
    resolve(someValue)
  })

You are passing a callback that defines the specific behavior of your promise.

export type TaskPriority = "user-blocking" | "user-visible" | "background";
export type SchedulerPostTaskOptions = {
// This priority is immutable and overrides the
// TaskController's priority.
priority?: TaskPriority;
delay?: number;
signal?: TaskSignal;
};
import { createLiveState, crdtSchema } from "@/lib/live-state";
export type NotebookJSON = {
title: string;
description: string;
cells: TextCell[];
};
const notebookCRDTSchema = {
title: crdtSchema.LiveText(),
import mapValues from 'lodash/mapValues';
export type Step<Anims> = {
[K in keyof Anims]: { value: number; progress: number };
};
export type Values<Anims> = { [K in keyof Anims]: number };
export interface SpringOptions {
/**
@gvergnaud
gvergnaud / 1-raf-throttle.ts
Last active August 9, 2023 19:36
raf-throttle.ts
type RafState<Args> =
| { type: 'waiting'; latestArgs: Args; rafId: number }
| { type: 'idle' };
export const animationFrameThrottle = <Args extends any[]>(
callback: (...args: Args) => unknown,
): ((...args: Args) => void) & { cancel: () => void } => {
let state: RafState<Args> = { type: 'idle' };
const cancel = () => {
@gvergnaud
gvergnaud / lazy-list.js
Last active July 31, 2023 23:57
Lazy List, implemented with es6 generators
/* ----------------------------------------- *
Lazy List Implementation
* ----------------------------------------- */
// Haskell-like infinite List, implemented with es6 generators
// Lazyness lets you do crazy stuff like
List.range(0, Infinity)
.drop(1000)
.map(n => -n)
export function usePrevious<T>(value: T): T | undefined {
const ref = React.useRef<T>();
React.useEffect(() => {
ref.current = value;
});
return ref.current;
}
useEffect(() => {

})

<>
  <Child log1 />
  {console.log("log3")}
  <Child log2 />
&gt;