Skip to content

Commit

Permalink
Add game system support
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelVo committed Apr 15, 2021
1 parent 8753a83 commit de4a289
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
## In development
### New features
- Added support for game systems

## 1.0.0
### Initial release
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
23 changes: 19 additions & 4 deletions src/socketlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down

0 comments on commit de4a289

Please sign in to comment.