-
-
Notifications
You must be signed in to change notification settings - Fork 825
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enhance Service Provider to Support Chained Parsing Methods #858
Comments
Just use a higher order function:
|
But in this case, it cannot be called as below and we have an error. await core.make('namespace/api').greet('World').firstParser(); server log is: It can only be called as follows: const result = await core.make('namespace/api').greet('World');
const firstParserResult = result.firstParser(); |
It is because of the
|
You actually do not need the await here, your operations are synchronous. |
If you do have all async operations then you have to do either
Just standard js async stuff :) |
Isn't this a better syntax? export class MyApiServiceProvider
{
constructor(core, options = {}) {
this.core = core;
this.options = options;
}
provides() {
return ['namespace/api'];
}
async init() {
this.core.singleton('namespace/api', () => ({
greet: name => this.greet(name)
}));
}
async greet(name) {
const result = `hi + ${name}`
// Return an object with chainable methods
return {
fistParser: () => this.fistParser(result),
secoundparse: () => this.secoundParse(result),
};
}
fistParser(input) {
// Define your first parsing logic here
console.log('first parse: ', input);
return {parsed: 'first', output: input};
}
secoundParser(input) {
// Define your secound parsing logic here
console.log('secound parse: ', input);
return {parsed: 'secound', output: input};
}
} |
I use your code, but in the log I have the following value:
|
How you write stuff is up to you. I just provide examples. Working example of what I've written above: https://codesandbox.io/p/sandbox/blue-cherry-9zqzlz?file=%2Fsrc%2Findex.js%3A35%2C1-36%2C1&workspaceId=17bde2a4-055b-48f4-944a-81ffb405ae07 This should print "Hello World Sync" from a sync chained provider, and "Hello World Acync" of the same, but chained async.
|
FYI: There is no special "OS.js stuff" going on here. A service provider simply provides an object, function or whatever you define in the registration (instance/singleton) callback. How this is implemented and consumed is fully up to you. |
If you look at the internal implementation of these providers, you'll se that a "contract" pattern is used to make it easier to maintain. Examples:
This can be abstracted further with other patterns. |
hello @andersevenrud.
I reviewed the section on creating a service provider in the osjs documentation.
This service provider is called as follows:
But I want to have a service provider that can be called this way:
The text was updated successfully, but these errors were encountered: