52 lines
938 B
Go
52 lines
938 B
Go
package dart
|
|
|
|
import (
|
|
"errors"
|
|
"sync"
|
|
)
|
|
|
|
type CloudContext struct {
|
|
database *Database
|
|
websocketManager *WebsocketManager
|
|
state map[string]any
|
|
mutex sync.Mutex
|
|
}
|
|
|
|
func (c *CloudContext) Database() (*Database, error) {
|
|
if c.database == nil {
|
|
return nil, errors.New("database is not enabled")
|
|
}
|
|
|
|
return c.database, nil
|
|
}
|
|
|
|
func (c *CloudContext) WebsocketManager() (*WebsocketManager, error) {
|
|
if c.websocketManager == nil {
|
|
return nil, errors.New("websocket manager is not enabled")
|
|
}
|
|
|
|
return c.websocketManager, nil
|
|
}
|
|
|
|
func (c *CloudContext) Set(key string, value any) {
|
|
c.mutex.Lock()
|
|
defer c.mutex.Unlock()
|
|
|
|
c.state[key] = value
|
|
}
|
|
|
|
func (c *CloudContext) Get(key string) (any, bool) {
|
|
c.mutex.Lock()
|
|
defer c.mutex.Unlock()
|
|
|
|
val, exists := c.state[key]
|
|
return val, exists
|
|
}
|
|
|
|
func (c *CloudContext) Delete(key string) {
|
|
c.mutex.Lock()
|
|
defer c.mutex.Unlock()
|
|
|
|
delete(c.state, key)
|
|
}
|