Skip to content

Instantly share code, notes, and snippets.

Avatar

Cole Lawrence colelawrence

View GitHub Profile
@colelawrence
colelawrence / tracked_unique_plugin.rs
Last active Sep 9, 2020
`shipyard_app` plugin for adding a "tracked" unique which is reset across updates.
View tracked_unique_plugin.rs
//! 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>>;
@colelawrence
colelawrence / tracked.rs
Created Sep 9, 2020
Change `Tracked` container for Rust to be used with Shipyard
View tracked.rs
use std::{fmt, mem, ops::Deref};
pub struct Tracked<T: ?Sized>(InnerTrackedState, T);
#[derive(PartialEq)]
enum InnerTrackedState {
New,
Modified,
NoChanges,
}
@colelawrence
colelawrence / install-hooks.sh
Created Jul 14, 2020
pre-commit configs which basically does what checks both rust and typescript files hmm...
View install-hooks.sh
git config core.hooksPath .githooks/
chmod +x .githooks/pre-commit
@colelawrence
colelawrence / wasm_tracing_layer.rs
Last active Jul 10, 2020
Implementing tracing Layer for use with browser performance
View wasm_tracing_layer.rs
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::*;
View promise-once-lazy.ts
/** 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 / diffSets.js
Last active May 25, 2020
Determine the difference between two iterables, returns {toAdd, toRemove}
View diffSets.js
/**
* @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) {
View prosemirror-changeset.d.ts
/**
* # 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) */
View get-cookie.ts
View .logger.ts
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;
View .define-i18n.ts
type LanguageKey = keyof Definitions<any>
let currentLanguage: LanguageKey = "en"
/**
* @param namespace most underlying i18n solutions like to have a namespace
*/
function define<T>(namespace: string, definitions: Definitions<T>): Translate<T> {
// ... set up with your existing translation library which supports fallbacks and such...
return key => `Key (${key}) has no definition`
You can’t perform that action at this time.