The Problem
When using Gumband you should only ever call new Gumband(...)
once. This is because only one SDK is allowed to connect to a Gumband Exhibit in the cloud at a time. Creating multiple new Gumband()
calls will violate this invariant and result in a message like the one below.
The Solution
If you need to call new Gumband()
more than once in your code you can prevent this error by using a singleton wrapping class.
How You can Use a Singleton
Creating a singleton class requires creating a static class property, and then setting the value of that property to this
. Then when the constructor runs you check for that static property first and try to return that instead of a whole new class.
Once you implement this pattern you can feel free to run new GumbandService()
(your wrapper class) multiple times in your code to get reference to any properties and methods you desire without duplicating calls to new Gumband()
.
// File: your-gumband-project/gumband/gumband-service-wrapper.js // Imports const { Gumband } = require("@deeplocal/gumband-node-sdk"); /** * A class that wraps the Gumband SDK and handles websocket messages * that come from the Gumband Cloud. */ class GumbandService { /** * A reference to the Gumband SDK instance. */ gumbandSDK; // Static variable to hold the singleton instance static instance; constructor() { // Check if an instance already exists // If it does, return the existing instance if (GumbandService.instance) { return GumbandService.instance; } // Create the Gumband SDK instance this.gumbandSDK = new Gumband(...contructorArgs); // Store the instance in the static variable GumbandService.instance = this; } // ... your other class methods } // Export the GumbandService class module.exports = { GumbandService };