adamdickinson / react-service

Expose internal application APIs as a service.
3 stars 1 forks source link

Feature Request: Extended Services #2

Open adamdickinson opened 4 years ago

adamdickinson commented 4 years ago

I'm looking to export two service providers from one package - one to handle a normal service, one to handle the mocked variation. Trouble is, both would need to use the same context in order to enable reuse of existing parts throughout the code. That is, I want to be able to switch the provider <MyService> with a mocked version <MyMockedService>.

I'm thinking the following workflow would work well:

const [MyService, useMyService, MyServiceContext] = createService(useMyServiceApi);
const MyMockedService = extendService(MyServiceContext, useMyMockedServiceApi);

They would essentially be the same process, differing only in whether context is provided or created. Alternatively, we could just re-use createService in these ways:

// createService(hook, context?) => [ServiceProvider, ServiceHook];
const [MyService, useMyService, MyServiceContext] = createService(useMyServiceApi);
const MyMockedService = createService(useMyMockedServiceApi, MyServiceContext);
// createServices(hookA, hookB, ...) => [ServiceProviderA, ServiceProviderB, ..., ServiceHook];
const [MyService, MyMockedService, useMyService] = createServices(useMyServiceApi, useMyMockedServiceApi);
adamdickinson commented 3 years ago

Done, and pretty much exactly as specified at the top. Need to add to docs.