From de4a2893958f426c4d78c9d5a4245b1674ca1a89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20V=C3=B6gele?= Date: Thu, 15 Apr 2021 09:51:46 +0200 Subject: [PATCH] Add game system support --- CHANGELOG.md | 4 ++++ README.md | 11 +++++++++++ src/socketlib.js | 23 +++++++++++++++++++---- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1361889..fd0046a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,2 +1,6 @@ +## In development +### New features +- Added support for game systems + ## 1.0.0 ### Initial release diff --git a/README.md b/README.md index e86f091..7db7a2d 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,17 @@ Call `registerModule` to make socketlib listen for sockets that come in for your **Return value**: A socket instance is returned, that is used for all further interactions with socketlib. +#### socketlib.registerSystem +```javascript +registerSystem(systemId); +``` + +Call `registerSystem` to make socketlib listen for sockets that come in for your game system. This is the first function in socketlib that your game system should call. + +- **systemId** the id of your game system as specified in your game system's manifest. + +**Return value**: A socket instance is returned, that is used for all further interactions with socketlib. + #### socket.register ```javascript socket.register(name, func); diff --git a/src/socketlib.js b/src/socketlib.js index e5cb133..9683bc4 100644 --- a/src/socketlib.js +++ b/src/socketlib.js @@ -39,17 +39,32 @@ class Socketlib { console.error(`socketlib | Failed to register socket for module '${moduleName}'. Please set '"socket":true' in your manifset and restart foundry (you need to reload your world - simply reloading your browser won't do).`); return undefined; } - const newSocket = new SocketlibSocket(moduleName); + const newSocket = new SocketlibSocket(moduleName, "module"); this.modules.set(moduleName, newSocket); return newSocket; } + + registerSystem(systemId) { + if (game.system.id !== systemId) { + console.error(`socketlib | Someone tried to register system '${systemId}', but that system isn't active. As a result the registration request has been ignored.`); + return undefined; + } + const existingSocket = this.system; + if (existingSocket) + return existingSocket; + if (!game.system.data.socket) { + console.error(`socketlib | Failed to register socket for system '${systemId}'. Please set '"socket":true' in your manifest and restart foundry (you need to reload your world - simply reloading your browser won't do).`); + } + const newSocket = new SocketlibSocket(systemId, "system"); + this.system = newSocket; + return newSocket; + } } class SocketlibSocket { - constructor(moduleName) { - this.moduleName = moduleName; + constructor(moduleName, moduleType) { this.functions = new Map(); - this.socketName = `module.${moduleName}`; + this.socketName = `${moduleType}.${moduleName}`; this.pendingRequests = new Map(); game.socket.on(this.socketName, this._onSocketReceived.bind(this)); }