-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathlab.go
64 lines (55 loc) · 1.24 KB
/
lab.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
59
60
61
62
63
64
// Copyright 2013 Martin Schnabel. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package lab provides a global module register.
package lab
// Module is a part of your program.
type Module interface{}
// Initer modules are initialized in registration order on lab start.
type Initer interface {
Init()
}
// Runner modules are run in a new goroutine on lab start.
type Runner interface {
Run()
}
var (
lock bool
list []Module
mods = map[string]Module{}
)
// Register registers a module by name. It must be called before the lab is started.
func Register(name string, mod Module) {
if lock {
panic("lab was started")
}
list = append(list, mod)
mods[name] = mod
}
// Mod returns a registered module with name or nil.
func Mod(name string) Module {
return mods[name]
}
// All returns all registered modules.
func All() []Module {
m := make([]Module, len(list))
copy(m, list)
return m
}
// Start locks the lab and initializes and runs all modules.
func Start() {
if lock {
panic("lab was started")
}
lock = true
for _, m := range list {
if i, ok := m.(Initer); ok {
i.Init()
}
}
for _, m := range list {
if r, ok := m.(Runner); ok {
go r.Run()
}
}
}