Skip to content

Commit

Permalink
reverse callback order so when port change fails the setting is not s…
Browse files Browse the repository at this point in the history
…aved. (#44)
  • Loading branch information
tigerinus authored Mar 8, 2024
1 parent 31f30ec commit 9045386
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions service/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down

0 comments on commit 9045386

Please sign in to comment.