From bf4b7f8fedc4e452686f2f9eca7311a1f7145f2b Mon Sep 17 00:00:00 2001 From: pandamicro Date: Tue, 21 May 2019 17:08:50 +0800 Subject: [PATCH] Improve once off logic in callbacks invoker (#4467) --- cocos/core/event/callbacks-invoker.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/cocos/core/event/callbacks-invoker.ts b/cocos/core/event/callbacks-invoker.ts index bbbb4f6bb0..b761cfe390 100644 --- a/cocos/core/event/callbacks-invoker.ts +++ b/cocos/core/event/callbacks-invoker.ts @@ -299,14 +299,17 @@ export class CallbacksInvoker { for (let i = 0, len = infos.length; i < len; ++i) { const info = infos[i]; if (info) { - if (info.target) { - info.callback.call(info.target, ...args); + const callback = info.callback; + const target = info.target; + // Pre off once callbacks to avoid influence on logic in callback + if (info.once) { + this.off(key, callback, target); + } + if (target) { + callback.call(target, ...args); } else { - info.callback(...args); - } - if (info.once) { - this.off(key, info.callback, info.target); + callback(...args); } } }