-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
eclipse-che/che#11306 add doc and comments
Signed-off-by: Yevhen Vydolob <yvydolob@redhat.com>
- Loading branch information
Showing
2 changed files
with
141 additions
and
1 deletion.
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
packages/plugin-ext/doc/how-to-add-new-plugin-namespace.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# This document how to add new plugin api namespace | ||
|
||
New Plugin API namespace should be packaged as Theia extension | ||
|
||
## Provide your API or namespace | ||
|
||
We assuming that you provide your API as separate npm package. | ||
In that package you can declare your api. | ||
Example `foo.d.ts`: | ||
|
||
```typescript | ||
declare module '@bar/foo' { | ||
export namespace fooBar { | ||
export function getFoo(): Foo; | ||
} | ||
} | ||
``` | ||
|
||
## Declare `ExtPluginApiProvider` implementation | ||
|
||
```typescript | ||
@injectable() | ||
export class FooPluginApiProvider implements ExtPluginApiProvider { | ||
provideApi(): ExtPluginApi { | ||
return { | ||
frontendExtApi: { | ||
initPath: '/path/to/foo/api/implementation.js', | ||
initFunction: 'fooInitializationFunction', | ||
initVariable: 'foo_global_variable' | ||
}, | ||
backendInitPath: path.join(__dirname, 'path/to/backend/foo/implementation.js') | ||
}; | ||
} | ||
} | ||
``` | ||
|
||
## Then you need to register `FooPluginApiProvider`, add next sample in your backend module: | ||
|
||
```typescript | ||
bind(FooPluginApiProvider).toSelf().inSingletonScope(); | ||
bind(Symbol.for(ExtPluginApiProvider)).toService(FooPluginApiProvider); | ||
``` | ||
|
||
## Next you need to implement `ExtPluginApiBackendInitializationFn`, which should hanlde `@bar/foo` module loading and instantiate `@foo/bar` API object, `path/to/backend/foo/implementation.js` example : | ||
|
||
```typescript | ||
export const provideApi: ExtPluginApiBackendInitializationFn = (rpc: RPCProtocol, pluginManager: PluginManager) => { | ||
cheApiFactory = createAPIFactory(rpc); | ||
plugins = pluginManager; | ||
|
||
if (!isLoadOverride) { | ||
overrideInternalLoad(); | ||
isLoadOverride = true; | ||
} | ||
|
||
}; | ||
|
||
function overrideInternalLoad(): void { | ||
const module = require('module'); | ||
const internalLoad = module._load; | ||
|
||
module._load = function (request: string, parent: any, isMain: {}) { | ||
if (request !== '@bar/foo') { | ||
return internalLoad.apply(this, arguments); | ||
} | ||
|
||
const plugin = findPlugin(parent.filename); | ||
if (plugin) { | ||
let apiImpl = pluginsApiImpl.get(plugin.model.id); | ||
if (!apiImpl) { | ||
apiImpl = cheApiFactory(plugin); | ||
pluginsApiImpl.set(plugin.model.id, apiImpl); | ||
} | ||
return apiImpl; | ||
} | ||
|
||
if (!defaultApi) { | ||
console.warn(`Could not identify plugin for '@bar/foo' require call from ${parent.filename}`); | ||
defaultApi = cheApiFactory(emptyPlugin); | ||
} | ||
|
||
return defaultApi; | ||
}; | ||
} | ||
|
||
function findPlugin(filePath: string): Plugin | undefined { | ||
return plugins.getAllPlugins().find(plugin => filePath.startsWith(plugin.pluginFolder)); | ||
} | ||
``` | ||
|
||
## Next you need to implement `createAPIFactory` factory function | ||
|
||
Example: | ||
|
||
```typescript | ||
import * as fooApi from '@bar/foo'; | ||
export function createAPIFactory(rpc: RPCProtocol): ApiFactory { | ||
const fooBarImpl = new FooBarImpl(rpc); | ||
return function (plugin: Plugin): typeof fooApi { | ||
const FooBar: typeof fooApi.fooBar = { | ||
getFoo(): fooApi.Foo{ | ||
return fooBarImpl.getFooImpl(); | ||
} | ||
} | ||
return <typeof fooApi>{ | ||
fooBar : FooBar | ||
}; | ||
} | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters