@@ -32,9 +32,10 @@ export default function patchMethod<
3232 this : Instance ,
3333 superMethod : SuperMethod ,
3434 ...args : Parameters < SuperMethod >
35- ) => ReturnType < SuperMethod >
35+ ) => ReturnType < SuperMethod > ,
36+ fallback ?: ( this : Instance , ...args : Parameters < SuperMethod > ) => ReturnType < SuperMethod >
3637) : Class {
37- const superMethod : SuperMethod = klass . prototype [ methodName ]
38+ const superMethod : SuperMethod = klass . prototype [ methodName ] || fallback
3839
3940 klass . prototype [ methodName ] = function (
4041 this : Instance ,
@@ -70,11 +71,21 @@ export function beforeMethod<
7071 Instance extends InstanceType < Class > ,
7172 K extends PropertiesOfType < Instance , Function > ,
7273 SuperMethod extends Instance [ K ]
73- > ( klass : Class , methodName : K , fn : ( this : Instance , ...args : Parameters < SuperMethod > ) => any ) {
74- return patchMethod ( klass , methodName , function ( superMethod , ...args ) {
75- fn . call ( this , ...args )
76- return superMethod ( ...args )
77- } )
74+ > (
75+ klass : Class ,
76+ methodName : K ,
77+ fn : ( this : Instance , ...args : Parameters < SuperMethod > ) => any ,
78+ fallback ?: ( this : Instance , ...args : Parameters < SuperMethod > ) => ReturnType < SuperMethod >
79+ ) {
80+ return patchMethod (
81+ klass ,
82+ methodName ,
83+ function ( superMethod , ...args ) {
84+ fn . call ( this , ...args )
85+ return superMethod ( ...args )
86+ } ,
87+ fallback
88+ )
7889}
7990
8091/**
@@ -109,11 +120,17 @@ export function afterMethod<
109120 this : Instance ,
110121 returnValue : ReturnType < SuperMethod > ,
111122 ...args : Parameters < SuperMethod >
112- ) => any
123+ ) => any ,
124+ fallback ?: ( this : Instance , ...args : Parameters < SuperMethod > ) => ReturnType < SuperMethod >
113125) {
114- return patchMethod ( klass , methodName , function ( superMethod , ...args ) {
115- const returnValue = superMethod ( ...args )
116- fn . call ( this , returnValue , ...args )
117- return returnValue
118- } )
126+ return patchMethod (
127+ klass ,
128+ methodName ,
129+ function ( superMethod , ...args ) {
130+ const returnValue = superMethod ( ...args )
131+ fn . call ( this , returnValue , ...args )
132+ return returnValue
133+ } ,
134+ fallback
135+ )
119136}
0 commit comments