mirror of
https://github.com/mainnika/mongox-go-driver.git
synced 2026-07-03 17:22:33 +00:00
Add onclose callback
This commit is contained in:
@@ -48,6 +48,8 @@ func (d *Database) LoadArray(target interface{}, filters ...interface{}) (err er
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
defer composed.OnClose().Invoke(d.Context(), target)
|
||||||
|
|
||||||
for i = 0; result.Next(d.Context()); {
|
for i = 0; result.Next(d.Context()); {
|
||||||
|
|
||||||
var elem interface{}
|
var elem interface{}
|
||||||
|
|||||||
@@ -25,6 +25,8 @@ func (d *Database) LoadOne(target interface{}, filters ...interface{}) (err erro
|
|||||||
return fmt.Errorf("can't create find result: %w", err)
|
return fmt.Errorf("can't create find result: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
defer composed.OnClose().Invoke(d.Context(), target)
|
||||||
|
|
||||||
hasNext := result.Next(d.Context())
|
hasNext := result.Next(d.Context())
|
||||||
if result.Err() != nil {
|
if result.Err() != nil {
|
||||||
return err
|
return err
|
||||||
|
|||||||
@@ -72,6 +72,9 @@ func (l *StreamLoader) Cursor() (cursor *mongox.Cursor) {
|
|||||||
|
|
||||||
// Close cursor
|
// Close cursor
|
||||||
func (l *StreamLoader) Close() (err error) {
|
func (l *StreamLoader) Close() (err error) {
|
||||||
|
|
||||||
|
_ = l.query.OnClose().Invoke(l.ctx, l.target)
|
||||||
|
|
||||||
return l.cur.Close(l.ctx)
|
return l.cur.Close(l.ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,10 @@ import (
|
|||||||
type Callback func(ctx context.Context, iter interface{}) (err error)
|
type Callback func(ctx context.Context, iter interface{}) (err error)
|
||||||
type Callbacks []Callback
|
type Callbacks []Callback
|
||||||
|
|
||||||
type OnDecode Callback
|
type (
|
||||||
|
OnDecode Callback
|
||||||
|
OnClose Callback
|
||||||
|
)
|
||||||
|
|
||||||
// Invoke callbacks sequence
|
// Invoke callbacks sequence
|
||||||
func (c Callbacks) Invoke(ctx context.Context, iter interface{}) (err error) {
|
func (c Callbacks) Invoke(ctx context.Context, iter interface{}) (err error) {
|
||||||
|
|||||||
@@ -130,8 +130,13 @@ func applyCallbacks(q *Query, f interface{}) (ok bool) {
|
|||||||
switch cb := f.(type) {
|
switch cb := f.(type) {
|
||||||
case OnDecode:
|
case OnDecode:
|
||||||
q.ondecode = append(q.ondecode, Callback(cb))
|
q.ondecode = append(q.ondecode, Callback(cb))
|
||||||
ok = true
|
case OnClose:
|
||||||
|
q.onclose = append(q.onclose, Callback(cb))
|
||||||
|
default:
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ok = true
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ type Query struct {
|
|||||||
skipper Skipper
|
skipper Skipper
|
||||||
preloader Preloader
|
preloader Preloader
|
||||||
ondecode Callbacks
|
ondecode Callbacks
|
||||||
|
onclose Callbacks
|
||||||
}
|
}
|
||||||
|
|
||||||
// And function pushes the elem query to the $and array of the query
|
// And function pushes the elem query to the $and array of the query
|
||||||
@@ -81,6 +82,10 @@ func (q *Query) OnDecode() (callbacks Callbacks) {
|
|||||||
return q.ondecode
|
return q.ondecode
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (q *Query) OnClose() (callbacks Callbacks) {
|
||||||
|
return q.onclose
|
||||||
|
}
|
||||||
|
|
||||||
// Empty checks the query for any content
|
// Empty checks the query for any content
|
||||||
func (q *Query) Empty() (isEmpty bool) {
|
func (q *Query) Empty() (isEmpty bool) {
|
||||||
return len(q.m) == 0
|
return len(q.m) == 0
|
||||||
|
|||||||
Reference in New Issue
Block a user