Skip to content
Gx
GitHub

GX4015: on:topic has no gx.Topic or no action

The argument of on:topic is not a gx.Topic, its handler is not an action, or one element has two on:topic directives.

Cause

The directive on:topic runs an action when server code publishes a topic key. The key is a server value. A loader makes it with gx.TopicKey and gives it to the component as a prop of type gx.Topic. The page carries a signed token of the key, so a viewer cannot listen to a key that the server did not give it.

Gx reports GX4015 in three cases:

  • The argument of on:topic does not have the type gx.Topic. A plain string has no signature.
  • The handler is not one route literal. Each page runs its own action, with the session of its viewer.
  • One element has two on:topic directives. An element listens to one topic.

Example

orders/route/route.go
package route

import "github.com/alternayte/gx"

type Fresh struct {
	gx.Route `POST /orders/fresh`
}
orders/List.gx
package orders

import "app/orders/route"

props {
  Key string
}

<ul id="orders" on:topic(p.Key)={route.Fresh{}}></ul>
orders/orders.go
package orders

import (
	"github.com/alternayte/gx"
	"app/orders/route"
)

var fresh = gx.Action(func(c *gx.Ctx, in route.Fresh) error {
	return nil
})

var Routes = gx.Collect(fresh)

gx check reports:

gx check
orders/List.gx:9:26: GX4015: the argument of on:topic is not a gx.Topic: it has the type string

Fix

  1. Change the type of the prop to gx.Topic: props { Orders gx.Topic }.
  2. In the loader of the page, make the value: gx.TopicKey(c, "orders:"+userID).
  3. Write the directive with the prop: on:topic(p.Orders)={route.Fresh{}}.
  4. For a second topic, put the second on:topic on a child element.