mirror of
https://github.com/flynx/pWiki.git
synced 2025-12-17 08:31:38 +00:00
more experimenting with async APIs...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
9f37a2f3e2
commit
dac806cbd1
41
pwiki.js
41
pwiki.js
@ -1064,11 +1064,28 @@ module.hiddenPromise = {
|
|||||||
|
|
||||||
then: function(func){
|
then: function(func){
|
||||||
var that = this
|
var that = this
|
||||||
|
|
||||||
|
// trigger lazy functions if present...
|
||||||
|
if(this.__lazy != null){
|
||||||
|
var lazy = this.__lazy
|
||||||
|
delete this.__lazy
|
||||||
|
|
||||||
|
return this
|
||||||
|
.then(lazy)
|
||||||
|
.then(func)
|
||||||
|
|
||||||
|
// clear any lazy stuff queued by the above to avoid any
|
||||||
|
// side-effects...
|
||||||
|
delete this.__lazy
|
||||||
|
}
|
||||||
|
|
||||||
|
// no promise...
|
||||||
if(this.__promise == null){
|
if(this.__promise == null){
|
||||||
this.__promise = new Promise(function(resolve, reject){
|
this.__promise = new Promise(function(resolve, reject){
|
||||||
resolve(func.call(that))
|
resolve(func.call(that))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// existing promise...
|
||||||
} else {
|
} else {
|
||||||
this.__promise = this.__promise.then(function(){
|
this.__promise = this.__promise.then(function(){
|
||||||
return func.apply(that, [].slice.call(arguments))
|
return func.apply(that, [].slice.call(arguments))
|
||||||
@ -1077,6 +1094,18 @@ module.hiddenPromise = {
|
|||||||
return this
|
return this
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// Like then, but the function will get called only if a .then(..) is
|
||||||
|
// called right after...
|
||||||
|
//
|
||||||
|
// NOTE: only the last lazy function is stored, the rest are discarded.
|
||||||
|
lazy: function(func){
|
||||||
|
this.__lazy = func
|
||||||
|
return this
|
||||||
|
},
|
||||||
|
clearLazy: function(){
|
||||||
|
delete this.__lazy
|
||||||
|
return this
|
||||||
|
},
|
||||||
|
|
||||||
// example method (sync)...
|
// example method (sync)...
|
||||||
//
|
//
|
||||||
@ -1101,13 +1130,12 @@ module.hiddenPromise = {
|
|||||||
// .data()
|
// .data()
|
||||||
// .then(function(value){ console.log(value) })
|
// .then(function(value){ console.log(value) })
|
||||||
//
|
//
|
||||||
// XXX would be nice to be able to make the getter lazy, i.e. do
|
|
||||||
// nothing unless a .then(..) is called right after...
|
|
||||||
//
|
|
||||||
sdata: function(d){
|
sdata: function(d){
|
||||||
|
this.clearLazy()
|
||||||
|
|
||||||
// get...
|
// get...
|
||||||
if(arguments.length == 0){
|
if(arguments.length == 0){
|
||||||
this.then(function(){ return this.__data })
|
this.lazy(function(){ return this.__data })
|
||||||
|
|
||||||
// set...
|
// set...
|
||||||
} else {
|
} else {
|
||||||
@ -1126,9 +1154,12 @@ module.hiddenPromise = {
|
|||||||
// a second)...
|
// a second)...
|
||||||
data: function(d){
|
data: function(d){
|
||||||
var that = this
|
var that = this
|
||||||
|
this.clearLazy()
|
||||||
|
|
||||||
// get...
|
// get...
|
||||||
if(arguments.length == 0){
|
if(arguments.length == 0){
|
||||||
this.then(function(){
|
//this.then(function(){
|
||||||
|
this.lazy(function(){
|
||||||
return new Promise(function(r){
|
return new Promise(function(r){
|
||||||
setTimeout(function(){ r(that.__data) }, 1000)
|
setTimeout(function(){ r(that.__data) }, 1000)
|
||||||
})
|
})
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user