Skip to content

Instantly share code, notes, and snippets.

View gvergnaud's full-sized avatar
🚀

Gabriel Vergnaud gvergnaud

🚀
View GitHub Profile
@gvergnaud
gvergnaud / 0-yjs-undo-revert-trick.ts
Last active August 14, 2024 13:37
Exploration to implement reverts using Yjs.
import * as Y from 'yjs';
/**
* In the current version of Yjs (13.x), there is a no
* builtin way to create and apply a revert to undo an
* arbitrary change.
*
* There are tricks to do it though, and they all revolve
* around the idea of using an `UndoManager` to undo a change,
* and reading the `Uint8Array` update that has been applied by undoing.
class EventEmitter<T extends object> {
listeners: { [K in keyof T]?: Set<(data: T[K]) => void> } = {};
on<K extends keyof T>(name: K, cb: (data: T[K]) => void) {
const listeners = this.listeners[name] ?? new Set();
listeners.add(cb);
this.listeners[name] = listeners;
return () => listeners.delete(cb);
}
emit<K extends keyof T>(name: K, data: T[K]) {
const listeners = this.listeners[name] ?? new Set();
package main
import (
"fmt"
"math/rand"
"time"
)
func interval() chan float64 {
channel := make(chan float64)
import { EditorState, Step, Transaction } from 'prosemirror-model';
type TransactionCreator = (state: EditorState) => Transaction;
const rebaseSteps = (stepsToRebase: Step[], committedSteps: Step[]): Step[] => {
if (!committedSteps.length) {
return stepsToRebase;
}
const committedStepsMaps = committedSteps.map((s) => s.getMap());
@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;
import { createLiveState, crdtSchema } from "@/lib/live-state";
export type NotebookJSON = {
title: string;
description: string;
cells: TextCell[];
};
const notebookCRDTSchema = {
title: crdtSchema.LiveText(),
@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 = () => {
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;
import { arg0, Call, Constant, Eval, Fn, Objects, Tuples } from "../src/index";
type Ok<A> = { tag: "Ok"; value: A };
type Err<B> = { tag: "Err"; value: B };
type Result<A, B> = Ok<A> | Err<B>;
type ParseError<expected = unknown, encountered = unknown> = {
expected: expected;