codeoverflow-org / nodecg-io

A NodeCG-bundle which implements Social Media API's in the NodeCG framework
https://nodecg.io
MIT License
108 stars 26 forks source link

Reduce boilerplate of service implementation #22

Closed hlxid closed 4 years ago

hlxid commented 4 years ago

Description

There currently exists way too much boilerplate in the client implementation. To do this the implementation should be moved into a class which extends a class provided by the framework. This base class could do things like getting the core using a nodecg instance and printing a log if it is not loaded.

TimTechDev commented 4 years ago

When we move

if (core === undefined) {
        nodecg.log.error("nodecg-io-core isn't loaded! Discord bundle won't function without it.");
        return undefined;
}

into the framework, when will it report that there is no framework?

hlxid commented 4 years ago

We can export the base class and import it in the service bundle just as a normal node dependency as they get linked for typings anyway. The constructor of that class can then do this call by using the nodecg instance that it gets passed by the service bundle. And that the node lib of the core is available is forced by the npm dependency. I have not tested it yet but to my understanding this should work.

TimTechDev commented 4 years ago

Yes, it does indeed work (tested in my branch enhancement/service-boilerplate). But why should we report that there is no framework when the code witch would report that is in the framework itself?

hlxid commented 4 years ago

That the node library is available is ensured by npm, but that the nodecg bundle has been loaded is a different story, we can't ensure that. People might forget to put it in or it might have crashed at startup, in which case nodecg just proceeds with loading the next bundles, meaning the service bundles. Thus we should still check that it has been loaded just as a safety measure.

TimTechDev commented 4 years ago

ok, I will add the check back.