From 326bc0e0171ba2e768dc4b7f724e0d2d6107e589 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20V=C3=B6gele?= Date: Fri, 16 Apr 2021 15:32:51 +0200 Subject: [PATCH] Execute functions locally if the current user is explicitly set as a recipient --- CHANGELOG.md | 5 +++++ src/socketlib.js | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 18748c8..aec2505 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## In development +### Bugfixes +- `ExecuteAsUser` and `ExecuteForUsers` didn't execute locally if the id of the current user was passed in as recipient. + + ## 1.0.1 ### New features - Added support for game systems diff --git a/src/socketlib.js b/src/socketlib.js index 9683bc4..7f2fc29 100644 --- a/src/socketlib.js +++ b/src/socketlib.js @@ -96,6 +96,8 @@ class SocketlibSocket { async executeAsUser(handler, userId, ...args) { const [name, func] = this._resolveFunction(handler); + if (userId === game.userId) + return func(...args); const user = game.users.get(userId); if (!user) throw new SocketlibInvalidUserError(`No user with id '${userId}' exists.`); @@ -118,7 +120,13 @@ class SocketlibSocket { if (!(recipients instanceof Array)) throw new TypeError("Recipients parameter must be an array of user ids."); const [name, func] = this._resolveFunction(handler); + const currentUserIndex = recipients.indexOf(game.userId); + if (currentUserIndex >= 0) + recipients.splice(currentUserIndex, 1); this._sendCommand(name, args, recipients); + if (currentUserIndex >= 0) + // TODO Make this function call run async, even if the function isn't delcared as async to prevent exceptions to propagate through executeForUsers + func(...args); } _sendRequest(handlerName, args, recipient) {