From 90453868969a87e099b3ff9779a89377b61821cd Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Fri, 8 Mar 2024 15:49:11 +0800 Subject: [PATCH] reverse callback order so when port change fails the setting is not saved. (#44) --- service/state.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/service/state.go b/service/state.go index f9ca09e..7f09dce 100644 --- a/service/state.go +++ b/service/state.go @@ -20,20 +20,21 @@ func NewState() *State { func (c *State) SetGatewayPort(port string) error { c.gatewayPort = port - return c.notifiyOnGatewayPortChange() + return c.notifyOnGatewayPortChange() } func (c *State) GetGatewayPort() string { return c.gatewayPort } +// Add func `f` to the stack. The stack of funcs will be called, in reverse order, when there is request to change the port. func (c *State) OnGatewayPortChange(f func(string) error) { c.onGatewayPortChange = append(c.onGatewayPortChange, f) } -func (c *State) notifiyOnGatewayPortChange() error { - for _, f := range c.onGatewayPortChange { - if err := f(c.gatewayPort); err != nil { +func (c *State) notifyOnGatewayPortChange() error { + for i := len(c.onGatewayPortChange) - 1; i >= 0; i-- { + if err := c.onGatewayPortChange[i](c.gatewayPort); err != nil { return err } }