Globally accessible SDK instance
fahad19 opened this issue · comments
Currently
We create a new SDK instance like this:
// featurevisorInstance.js
import { createInstance } from "@featurevisor/sdk";
export const instance = createInstance({
datafileUrl: "...",
});
If we wish to use this SDK anywhere, we have to pass the instance by reference.
This is somewhat manageable when using other frameworks, since they popularize the idea of passing around values by reference:
- React.js: https://featurevisor.com/docs/react/
- Vue.js: https://featurevisor.com/docs/vue/
- Express.js: https://featurevisor.com/docs/frameworks/express/
Challenges
While the API is clean and does not pollute any globals anywhere, it might be beneficial for many if:
- there was a quick way to instantiate the SDK in one place, and then
- access the same instance from anywhere else in the app
- without needing any deep
require()
paths
From above code snippet, we might import the Featurevisor SDK instance as follows:
// somewhere/deep/in/my/app.js
import { instance } from "../../../featurevisorInstance";
Proposed API
We could introduce a new global
option in createInstance()
:
// featurevisorInstance.js
import { createInstance } from "@featurevisor/sdk";
// no need to export
const instance = createInstance({
datafileUrl: "...",
// new option
global: true,
});
Now from anywhere in the app, we could import the instance directly from the package:
// somewhere/deep/in/my/app.js
import { instance } from "@featurevisor/sdk";
or we could introduce a new getInstance()
function:
// somewhere/deep/in/my/app.js
import { getInstance } from "@featurevisor/sdk";
const instance = getInstance();
I am still very undecided about whether this is a good or a bad approach, but I am all for developer convenience.
In short, the only benefit that can't be achieved by instantiating and exporting the Featurevisor from the application level's module:
// my/app/modules/featurevisor.js
import { createInstance } from "@featurevisor/sdk";
export const instance = createInstance({
datafileUrl: "...",
});
...is the nice and short import path:
// my/app/deep/in/somewhere.js
import { instance } from "../../../modules/featurevisor.js";
In my opinion, that doesn't justify adding more complexity to the API. Better keep the library simple.
@1e9y: thanks for the feedback!
I will close the issue for now then.
the use case for accessing the same instance from various different modules was found here: https://featurevisor.com/docs/frameworks/nextjs/