473,387 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Closures for undo: bad idea?

I'm working on an application that does some DOM manipulation. I want
to add a mechanism for "Undo" so that the user can revert to the
previous state after performing a mistaken action. Simple idea, not
necessarily simple to implement.

My first idea for this is to use closures and an array that tracks the
inverse of the user's actions. Here's a simple example in which the
user can only remove DOM nodes and then undo the removal repeatedly
until they've reached their desired state (or the original state):

var App = {

undo: [],

removeNode: function(node)
{
//remove the node
var parent = node.parentNode;
var nextSibling = node.nextSibling;
parent.removeChild(node);

//inverse action goes into undo buffer
App.undo.push(function() {
if(nextSibling)
parent.insertBefore(node, nextSibling);
else
parent.appendChild(node);
});
},

undoLast: function()
{
if(App.undo.length)
(App.undo.pop())();
}
};

(Obviously the removeNode and undoLast functions are invoked from
elsewhere in response to user actions.)

My concern is with the closures created in the removeNode function.
This enables the undo to take place quite easily, but I am worried about
memory leakage.

Let's say the user undoes a few actions, and then performs a few more
actions. Are the nodes associated with the actions that were undone
still around in memory? There is now no way to access the nodes, but
will they be garbage collected or are they just leaking memory like a sieve?

Has anyone implemented undo this way? Any other strategies I should
consider?

Thanks,
Jeremy
Jun 27 '08 #1
1 1621
Jeremy,
It's difficult to be definitive about this and the outcome might well be
browser dependent.

On one hand, when the use performs an undo, your undo function is pulled
of the undo stack and executed. After that,
there are no outstanding references to the function and you would think
that the garbage collector wold clean up the function and in turn
release references to state variables (such as nextSibling) help by the
closure.

On the other hand, I've been caught with closure induced memory leak
esp. in IE before and so I would probably use a slightly different
approach. Still keep the undo stack but change what you store on it.
Instead of pushing a closure, push an object containing arguments and
a reference to a statically generated function to call to effect the
undo.

The undo function facility is invoked like the following then:

var App = {

undo: [], // an array of "undo" Objects

undoLast: function()
{
var oUndo = App.undo.pop();

if ( oUndo !== undefined) {
oUndo.fnUndo.apply( oUndo.thisp, oUndo.args);
}
}

};

What you need to complete is the code to record the undo Objects.

Cheers
Justin Johansson

Jeremy wrote:
I'm working on an application that does some DOM manipulation. I want
to add a mechanism for "Undo" so that the user can revert to the
previous state after performing a mistaken action. Simple idea, not
necessarily simple to implement.

My first idea for this is to use closures and an array that tracks the
inverse of the user's actions. Here's a simple example in which the
user can only remove DOM nodes and then undo the removal repeatedly
until they've reached their desired state (or the original state):
<cut/>
>
(Obviously the removeNode and undoLast functions are invoked from
elsewhere in response to user actions.)

My concern is with the closures created in the removeNode function. This
enables the undo to take place quite easily, but I am worried about
memory leakage.

Let's say the user undoes a few actions, and then performs a few more
actions. Are the nodes associated with the actions that were undone
still around in memory? There is now no way to access the nodes, but
will they be garbage collected or are they just leaking memory like a
sieve?

Has anyone implemented undo this way? Any other strategies I should
consider?

Thanks,
Jeremy
Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Enigman O'Maly | last post by:
I've written, er, that is, recorded and re-written a lot of little useful macros to help me with a spreadsheet dealing with financial investments. Very handy they are, yes, but sometimes I wish I...
1
by: black | last post by:
I'm coding with Tkinter and i wonder whether we could get current OS' clipboard available, and event more, anyone can inspires me how we can achieve undo and redo function ? thanx~ ...
14
by: Alexander May | last post by:
When I define a function in the body of a loop, why doesn't the function "close" on the loop vairable? See example below. Thanks, Alex C:\Documents and Settings\Alexander May>python Python...
5
by: paolo veronelli | last post by:
I've a vague idea of the differences,I don't know scheme anyway. I'd like to see an example to show what is missing in python about closures and possibly understand if ruby is better in this...
4
by: Marc Tanner | last post by:
Hello, I am currently working on a eventhandling system or something similar, and have the problem of loosing scope. I have read many interesting posts on this group and the faq article about...
4
by: lesperancer | last post by:
I've got a comboBox with 2 choices and based on the choice, I make button A or button B visible if choose choice 1, button A is visible now, I undo the choice so that the comboBox is null, but...
16
by: Karl Kofnarson | last post by:
Hi, while writing my last program I came upon the problem of accessing a common local variable by a bunch of functions. I wanted to have a function which would, depending on some argument,...
5
by: Steve | last post by:
I have created my own context menu. Everything works except undo'ing deleted text. My code for these two events are: Private Sub tsmiUndo_Click(ByVal sender As Object, ByVal e As...
4
by: king kikapu | last post by:
Hi, i am trying, to no avail yet, to take a C#'s overloaded functions skeleton and rewrite it in Python by using closures. I read somewhere on the net (http://dirtsimple.org/2004/12/python-is-...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.