surrealpatch/db/live.go
2021-12-14 08:13:19 +00:00

78 lines
1.8 KiB
Go

// Copyright © 2016 SurrealDB Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package db
import (
"sync"
"context"
"github.com/surrealdb/fibre"
"github.com/surrealdb/surrealdb/cnf"
"github.com/surrealdb/surrealdb/sql"
)
var sockets sync.Map
func register(fib *fibre.Context, id string) func() {
return func() {
sockets.Store(id, &socket{
fibre: fib,
sends: make(map[string][]interface{}),
items: make(map[string][]interface{}),
lives: make(map[string]*sql.LiveStatement),
})
}
}
func deregister(fib *fibre.Context, id string) func() {
return func() {
if sck, ok := sockets.Load(id); ok {
sck.(*socket).deregister(id)
}
}
}
func (e *executor) executeLive(ctx context.Context, stm *sql.LiveStatement) (out []interface{}, err error) {
if err := e.access(ctx, cnf.AuthNO); err != nil {
return nil, err
}
if sck, ok := sockets.Load(e.id); ok {
return sck.(*socket).executeLive(e, ctx, stm)
}
return nil, &LiveError{}
}
func (e *executor) executeKill(ctx context.Context, stm *sql.KillStatement) (out []interface{}, err error) {
if err := e.access(ctx, cnf.AuthNO); err != nil {
return nil, err
}
if sck, ok := sockets.Load(e.id); ok {
return sck.(*socket).executeKill(e, ctx, stm)
}
return nil, &LiveError{}
}