Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
public
/
sequelize
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
不要怂,就是干,撸起袖子干!
Commit ae6719b9
authored
Jun 30, 2012
by
sdepold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
return results of emitters to the query chainer
1 parent
a640bb6a
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
73 additions
and
20 deletions
lib/query-chainer.js
spec/query-chainer.spec.js
lib/query-chainer.js
View file @
ae6719b
...
@@ -4,13 +4,15 @@ module.exports = (function() {
...
@@ -4,13 +4,15 @@ module.exports = (function() {
var
QueryChainer
=
function
(
emitters
)
{
var
QueryChainer
=
function
(
emitters
)
{
var
self
=
this
var
self
=
this
this
.
finishedEmits
=
0
this
.
finishedEmits
=
0
this
.
emitters
=
[]
this
.
emitters
=
[]
this
.
serials
=
[]
this
.
serials
=
[]
this
.
fails
=
[]
this
.
fails
=
[]
this
.
finished
=
false
this
.
serialResults
=
[]
this
.
wasRunning
=
false
this
.
emitterResults
=
[]
this
.
eventEmitter
=
null
this
.
finished
=
false
this
.
wasRunning
=
false
this
.
eventEmitter
=
null
emitters
=
emitters
||
[]
emitters
=
emitters
||
[]
emitters
.
forEach
(
function
(
emitter
)
{
self
.
add
(
emitter
)
})
emitters
.
forEach
(
function
(
emitter
)
{
self
.
add
(
emitter
)
})
...
@@ -31,13 +33,14 @@ module.exports = (function() {
...
@@ -31,13 +33,14 @@ module.exports = (function() {
var
self
=
this
var
self
=
this
this
.
eventEmitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
this
.
eventEmitter
=
new
Utils
.
CustomEventEmitter
(
function
()
{
self
.
wasRunning
=
true
self
.
wasRunning
=
true
finish
.
call
(
self
)
finish
.
call
(
self
,
'emitterResults'
)
})
})
return
this
.
eventEmitter
.
run
()
return
this
.
eventEmitter
.
run
()
}
}
QueryChainer
.
prototype
.
runSerially
=
function
(
options
)
{
QueryChainer
.
prototype
.
runSerially
=
function
(
options
)
{
var
self
=
this
var
self
=
this
,
serialCopy
=
Utils
.
_
.
clone
(
this
.
serials
)
options
=
Utils
.
_
.
extend
({
options
=
Utils
.
_
.
extend
({
skipOnError
:
false
skipOnError
:
false
...
@@ -67,16 +70,20 @@ module.exports = (function() {
...
@@ -67,16 +70,20 @@ module.exports = (function() {
onError
(
'Skipped due to earlier error!'
)
onError
(
'Skipped due to earlier error!'
)
}
else
{
}
else
{
var
emitter
=
serial
.
klass
[
serial
.
method
].
apply
(
serial
.
klass
,
serial
.
params
)
var
emitter
=
serial
.
klass
[
serial
.
method
].
apply
(
serial
.
klass
,
serial
.
params
)
emitter
.
success
(
function
()
{
if
(
serial
.
options
.
success
)
emitter
.
success
(
function
(
result
)
{
self
.
serialResults
[
serialCopy
.
indexOf
(
serial
)]
=
result
if
(
serial
.
options
.
success
)
{
serial
.
options
.
success
(
serial
.
klass
,
onSuccess
)
serial
.
options
.
success
(
serial
.
klass
,
onSuccess
)
else
}
else
{
onSuccess
()
onSuccess
()
}
}).
error
(
onError
)
}).
error
(
onError
)
}
}
}
else
{
}
else
{
self
.
wasRunning
=
true
self
.
wasRunning
=
true
finish
.
call
(
self
)
finish
.
call
(
self
,
'serialResults'
)
}
}
}
}
...
@@ -91,14 +98,15 @@ module.exports = (function() {
...
@@ -91,14 +98,15 @@ module.exports = (function() {
var
self
=
this
var
self
=
this
emitter
emitter
.
success
(
function
()
{
.
success
(
function
(
result
)
{
self
.
emitterResults
[
self
.
emitters
.
indexOf
(
emitter
)]
=
result
self
.
finishedEmits
++
self
.
finishedEmits
++
finish
.
call
(
self
)
finish
.
call
(
self
,
'emitterResults'
)
})
})
.
error
(
function
(
err
)
{
.
error
(
function
(
err
)
{
self
.
finishedEmits
++
self
.
finishedEmits
++
self
.
fails
.
push
(
err
)
self
.
fails
.
push
(
err
)
finish
.
call
(
self
)
finish
.
call
(
self
,
'emitterResults'
)
})
})
.
on
(
'sql'
,
function
(
sql
)
{
.
on
(
'sql'
,
function
(
sql
)
{
if
(
self
.
eventEmitter
)
{
if
(
self
.
eventEmitter
)
{
...
@@ -107,17 +115,18 @@ module.exports = (function() {
...
@@ -107,17 +115,18 @@ module.exports = (function() {
})
})
}
}
var
finish
=
function
()
{
var
finish
=
function
(
resultsName
)
{
this
.
finished
=
true
this
.
finished
=
true
if
(
this
.
emitters
.
length
>
0
)
if
(
this
.
emitters
.
length
>
0
)
{
this
.
finished
=
(
this
.
finishedEmits
==
this
.
emitters
.
length
)
this
.
finished
=
(
this
.
finishedEmits
==
this
.
emitters
.
length
)
else
if
(
this
.
serials
.
length
>
0
)
}
else
if
(
this
.
serials
.
length
>
0
)
{
this
.
finished
=
(
this
.
finishedEmits
==
this
.
serials
.
length
)
this
.
finished
=
(
this
.
finishedEmits
==
this
.
serials
.
length
)
}
if
(
this
.
finished
&&
this
.
wasRunning
)
{
if
(
this
.
finished
&&
this
.
wasRunning
)
{
var
status
=
(
this
.
fails
.
length
==
0
?
'success'
:
'error'
)
var
status
=
(
this
.
fails
.
length
==
0
?
'success'
:
'error'
)
,
result
=
(
this
.
fails
.
length
==
0
?
result
:
this
.
fails
)
,
result
=
(
this
.
fails
.
length
==
0
?
this
[
resultsName
]
:
this
.
fails
)
this
.
eventEmitter
.
emit
(
status
,
result
)
this
.
eventEmitter
.
emit
(
status
,
result
)
}
}
...
...
spec/query-chainer.spec.js
View file @
ae6719b
...
@@ -77,4 +77,48 @@ describe('QueryChainer', function() {
...
@@ -77,4 +77,48 @@ describe('QueryChainer', function() {
emitter3
.
run
()
emitter3
.
run
()
})
})
})
})
describe
(
'runSerially'
,
function
()
{
it
(
'finishes when all emitters are finished'
,
function
(
done
)
{
var
emitter1
=
new
CustomEventEmitter
(
function
(
e
)
{
e
.
emit
(
'success'
)
})
var
emitter2
=
new
CustomEventEmitter
(
function
(
e
)
{
e
.
emit
(
'success'
)
})
this
.
queryChainer
.
add
(
emitter1
,
'run'
)
this
.
queryChainer
.
add
(
emitter2
,
'run'
)
this
.
queryChainer
.
runSerially
().
success
(
function
()
{
assert
(
true
)
done
()
})
})
it
(
"returns the result of the passed emitters"
,
function
(
done
)
{
var
emitter1
=
new
CustomEventEmitter
(
function
(
e
)
{
e
.
emit
(
'success'
,
1
)
})
this
.
queryChainer
.
add
(
emitter1
,
'run'
)
this
.
queryChainer
.
runSerially
().
success
(
function
(
results
)
{
expect
(
results
).
toBeDefined
()
expect
(
results
.
length
).
toEqual
(
1
)
expect
(
results
[
0
]).
toEqual
(
1
)
done
()
})
})
it
(
"returns the result of the passed emitters in the order of the occurrence of adding the emitters"
,
function
(
done
)
{
var
emitter1
=
new
CustomEventEmitter
(
function
(
e
)
{
e
.
emit
(
'success'
,
1
)
})
,
emitter2
=
new
CustomEventEmitter
(
function
(
e
)
{
setTimeout
(
function
()
{
e
.
emit
(
'success'
,
2
)
},
100
)
})
,
emitter3
=
new
CustomEventEmitter
(
function
(
e
)
{
e
.
emit
(
'success'
,
3
)
})
this
.
queryChainer
.
add
(
emitter1
,
'run'
)
this
.
queryChainer
.
add
(
emitter2
,
'run'
)
this
.
queryChainer
.
add
(
emitter3
,
'run'
)
this
.
queryChainer
.
runSerially
().
success
(
function
(
results
)
{
expect
(
results
.
length
).
toEqual
(
3
)
expect
(
results
).
toEqual
([
1
,
2
,
3
])
done
()
})
})
})
})
})
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment