agent-backend/contracts/EventPublisher/EventPublisher.yjs
Frank.R.Wu ad2b0fb6c3 feat: update event mechanism
add event type local and global, clients have to use contractID and topic to subscribe local event; allow clients to subscribe topics (will not be recorded)
2021-10-31 23:07:14 +08:00

53 lines
1.9 KiB
Plaintext

contract EventPublisher {
/*
* event declaration: declare event topics
* equivalent to
* event AT_LEAST_ONCE te;
* can be used by function te and tes
* function te: publish an event with topic 'te' and default semantics AT_LEAST_ONCE
* function tes: publish an event with topic 'te' and another semantics
*/
event global te;
/*
* event declaration with semantics
* valid semantics: AT_LEASE_ONCE, AT_MOST_ONCE, and ONLY_ONCE
* can be used by function tews and tewss
* function tews: publish an event with topic 'tews' and semantics AT_MOST_ONCE
* function tewss: publish an event with topic 'tews' and another semantics
*/
event global tews(AT_MOST_ONCE);
// publish an event with declared topic and default semantics AT_LEAST_ONCE
export function pub1(e) {
var content = YancloudUtil.random() + ' EventPublisher.pub1';
// equivalent to
// tes(content, 'AT_LEAST_ONCE')
te(content);
return 'done';
}
// publish an event with declared topic and semantics
export function pub2(e) {
var content = YancloudUtil.random() + ' EventPublisher.pub2';
// equivalent to
// tewss(content, 'AT_MOST_ONCE')
tews(content);
return 'done';
}
// YancloudUtil.pubEvent and YancloudUtil.pubEventConstraint are used to publish events without event declarations
export function pub3(constraint) {
var content = YancloudUtil.random() + ' EventPublisher.pub3';
if (!constraint) {
/*
* YancloudUtil.pubEvent is used to publish events with a some topic and default semantics AT_LEAST_ONCE
* equivalent to
* YancloudUtil.pubEventConstraint('topic', content, 'AT_LEAST_ONCE');
*/
YancloudUtil.pubEvent('topic', content);
}
else {
// YancloudUtil.pubEventConstraint is used to publish events with a topic and another semantics
YancloudUtil.pubEventConstraint('topic', content, constraint);
}
return 'done';
}
}