Adam Shaw 7 лет назад
Родитель
Сommit
c382bc0976
1 измененных файлов с 57 добавлено и 0 удалено
  1. 57 0
      src/plugin-system.ts

+ 57 - 0
src/plugin-system.ts

@@ -0,0 +1,57 @@
+import { reducerFunc } from './reducers/types'
+
+export interface PluginDefInput {
+  deps?: PluginDef[]
+  reducers?: reducerFunc[]
+}
+
+export interface PluginHooks {
+  reducers: reducerFunc[]
+}
+
+export interface PluginDef extends PluginHooks {
+  id: string
+  deps: PluginDef[]
+}
+
+let uid = 0
+
+export function createPlugin(input: PluginDefInput): PluginDef {
+  return {
+    id: String(uid++),
+    deps: input.deps || [],
+    reducers: input.reducers || []
+  }
+}
+
+export class PluginSystem {
+
+  hooks: PluginHooks
+  addedHash: { [pluginId: string]: true }
+
+  constructor() {
+    this.hooks = {
+      reducers: []
+    }
+    this.addedHash = {}
+  }
+
+  add(plugin: PluginDef) {
+    if (!this.addedHash[plugin.id]) {
+      this.addedHash[plugin.id] = true
+
+      for (let dep of plugin.deps) {
+        this.add(dep)
+      }
+
+      this.hooks = combineHooks(this.hooks, plugin)
+    }
+  }
+
+}
+
+function combineHooks(hooks0: PluginHooks, hooks1: PluginHooks): PluginHooks {
+  return {
+    reducers: hooks0.reducers.concat(hooks1.reducers)
+  }
+}