github.com/15mga/kiwi@v0.0.2-0.20240324021231-b95d5c3ac751/graph/graph_elem.go (about) 1 package graph 2 3 import ( 4 "github.com/15mga/kiwi/ds" 5 "github.com/15mga/kiwi/util" 6 ) 7 8 type IGraphElem interface { 9 Name() string 10 Path() string 11 Comment() string 12 SetComment(c string) 13 Data() util.M 14 SetData(data util.M) 15 Start() *util.Err 16 Enable() *util.Err //即使已可用,也会重复执行 17 Disable() *util.Err //即使不可用,也会重复执行 18 SetEnable(enable bool) *util.Err //已经是该状态不会重复执行 19 Enabled() bool 20 AddBeforeEnable(fn util.BoolToErr) //添加节点切换是否可用之前调用 21 DelBeforeEnable(fn util.BoolToErr) //移除节点切换是否可用之前调用 22 AddAfterEnable(fn util.FnBool) //添加节点切换是否可用之后调用 23 DelAfterEnable(fn util.FnBool) //移除节点切换是否可用之后调用 24 Graph() IGraph 25 RootGraph() IGraph 26 } 27 28 func newGraphElem(g IGraph, name string) IGraphElem { 29 e := &graphElem{ 30 name: name, 31 enabled: true, 32 beforeEnable: ds.NewFnErrLink1[bool](), 33 afterEnable: ds.NewFnLink1[bool](), 34 g: g, 35 data: util.M{}, 36 } 37 sg, ok := g.(ISubGraph) 38 if ok { 39 e.path = sg.Path() + "." + name 40 } else { 41 e.path = name 42 } 43 pg, ok := g.(ISubGraph) 44 if ok { 45 e.rg = pg.RootGraph() 46 } else { 47 e.rg = e.Graph() 48 } 49 return e 50 } 51 52 type graphElem struct { 53 name string 54 comment string 55 path string 56 data util.M 57 enabled bool 58 beforeEnable *ds.FnErrLink1[bool] 59 afterEnable *ds.FnLink1[bool] 60 g IGraph 61 rg IGraph 62 } 63 64 func (e *graphElem) Name() string { 65 return e.name 66 } 67 68 func (e *graphElem) Path() string { 69 return e.path 70 } 71 72 func (e *graphElem) Comment() string { 73 return e.comment 74 } 75 76 func (e *graphElem) SetComment(comment string) { 77 e.comment = comment 78 } 79 80 func (e *graphElem) Data() util.M { 81 return e.data 82 } 83 84 func (e *graphElem) SetData(data util.M) { 85 e.data = data 86 } 87 88 func (e *graphElem) Start() *util.Err { 89 if e.enabled { 90 err := e.Enable() 91 if err != nil { 92 return err 93 } 94 } 95 return nil 96 } 97 98 func (e *graphElem) Enable() *util.Err { 99 if e.beforeEnable != nil { 100 err := e.beforeEnable.Invoke(true) 101 if err != nil { 102 return err 103 } 104 } 105 e.enabled = true 106 e.afterEnable.Invoke(true) 107 return nil 108 } 109 110 func (e *graphElem) Disable() *util.Err { 111 if e.beforeEnable != nil { 112 err := e.beforeEnable.Invoke(false) 113 if err != nil { 114 return err 115 } 116 } 117 e.enabled = false 118 e.afterEnable.Invoke(false) 119 return nil 120 } 121 122 func (e *graphElem) SetEnable(enable bool) *util.Err { 123 if e.enabled == enable { 124 return nil 125 } 126 if enable { 127 return e.Enable() 128 } 129 130 return e.Disable() 131 } 132 133 func (e *graphElem) AddBeforeEnable(fn util.BoolToErr) { 134 if e.beforeEnable == nil { 135 e.beforeEnable = ds.NewFnErrLink1[bool]() 136 } 137 e.beforeEnable.Push(fn) 138 } 139 140 func (e *graphElem) DelBeforeEnable(fn util.BoolToErr) { 141 if e.beforeEnable == nil { 142 return 143 } 144 e.beforeEnable.Del(fn) 145 } 146 147 func (e *graphElem) AddAfterEnable(fn util.FnBool) { 148 if e.afterEnable == nil { 149 e.afterEnable = ds.NewFnLink1[bool]() 150 } 151 e.afterEnable.Push(fn) 152 } 153 154 func (e *graphElem) DelAfterEnable(fn util.FnBool) { 155 if e.afterEnable == nil { 156 return 157 } 158 e.afterEnable.Del(fn) 159 } 160 161 func (e *graphElem) Enabled() bool { 162 return e.enabled 163 } 164 165 func (e *graphElem) RootGraph() IGraph { 166 return e.rg 167 } 168 169 func (e *graphElem) Graph() IGraph { 170 return e.g 171 }