473,406 Members | 2,217 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,406 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 1623
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.