work on async collection loading...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2017-10-08 18:49:19 +03:00
parent 66c09861c6
commit 4debb96942
3 changed files with 98 additions and 33 deletions

View File

@ -232,8 +232,61 @@ var CollectionActions = actions.Actions({
})], })],
// XXX should this queue already running calls or a specific collection????
// ...I think yes!!
ensureCollection: ['- Collections/',
core.doc`Ensure a collection exists and is consistent...
.ensureCollection(title)
-> promise(collection)
This will:
- create a collection if it does not exist
- initialize if needed
`,
function(collection){
var that = this
var running = this.__running_collection_ensure =
this.__running_collection_ensure || {}
// create collection if needed...
;(!this.collections
|| !(collection in this.collections))
&& this.newCollection(collection)
var collection_data = this.collections[collection]
var handlers = this.collection_handlers
// sync collection calls...
// XXX do we need timeuts here????
if(running[collection]){
return running[collection]
}
return new Promise(function(resolve, reject){
Promise
.all(Object.keys(handlers)
// filter relevant handlers...
.filter(function(format){
return format == '*' || collection_data[format] })
// run handlers...
.map(function(format){
return that[handlers[format]](collection, collection_data) }))
.then(function(){
delete running[collection]
resolve(collection_data) })
.catch(function(err){
delete running[collection]
reject(err) })
})
}],
// Collection life-cycle... // Collection life-cycle...
// //
// NOTE: if collection does not exist this will do nothing...
// NOTE: this is not sync, if it is needed to trigger post collection
// loading then bind to collectionLoading.post...
loadCollection: ['- Collections/', loadCollection: ['- Collections/',
core.doc`Load collection... core.doc`Load collection...
@ -336,14 +389,9 @@ var CollectionActions = actions.Actions({
} }
// load collection... // load collection...
Promise // XXX should this be sync???
.all(Object.keys(handlers) //return this.ensureCollection(collection)
// filter relevant handlers... this.ensureCollection(collection)
.filter(function(format){
return format == '*' || collection_data[format] })
// run handlers...
.map(function(format){
return that[handlers[format]](collection, collection_data) }))
.then(function(){ .then(function(){
var data = collection_data.data var data = collection_data.data
@ -632,11 +680,6 @@ var CollectionActions = actions.Actions({
return return
} }
// create collection if needed...
(!this.collections
|| !(collection in this.collections))
&& this.newCollection(collection)
gids = gids == 'loaded' ? gids = gids == 'loaded' ?
this.data.getImages('loaded') this.data.getImages('loaded')
: gids == 'ribbon' ? : gids == 'ribbon' ?
@ -689,29 +732,26 @@ var CollectionActions = actions.Actions({
return return
} }
(!this.collections
|| !(collection in this.collections))
&& this.newCollection(collection)
// if only collection is given, reset align to null... // if only collection is given, reset align to null...
align = align === collection ? null : align align = align === collection ? null : align
if(this.collections && this.collections[collection]){ // create collection if it does not exist...
//this.collections[collection].data.join(align, data || this.data.clone()) // XXX should this be async???
var res = this.collections[collection].data = (data || this.data) //return this.ensureCollection(collection)
.clone() this.ensureCollection(collection)
.join(align, this.collections[collection].data) .then((function(){
//this.collections[collection].data.join(align, data || this.data.clone())
var res = this.collections[collection].data = (data || this.data)
.clone()
.join(align, this.collections[collection].data)
// joining into the current collection... // joining into the current collection...
if(collection == this.collection){ if(collection == this.collection){
var cur = this.current var cur = this.current
this.data = res this.data = res
this.data.current = cur this.data.current = cur
} }
}).bind(this))
} else {
this.saveCollection(collection)
}
}], }],
uncollect: ['Collections|Image/Remove from collection', uncollect: ['Collections|Image/Remove from collection',
core.doc`Remove gid(s) from collection... core.doc`Remove gid(s) from collection...

View File

@ -2649,6 +2649,22 @@ var WidgetTestActions = actions.Actions({
makeAction: ['- Test/', makeAction: ['- Test/',
function(name){ function(name){
this[name] = actions.Action.apply(actions.Action, arguments) }], this[name] = actions.Action.apply(actions.Action, arguments) }],
// promise handling...
//
// also see corresponding WidgetTest.handlers
syncAction: ['- Test/',
//{await: true},
function(t){
return new Promise(function(resolve){
setTimeout(function(){ resolve() }, t || 1000) })
}],
asyncAction: ['- Test/',
{await: false},
function(t){
return new Promise(function(resolve){
setTimeout(function(){ resolve() }, t || 1000) })
}],
}) })
var WidgetTest = var WidgetTest =
@ -2661,6 +2677,15 @@ module.WidgetTest = core.ImageGridFeatures.Feature({
'ui-browse-actions', 'ui-browse-actions',
], ],
actions: WidgetTestActions, actions: WidgetTestActions,
handlers: [
['asyncAction.pre syncAction.pre',
function(){
console.log('PRE')
return function(){
console.log('POST') }
}],
],
}) })

View File

@ -22,7 +22,7 @@
"fs-walk": "^0.0.1", "fs-walk": "^0.0.1",
"glob": "^4.0.6", "glob": "^4.0.6",
"guarantee-events": "^1.0.0", "guarantee-events": "^1.0.0",
"ig-actions": "^3.8.0", "ig-actions": "^3.9.0",
"ig-features": "^3.3.2", "ig-features": "^3.3.2",
"ig-object": "^1.0.2", "ig-object": "^1.0.2",
"openseadragon": "^2.3.1", "openseadragon": "^2.3.1",