diff --git a/context.go b/context.go index 0e3579e..6d31c4c 100644 --- a/context.go +++ b/context.go @@ -1145,6 +1145,24 @@ func NewContext(db * badger.DB, log Logger) (*Context, error) { if err != nil { return nil, err } + switch source := p.Source.(type) { + case *StatusSignal: + ctx.Context.Log.Logf("test", "StatusSignal: %+v", source) + cached_node, cached := ctx.NodeCache[source.Source] + ctx.Context.Log.Logf("test", "Cached: %t", cached) + if cached { + for ext_type, ext_changes := range(source.Changes) { + cached_ext, cached := cached_node.Data[ext_type] + if cached { + for _, field := range(ext_changes) { + delete(cached_ext, string(field)) + } + cached_node.Data[ext_type] = cached_ext + } + } + ctx.NodeCache[source.Source] = cached_node + } + } return ResolveNode(ctx.Server.ID, p) }, }, diff --git a/gql_node.go b/gql_node.go index 3ef0991..cef4633 100644 --- a/gql_node.go +++ b/gql_node.go @@ -94,6 +94,7 @@ func GetResolveFields(id NodeID, ctx *ResolveContext, p graphql.ResolveParams) ( return m, nil } +// TODO: instead of doing the read right away, check if any new fields need to be read func ResolveNode(id NodeID, p graphql.ResolveParams) (NodeResult, error) { ctx, err := PrepResolve(p) if err != nil { diff --git a/gql_test.go b/gql_test.go index 8697468..65a991a 100644 --- a/gql_test.go +++ b/gql_test.go @@ -18,7 +18,7 @@ import ( func TestGQLSubscribe(t *testing.T) { - ctx := logTestContext(t, []string{"test", "listener", "changes"}) + ctx := logTestContext(t, []string{"test"}) n1, err := NewNode(ctx, nil, "Lockable", 10, NewLockableExt(nil)) fatalErr(t, err) @@ -94,7 +94,7 @@ func TestGQLSubscribe(t *testing.T) { n, err = ws.Read(resp) fatalErr(t, err) - ctx.Log.Logf("test", "SUB: %s", resp[:n]) + ctx.Log.Logf("test", "SUB1: %s", resp[:n]) lock_id, err := LockLockable(ctx, gql) fatalErr(t, err) @@ -111,7 +111,15 @@ func TestGQLSubscribe(t *testing.T) { n, err = ws.Read(resp) fatalErr(t, err) - ctx.Log.Logf("test", "SUB: %s", resp[:n]) + ctx.Log.Logf("test", "SUB2: %s", resp[:n]) + + n, err = ws.Read(resp) + fatalErr(t, err) + ctx.Log.Logf("test", "SUB3: %s", resp[:n]) + + n, err = ws.Read(resp) + fatalErr(t, err) + ctx.Log.Logf("test", "SUB4: %s", resp[:n]) // TODO: check that there are no more messages sent to ws within a timeout } diff --git a/lockable.go b/lockable.go index a040eaf..d470655 100644 --- a/lockable.go +++ b/lockable.go @@ -129,7 +129,7 @@ func (ext *LockableExt) HandleUnlockSignal(ctx *Context, node *Node, source Node messages = append(messages, SendMsg{source, NewErrorSignal(signal.Id, "not_owner")}) } else { if len(ext.Requirements) == 0 { - changes = append(changes, "state", "owner", "pending_owner") + changes = append(changes, "lockable_state", "owner", "pending_owner") ext.Owner = nil @@ -139,7 +139,7 @@ func (ext *LockableExt) HandleUnlockSignal(ctx *Context, node *Node, source Node messages = append(messages, SendMsg{source, NewSuccessSignal(signal.Id)}) } else { - changes = append(changes, "state", "waiting", "requirements", "pending_owner") + changes = append(changes, "lockable_state", "waiting", "requirements", "pending_owner") ext.PendingOwner = nil @@ -173,7 +173,7 @@ func (ext *LockableExt) HandleLockSignal(ctx *Context, node *Node, source NodeID switch ext.State { case Unlocked: if len(ext.Requirements) == 0 { - changes = append(changes, "state", "owner", "pending_owner") + changes = append(changes, "lockable_state", "owner", "pending_owner") ext.Owner = new(NodeID) *ext.Owner = source @@ -184,7 +184,7 @@ func (ext *LockableExt) HandleLockSignal(ctx *Context, node *Node, source NodeID ext.State = Locked messages = append(messages, SendMsg{source, NewSuccessSignal(signal.Id)}) } else { - changes = append(changes, "state", "requirements", "waiting", "pending_owner") + changes = append(changes, "lockable_state", "requirements", "waiting", "pending_owner") ext.PendingOwner = new(NodeID) *ext.PendingOwner = source @@ -221,7 +221,7 @@ func (ext *LockableExt) HandleErrorSignal(ctx *Context, node *Node, source NodeI switch ext.State { case Locking: - changes = append(changes, "state", "requirements") + changes = append(changes, "lockable_state", "requirements") ext.Requirements[id] = Unlocked @@ -242,11 +242,11 @@ func (ext *LockableExt) HandleErrorSignal(ctx *Context, node *Node, source NodeI } if unlocked == len(ext.Requirements) { - changes = append(changes, "owner", "state") + changes = append(changes, "owner", "lockable_state") ext.State = Unlocked ext.Owner = nil } else { - changes = append(changes, "state") + changes = append(changes, "lockable_state") ext.State = AbortingLock } @@ -271,7 +271,7 @@ func (ext *LockableExt) HandleErrorSignal(ctx *Context, node *Node, source NodeI } if unlocked == len(ext.Requirements) { - changes = append(changes, "owner", "state") + changes = append(changes, "owner", "lockable_state") ext.State = Unlocked ext.Owner = nil } @@ -309,7 +309,7 @@ func (ext *LockableExt) HandleSuccessSignal(ctx *Context, node *Node, source Nod } if locked == len(ext.Requirements) { - changes = append(changes, "state", "owner", "req_id") + changes = append(changes, "lockable_state", "owner", "req_id") ext.State = Locked ext.Owner = new(NodeID) @@ -338,7 +338,7 @@ func (ext *LockableExt) HandleSuccessSignal(ctx *Context, node *Node, source Nod } if unlocked == len(ext.Requirements) { - changes = append(changes, "state", "pending_owner", "req_id") + changes = append(changes, "lockable_state", "pending_owner", "req_id") messages = append(messages, SendMsg{*ext.PendingOwner, NewErrorSignal(*ext.ReqID, "not_unlocked: %s", ext.State)}) ext.State = Unlocked @@ -359,7 +359,7 @@ func (ext *LockableExt) HandleSuccessSignal(ctx *Context, node *Node, source Nod } if unlocked == len(ext.Requirements) { - changes = append(changes, "state", "owner", "req_id") + changes = append(changes, "lockable_state", "owner", "req_id") messages = append(messages, SendMsg{*ext.Owner, NewSuccessSignal(*ext.ReqID)}) ext.State = Unlocked