From 2a4d14bb038ae88a44e8deff431a5123ff377935 Mon Sep 17 00:00:00 2001 From: Nishimura-Katsuo <38635886+Nishimura-Katsuo@users.noreply.github.com> Date: Fri, 31 Aug 2018 21:53:21 -0600 Subject: [PATCH] Packet.addListener & Misc.poll added (#662) Packet.addListener & Misc.poll added --- d2bs/kolbot/libs/common/Misc.js | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/d2bs/kolbot/libs/common/Misc.js b/d2bs/kolbot/libs/common/Misc.js index 94e29dc97..10df835ac 100644 --- a/d2bs/kolbot/libs/common/Misc.js +++ b/d2bs/kolbot/libs/common/Misc.js @@ -1938,6 +1938,19 @@ MainLoop: } return obj; + }, + + poll: function (check, timeout = 6000, sleep = 40) { + let ret, start = getTickCount(); + + while (getTickCount() - start <= timeout) { + if ((ret = check())) + return ret; + + delay(sleep); + } + + return false; } }; @@ -2344,7 +2357,22 @@ CursorLoop: if (value > 0) { getPacket(1, 0x1d, 1, stat, 1, value); } - } + }, + + addListener: function (packetType, callback) { // specialized wrapper for addEventListener + if (typeof packetType === 'number') + packetType = [packetType]; + + if (typeof packetType === 'object' && packetType.length) { + addEventListener('gamepacket', packet => (packetType.indexOf(packet[0]) > -1 ? callback(packet) : false)); + + return callback; + } + + return null; + }, + + removeListener: callback => removeEventListener('gamepacket', callback), // just a wrapper }; var Messaging = {