summaryrefslogtreecommitdiff
path: root/plugins/temporal/workflow/id_registry.go
blob: ac75cbda58b51fbc7f109ddbf02251a4e85fa0c6 (plain)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package workflow

import (
	"sync"

	bindings "go.temporal.io/sdk/internalbindings"
)

// used to gain access to child workflow ids after they become available via callback result.
type idRegistry struct {
	mu        sync.Mutex
	ids       map[uint64]entry
	listeners map[uint64]listener
}

type listener func(w bindings.WorkflowExecution, err error)

type entry struct {
	w   bindings.WorkflowExecution
	err error
}

func newIDRegistry() *idRegistry {
	return &idRegistry{
		ids:       map[uint64]entry{},
		listeners: map[uint64]listener{},
	}
}

func (c *idRegistry) listen(id uint64, cl listener) {
	c.mu.Lock()
	defer c.mu.Unlock()

	c.listeners[id] = cl

	if e, ok := c.ids[id]; ok {
		cl(e.w, e.err)
	}
}

func (c *idRegistry) push(id uint64, w bindings.WorkflowExecution, err error) {
	c.mu.Lock()
	defer c.mu.Unlock()

	e := entry{w: w, err: err}
	c.ids[id] = e

	if l, ok := c.listeners[id]; ok {
		l(e.w, e.err)
	}
}