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

dynadux-history-middleware

v1.0.8

Published

The dynadux-history-middleware

Downloads

17

Readme

Dynadux Logo

dynadux history middleware

Travel in time with dynadux.

This middleware offers the Undo/Redo feature.

It offers a history limit so it won't explode your memory.

Supports restore points so you can travel in time with named restore points.

Create a store

Let's create a store with dynadux.

Our store exposes the history with a few methods where dispatching events to the dynaduxHistoryMiddleware

If you are unfamiliar with dynadux learn it here.

const actions = {
  CLEAR: 'CLEAR',
  ADD_TODO: 'ADD_TODO',
  REMOVE_TODO: 'REMOVE_TODO',
};

const createTodoAppStore = () => {
  const store = createStore<ITodoAppState>({
    initialState: {
      todos: [],
    },
    middlewares: [
      dynaduxHistoryMiddleware(),
    ],
    reducers: {
      [actions.CLEAR]: () => {
        return {
          todos: [],
        };
      },
      [actions.ADD_TODO]: ({state: {todos}, payload}) => {
        return {
          todos: todos.concat(payload),
        };
      },
      [actions.REMOVE_TODO]: ({state: {todos}, payload: todoId}) => {
        return {
          todos: todos.filter(todo => todo.id !== todoId),
        };
      },
    },
  });

  return {
    get state() { return store.state; },
    clear: () => store.dispatch(actions.CLEAR),
    addTodo: (todo: ITodo) => store.dispatch(actions.ADD_TODO, todo),
    removeTodo: (todoId: string) => store.dispatch(actions.REMOVE_TODO, todoId),
    history: {
      prev: () => store.dispatch(EDynaduxHistoryMiddlewareActions.PREV),
      next: () => store.dispatch(EDynaduxHistoryMiddlewareActions.NEXT),
      setRestorePoint: (name: string) => store.dispatch(EDynaduxHistoryMiddlewareActions.SET_RESTORE_POINT, {name}),
      activateRestorePoint: (name: string) => store.dispatch(EDynaduxHistoryMiddlewareActions.ACTIVATE_RESTORE_POINT, {name}),
    },
  };
};

// create the store
const appStore = createTodoAppStore();

To get the todos at any time you evaluate appStore.state.todos.

History limit

The middleware accepts an object as an argument with the historySize property.

This property indicates how many history items will be available to travel in the past.

    middlewares: [
      dynaduxHistoryMiddleware({historySize: 100}),
    ],

If you don't define it, history is unlimited.

Travel in time

You can travel in time dispatching actions EDynaduxHistoryMiddlewareActions.PREV/NEXT.

Our store above is smart and we can use the appStore.history.prev/next() accordingly.

// Clear the store
appStore.clear();


// Add a couple of todos
appStore.addTodo({id: '301', label: 'Before work beers', done: false});
appStore.addTodo({id: '302', label: 'After work beers', done: false});
appStore.addTodo({id: '303', label: 'Evening beers', done: false});
// The todos now are: 301, 302, 303

// Let's go back in time
appStore.history.prev();
// The todos now are: 301, 302

// Let's go forward
appStore.history.next();
// The todos now are: 301, 302, 303

// Try to go forward
appStore.history.next();
// The todos still are: 301, 302, 303

Restore points

At any time you can set a restore point. To do that you call appStore.history.setRestorePoint('nice').

Nothing is changed in the state.

Later you can call the setRestorePoint with the same name and this will override the previous state.

After a time, when you want to go back, you can call appStore.history.activateRestorePoint('nice').

Restore points feature is extremely useful for apps with progress steps, editor apps, game apps, etc.

Restore points are introduced for the first time by this dynadux middleware. This was possible with the flexibility that dynadux offers.

Restore points feature doesn't duplicate data but it uses indexes. So adding restore points don't increase the used memory.


// Clear the store
appStore.clear();

// Add a couple of todos
appStore.addTodo({id: '301', label: 'Before work beers', done: false});
appStore.addTodo({id: '302', label: 'After work beers', done: false});
// The todos now are: 301, 302

// Create a restore point with name "basics"
appStore.history.setRestorePoint('basics');

// Add a todo
appStore.addTodo({id: '303', label: 'Evening beers', done: false});
// Now the todos are: 301, 302, 303

// Create a restore point with the name evening
appStore.history.setRestorePoint('evening');

// Activate the restore point basics
appStore.history.activateRestorePoint('basics');
// The todos now are: 301, 302, as it was at the set store point previously

// Activate the restore point evening
appStore.history.activateRestorePoint('evening');
// Now the todos are: 301, 302, 303


// Let's go back in time, make a change and try to activate a future restore point

// Go back in time to basics
appStore.history.activateRestorePoint('basics');
// Add a new todo
appStore.addTodo({id: '304', label: 'Sleep', done: false});    // This add deleted the future evening tag
// The todos now are 301, 302, 304
// Try to go to evening
appStore.history.activateRestorePoint('evening');
// The todos still are 301, 302, 304
// A console.error is occured
    

API

Middleware support the below actions

EDynaduxHistoryMiddlewareActions.PREV

Payload: none

It goes on dispatch back in time,

EDynaduxHistoryMiddlewareActions.NEXT

Payload: none

It goes on dispatch forward in time,

EDynaduxHistoryMiddlewareActions.SET_RESTORE_POINT

Payload: {name: string}

It creates a restore point with a name.

If the name is already used it overrides the last one.

EDynaduxHistoryMiddlewareActions.ACTIVATE_RESTORE_POINT

Payload: {name: string}

Activates a restore point.

If the restore point doesn't exist a console.error will be occured but not an exception.

EDynaduxHistoryMiddlewareActions.GET_HISTORY

Payload: {stateTargetPropertyName: string}

This will create a property in the state of the store, with the name, the value of the payload's stateTargetPropertyName property.

In the state it will save the collected history items.