Skip to content

Commit a317a79

Browse files
committed
Check for handler presence before invoking it
If a waypoint unmounts another waypoint as part of handling an onEnter/onLeave, and the two waypoints are attached to the same scrollable ancestor, we could end up with this error: Uncaught TypeError: handlers[index] is not a function Consider the following example, where the first handler removes the second: ```js const a = {} a.handlers = { 1: function() { delete a.handlers[2]; }, 2: function() {}, }; Object.keys(a.handlers).forEach(function(index) { a.handlers[index](); }); // => Uncaught TypeError: a.handlers[index] is not a function(…) ``` Adding a guard against undefined handlers fixes the problem. Testing this turned out to be quite hard, so for now I'm just rolling with a manual fix.
1 parent 6ce28c6 commit a317a79

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/waypoint.jsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,14 @@ class TargetEventHandlers {
7171
handleEvent(eventName, event) {
7272
const { handlers } = this.getEventHandlers(eventName);
7373
Object.keys(handlers).forEach(function(index) {
74-
handlers[index](event);
74+
const handler = handlers[index];
75+
if (handler) {
76+
// We need to check for presence here because a handler function may
77+
// cause later handlers to get removed. This can happen if you for
78+
// instance have a waypoint that unmounts another waypoint as part of an
79+
// onEnter/onLeave handler.
80+
handler(event);
81+
}
7582
});
7683
}
7784

0 commit comments

Comments
 (0)