From aa2df41c533e021aa4a1acb4b7e6c79bccfe7b50 Mon Sep 17 00:00:00 2001 From: TrickyPi <33021497+TrickyPi@users.noreply.github.com> Date: Thu, 18 Aug 2022 20:13:34 +0800 Subject: [PATCH] fix: use a right reference for hooks (#156) --- src/patch/xmlhttprequest.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/patch/xmlhttprequest.js b/src/patch/xmlhttprequest.js index d8a510c..795d20f 100644 --- a/src/patch/xmlhttprequest.js +++ b/src/patch/xmlhttprequest.js @@ -8,6 +8,7 @@ import { import { EventEmitter } from "../misc/event-emitter"; import headers from "../misc/headers"; import formData from "./formdata"; +import hooks from "../misc/hooks"; const nullify = res => (res === undefined ? null : res); @@ -143,11 +144,11 @@ const Xhook = function() { return; } //before emitting 4, run all 'after' hooks in sequence - const hooks = xhook.listeners("after"); + const afterHooks = hooks.listeners("after"); var process = function() { - if (hooks.length > 0) { + if (afterHooks.length > 0) { //execute each 'before' hook one at a time - const hook = hooks.shift(); + const hook = afterHooks.shift(); if (hook.length === 2) { hook(request, response); process(); @@ -207,7 +208,7 @@ const Xhook = function() { // initialise 'withCredentials' on facade xhr in browsers with it // or if explicitly told to do so - if ("withCredentials" in xhr || xhook.addWithCredentials) { + if ("withCredentials" in xhr) { facade.withCredentials = false; } facade.status = 0; @@ -295,10 +296,10 @@ const Xhook = function() { xhr.send(request.body); }; - const hooks = xhook.listeners("before"); - //process hooks sequentially + const beforeHooks = hooks.listeners("before"); + //process beforeHooks sequentially var process = function() { - if (!hooks.length) { + if (!beforeHooks.length) { return send(); } //go to next hook OR optionally provide response @@ -316,7 +317,7 @@ const Xhook = function() { setReadyState(4); return; } - //continue processing until no hooks left + //continue processing until no beforeHooks left process(); }; //specifically provide headers (readyState 2) @@ -330,7 +331,7 @@ const Xhook = function() { setReadyState(3); }; - const hook = hooks.shift(); + const hook = beforeHooks.shift(); //async or sync? if (hook.length === 1) { done(hook(request));