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 e52cf435
authored
Sep 10, 2014
by
overlookmotel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Hooks can return undefined
1 parent
c07098ad
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
32 additions
and
51 deletions
lib/hooks.js
lib/hooks.js
View file @
e52cf43
...
...
@@ -35,6 +35,22 @@ var Utils = require('./utils')
* @mixin Hooks
*/
var
Hooks
=
module
.
exports
=
function
()
{};
var
hookTypes
=
{
beforeValidate
:
{
params
:
2
},
afterValidate
:
{
params
:
2
},
beforeCreate
:
{
params
:
2
},
afterCreate
:
{
params
:
2
},
beforeDestroy
:
{
params
:
2
},
afterDestroy
:
{
params
:
2
},
beforeUpdate
:
{
params
:
2
},
afterUpdate
:
{
params
:
2
},
beforeBulkCreate
:
{
params
:
2
},
afterBulkCreate
:
{
params
:
2
},
beforeBulkDestroy
:
{
params
:
1
},
afterBulkDestroy
:
{
params
:
1
},
beforeBulkUpdate
:
{
params
:
1
},
afterBulkUpdate
:
{
params
:
1
}
};
var
hookAliases
=
{
beforeDelete
:
'beforeDestroy'
,
afterDelete
:
'afterDestroy'
,
...
...
@@ -59,16 +75,18 @@ Hooks.replaceHookAliases = function(hooks) {
return
hooks
;
};
Hooks
.
runHooks
=
function
()
{
Hooks
.
runHooks
=
function
(
hooks
)
{
var
self
=
this
,
tick
=
0
,
hooks
=
arguments
[
0
]
,
lastIndex
=
arguments
.
length
-
1
,
fn
=
typeof
arguments
[
lastIndex
]
===
'function'
?
arguments
[
lastIndex
]
:
null
,
fnArgs
=
Array
.
prototype
.
slice
.
call
(
arguments
,
1
,
fn
?
lastIndex
:
arguments
.
length
)
,
resolveArgs
=
fnArgs
;
,
fn
,
fnArgs
=
Array
.
prototype
.
slice
.
call
(
arguments
,
1
)
,
hookType
;
if
(
typeof
fnArgs
[
fnArgs
.
length
-
1
]
===
'function'
)
{
fn
=
fnArgs
.
pop
();
}
if
(
typeof
hooks
===
'string'
)
{
hookType
=
hooks
;
hooks
=
this
.
options
.
hooks
[
hooks
]
||
[];
}
...
...
@@ -76,53 +94,20 @@ Hooks.runHooks = function() {
hooks
=
hooks
===
undefined
?
[]
:
[
hooks
];
}
var
promise
=
new
Promise
(
function
(
resolve
,
reject
)
{
if
(
hooks
.
length
<
1
)
{
return
resolve
(
fnArgs
);
}
var
run
=
function
(
hook
)
{
if
(
!
hook
)
{
return
resolve
(
resolveArgs
);
}
var
promise
=
Promise
.
map
(
hooks
,
function
(
hook
)
{
if
(
typeof
hook
===
'object'
)
{
hook
=
hook
.
fn
;
}
var
maybePromise
=
hook
.
apply
(
self
,
fnArgs
.
concat
(
function
()
{
tick
++
;
if
(
!!
arguments
[
0
])
{
return
reject
(
arguments
[
0
]);
}
if
(
arguments
.
length
)
{
resolveArgs
=
Array
.
prototype
.
slice
.
call
(
arguments
,
1
);
if
(
hookType
&&
hook
.
length
>
hookTypes
[
hookType
].
params
)
{
hook
=
Promise
.
promisify
(
hook
,
self
);
}
return
run
(
hooks
[
tick
]);
}));
if
(
Utils
.
Promise
.
is
(
maybePromise
))
{
maybePromise
.
spread
(
function
()
{
tick
++
;
if
(
arguments
.
length
)
{
resolveArgs
=
Array
.
prototype
.
slice
.
call
(
arguments
);
}
return
run
(
hooks
[
tick
]);
},
reject
);
}
};
run
(
hooks
[
tick
]);
});
return
hook
.
apply
(
self
,
fnArgs
);
},
{
concurrency
:
1
}).
return
();
if
(
fn
)
{
promise
=
promise
.
spread
(
function
()
{
fn
.
apply
(
self
,
[
null
].
concat
(
Array
.
prototype
.
slice
.
apply
(
arguments
)));
},
fn
);
return
promise
.
nodeify
(
fn
);
}
return
promise
;
...
...
@@ -147,16 +132,12 @@ Hooks.addHook = function(hookType, name, fn) {
name
=
null
;
}
var
method
=
function
()
{
return
fn
.
apply
(
this
,
Array
.
prototype
.
slice
.
call
(
arguments
,
0
,
arguments
.
length
-
1
).
concat
(
arguments
[
arguments
.
length
-
1
]));
};
// Aliases
hookType
=
hookAliases
[
hookType
]
||
hookType
;
// Just in case if we override the default DAOFactory.options
this
.
options
.
hooks
[
hookType
]
=
this
.
options
.
hooks
[
hookType
]
||
[];
this
.
options
.
hooks
[
hookType
][
this
.
options
.
hooks
[
hookType
].
length
]
=
!!
name
?
{
name
:
name
,
fn
:
method
}
:
method
;
this
.
options
.
hooks
[
hookType
][
this
.
options
.
hooks
[
hookType
].
length
]
=
!!
name
?
{
name
:
name
,
fn
:
fn
}
:
fn
;
return
this
;
};
...
...
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