Giter VIP home page Giter VIP logo

Comments (8)

atuttle avatar atuttle commented on August 26, 2024

Taking a stab at this tonight...

from underscorecf.

atuttle avatar atuttle commented on August 26, 2024

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?

from underscorecf.

atuttle avatar atuttle commented on August 26, 2024

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.

from underscorecf.

russplaysguitar avatar russplaysguitar commented on August 26, 2024

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).

from underscorecf.

atuttle avatar atuttle commented on August 26, 2024

I haven't tried it yet. :)

from underscorecf.

russplaysguitar avatar russplaysguitar commented on August 26, 2024

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.

from underscorecf.

atuttle avatar atuttle commented on August 26, 2024

Here it comes... ;)

from underscorecf.

russplaysguitar avatar russplaysguitar commented on August 26, 2024

Debounce was added by: bd9ff52

from underscorecf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.