Publish (Instance)
{{#images:versionlatest-32x32.png|stock}} <source lang='delphi'>procedure Publish(const iTopic:string; const iParam:variant);</source>
Cette procédure permet d'envoyer un message aux abonnés d'un sujet.
iTopic | Sujet du message. |
iParam | Paramètre du message. |
Exemple :
Utilisation basique :
<source lang='delphi'>
procedure MyObject.Handler(const iTopic:string; const iEvent:Variant); begin
unEntier := unEntier+1;
end;
function MyObject.doPublishSubscribe(const sTopic:string; const pTopic:string):boolean; begin
unEntier := 0; Subscribe(sTopic,Handler); try // if pTopic match sTopic the Handler method will be called. // This message has no parameter, we can pass anything in the parameter Publish(pTopic,0); Result := unEntier=1; finally UnSubscribe(sTopic); end;
end;
</source>
Utilisation d'un paramètre objet :
<source lang='delphi'>
procedure Handler(const iTopic:string; const iEvent:variant); var obj:TitObject; begin
obj := iEvent; if (obj is ClassNPA) then unEntier := (obj as ClassNPA).unEntier;
end;
function doPublishSubscribe(const sTopic:string; const pTopic:string):boolean; var event:ClassNPA; begin
unEntier := 0; event := ClassNPA.Create; event.unEntier := 1; Subscribe(sTopic,Handler); try Publish(pTopic,event); Result := unEntier=1; finally UnSubscribe(sTopic); end;
end;
</source>
Souscription dans une règle d'initialisation :
<source lang='delphi'> unit TestSYFREWF; interface
Type
PublishSubscribe = Class(TitObject) private procedure Action_Initialize:boolean; public Procedure doPublish; Procedure Handler(const iTopic:string; const iEvent:variant); end;
Implementation
{PublishSubscribe}
procedure PublishSubscribe.Action_Initialize:boolean; //procedure Action_Initialize:boolean; begin
// We subscribe in an initialization rule. SubScribe('/MyTopic/*','Handler');
end;
Procedure PublishSubscribe.Handler(const iTopic:string; const iEvent:variant); //Procedure Handler(const iTopic:string; const iEvent:variant); var url:string; begin
// This handler is a router which republish a message to the UI
// Build a first message with the current datetime. // The value is passed inside the topic which is formatted as an URL. url := 'forms://testpublishsubscribe/controls/memo'; urlConcatParameter(url,'text', FormatDatetime('c',now)); Publish(url,0); // Republish the message // The value is passed in the parameter Publish('forms://testpublishsubscribe/controls/memo/text',iEvent);
end;
Procedure PublishSubscribe.doPublish; //Procedure doPublish; begin
// Test case // Publish a message to my topic so that the handler will republish it Publish('/MyTopic/subject', 'some text');
end;
</source>
Voir aussi :
{{#if:Objets métiers (tech)|
{{#if:Objets métiers (tech)|— Objets métiers (tech) |}} — Développement DSM —
|
{{#if:|— [[{{{1}}}]] |}} — Développement DSM —
}}