Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagetypescript
export type Settings = { enabled: false; } | {
  /**
   * @title Enabled
   * @default false
   */
  enabled: true;
  
  /**
   * @title Some variable
   * @default “some-value”
   */
  some_variable: string;
};

Example with array of item:

Code Block
interface Item{
  /**
   * @title Name
   * @default ""
   */
  name: string;

  /**
   * @title Code
   * @default ""
   */
  code: string;
}

interface Items extends Array<Item>{}

export type Settings = { enabled: false } | {
  /**
   * @title Enabled
   */
  enabled: true;

  items: Items;
}

Note the conditional type here. In this example an instance of type Settings may have one of 2 interfaces: either enabled field has a value of false, or it is true and then some_variable field is required. When a settings form is rendered that would mean that some_variable field will be hidden, unless enabled field has a value of true (it will be rendered as a checkbox, so the checkbox must be checked).

...

Module-to-app communication

...

A module is able to send messages to an application by invoking an RPC call on GdmAgent module. Reference implementation in JS:

...

Code Block
const gridos = require(‘./gridos.js’gridos’);

...

const client = await gridos.connect();

...

client.broadcast({ type: 'Test.Message', some: 'data' });

Receiving messages in the app

From the app side, @ombori/ga-messaging library should be used. Reference implementation:

Code Block
import { useSubscribe } from ‘@ombori/ga-messaging’;

const App = () => {

   ...
   const {count, setCount} = useState(0);
   useSubscribe('Test.Message', (msg) => { useBroadcast
     setCount(ct => ct + 1);
   }, [setCount]);

  return <div>Received {count} messages</div>
};

App-to-module communication

To send messages from app to a module, use @ombori/ga-messaging library. Example:

Code Block
import { usePublish } from ‘@ombori/ga-messaging’;

const App = () => {

   ...

   const broadcastpublish = useBroadcastusePublish();
   const onClick = useCallback(() => {
     broadcast     publish({type: ‘Button‘Hello.Clicked’There’, some: ‘data here’}); 
   }, [broadcastpublish]);

  return <button onClick={onClick} />
};

Receiving messages in the module

A module is receiving messages via an inbound RPC call of broadcast method. Reference implementation:

Code Block
client.onMethod(broadcast => (req, res) => {
  try {
    const data = req.payload;
    swtch(data.type) {
      case 'Hello.There':
        console.log('a hello event is received', data);
      break;
    }
    res.send(200, 'ok');
  } catch (e) {
    res.send(500, e.toString())
  }
})

Nodejs-based modules can use gridos library instead:

Code Block
const gridos = require('./gridos');

...

const client = await gridos.connect();

...

client.onEvent('Hello.There', (data) => {
  console.log('a hello event is received', data);
});