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

redux-live-undo

v0.1.1

Published

A high-order reducer that enables generic undo/redo functionality without giving up live updates

Downloads

518

Readme

redux-live-undo

A generic, high-order reducer that allows you to transparently add undo/redo functionality on top of other redux reducers. redux-live-undo allows state to be updated as users type without creating undo steps for every state change.

CircleCI

Inspired by the omnidan's work on redux-undo, redux-live-undo provides a simpler interface with admittedly less options but more flexibility. Most importantly, redux-live-undo has the concept of "history checkpoints" that allow you to dispatch actions without making every change to the state a step in the undo history.

For example, this allows you live updating of UI state (such as in a text input) without creating an undo step for every single keypress.

redux-live-undo also allows you to track entire sections of your application state as a single history so undo/redo actions can be synced across various types of data.

Brief Overview

redux-live-undo tracks a history of state of its sub-reducers. The state returned by redux-live-undo has this shape:

  • present: the current state of the sub-reducers.
  • past: an array of past state checkpoints, ordered from oldest to newest.
  • future: an array of future state checkpoints, order from newest to oldest. Only populated after an undo or redo.

Note: New history checkpoints are only recorded when the action dispatched has the undoableHistoryCheckpoint property set to true and changeDetector returns true.

Reducers can optionally provide a changeDetector function to override what changes are allowed to be checkpointable.

Action flags

Flags on actions can affect the behavior of the undo history state:

  • undoableHistoryCheckpoint: Marks when the next state should be considered a checkpoint in the undo history
  • undoableIrreversibleCheckpoint: Marks when the next state should clear any undo history. Typically used if related state in another system (eg. a backend service) cannot be reversed.

Example Usage

Given a simple reducer that tracks the state of a string:

const TEXT_UPDATE = 'TEXT_UPDATE';

function StringReducer(state = '', action = {}) {
  switch (action.type) {
    case TEXT_UPDATE:
      return action.value;
    default:
      return state;
  }
}

Use redux-live-undo to add reducer functionality:

import Undoable from 'redux-live-undo';

const rootReducer = Undoable({
  string: StringReducer
});

When passed to combinedReducers, you will have access to present, past, and future states:

import { createStore } from 'redux';

const store = createStore(rootReducer);
store.getState();
// => {
//   past: [''],
//   present: {
//     string: ''
//   },
//   future: []
// }

Dispatch an update as an input changes:

store.dispatch({
  type: TEXT_UPDATE,
  value: 'H'
});

store.getState();
// => {
//   past: [''],
//   present: {
//     string: 'H'
//   },
//   future: []
// }

Dispatch an update with a checkpoint when you want a snapshot to jump back to, for example, when the input blurs:

store.dispatch({
  type: TEXT_UPDATE,
  value: 'Hi',
  undoableHistoryCheckpoint: true
});

store.getState();
// => {
//   past: ['', 'Hi'],
//   present: {
//     string: 'Hi'
//   },
//   future: []
// }

Undo to the last checkpoint:

import { UNDO } from 'redux-live-undo';

store.dispatch({
  type: UNDO
});

store.getState();
// => {
//   past: [''],
//   present: {
//     string: ''
//   },
//   future: ['Hi']
// }

Redo the action:

import { REDO } from 'redux-live-undo';

store.dispatch({
  type: REDO
});

store.getState();
// => {
//   past: ['', 'Hi'],
//   present: {
//     string: 'Hi'
//   },
//   future: []
// }

Credits

  • @joshdover
  • @tornstrom