11import { DevtoolServer } from './devtool'
22import { PORT , READY_MESSAGE , RequestDetail } from '../common'
3- import zlib from 'node:zlib'
43import { Server } from 'ws'
54import { log } from '../utils'
65import { ResourceService } from './resource-service'
7- import { EffectCleaner , PluginInstance } from './module/common'
6+ import { PluginInstance } from './module/common'
87
98export interface RequestCenterInitOptions {
109 port : number
@@ -26,7 +25,6 @@ export class RequestCenter {
2625 public resourceService : ResourceService
2726 private devtool : DevtoolServer
2827 private server : Server
29- private effects : Array < EffectCleaner > = [ ]
3028 private listeners : Record < string , Set < DevtoolMessageListener > | undefined > = { }
3129 private options : RequestCenterInitOptions
3230 constructor ( options : RequestCenterInitOptions ) {
@@ -67,18 +65,29 @@ export class RequestCenter {
6765 this . server = this . initServer ( )
6866 }
6967
70- #plugins: PluginInstance [ ] = [ ]
71- public loadPlugins ( plugins : PluginInstance [ ] ) {
68+ #plugins: PluginInstance < any > [ ] = [ ]
69+ #pluginOutputs?: Map < string , any >
70+ public loadPlugins ( plugins : PluginInstance < any > [ ] ) {
7271 this . #plugins = plugins
73- const effects = plugins
74- . map ( ( plugin ) =>
75- plugin ( {
76- devtool : this . devtool ,
77- core : this
78- } )
79- )
80- . filter ( Boolean ) as Array < EffectCleaner >
81- this . effects . push ( ...effects )
72+ this . #pluginOutputs = new Map ( )
73+
74+ plugins . forEach ( ( plugin ) => {
75+ const output = plugin ( {
76+ devtool : this . devtool ,
77+ core : this ,
78+ plugins : this . #plugins
79+ } )
80+ this . #pluginOutputs! . set ( plugin . id , output )
81+ } )
82+ }
83+
84+ public usePlugin < T = null > ( id : string ) {
85+ if ( ! this . #pluginOutputs) {
86+ return null as T
87+ }
88+
89+ const output = this . #pluginOutputs. get ( id )
90+ return output as T
8291 }
8392
8493 public on ( method : string , listener : DevtoolMessageListener ) {
@@ -94,7 +103,6 @@ export class RequestCenter {
94103 public close ( ) {
95104 this . server . close ( )
96105 this . devtool . close ( )
97- this . effects . forEach ( ( effect ) => effect ( ) )
98106 }
99107
100108 private initServer ( ) {
0 commit comments