-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRootContext.go
58 lines (43 loc) · 1.43 KB
/
RootContext.go
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
52
53
54
55
56
57
58
package context
// The RootContext interface is returned by the [NewRootContext] function.
type RootContext interface {
// Creates new Context from your instance what implements [ContextedInstance] interface.
// If current root context is already in closing state it returns [ClosingIsInProcessForFreezeError] or [ClosingIsInProcessForDisposingError]
NewContextFor(instance ContextedInstance) (ChildContext, error)
// Waits till current root context would be Closeed.
Wait()
// Close current root context and all childs according reverse order.
Close()
}
type rootContext struct {
instance ContextedInstance
context *context
done chan struct{}
}
// NewRootContext function generates and starts new root context
func NewRootContext(instance ContextedInstance) RootContext {
root := &rootContext{
instance: instance,
done: make(chan struct{}),
}
emptyContext := newEmptyContext()
rootContext, _ := newContextFor(emptyContext, root)
root.context = rootContext
return root
}
// Wait ...
func (root *rootContext) Wait() {
<-root.done
}
// Close ...
func (root *rootContext) Close() {
root.context.Close()
}
func (root *rootContext) Go(current Context) {
root.instance.Go(current)
close(root.done)
}
// This function uses to generate new child context from root or other child context
func (root *rootContext) NewContextFor(instance ContextedInstance) (ChildContext, error) {
return root.context.NewContextFor(instance)
}