russplaysguitar / UnderscoreCF

An UnderscoreJS port for Coldfusion. Functional programming library.
http://russplaysguitar.github.com/UnderscoreCF/
MIT License
89 stars 38 forks source link

debounce #11

Closed russplaysguitar closed 11 years ago

russplaysguitar commented 11 years ago

http://underscorejs.org/#debounce

atuttle commented 11 years ago

Taking a stab at this tonight...

atuttle commented 11 years ago

The more I look at this, the more I think it's not a practical function to use in CFML.

My implementation was starting to look like this:

public any function debounce(func, wait, immediate) {
    var lastInvokeTime = GetTickCount();
    var cancel = 0;
    return function () {
        var thisInvokeTime = GetTickCount();
        if (thisInvokeTime - lastInvokeTime >= wait) {
            createObject("java","java.lang.Thread").sleep(wait);
            if (cancel > 0){
                cancel--;
                return;
            }
            func();
        }
        else {
            cancel++;
        }
        lastInvokeTime = GetTickCount();
    };
}

It's not finished yet, but it's got me thinking about asynchronicity. When you call _.debounce() you'll get back a function that essentially sets a timer (in JS: setTimeout(callback, wait); ) and, if called again before the timer is complete, resets the timeout and starts over. CF doesn't have a setTimeout equivalent, so we'd have to use a combination of threads and sleep instead (so that the main thread is not locked waiting for the sleep to complete).

However, CF Threads can only access shared scopes until the thread from which they were spawned completes, so we can't always count on being able to look for a should-cancel flag there. In addition, it limits the ability of the callback to do useful things with those shared scopes.

I think this would be ok if threads worked like closures, but I'm not sure that they do. I'm kind of at an impasse. Any thoughts?

atuttle commented 11 years ago

I did some more reading on threads and came up with this:

public any function debounce(func, wait, immediate) {
    var threadName = createUUID();
    return function () {
        if (cfthread[threadName].status neq "completed"){
            thread action='terminate' name='#threadName#';
        }
        thread action='run' name='#threadName#'{
            thread action='sleep' duration='#wait#';
            func();
        }
    };
}

I'm still concerned it will have limited utility because the callback is called from within a thread, but this may be as close as we'll get.

russplaysguitar commented 11 years ago

Does that work? I've had problems with accessing var-scoped variables from within a thread.

One thing I'd like to do is implement setTimeout and clearTimeout for CF, then call it from debounce in the same way that Underscore.js does it.

I'm not as concerned about the actual utility of the function as I am about trying to match the logic from Underscore.js. I figure that eventually there might be someone out there who wants to do this exact thing (even though I can't think of why).

atuttle commented 11 years ago

I haven't tried it yet. :)

russplaysguitar commented 11 years ago

The only way I've been able to get it to work is by assigning the variable to the variables scope, which sucks. I haven't tried the request (or session?) scopes though.

atuttle commented 11 years ago

Here it comes... ;)

russplaysguitar commented 11 years ago

Debounce was added by: bd9ff529d179fa507fc7d29ebd9d4961f1d7af88