added support for passing more than one id in the Hook.Unbind method for consistency with the router
This commit is contained in:
		
							parent
							
								
									0ac4a388c0
								
							
						
					
					
						commit
						f1b199b35c
					
				| 
						 | 
					@ -1,3 +1,8 @@
 | 
				
			||||||
 | 
					## (WIP)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					- Added support for passing more than one id in the `Hook.Unbind` method for consistency with the router.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## v0.23.1
 | 
					## v0.23.1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
- Added `RequestEvent.Blob(status, contentType, bytes)` response write helper ([#5940](https://github.com/pocketbase/pocketbase/discussions/5940)).
 | 
					- Added `RequestEvent.Blob(status, contentType, bytes)` response write helper ([#5940](https://github.com/pocketbase/pocketbase/discussions/5940)).
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -112,15 +112,17 @@ func (h *Hook[T]) BindFunc(fn func(e T) error) string {
 | 
				
			||||||
	return h.Bind(&Handler[T]{Func: fn})
 | 
						return h.Bind(&Handler[T]{Func: fn})
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Unbind removes a single hook handler by its id.
 | 
					// Unbind removes one or many hook handler by their id.
 | 
				
			||||||
func (h *Hook[T]) Unbind(id string) {
 | 
					func (h *Hook[T]) Unbind(idsToRemove ...string) {
 | 
				
			||||||
	h.mu.Lock()
 | 
						h.mu.Lock()
 | 
				
			||||||
	defer h.mu.Unlock()
 | 
						defer h.mu.Unlock()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for i := len(h.handlers) - 1; i >= 0; i-- {
 | 
						for _, id := range idsToRemove {
 | 
				
			||||||
		if h.handlers[i].Id == id {
 | 
							for i := len(h.handlers) - 1; i >= 0; i-- {
 | 
				
			||||||
			h.handlers = append(h.handlers[:i], h.handlers[i+1:]...)
 | 
								if h.handlers[i].Id == id {
 | 
				
			||||||
			break // for now stop on the first occurrence since we don't allow handlers with duplicated ids
 | 
									h.handlers = append(h.handlers[:i], h.handlers[i+1:]...)
 | 
				
			||||||
 | 
									break // for now stop on the first occurrence since we don't allow handlers with duplicated ids
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -70,6 +70,7 @@ func TestHookUnbind(t *testing.T) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	calls := ""
 | 
						calls := ""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						id0 := h.BindFunc(func(e *Event) error { calls += "0"; return e.Next() })
 | 
				
			||||||
	id1 := h.BindFunc(func(e *Event) error { calls += "1"; return e.Next() })
 | 
						id1 := h.BindFunc(func(e *Event) error { calls += "1"; return e.Next() })
 | 
				
			||||||
	h.BindFunc(func(e *Event) error { calls += "2"; return e.Next() })
 | 
						h.BindFunc(func(e *Event) error { calls += "2"; return e.Next() })
 | 
				
			||||||
	h.Bind(&Handler[*Event]{
 | 
						h.Bind(&Handler[*Event]{
 | 
				
			||||||
| 
						 | 
					@ -78,11 +79,11 @@ func TestHookUnbind(t *testing.T) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	h.Unbind("missing") // should do nothing and not panic
 | 
						h.Unbind("missing") // should do nothing and not panic
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if total := len(h.handlers); total != 3 {
 | 
						if total := len(h.handlers); total != 4 {
 | 
				
			||||||
		t.Fatalf("Expected %d handlers, got %d", 3, total)
 | 
							t.Fatalf("Expected %d handlers, got %d", 4, total)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	h.Unbind(id1)
 | 
						h.Unbind(id1, id0)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if total := len(h.handlers); total != 2 {
 | 
						if total := len(h.handlers); total != 2 {
 | 
				
			||||||
		t.Fatalf("Expected %d handlers, got %d", 2, total)
 | 
							t.Fatalf("Expected %d handlers, got %d", 2, total)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue