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
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
35 additions
and
54 deletions
lib/hooks.js
lib/hooks.js
View file @
e52cf43
...
@@ -35,6 +35,22 @@ var Utils = require('./utils')
...
@@ -35,6 +35,22 @@ var Utils = require('./utils')
* @mixin Hooks
* @mixin Hooks
*/
*/
var
Hooks
=
module
.
exports
=
function
()
{};
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
=
{
var
hookAliases
=
{
beforeDelete
:
'beforeDestroy'
,
beforeDelete
:
'beforeDestroy'
,
afterDelete
:
'afterDestroy'
,
afterDelete
:
'afterDestroy'
,
...
@@ -59,16 +75,18 @@ Hooks.replaceHookAliases = function(hooks) {
...
@@ -59,16 +75,18 @@ Hooks.replaceHookAliases = function(hooks) {
return
hooks
;
return
hooks
;
};
};
Hooks
.
runHooks
=
function
()
{
Hooks
.
runHooks
=
function
(
hooks
)
{
var
self
=
this
var
self
=
this
,
tick
=
0
,
fn
,
hooks
=
arguments
[
0
]
,
fnArgs
=
Array
.
prototype
.
slice
.
call
(
arguments
,
1
)
,
lastIndex
=
arguments
.
length
-
1
,
hookType
;
,
fn
=
typeof
arguments
[
lastIndex
]
===
'function'
?
arguments
[
lastIndex
]
:
null
,
fnArgs
=
Array
.
prototype
.
slice
.
call
(
arguments
,
1
,
fn
?
lastIndex
:
arguments
.
length
)
if
(
typeof
fnArgs
[
fnArgs
.
length
-
1
]
===
'function'
)
{
,
resolveArgs
=
fnArgs
;
fn
=
fnArgs
.
pop
();
}
if
(
typeof
hooks
===
'string'
)
{
if
(
typeof
hooks
===
'string'
)
{
hookType
=
hooks
;
hooks
=
this
.
options
.
hooks
[
hooks
]
||
[];
hooks
=
this
.
options
.
hooks
[
hooks
]
||
[];
}
}
...
@@ -76,53 +94,20 @@ Hooks.runHooks = function() {
...
@@ -76,53 +94,20 @@ Hooks.runHooks = function() {
hooks
=
hooks
===
undefined
?
[]
:
[
hooks
];
hooks
=
hooks
===
undefined
?
[]
:
[
hooks
];
}
}
var
promise
=
new
Promise
(
function
(
resolve
,
reject
)
{
var
promise
=
Promise
.
map
(
hooks
,
function
(
hook
)
{
if
(
hooks
.
length
<
1
)
{
if
(
typeof
hook
===
'object'
)
{
return
resolve
(
fnArgs
)
;
hook
=
hook
.
fn
;
}
}
var
run
=
function
(
hook
)
{
if
(
hookType
&&
hook
.
length
>
hookTypes
[
hookType
].
params
)
{
if
(
!
hook
)
{
hook
=
Promise
.
promisify
(
hook
,
self
);
return
resolve
(
resolveArgs
);
}
}
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
);
}
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
);
}
};
r
un
(
hooks
[
tick
]
);
r
eturn
hook
.
apply
(
self
,
fnArgs
);
});
}
,
{
concurrency
:
1
}).
return
(
);
if
(
fn
)
{
if
(
fn
)
{
promise
=
promise
.
spread
(
function
()
{
return
promise
.
nodeify
(
fn
);
fn
.
apply
(
self
,
[
null
].
concat
(
Array
.
prototype
.
slice
.
apply
(
arguments
)));
},
fn
);
}
}
return
promise
;
return
promise
;
...
@@ -147,16 +132,12 @@ Hooks.addHook = function(hookType, name, fn) {
...
@@ -147,16 +132,12 @@ Hooks.addHook = function(hookType, name, fn) {
name
=
null
;
name
=
null
;
}
}
var
method
=
function
()
{
return
fn
.
apply
(
this
,
Array
.
prototype
.
slice
.
call
(
arguments
,
0
,
arguments
.
length
-
1
).
concat
(
arguments
[
arguments
.
length
-
1
]));
};
// Aliases
// Aliases
hookType
=
hookAliases
[
hookType
]
||
hookType
;
hookType
=
hookAliases
[
hookType
]
||
hookType
;
// Just in case if we override the default DAOFactory.options
// 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
]
||
[];
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
;
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