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

@joe_kerr/undojs

v0.2.0

Published

Everything should be undoable!

Downloads

35

Readme

UndoJs (BETA)

Accidentally deleted all of it instead of just the bit you wanted? On our deskops practically everything has an undo function. And yet, when on the web how come users almost always need to figure out themselves how to undo stuff they accidentally did?

Everything should be undoable!

UndoJs seeks to facilitate the everything-undoable principle. It acts as a wrapper for existing functions.

Features

  • Undo any function call
  • Just wrap existing functions
  • Commands can be executed and undone in batches
  • Optional caching

Overview

UndoJs took away the idea of the command pattern that functions can be put aside and called or undone later. With a controller you decide what command to be called, when to call it and when to undo it.

The design goals were to make the command logic independent from the functions that are wrapped and to be able to call commands like so:

undojs.execute("someCommand", [a, b]);

// <=>

undojs.undo();

Beta notice

Practice testing in async environments will be in focus of the beta. Interface changes (file names, return types) are likely.

Demo

See it in action.

Install

npm install @joe_kerr/undojs

Dev environment

import {Undojs} from "@joe_kerr/undojs"; 

Browser

<script src="path_to_node_modules/@joe_kerr/undojs/dist/undojs.browser.js"></script>
<script>
const undojs = new window.undojs.Undojs();
</scrip>

Usage

Register and use of command

// elsewhere

async serverPost(data) {...}

async serverGet(id) {...}


// commands setup

const undojs = new UndoJs();

const command = {
	name: "move",
	execute: serverPost
	
	async undo(params, returned, cached) {
		const id = params[0];
		const orgRes = await returned;
		const cache = await cached;
		cache.id = id;
		
		return (orgRes.err === null) ? serverPost(cache) : null;
	},
	
	async cache(params) {
		const id = params[0];
		const cache = await serverGet(id);
		return {x: cache.x, y: cache.y};
	}
};

undojs.register(command);

// move element 1 to location 120, 60
undojs.execute("move", {id: 1, x: 120, y: 60});

// move elements 2 and 3 in one operation
undojs.recordBatch([
	{name: "move", params: {id: 2, x: 240, y: 120}},
	{name: "move", params: {id: 3, x: 480, y: 180}}
]);

// move back elements 3 and 2 to their previous position in one operation
undojs.undo();

// move back element 1
undojs.undo();

Caching

If you want to undo e.g. an addition operation (add(x)), you simply call add() again with minus the original parameter. What if you want to undo a style change of an element? You cannot really derive that information from known data. It is very helpful to have a caching function that provides an input to the undo function.

However, there is a trade-off. You must cache before you change state. So the cache function must be forced to run before the execute function. If caching involves fetching data from a server, this can slow down the command.

Versions

0.2.0

  • Add: Reset and destroy for app restart / clean up
  • Chg: Minor cleanups, fixes, changes

0.1.0

  • Public beta release.

Copyright

MIT (c) Joe Kerr 2019