This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * TIL that you can use try {} finally { cancelation() } | |
| * inside a generator function and call `.return()` on the generator | |
| * to run the cancelation of all generators currently running in the call stack! | |
| * | |
| * It means we can implement an Observable class with cancelation support | |
| * all with only generators: | |
| */ | |
| class Observable<T> { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Why? | |
| * - With LLMs we often need to parse partial JSON strings incrementally, | |
| * as they are being generated. | |
| * - open-source Partial JSON parser all have problems: | |
| * - They are ineficient: re-parsing the full JSON on each update | |
| * - They don't stream updates of string values. | |
| * - They don't guarantee that a given position in a JSON will remain of the same type. | |
| * | |
| * What is this? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React from "react"; | |
| /** | |
| * Hook to run effects related to a dom element. | |
| * Unlike useEffect, this hooks returns a ref callback. | |
| * The effect will be run every time the element changes. | |
| */ | |
| const useElementEffect = <T extends HTMLElement>( | |
| callback: (el: T) => (() => void) | undefined | void, | |
| deps: React.DependencyList, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Moveable tree. | |
| * | |
| * This implementation uses fractional indexing for children positions | |
| * and a parent id on the children for descendance. | |
| * | |
| * Pros: | |
| * - move operations are last-writer-wins. | |
| * - move operations can't produce duplicates. | |
| * - Can be built on top of existing last-writer-wins map crdts. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "math/rand" | |
| "time" | |
| ) | |
| func interval() chan float64 { | |
| channel := make(chan float64) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type TextMarks = | |
| | { type: "bold" } | |
| | { type: "italic" } | |
| | { type: "code" } | |
| | { type: "strike" } | |
| | { type: "textStyle"; attrs: Partial<CSSStyleDeclaration> } | |
| | { | |
| type: "link"; | |
| attrs: { | |
| href: string; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { createLiveState, crdtSchema } from "@/lib/live-state"; | |
| export type NotebookJSON = { | |
| title: string; | |
| description: string; | |
| cells: TextCell[]; | |
| }; | |
| const notebookCRDTSchema = { | |
| title: crdtSchema.LiveText(), |
NewerOlder