Skip to content

Commit

Permalink
fix(adapter-puppeteer): Add prependListener [email protected] removed (#…
Browse files Browse the repository at this point in the history
…368)

Co-authored-by: renfangchao <[email protected]>
  • Loading branch information
RFC2109 and renfangchao authored Dec 12, 2020
1 parent 326ddd2 commit 6c35fd3
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion packages/@pollyjs/adapter-puppeteer/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,26 @@ export default class PuppeteerAdapter extends Adapter {
if (this[LISTENERS].has(target)) {
const listeners = this[LISTENERS].get(target);

// puppeteer remove prependListener after v4.0.0, polyfill it if missing
const prependListenerPolyfill = function(event, handler) {
const all = this.emitter.all;
const handlers = all.get(event);
const added = handlers && handlers.unshift(handler);

if (!added) {
all.set(event, [handler]);
}
};

for (const eventName in listeners) {
target[methodName].apply(target, [eventName, listeners[eventName]]);
const prependListener =
target.prependListener || prependListenerPolyfill;
const fn =
methodName === 'prependListener'
? prependListener
: target[methodName];

fn.apply(target, [eventName, listeners[eventName]]);
}
}
}
Expand Down

0 comments on commit 6c35fd3

Please sign in to comment.