npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mobx-shallow-undo

v1.1.0

Published

Drop-in undo & redo for shallow changes in Mobx

Downloads

265

Readme

Mobx Shallow Undo Build Status Available on NPM

Part of HTTP Toolkit: powerful tools for building, testing & debugging HTTP(S)

Drop-in undo & redo for shallow changes in Mobx

Mobx-shallow-undo is a tiny zero-dependency library for shallow undo/redo on any mobx observable value.

If you have some changing but internally immutable state anywhere in your app - from core state in your app-wide store down to individual observable fields on your React components - this lets you immediately add undo/redo to that state, and freely flip back and forth through its whole history, with zero hassle or configuration required.

For example, HTTP Toolkit uses this to support undo/redo in an autocompleting tagged input field, where undo behaviour needs to manage some of the state in the component, not just the text in the input field itself.

This is designed for simple cases, where a single observable value (or a single property of an observable object) is changing between different immutable values. This does not handle mutations inside the value or track undo states recursively, it just tracks the shallow undo/redo state of an observable (if you want to add undo/redo in any state anywhere though immutability is strongly recommended, or you're going to have a very bad time!)

TL;DR: Mobx-shallow-undo lets you do this:

import * as mobx from 'mobx';
import { trackUndo } from 'mobx-shallow-undo';

const myObservable = mobx.observable({ a: 1 });

const myUndoer = trackUndo(
    // Getter, this will be observed:
    () => myObservable.a,
    // Setter, to reset the value on undo/redo:
    (value) => { myObservable.a = value }
);

myObservable.a = 2;
myObservable.a = 3;
myObservable.a = 1000;

myUndoer.undo();
myUndoer.undo();

// myObservable = { a: 2 };

myUndoer.redo();
// myObservable = { a: 3 };

Getting Started

npm install mobx-shallow-undo
import { trackUndo } from 'mobx-shallow-undo';

const undoer = trackUndo(
    () => /* Read the undoable observable */,
    (value) => { /* Update the undoable observable */ }
);

undoer.undo(); // Undo the last change, if possible
undoer.redo(); // Redo the last undo, if possible

API

trackUndo(getter, setter)

Creates an undoer. getter and setter must be synchronous functions to get and set an observable value.

The getter takes no arguments, whilst the setter takes one argument: the new value.

This returns an undoer.

undoer.undo()

Undoes the latest change. If the undo stack is empty this does nothing.

undoer.redo()

Redoes the latest undo. If we're already on the latest change this does nothing.

Changes to the observed value after an undo clear the redo stack, just like undo in every other application.

undoer.dispose()

Stops observing the observable and throws away all historical data.

Any future calls to undo() or redo() will throw an error.