From f850d2df18dcffb8da57b979ca0a5a8c40029eb0 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Thu, 3 Dec 2020 04:03:20 +0300 Subject: [PATCH] reworked core.debounce(..) + moved to actions.debounce(..) for status bar updates... Signed-off-by: Alex A. Naanou --- Viewer/features/core.js | 105 ++++++++++++++++++++--------------- Viewer/features/examples.js | 6 +- Viewer/features/metadata.js | 9 +-- Viewer/features/sharp.js | 3 +- Viewer/features/ui-status.js | 1 + Viewer/package-lock.json | 12 ++-- Viewer/package.json | 4 +- 7 files changed, 80 insertions(+), 60 deletions(-) diff --git a/Viewer/features/core.js b/Viewer/features/core.js index b6684f90..57183bef 100755 --- a/Viewer/features/core.js +++ b/Viewer/features/core.js @@ -1373,8 +1373,12 @@ module.Cache = ImageGridFeatures.Feature({ // timeout: number, // returns: 'cached' | 'dropped', // 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 = module.debounce = function(options, func){ @@ -1383,52 +1387,67 @@ function(options, func){ func = args.pop() options = args.pop() || {} - if(typeof(options) == typeof(123)){ - options.timeout = options - } + typeof(options) == typeof(123) + && (options.timeout = options) // closure state... - var res = undefined + var res + var last_args var debounced = false var retriggered = 0 - var f = function(...args){ - if(!debounced){ - res = func instanceof Function ? - func.call(this, ...args) - // alias... - : this.parseStringAction.callAction(this, func, ...args) - res = options.returns != 'cahced' ? res : undefined + // call the action... + var call = function(context, ...args){ + return func instanceof Function ? + func.call(context, ...args) + // alias... + : this.parseStringAction.callAction(context, func, ...args) } - // start the timer... - debounced = setTimeout( - function(){ - // callback... - options.callback instanceof Function - && options.callback.call(this, retriggered, args) + return object.mixin( + function(...args){ + var retrigger + // call... + if(!debounced){ + res = call(this, ...args) + res = options.returns != 'cahced' ? + res + : undefined - // cleanup... - retriggered = 0 - res = undefined - debounced = false - }.bind(this), - options.timeout - || this.config['debounce-action-timeout'] - || 200) + // start the timer... + debounced = setTimeout( + function(){ + var c + // callback... + options.callback instanceof Function + && (c = options.callback.call(this, retriggered, args)) + // 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 || {}, { handler: function(item){ return func.call(that, item, ...args) } })) - sync - && (q.sync_start = true) // pre-process args... arg_handler && ([items, ...args] = @@ -2602,10 +2619,9 @@ function(name, func){ var res = new Promise(function(resolve, reject){ q.then(resolve, reject) }) // sync start... - // NOTE: we need to explicitly .start() here because the - // queue could be waiting for a timeout - sync - && q.start() + // NOTE: we need to explicitly .start(true) here because the + // queue could be waiting for a timeout... + q.start(sync) return res }), { toString: function(){ @@ -2732,6 +2748,7 @@ var TaskActions = actions.Actions({ || queue.clear() } var cleanup = function(){ return function(){ + queue.stop() // XXX handle error state... //logger // && logger.emit('close') diff --git a/Viewer/features/examples.js b/Viewer/features/examples.js index 4168d496..1bf65b6a 100755 --- a/Viewer/features/examples.js +++ b/Viewer/features/examples.js @@ -299,10 +299,10 @@ var ExampleActions = actions.Actions({ reverse && items.reverse() return [items, ...args] }, - function(item, ...args){ - console.log('Queue handler action!!', item, ...args) + function(item, timeout, ...args){ + console.log('Queue handler action!!', item, timeout, ...args) return new Promise(function(resolve){ - setTimeout(resolve, 100) }) })], + setTimeout(resolve, timeout || 100) }) })], // // NOTE: action name and task name should be the same to avoid diff --git a/Viewer/features/metadata.js b/Viewer/features/metadata.js index d1411643..55b9c665 100755 --- a/Viewer/features/metadata.js +++ b/Viewer/features/metadata.js @@ -116,13 +116,14 @@ var MetadataReaderActions = actions.Actions({ NOTE: also see: .cacheMetadata(..) `, core.queueHandler('Read image metadata', - function(image, force){ + {quiet: true}, + function(queue, image, force){ return [ - images == 'all' ? + image == 'all' ? this.images.keys() - : images == 'loaded' ? + : image == 'loaded' ? this.data.getImages('loaded') - : images, + : image, force, ] }, function(image, force){ diff --git a/Viewer/features/sharp.js b/Viewer/features/sharp.js index 4390f9c4..90ff3fcd 100755 --- a/Viewer/features/sharp.js +++ b/Viewer/features/sharp.js @@ -638,7 +638,8 @@ var SharpActions = actions.Actions({ NOTE: for info on full metadata format see: .readMetadata(..) `, core.queueHandler('Cache image metadata', - {quiet: true}, + // XXX timeouts still need tweaking... + {quiet: true, pool_size: 2, busy_timeout: 400}, // parse args... function(queue, image, logger){ var force = false diff --git a/Viewer/features/ui-status.js b/Viewer/features/ui-status.js index bdf5aad7..9ffb1326 100755 --- a/Viewer/features/ui-status.js +++ b/Viewer/features/ui-status.js @@ -635,6 +635,7 @@ var StatusBarActions = actions.Actions({ handler.call(that, item, gid, img) } }) } }, null)], updateStatusBar: ['- Interface/Update satus bar', + {precall: actions.debounce()}, 'toggleStatusBar: "!"'], resetStatusBar: ['Interface/Reset status bar', diff --git a/Viewer/package-lock.json b/Viewer/package-lock.json index 2c666d45..fa38073b 100755 --- a/Viewer/package-lock.json +++ b/Viewer/package-lock.json @@ -1073,9 +1073,9 @@ "integrity": "sha512-4vf7I2LYV/HaWerSo3XmlMkp5eZ83i+/CDluXi/IGTs/O1sejBNhTtnxzmRZfvOUqj7lZjqHkeTvpgSFDlWZTg==" }, "ig-actions": { - "version": "3.24.19", - "resolved": "https://registry.npmjs.org/ig-actions/-/ig-actions-3.24.19.tgz", - "integrity": "sha512-b4Lz4qD/aw7MOcNDgGn/y6992EjeFHQw6tmv1AfX1ZeWdpg9TjPjkdFy5NLpdGknURM/b1I06AkxYLgBAeOQyQ==", + "version": "3.24.20", + "resolved": "https://registry.npmjs.org/ig-actions/-/ig-actions-3.24.20.tgz", + "integrity": "sha512-V5jOhwxjRMoNS1vT6O0nZKh9063FAxtnQoo4G/iH1Kf9jKGqDNlXY4WhoimxGdJF9cdYs+LGxLFjkjek+jaAcg==", "requires": { "ig-object": "^5.4.12" } @@ -1110,9 +1110,9 @@ "integrity": "sha512-9kZM80Js9/eTwXN9VXwLDC1wDJ7gIAdYU9GIzb5KJmNcLAMaW+zhgFrwFFMrcSfggUuadgnqSrS41E4XLe8JZw==" }, "ig-types": { - "version": "5.0.32", - "resolved": "https://registry.npmjs.org/ig-types/-/ig-types-5.0.32.tgz", - "integrity": "sha512-AKfatN0z3hURn9J7JSCaImZnkpr42WMozVLBJeAeC0urkLEU3NUcMEmuDR57dsI5vu9A3d+tyIiRxd5If/3VaQ==", + "version": "5.0.34", + "resolved": "https://registry.npmjs.org/ig-types/-/ig-types-5.0.34.tgz", + "integrity": "sha512-HQADOcjAqkZ++lVavo8lb+qzcbkd33lshgf2/TMXAh4DJLxIIioNj8skPpKjzK0BQMHxd5wvPCwGt29s/ydg5g==", "requires": { "ig-object": "^5.4.12", "object-run": "^1.0.1" diff --git a/Viewer/package.json b/Viewer/package.json index dc81e786..6d97c484 100755 --- a/Viewer/package.json +++ b/Viewer/package.json @@ -28,11 +28,11 @@ "generic-walk": "^1.4.0", "glob": "^7.1.6", "guarantee-events": "^1.0.0", - "ig-actions": "^3.24.19", + "ig-actions": "^3.24.20", "ig-argv": "^2.15.0", "ig-features": "^3.4.2", "ig-object": "^5.4.12", - "ig-types": "^5.0.32", + "ig-types": "^5.0.34", "moment": "^2.29.1", "object-run": "^1.0.1", "requirejs": "^2.3.6",