MakePlugin creates a plugin caller and register stack manager The parameter super presents if the plugin can be disabled. It returns a register function and a caller function The register function is used to register a plugin, it will be called in the plugin's init function The caller function is us
(super bool)
| 150 | // The register function is used to register a plugin, it will be called in the plugin's init function |
| 151 | // The caller function is used to call all registered plugins |
| 152 | func MakePlugin[T Base](super bool) (CallFn[T], RegisterFn[T]) { |
| 153 | stack := Stack[T]{} |
| 154 | |
| 155 | call := func(fn Caller[T]) error { |
| 156 | for _, p := range stack.plugins { |
| 157 | // If the plugin is disabled, skip it |
| 158 | if !super && !StatusManager.IsEnabled(p.Info().SlugName) { |
| 159 | continue |
| 160 | } |
| 161 | |
| 162 | if err := fn(p); err != nil { |
| 163 | return err |
| 164 | } |
| 165 | } |
| 166 | return nil |
| 167 | } |
| 168 | |
| 169 | register := func(p T) { |
| 170 | for _, plugin := range stack.plugins { |
| 171 | if plugin.Info().SlugName == p.Info().SlugName { |
| 172 | panic("plugin " + p.Info().SlugName + " is already registered") |
| 173 | } |
| 174 | } |
| 175 | stack.plugins = append(stack.plugins, p) |
| 176 | } |
| 177 | |
| 178 | return call, register |
| 179 | } |
| 180 | |
| 181 | type statusManager struct { |
| 182 | lock sync.Mutex |