-
Notifications
You must be signed in to change notification settings - Fork 1
/
function.go
46 lines (35 loc) · 1.01 KB
/
function.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
package stream
import (
"sync"
)
// An argument to a stream function. Might be anythins, only exists for type safety.
type FArg interface{}
// A function for building a stream from JSON definition.
type Function func(Context, []FArg) (Stream, error)
/*
A data associated with a stream for building streams from JSON definitions.
Currently, just the multiplexer for stream variables to be used multiple times.
*/
type StreamContext struct {
stream Stream
mp *StreamMultiplexer
}
// A context to be used by functions for building a stream from JSON definition.
type Context map[string]*StreamContext
var functions map[string]Function
var flock sync.Mutex
// Register stream function by name for building it from JSON definitions.
func Register(name string, fn Function) {
flock.Lock()
defer flock.Unlock()
if functions == nil {
functions = map[string]Function{}
}
functions[name] = fn
}
func getFn(name string) (Function, bool) {
flock.Lock()
defer flock.Unlock()
fn, ok := functions[name]
return fn, ok
}