reworked core.debounce(..) + moved to actions.debounce(..) for status bar updates...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-12-03 04:03:20 +03:00
parent 7122c619f4
commit f850d2df18
7 changed files with 80 additions and 60 deletions

View File

@ -1373,8 +1373,12 @@ module.Cache = ImageGridFeatures.Feature({
// timeout: number, // timeout: number,
// returns: 'cached' | 'dropped', // returns: 'cached' | 'dropped',
// callback: function(retriggered, args), // callback: function(retriggered, args),
//
// postcall: true,
// } // }
// //
// XXX might be a good ide to move this someplace generic...
// XXX this is not debouncing pre/post calls, just the base action...
var debounce = var debounce =
module.debounce = module.debounce =
function(options, func){ function(options, func){
@ -1383,52 +1387,67 @@ function(options, func){
func = args.pop() func = args.pop()
options = args.pop() || {} options = args.pop() || {}
if(typeof(options) == typeof(123)){ typeof(options) == typeof(123)
options.timeout = options && (options.timeout = options)
}
// closure state... // closure state...
var res = undefined var res
var last_args
var debounced = false var debounced = false
var retriggered = 0 var retriggered = 0
var f = function(...args){ // call the action...
if(!debounced){ var call = function(context, ...args){
res = func instanceof Function ? return func instanceof Function ?
func.call(this, ...args) func.call(context, ...args)
// alias... // alias...
: this.parseStringAction.callAction(this, func, ...args) : this.parseStringAction.callAction(context, func, ...args) }
res = options.returns != 'cahced' ? res : undefined
// start the timer... return object.mixin(
debounced = setTimeout( function(...args){
function(){ var retrigger
// callback... // call...
options.callback instanceof Function if(!debounced){
&& options.callback.call(this, retriggered, args) res = call(this, ...args)
res = options.returns != 'cahced' ?
res
: undefined
// cleanup... // start the timer...
retriggered = 0 debounced = setTimeout(
res = undefined function(){
debounced = false var c
}.bind(this), // callback...
options.timeout options.callback instanceof Function
|| this.config['debounce-action-timeout'] && (c = options.callback.call(this, retriggered, args))
|| 200) // retrigger...
options.postcall
&& retriggered > 0
&& c !== false
// XXX should this be a debounced call or a normal call...
// XXX this is not the actual action thus no
// handlers will be triggered...
&& call(this, ...last_args)
// cleanup...
retriggered = 0
res = undefined
debounced = false }.bind(this),
options.timeout
|| this.config['debounce-action-timeout']
|| 200)
// skip...
} else {
retriggered++
last_args = args
return res } },
{
toString: function(){
return `// debounced...\n${
doc([ func instanceof Function ?
func.toString()
: func ])}` },
}) }
} else {
retriggered++
return res
}
}
f.toString = function(){
return `// debounced...\n${
doc([ func instanceof Function ? func.toString() : func ])}`
}
return f
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -2590,8 +2609,6 @@ function(name, func){
opts || {}, opts || {},
{ handler: function(item){ { handler: function(item){
return func.call(that, item, ...args) } })) return func.call(that, item, ...args) } }))
sync
&& (q.sync_start = true)
// pre-process args... // pre-process args...
arg_handler arg_handler
&& ([items, ...args] = && ([items, ...args] =
@ -2602,10 +2619,9 @@ function(name, func){
var res = new Promise(function(resolve, reject){ var res = new Promise(function(resolve, reject){
q.then(resolve, reject) }) q.then(resolve, reject) })
// sync start... // sync start...
// NOTE: we need to explicitly .start() here because the // NOTE: we need to explicitly .start(true) here because the
// queue could be waiting for a timeout // queue could be waiting for a timeout...
sync q.start(sync)
&& q.start()
return res }), return res }),
{ {
toString: function(){ toString: function(){
@ -2732,6 +2748,7 @@ var TaskActions = actions.Actions({
|| queue.clear() } || queue.clear() }
var cleanup = function(){ var cleanup = function(){
return function(){ return function(){
queue.stop()
// XXX handle error state... // XXX handle error state...
//logger //logger
// && logger.emit('close') // && logger.emit('close')

View File

@ -299,10 +299,10 @@ var ExampleActions = actions.Actions({
reverse reverse
&& items.reverse() && items.reverse()
return [items, ...args] }, return [items, ...args] },
function(item, ...args){ function(item, timeout, ...args){
console.log('Queue handler action!!', item, ...args) console.log('Queue handler action!!', item, timeout, ...args)
return new Promise(function(resolve){ return new Promise(function(resolve){
setTimeout(resolve, 100) }) })], setTimeout(resolve, timeout || 100) }) })],
// //
// NOTE: action name and task name should be the same to avoid // NOTE: action name and task name should be the same to avoid

View File

@ -116,13 +116,14 @@ var MetadataReaderActions = actions.Actions({
NOTE: also see: .cacheMetadata(..) NOTE: also see: .cacheMetadata(..)
`, `,
core.queueHandler('Read image metadata', core.queueHandler('Read image metadata',
function(image, force){ {quiet: true},
function(queue, image, force){
return [ return [
images == 'all' ? image == 'all' ?
this.images.keys() this.images.keys()
: images == 'loaded' ? : image == 'loaded' ?
this.data.getImages('loaded') this.data.getImages('loaded')
: images, : image,
force, force,
] }, ] },
function(image, force){ function(image, force){

View File

@ -638,7 +638,8 @@ var SharpActions = actions.Actions({
NOTE: for info on full metadata format see: .readMetadata(..) NOTE: for info on full metadata format see: .readMetadata(..)
`, `,
core.queueHandler('Cache image metadata', core.queueHandler('Cache image metadata',
{quiet: true}, // XXX timeouts still need tweaking...
{quiet: true, pool_size: 2, busy_timeout: 400},
// parse args... // parse args...
function(queue, image, logger){ function(queue, image, logger){
var force = false var force = false

View File

@ -635,6 +635,7 @@ var StatusBarActions = actions.Actions({
handler.call(that, item, gid, img) } }) } }, handler.call(that, item, gid, img) } }) } },
null)], null)],
updateStatusBar: ['- Interface/Update satus bar', updateStatusBar: ['- Interface/Update satus bar',
{precall: actions.debounce()},
'toggleStatusBar: "!"'], 'toggleStatusBar: "!"'],
resetStatusBar: ['Interface/Reset status bar', resetStatusBar: ['Interface/Reset status bar',

View File

@ -1073,9 +1073,9 @@
"integrity": "sha512-4vf7I2LYV/HaWerSo3XmlMkp5eZ83i+/CDluXi/IGTs/O1sejBNhTtnxzmRZfvOUqj7lZjqHkeTvpgSFDlWZTg==" "integrity": "sha512-4vf7I2LYV/HaWerSo3XmlMkp5eZ83i+/CDluXi/IGTs/O1sejBNhTtnxzmRZfvOUqj7lZjqHkeTvpgSFDlWZTg=="
}, },
"ig-actions": { "ig-actions": {
"version": "3.24.19", "version": "3.24.20",
"resolved": "https://registry.npmjs.org/ig-actions/-/ig-actions-3.24.19.tgz", "resolved": "https://registry.npmjs.org/ig-actions/-/ig-actions-3.24.20.tgz",
"integrity": "sha512-b4Lz4qD/aw7MOcNDgGn/y6992EjeFHQw6tmv1AfX1ZeWdpg9TjPjkdFy5NLpdGknURM/b1I06AkxYLgBAeOQyQ==", "integrity": "sha512-V5jOhwxjRMoNS1vT6O0nZKh9063FAxtnQoo4G/iH1Kf9jKGqDNlXY4WhoimxGdJF9cdYs+LGxLFjkjek+jaAcg==",
"requires": { "requires": {
"ig-object": "^5.4.12" "ig-object": "^5.4.12"
} }
@ -1110,9 +1110,9 @@
"integrity": "sha512-9kZM80Js9/eTwXN9VXwLDC1wDJ7gIAdYU9GIzb5KJmNcLAMaW+zhgFrwFFMrcSfggUuadgnqSrS41E4XLe8JZw==" "integrity": "sha512-9kZM80Js9/eTwXN9VXwLDC1wDJ7gIAdYU9GIzb5KJmNcLAMaW+zhgFrwFFMrcSfggUuadgnqSrS41E4XLe8JZw=="
}, },
"ig-types": { "ig-types": {
"version": "5.0.32", "version": "5.0.34",
"resolved": "https://registry.npmjs.org/ig-types/-/ig-types-5.0.32.tgz", "resolved": "https://registry.npmjs.org/ig-types/-/ig-types-5.0.34.tgz",
"integrity": "sha512-AKfatN0z3hURn9J7JSCaImZnkpr42WMozVLBJeAeC0urkLEU3NUcMEmuDR57dsI5vu9A3d+tyIiRxd5If/3VaQ==", "integrity": "sha512-HQADOcjAqkZ++lVavo8lb+qzcbkd33lshgf2/TMXAh4DJLxIIioNj8skPpKjzK0BQMHxd5wvPCwGt29s/ydg5g==",
"requires": { "requires": {
"ig-object": "^5.4.12", "ig-object": "^5.4.12",
"object-run": "^1.0.1" "object-run": "^1.0.1"

View File

@ -28,11 +28,11 @@
"generic-walk": "^1.4.0", "generic-walk": "^1.4.0",
"glob": "^7.1.6", "glob": "^7.1.6",
"guarantee-events": "^1.0.0", "guarantee-events": "^1.0.0",
"ig-actions": "^3.24.19", "ig-actions": "^3.24.20",
"ig-argv": "^2.15.0", "ig-argv": "^2.15.0",
"ig-features": "^3.4.2", "ig-features": "^3.4.2",
"ig-object": "^5.4.12", "ig-object": "^5.4.12",
"ig-types": "^5.0.32", "ig-types": "^5.0.34",
"moment": "^2.29.1", "moment": "^2.29.1",
"object-run": "^1.0.1", "object-run": "^1.0.1",
"requirejs": "^2.3.6", "requirejs": "^2.3.6",