Skip to content

Commit 3ad36f2

Browse files
重构 callback
1 parent 75a7ae6 commit 3ad36f2

File tree

2 files changed

+39
-36
lines changed

2 files changed

+39
-36
lines changed

callback.go

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,42 +19,10 @@ import (
1919
"github.com/chenmingyong0423/go-mongox/operation"
2020
)
2121

22-
func RegisterBeforeInsert(name string, cb callback.CbFn) {
23-
callback.Callbacks.Register(operation.OpTypeBeforeInsert, name, cb)
22+
func Register(name string, cb callback.CbFn, opType operation.OpType) {
23+
callback.Callbacks.Register(opType, name, cb)
2424
}
2525

26-
func RegisterAfterInsert(name string, cb callback.CbFn) {
27-
callback.Callbacks.Register(operation.OpTypeAfterInsert, name, cb)
28-
}
29-
30-
func RegisterBeforeUpdate(name string, cb callback.CbFn) {
31-
callback.Callbacks.Register(operation.OpTypeBeforeUpdate, name, cb)
32-
}
33-
34-
func RegisterAfterUpdate(name string, cb callback.CbFn) {
35-
callback.Callbacks.Register(operation.OpTypeAfterUpdate, name, cb)
36-
}
37-
38-
func RegisterBeforeDelete(name string, cb callback.CbFn) {
39-
callback.Callbacks.Register(operation.OpTypeBeforeDelete, name, cb)
40-
}
41-
42-
func RegisterAfterDelete(name string, cb callback.CbFn) {
43-
callback.Callbacks.Register(operation.OpTypeAfterDelete, name, cb)
44-
}
45-
46-
func RegisterBeforeUpsert(name string, cb callback.CbFn) {
47-
callback.Callbacks.Register(operation.OpTypeBeforeUpsert, name, cb)
48-
}
49-
50-
func RegisterAfterUpsert(name string, cb callback.CbFn) {
51-
callback.Callbacks.Register(operation.OpTypeAfterUpsert, name, cb)
52-
}
53-
54-
func RegisterBeforeFind(name string, cb callback.CbFn) {
55-
callback.Callbacks.Register(operation.OpTypeBeforeFind, name, cb)
56-
}
57-
58-
func RegisterAfterFind(name string, cb callback.CbFn) {
59-
callback.Callbacks.Register(operation.OpTypeAfterFind, name, cb)
26+
func Remove(name string, opType operation.OpType) {
27+
callback.Callbacks.Remove(opType, name)
6028
}

callback/callback.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,41 @@ func (c *Callback) Register(opType operation.OpType, name string, fn CbFn) {
168168
}
169169
}
170170

171+
func (c *Callback) Remove(opType operation.OpType, name string) {
172+
switch opType {
173+
case operation.OpTypeBeforeInsert:
174+
c.beforeInsert = c.remove(c.beforeInsert, name)
175+
case operation.OpTypeAfterInsert:
176+
c.afterInsert = c.remove(c.afterInsert, name)
177+
case operation.OpTypeBeforeUpdate:
178+
c.beforeUpdate = c.remove(c.beforeUpdate, name)
179+
case operation.OpTypeAfterUpdate:
180+
c.afterUpdate = c.remove(c.afterUpdate, name)
181+
case operation.OpTypeBeforeDelete:
182+
c.beforeDelete = c.remove(c.beforeDelete, name)
183+
case operation.OpTypeAfterDelete:
184+
c.afterDelete = c.remove(c.afterDelete, name)
185+
case operation.OpTypeBeforeUpsert:
186+
c.beforeUpsert = c.remove(c.beforeUpsert, name)
187+
case operation.OpTypeAfterUpsert:
188+
c.afterUpsert = c.remove(c.afterUpsert, name)
189+
case operation.OpTypeBeforeFind:
190+
c.beforeFind = c.remove(c.beforeFind, name)
191+
case operation.OpTypeAfterFind:
192+
c.afterFind = c.remove(c.afterFind, name)
193+
}
194+
}
195+
196+
func (c *Callback) remove(callbackHandlers []callbackHandler, name string) []callbackHandler {
197+
for i, handler := range callbackHandlers {
198+
if handler.name == name {
199+
callbackHandlers = append(callbackHandlers[:i], callbackHandlers[i+1:]...)
200+
break
201+
}
202+
}
203+
return callbackHandlers
204+
}
205+
171206
type callbackHandler struct {
172207
name string
173208
fn CbFn

0 commit comments

Comments
 (0)