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

object-time-travel

v0.0.5

Published

reset data to the previous

Downloads

8

Readme

object-time-travel

Installation

Using npm:

$ npm i --save object-time-travel

In Node.js:

const rollback = require('object-time-travel').default;
const recorder = new rollback()
let data = {
    id: 0,
    list: [],
    child: {
        name: 'child',
    }
};
let list = data.child;//

recorder.pipe(data);// link

//start changing
for (let i = 0; i < 10; i++) {
    data.id = i;
    data.list.push(i);
    data.child.name = 'child' + i;
    data.say = function () {
        console.log(i, 'oooooo')
    }
    recorder.commit('commit '+ i);// record snap shot
}

//rollback test
setTimeout(function () {
    recorder.reset('commit '+ 3);
    data.say();// 3 oooooo
    console.log(list);// { name: 'child3' }
    console.log(data);//{ id: 3, list: [ 0, 1, 2, 3 ], child: { name: 'child3' }, say: [Function (anonymous)] }
}, 500)

In Node.js:

Here is an example of Partial reduction ; you can only rollback several fields every time;

const rollback = require('object-time-travel').default;
const recorder = new rollback()
 let data = {
    id: 1,
    list: [],
    child: {
        name: 1,
        info: {
            txt: ''
        }
    }
}
recorder.pipe(data)
.watch('id', (value) => { //when your reset makes this field change,it runs;
      console.log('id=',value)
})
.watch('child.info.txt', (value) => { //when your reset makes this field change,it runs;
     console.log('child.info.txt=',value)
}) ; // link

//start changing
for (let i = 0; i < 10; i++) {
    data.id = i;
    data.list.push(i);
    data.child.name = 'child' + i;
    data.child.info.txt = 'txt=' + i;
    recorder.commit('commit ' + i); // record snap shot
}

 data.child={}
 recorder.commit('clear child'); // record snap shot

//first rollback [id,child] to 'commit 3'
recorder.reset('commit ' + 3, {
    paths: ['id', 'child'], //rollback these fields; it's invalid to array's index,such as 'list.0' will not work
    ignore: ['child.info'], //but ignore these fields;
});

console.log(data)

// result 
// {
//     id: 3, //commit 3
//     list:  [ 0, 1, 2, 3, 4, 5 ,6 ,7,8,9], // commit 9;because option.paths didn't allow;
//     child: { 
//          name: 'child3', //commit 3 ; here child.info is ignored and it has been deleted by commit 'clear child'
    // }
// }

recorder.reset('clear child'); 
console.log(data)
//  

// { 
//     id: 9, 
//     list: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
//     child: {}
// }

In Node.js:

Asynchronously update some fields while reseting;

const rollback = require('object-time-travel').default;
const recorder = new rollback()
let data = {
    id: 0,
    list: [],
    child: {
        name: 'child',
    }
};
let list = data.child;//

recorder.pipe(data);// link

//start changing
for (let i = 0; i < 10; i++) {
    data.id = i;
    data.list.push(i);
    data.child.name = 'child' + i;
    data.say = function () {
        console.log(i, 'oooooo')
    }
    recorder.commit('commit '+ i);// record snap shot
}

recorder.reset('commit 3', {
    async: {
        'list': (callback) => {
            setTimeout(() =>{
                callback([11, 22, 33])
                console.log(data);
                // {
                //     id: 3,
                //     list: [ 11, 22, 33 ], // this field is reseted by 'cb([11, 22, 33])'
                //     child: { name: 'child3', info: { txt: 'txt=3' } }
                // }
            },1000)
        }
    }
})

console.log(data);
// {
//     id: 3,
//     list: [ 0, 1, 2, 3 ],
//     child: { name: 'child3', info: { txt: 'txt=3' } }
// }

All suggestions and opinions are welcome.

QQ:454413790

Email: [email protected]