Code Monkey home page Code Monkey logo

immutable-undo's Introduction

Immutable Undo

Simple data structure for and undo/redo stack, using ImmutableJS. The structure holds a stack of undo states, and a stack of redo states. It does not keep a reference to the current state to avoid duplicating data if your state is already stored in another structure.

Installation

npm install --save immutable-undo

Reference

Static

History.create

  • {Object} [opts] Option object
  • {Number} [opts.maxUndos=500] Maximum number of undos stored. Beyond that, some undos are dropped, according to the specified drop strategy.
  • { (History) => History } [opts.strategy=History.lru] The drop strategy. See strategies.
  • return {History} An empty history
var history = History.create({
    maxUndos: 100
});

Properties

canUndo

  • return {Boolean} True if the history has undos

canRedo

  • return {Boolean} True if the history has redos

previous

  • return {State | Undefined} The most recent state in the undo stack

next

  • return {State | Undefined} The most recent state in the redo stack

Methods

push

  • {State} newState The state to push on top of the undo stack
  • return {History}

Pushes a new state on the undo stack, and clears the redo stack

undo

  • {State} current The current state, that will be pushed on the redo stack
  • return {History} The history with the undo stack popped once, and the current state added to redos.

redo

  • {State} current The current state, that will be pushed on the undo stack
  • return {History} The history with the redo stack popped once, and the current state added to redos

Strategies

These are built-in strategies for keeping the undo stack under its size limit.

History.lru

Least Recently Used. Simply drops the oldest undo to make room for new one.

History.smooth

Will always keep the initial state (the first undo), and will drop undos such as the oldest undos grow more and more spaced in time, but the more recent undos keep a high precision.

You can imagine the density of undos over time becoming more like this curve:

curve

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.