@@ -2,16 +2,24 @@ let idInc = 0;
22
33const genPluginMethod = ( orig , pluginName , smp , type ) =>
44 function ( method , func ) {
5- const id = idInc ++ ;
6- const timeEventName = type + "/" + method ;
7-
85 const wrappedFunc = ( ...args ) => {
6+ const id = idInc ++ ;
7+ const timeEventName = pluginName + "/" + type + "/" + method ;
8+ // we don't know if there's going to be a callback applied to a particular
9+ // call, so we just set it multiple times, letting each one override the last
10+ const addEndEvent = ( ) =>
11+ smp . addTimeEvent ( "plugins" , timeEventName , "end" , { id } ) ;
12+
913 smp . addTimeEvent ( "plugins" , timeEventName , "start" , {
1014 id,
1115 name : pluginName ,
1216 } ) ;
13- const ret = func . apply ( this , args . map ( a => wrap ( a , pluginName , smp ) ) ) ;
14- smp . addTimeEvent ( "plugins" , timeEventName , "end" , { id } ) ;
17+ const ret = func . apply (
18+ this ,
19+ args . map ( a => wrap ( a , pluginName , smp , addEndEvent ) )
20+ ) ;
21+ addEndEvent ( ) ;
22+
1523 return ret ;
1624 } ;
1725
@@ -27,17 +35,42 @@ const construcNamesToWrap = [
2735 "ContextModuleFactory" ,
2836] ;
2937
30- const wrap = ( orig , pluginName , smp ) => {
31- const origConstrucName = orig && orig . constructor && orig . constructor . name ;
32- const shouldWrap = construcNamesToWrap . includes ( origConstrucName ) ;
33- if ( ! shouldWrap ) return orig ;
38+ const wrap = ( orig , pluginName , smp , addEndEvent ) => {
39+ if ( ! orig ) return orig ;
40+
41+ const getOrigConstrucName = target =>
42+ target && target . constructor && target . constructor . name ;
43+ const getShouldWrap = target => {
44+ const origConstrucName = getOrigConstrucName ( target ) ;
45+ return construcNamesToWrap . includes ( origConstrucName ) ;
46+ } ;
47+ const shouldWrap = getShouldWrap ( orig ) ;
48+ const shouldSoftWrap = Object . values ( orig ) . some ( getShouldWrap ) ;
49+
50+ if ( ! shouldWrap && ! shouldSoftWrap ) {
51+ const vanillaFunc =
52+ typeof orig === "function" &&
53+ orig &&
54+ orig . prototype &&
55+ orig . prototype . constructor !== orig ;
56+ return vanillaFunc
57+ ? function ( ) {
58+ const ret = orig ( ) ;
59+ addEndEvent ( ) ;
60+ return ret ;
61+ }
62+ : orig ;
63+ }
3464
3565 const proxy = new Proxy ( orig , {
3666 get : ( target , property ) => {
37- if ( property === "plugin" )
38- return genPluginMethod ( orig , pluginName , smp , origConstrucName ) . bind (
39- proxy
40- ) ;
67+ if ( shouldWrap && property === "plugin" )
68+ return genPluginMethod (
69+ orig ,
70+ pluginName ,
71+ smp ,
72+ getOrigConstrucName ( target )
73+ ) . bind ( proxy ) ;
4174
4275 if ( typeof orig [ property ] === "function" )
4376 return orig [ property ] . bind ( proxy ) ;
@@ -47,7 +80,7 @@ const wrap = (orig, pluginName, smp) => {
4780 return Reflect . set ( target , property , value ) ;
4881 } ,
4982 deleteProperty : ( target , property ) => {
50- delete target [ property ] ;
83+ return Reflect . deleteProperty ( target , property ) ;
5184 } ,
5285 } ) ;
5386
0 commit comments