I'm building an app in JavaScript (ES6). I'm more familiar with C#, so if my lingo is a bit off, I apologize.
Anyways, in my app, I want to create a service that supports other services. Basically, when my service is created, I want to dynamically load some services that implement my "interface". That "interface" has a single function named "sendMessage". Then, when I call MyService.sendMessage('someMessage'), that message will automatically get sent out via the registered services. I'm trying to accomplish something like this:
const MyService {
initialize: function() {
this.services = [];
// Somehow load services here
// This function will send the message to the plugins
this.sendMessage = function(msg) {
for (let service in services) {
service.sendMessage(msg);
}
};
}
};
The above doesn't work because, the services aren't getting populated in anyway. I know that JavaScript doesn't have an interface
keyword like C#. For that reason, I know that I need a way to "register" services. My question But, I'm not sure how to do this.
How do I create a service such that a third-party developer can plugin to my service?