Skip to content

Instantly share code, notes, and snippets.

const toString = str => (typeof str === 'function' ? str.name : String(str));
export function getLogger(...names: (string | Function)[]): Logger {
return createLogger(names.map(toString));
}
interface Logger {
info(message: string, ...args: any[]): void;
debug(message: string, ...args: any[]): void;
warn(message: string, ...args: any[]): void;
/**
* # prosemirror-changeset
* This is a helper module that can turn a sequence of document changes into a set of insertions and deletions, for example to display them in a change-tracking interface. Such a set can be built up incrementally, in order to do such change tracking in a halfway performant way during live editing.
* This code is licensed under an MIT licence.
* ## Programming interface
* Insertions and deletions are represented as ‘spans’—ranges in the document. The deleted spans refer to the original document, whereas the inserted ones point into the current document.
* It is possible to associate arbitrary data values with such spans, for example to track the user that made the change, the timestamp at which it was made, or the step data necessary to invert it again.
*
* Based on last [commit made Mar 12, 2019](https://github.com/ProseMirror/prosemirror-changeset/tree/11cb7b1fc05357c0bac5c444eab48748e4c8aa3b) */
@colelawrence
colelawrence / diffSets.js
Last active May 25, 2020 22:20
Determine the difference between two iterables, returns {toAdd, toRemove}
/**
* @template T
* @param {Iterable<T>} original
* @param {Iterable<T>} updated
*/
function diffSets(original, updated) {
const toRemove = new Set(original);
const toAdd = new Set();
// compare which items are new to us
for (const id of updated) {
/**
* Check the equality of anything
*
* **Example return**
*
* { "permissions.foo": [{}, { "a": 1 }] }
*
* @param {any} obj1
* @param {any} obj2
/** Lazy promise fetch will reuse promise if one was already created */
export function lazy<F extends () => Promise<any>>(f: F): F {
let once: ReturnType<F> | undefined
// @ts-ignore
return function () {
// @ts-ignore
if (once === undefined) once = f.apply(this, arguments)
return once
}
}
@colelawrence
colelawrence / wasm_tracing_layer.rs
Last active July 10, 2020 18:31
Implementing tracing Layer for use with browser performance
use std::fmt::{self, Write};
use std::sync::atomic::AtomicUsize;
use tracing;
use tracing::field::{Field, Visit};
use tracing::Subscriber;
use tracing_subscriber;
use tracing_subscriber::layer::*;
use tracing_subscriber::registry::*;
use wasm_bindgen::prelude::*;
@colelawrence
colelawrence / install-hooks.sh
Created July 14, 2020 20:46
pre-commit configs which basically does what checks both rust and typescript files hmm...
git config core.hooksPath .githooks/
chmod +x .githooks/pre-commit
@colelawrence
colelawrence / tracked.rs
Created September 9, 2020 13:25
Change `Tracked` container for Rust to be used with Shipyard
use std::{fmt, mem, ops::Deref};
pub struct Tracked<T: ?Sized>(InnerTrackedState, T);
#[derive(PartialEq)]
enum InnerTrackedState {
New,
Modified,
NoChanges,
}
@colelawrence
colelawrence / tracked_unique_plugin.rs
Last active September 9, 2020 14:10
`shipyard_app` plugin for adding a "tracked" unique which is reset across updates.
//! A small change tracker to wrap around a unique so you can determine if the value changes between updates.
use crate::prelude::*;
use std::{fmt, ops::Deref, ops::DerefMut};
pub struct TrackedUnique<T: ?Sized>(InnerTrackedState, T);
pub type TrackedUniqueView<'a, T> = UniqueView<'a, TrackedUnique<T>>;
pub type TrackedUniqueViewMut<'a, T> = UniqueViewMut<'a, TrackedUnique<T>>;