Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

debounce #11

Closed
russplaysguitar opened this issue Dec 1, 2012 · 8 comments
Closed

debounce #11

russplaysguitar opened this issue Dec 1, 2012 · 8 comments
Milestone

Comments

@russplaysguitar
Copy link
Owner

http://underscorejs.org/#debounce

@atuttle
Copy link
Contributor

atuttle commented Dec 9, 2012

Taking a stab at this tonight...

@atuttle
Copy link
Contributor

atuttle commented Dec 10, 2012

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
Copy link
Contributor

atuttle commented Dec 10, 2012

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
Copy link
Owner Author

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
Copy link
Contributor

atuttle commented Dec 13, 2012

I haven't tried it yet. :)

@russplaysguitar
Copy link
Owner Author

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
Copy link
Contributor

atuttle commented Dec 13, 2012

Here it comes... ;)

@russplaysguitar
Copy link
Owner Author

Debounce was added by: bd9ff52

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants