Skip to content

Commit

Permalink
feat: add dashkit reload plugins method (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
flops authored Sep 19, 2024
1 parent f9edfb3 commit 078bf73
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/components/DashKit/DashKit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,12 @@ export class DashKit extends React.PureComponent<DashKitInnerProps> {
});
}

static reloadPlugins(...plugins: Plugin[]) {
plugins.forEach((plugin) => {
registerManager.reloadPlugin(plugin);
});
}

static setSettings(settings: Settings) {
registerManager.setSettings(settings);
}
Expand Down
16 changes: 13 additions & 3 deletions src/components/DashKit/__stories__/DashKit.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ if (!getInitialized()) {
apiHandler: ({text}) => Promise.resolve({result: text}),
}),
);
DashKit.registerPlugins({

const customPlugin = {
type: 'custom',
defaultLayout: {
w: 10,
h: 10,
w: 20,
h: 20,
},
renderer: function CustomPlugin() {
return (
Expand All @@ -62,6 +63,15 @@ if (!getInitialized()) {
</div>
);
},
};

DashKit.registerPlugins(customPlugin);
DashKit.reloadPlugins({
...customPlugin,
defaultLayout: {
w: 10,
h: 10,
},
});

DashKit.setSettings({
Expand Down
9 changes: 8 additions & 1 deletion src/utils/register-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,20 @@ export class RegisterManager {
};

registerPlugin(plugin: Plugin) {
const {type, defaultLayout = {}, ...item} = plugin;
const {type} = plugin;
if (type in this._items) {
throw new Error(`DashKit.registerPlugins: type ${type} уже был зарегистрирован`);
}
this.reloadPlugin(plugin);
}

reloadPlugin(plugin: Plugin) {
const {type, defaultLayout = {}, ...item} = plugin;

if (typeof plugin.renderer !== 'function') {
throw new Error('DashKit.registerPlugins: renderer должна быть функцией');
}

this._items[type] = {
...item,
type,
Expand Down

0 comments on commit 078bf73

Please sign in to comment.