Skip to content

Commit

Permalink
Packet.addListener & Misc.poll added (#662)
Browse files Browse the repository at this point in the history
Packet.addListener & Misc.poll added
  • Loading branch information
Nishimura-Katsuo authored and noah- committed Sep 1, 2018
1 parent 9357d30 commit 2a4d14b
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion d2bs/kolbot/libs/common/Misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
};

Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit 2a4d14b

Please sign in to comment.