Implement message exchange between modules
Level: intermediate
Example
Create the UsrSomeModule
module. Implement the following messages in the module:
MessageToSubscribe
address message that has Subscribe directionMessageToPublish
broadcast message that has Publish direction
Subscribe to the MessageToSubscribe
message sent by another module. Cancel the message registration.
1. Create a module
-
Open the Configuration section and select a user-made package to add the schema.
-
Click Add → Module on the section list toolbar.
-
Fill out the schema properties in the Module Designer.
- Set Code to "UsrSomeModule."
- Set Title to "SomeModule."
Click Apply to apply the changes.
-
Add the source code in the Module Designer.
UsrSomeModule/* Declare a module called UsrSomeModule. The module has no dependencies.
Therefore, an empty array is passed as the second module parameter. */
define("UsrSomeModule", [], function() {
Ext.define("Terrasoft.configuration.UsrSomeModule", {
alternateClassName: "Terrasoft.UsrSomeModule",
extend: "Terrasoft.BaseModule",
Ext: null,
sandbox: null,
Terrasoft: null,
init: function() {
this.callParent(arguments);
},
destroy: function() {
this.callParent(arguments);
}
});
return Terrasoft.UsrSomeModule;
}); -
Click Save on the Module Designer’s toolbar.
2. Register a message
- Declare message configuration objects in the
messages
schema property. - Add to the
init()
method thesandbox.registerMessages()
method call that registers messages.
Register a module message
...
/* Collection of the configuration message objects. */
messages: {
"MessageToSubscribe": {
mode: Terrasoft.MessageMode.PTP,
direction: Terrasoft.MessageDirectionType.SUBSCRIBE
},
"MessageToPublish": {
mode: Terrasoft.MessageMode.BROADCAST,
direction: Terrasoft.MessageDirectionType.PUBLISH
}
},
...
init: function() {
this.callParent(arguments);
/* Register a message collection. */
this.sandbox.registerMessages(this.messages);
},
...
3. Publish a message
- Implement the
processMessages()
method in the module schema. - In the
processMessages()
method, call thesandbox.publish()
method that publishes theMessageToPublish
message. - Add the
processMessages()
method call to theinit()
method.
Publish a module message
...
init: function() {
...
this.processMessages();
},
...
processMessages: function() {
this.sandbox.publish("MessageToPublish", null, [this.sandbox.id]);
},
...
4. Subscribe to a message
- Add the
sandbox.subscribe()
method call to theprocessMessages()
method. Thesandbox.subscribe()
method subscribes to theMessageToSubscribe
message sent by another module. - Specify the
onMessageSubscribe()
handler method in the method parameters and add it to the module source code.
Subscribe to a message from another module
...
processMessages: function() {
this.sandbox.subscribe("MessageToSubscribe", this.onMessageSubscribe, this, ["resultTag"]);
this.sandbox.publish("MessageToPublish", null, [this.sandbox.id]);
},
onMessageSubscribe: function(args) {
console.log("'MessageToSubscribe' received");
/* Modify the parameter. */
args.arg1 = 15;
args.arg2 = "new arg2";
/* Return the result. */
return args;
},
...
5. Cancel the message registration
Cancel the message registration
...
destroy: function() {
if (this.messages) {
var messages = this.Terrasoft.keys(this.messages);
/* Cancel the message array registration. */
this.sandbox.unRegisterMessages(messages);
}
this.callParent(arguments);
}
...
Source code
UsrSomeModule
/* Declare a module called UsrSomeModule. The module has no dependencies.
Therefore, an empty array is passed as the second module parameter. */
define("UsrSomeModule", [], function() {
Ext.define("Terrasoft.configuration.UsrSomeModule", {
alternateClassName: "Terrasoft.UsrSomeModule",
extend: "Terrasoft.BaseModule",
Ext: null,
sandbox: null,
Terrasoft: null,
/* Collection of the configuration message objects. */
messages: {
"MessageToSubscribe": {
mode: Terrasoft.MessageMode.PTP,
direction: Terrasoft.MessageDirectionType.SUBSCRIBE
},
"MessageToPublish": {
mode: Terrasoft.MessageMode.BROADCAST,
direction: Terrasoft.MessageDirectionType.PUBLISH
}
},
init: function() {
this.callParent(arguments);
/* Register a message collection. */
this.sandbox.registerMessages(this.messages);
this.processMessages();
},
processMessages: function() {
this.sandbox.subscribe("MessageToSubscribe", this.onMessageSubscribe, this, ["resultTag"]);
this.sandbox.publish("MessageToPublish", null, [this.sandbox.id]);
},
onMessageSubscribe: function(args) {
console.log("'MessageToSubscribe' received");
/* Modify the parameter. */
args.arg1 = 15;
args.arg2 = "new arg2";
/* Return the result. */
return args;
},
destroy: function() {
if (this.messages) {
var messages = this.Terrasoft.keys(this.messages);
/* Cancel the message array registration. */
this.sandbox.unRegisterMessages(messages);
}
this.callParent(arguments);
}
});
return Terrasoft.UsrSomeModule;
});