Comments (8)
Taking a stab at this tonight...
from underscorecf.
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.
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.
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.
I haven't tried it yet. :)
from underscorecf.
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.
Here it comes... ;)
from underscorecf.
Debounce was added by: bd9ff52
from underscorecf.
Related Issues (20)
- Make toQuery(arrayOfStructs) handle variable-size structs
- Docs CSS and formatting HOT 8
- XML support for toQuery() HOT 1
- Add toXml(collection, [*elementNames])
- Add examples for object()
- Compatibility: Railo for-in loops on structs are wrong
- xml support for _.size()
- _.type(variable)
- _.toArray(string) should return an array of characters
- Port 1.4.4 changes HOT 1
- Port changes to _.uniq and _.times
- Add _.REReplace(string, regex, callback, [scope], [context])
- implement _.transform(collection, callback, accumulator, context) : any (from lo-dash)
- toArray() uses for-in loop on a list variable which is not supported HOT 4
- Get Travis-CI working HOT 4
- variables._ is undefined HOT 5
- Test in Coldfusion 11 HOT 15
- is this still an active project? HOT 1
- Publish to ForgeBox.io? HOT 3
- Calling slice with a "to" 1 value greater than the length of the array causes an error HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from underscorecf.