Skip to content

Commit

Permalink
chore: update plugins example and incorrect types in local manager
Browse files Browse the repository at this point in the history
  • Loading branch information
SewerynKras committed Mar 28, 2024
1 parent e0bd7c8 commit 7dd8bfe
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
19 changes: 9 additions & 10 deletions src/plugins/examplePlugin.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import { MarketApi } from "ya-ts-client";
import { GolemPlugin, registerGlobalPlugin } from "./pluginManager";
import { LocalPluginManager } from "./localPluginManager";
Expand Down Expand Up @@ -37,9 +38,7 @@ const cpuArchitecturePlugin: GolemPlugin = {
},
};

registerGlobalPlugin(linearPricingOnlyPlugin);

class ExampleDemandManager {
class Demand {
private pluginManager = new LocalPluginManager();
constructor(plugins?: GolemPlugin[]) {
if (plugins) {
Expand All @@ -61,11 +60,11 @@ class ExampleDemandManager {
}
}

const demandManager0 = new ExampleDemandManager([cpuArchitecturePlugin]);
const demandManager1 = new ExampleDemandManager();

const demandWithCpuArchitecture = await demandManager0.publish();
const demandWithOnlyLinearPricing = await demandManager1.publish();
registerGlobalPlugin(linearPricingOnlyPlugin);
const demand0 = new Demand([cpuArchitecturePlugin]);
const demand1 = new Demand();

console.log(demandWithCpuArchitecture);
console.log(demandWithOnlyLinearPricing);
// 👇 this demand will have pricing model and architecture set by the plugins
const demandWithCpuArchitecture = await demand0.publish();
// 👇 this demand will have only pricing model set by the global plugin
const demandWithOnlyLinearPricing = await demand1.publish();
9 changes: 4 additions & 5 deletions src/plugins/localPluginManager.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import EventEmitter from "eventemitter3";
import { MarketEvents, MarketHooks } from "./marketPluginContext";
import { GlobalPluginManager, PluginManager } from "./pluginManager";
import { AllEvents, AllHooks, GlobalPluginManager, PluginManager } from "./pluginManager";

/**
* Plugin manager that will combine local and global plugins.
* Global plugins should be registered using `GlobalPluginManager`.
* Global hooks will be executed before local hooks, in order of registration.
*/
export class LocalPluginManager extends PluginManager {
getHooks<T extends keyof MarketHooks>(hookName: T): MarketHooks[T][] {
getHooks<T extends keyof AllHooks>(hookName: T): AllHooks[T][] {
const localHooks = super.getHooks(hookName);
const globalHooks = GlobalPluginManager.getHooks(hookName);
return [...globalHooks, ...localHooks];
}
public emitEvent<T extends keyof MarketEvents>(
public emitEvent<T extends keyof AllEvents>(
eventName: T,
...args: EventEmitter.ArgumentMap<MarketEvents>[Extract<T, keyof MarketEvents>]
...args: EventEmitter.ArgumentMap<AllEvents>[Extract<T, keyof AllEvents>]
): void {
GlobalPluginManager.emitEvent(eventName, ...args);
super.emitEvent(eventName, ...args);
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/pluginManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export type GolemPlugin = {
register(context: GolemPluginContext): void;
};

type AllHooks = MarketHooks; // | DeploymentHooks | PaymentHooks | ...
type AllEvents = MarketEvents; // | DeploymentEvents | PaymentEvents | ...
export type AllHooks = MarketHooks; // | DeploymentHooks | PaymentHooks | ...
export type AllEvents = MarketEvents; // | DeploymentEvents | PaymentEvents | ...

export class PluginManager {
private eventEmitter = new EventEmitter<AllEvents>();
Expand Down

0 comments on commit 7dd8bfe

Please sign in to comment.