I would like to feature flag a service in my React app so I can toggle it on and off by changing a boolean in a config file. However, I am unsure as to how best to do so. The service is as below:
class Service {
constructor() {
// do stuff
}
doA() {
// do stuff
}
doB() {
// do stuff
}
// more methods
}
export const serviceInstance = new Service();
This service's methods are called in different parts of the application.
Currently, I am flagging it by creating another class with the same methods and attributes as Service and then exporting the relevant instance based on the feature flag boolean:
class ServiceMock {
constructor() {
return;
}
doA() {
return;
}
doB() {
return;
}
// more empty return methods
}
export const serviceInstance = serviceFlag ? new Service() : new ServiceMock();
It works but I am worried that this is rather inefficient, especially when the number of methods and attributes grow. Is there a better way to feature flag something like this?
I'm new to TypeScript, but does this help any?
If anyone has improvements or suggestions, I'm still struggling with coming to TypeScript from python and would be happy to improve this answer.
It runs without error at TypeScript Playground.