-
Notifications
You must be signed in to change notification settings - Fork 6
/
watcher.go
97 lines (79 loc) · 2.83 KB
/
watcher.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package watcher
import (
"context"
"github.com/goradd/goradd/pkg/messageServer"
)
// The injected watcher. See the application initialization process for Watcher creation.
var Watcher WatcherI
type WatcherI interface {
MakeKey(ctx context.Context, dbKey string, table string, pk string) string
BroadcastUpdate(ctx context.Context, dbKey string, table string, pk string, fieldKeys []string)
BroadcastInsert(ctx context.Context, dbKey string, table string, pk string)
BroadcastDelete(ctx context.Context, dbKey string, table string, pk string)
BroadcastBulkChange(ctx context.Context, dbKey string, table string)
}
type DefaultWatcher struct {
}
func (*DefaultWatcher) MakeKey(ctx context.Context, dbKey string, table string, pk string) string {
k := dbKey + "." + table
if pk != "" {
k += "." + pk
}
return k
}
func (w *DefaultWatcher) BroadcastUpdate(ctx context.Context, dbKey string, table string, pk string, fieldKeys []string) {
tableChannel := w.MakeKey(ctx, dbKey, table, "")
pkChannel := w.MakeKey(ctx, dbKey, table, pk)
message := make(map[string]interface{})
message["pk"] = pk
message["fields"] = fieldKeys
message["op"] = "upd"
messageServer.Send(tableChannel, "*")
messageServer.Send(pkChannel, message)
}
func (w *DefaultWatcher) BroadcastInsert(ctx context.Context, dbKey string, table string, pk string) {
tableChannel := w.MakeKey(ctx, dbKey, table, "")
message := make(map[string]interface{})
message["pk"] = pk
message["op"] = "ins"
messageServer.Send(tableChannel, "*")
}
func (w *DefaultWatcher) BroadcastDelete(ctx context.Context, dbKey string, table string, pk string) {
tableChannel := w.MakeKey(ctx, dbKey, table, "")
message := make(map[string]interface{})
message["pk"] = pk
message["op"] = "del"
messageServer.Send(tableChannel, "*")
}
func (w *DefaultWatcher) BroadcastBulkChange(ctx context.Context, dbKey string, table string) {
tableChannel := w.MakeKey(ctx, dbKey, table, "")
message := make(map[string]interface{})
message["op"] = "chg"
messageServer.Send(tableChannel, "*")
}
func BroadcastUpdate(ctx context.Context, dbKey string, table string, pk string, fieldKeys []string) {
if Watcher != nil {
Watcher.BroadcastUpdate(ctx, dbKey, table, pk, fieldKeys)
}
}
func BroadcastInsert(ctx context.Context, dbKey string, table string, pk string) {
if Watcher != nil {
Watcher.BroadcastInsert(ctx, dbKey, table, pk)
}
}
func BroadcastDelete(ctx context.Context, dbKey string, table string, pk string) {
if Watcher != nil {
Watcher.BroadcastDelete(ctx, dbKey, table, pk)
}
}
func BroadcastBulkChange(ctx context.Context, dbKey string, table string) {
if Watcher != nil {
Watcher.BroadcastBulkChange(ctx, dbKey, table)
}
}
func MakeKey(ctx context.Context, dbKey string, table string, pk string) string {
if Watcher == nil {
return ""
}
return Watcher.MakeKey(ctx, dbKey, table, pk)
}