-
Notifications
You must be signed in to change notification settings - Fork 12
/
remote_object.go
96 lines (85 loc) · 2.76 KB
/
remote_object.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
96
package enigma
import (
"context"
"sync"
"github.com/goccy/go-json"
)
type (
// RemoteObject represents an object inside Qlik Associative Engine.
RemoteObject struct {
*ObjectInterface
*session
mutex sync.Mutex
changedChannels map[chan struct{}]bool
closedCh chan struct{}
}
)
// ChangedChannel returns a channel that will receive changes when the underlying object is invalidated.
func (r *RemoteObject) ChangedChannel() chan struct{} {
channel := make(chan struct{}, 16)
r.changedChannels[channel] = true
return channel
}
// RemoveChangeChannel unregisters a channel from further events.
func (r *RemoteObject) RemoveChangeChannel(channel chan struct{}) {
r.mutex.Lock()
if r.changedChannels[channel] != false {
delete(r.changedChannels, channel)
close(channel)
}
r.mutex.Unlock()
}
// Closed returns a channel that is closed when the remote object in Qlik Associative Engine is closed
func (r *RemoteObject) Closed() chan struct{} {
return r.closedCh
}
func (r *RemoteObject) signalChanged() {
r.mutex.Lock()
for channel := range r.changedChannels {
channel <- struct{}{}
}
r.mutex.Unlock()
}
func (r *RemoteObject) signalClosed() {
r.mutex.Lock()
close(r.closedCh)
for channel := range r.changedChannels {
if r.changedChannels[channel] != false {
delete(r.changedChannels, channel)
close(channel)
}
}
// Clear it
r.changedChannels = make(map[chan struct{}]bool)
r.mutex.Unlock()
}
// RPC invokes a method on the remote object. Not intended to be used directly but rather from generated schema code.
func (r *RemoteObject) RPC(ctx context.Context, method string, apiResponse interface{}, params ...interface{}) error {
invocationResponse := r.interceptorChain(ctx, &Invocation{RemoteObject: r, Method: method, Params: ensureAllEncodable(params)})
if invocationResponse.Error != nil {
return invocationResponse.Error
}
if apiResponse != nil {
err := json.Unmarshal(invocationResponse.Result, apiResponse)
if err != nil {
return err
}
}
return nil
}
// GetRemoteObject creates a new RemoteObject (proxy object for serverside object). Not intended to be used directly but rather from generated schema code.
func (r *RemoteObject) GetRemoteObject(objectInterface *ObjectInterface) *RemoteObject {
return r.session.getOrCreateRemoteObject(r.session, objectInterface)
}
// newRemoteObject creates a new RemoteObject instance
func newRemoteObject(session *session, objectInterface *ObjectInterface) *RemoteObject {
remoteObject := &RemoteObject{
session: session,
ObjectInterface: objectInterface,
changedChannels: make(map[chan struct{}]bool),
mutex: sync.Mutex{},
closedCh: make(chan struct{}),
}
// Signal that the object is by definition changed from the beginning
return remoteObject
}