summaryrefslogtreecommitdiffhomepage
path: root/go/observable.go
blob: 9a38802cf74ebf84927c4da2a588b1086d5d5b4b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
 * Copyright (C) NGINX, Inc.
 */

package unit

import (
	"sync"
)

type observable struct {
	sync.Mutex
	observers []chan int
}

func (o *observable) attach(c chan int) {
	o.Lock()
	defer o.Unlock()

	o.observers = append(o.observers, c)
}

func (o *observable) notify(e int) {
	o.Lock()
	defer o.Unlock()

	for _, v := range o.observers {
		v <- e
	}

	o.observers = nil
}