- Maintain a structure marketFeeds with dynamic fields feedname. Each field has two subfields: subscriberHandle and listenerHandle.
- Input handle to new contract ( broker.addContract(contractHandle) ).
Anyone listening for a particular event?
2 次查看(过去 30 天)
显示 更早的评论
Hi,
I have an object that can fire many events, and computing the events takes a significant amount of time. Therefore, I would like to compute the events only if I know there is someone listening for them.
Is there any way for the object to get notified when a listener is added or removed? Is there any way to get the list of current listeners? Is there any way to check the count of active listeners for a given event?
Thanks, Joan
0 个评论
采纳的回答
Andrew Newell
2011-3-21
@Joan, This is an interesting problem! I don't know what the ideal solution is, but maybe you could create three classes: subscriber, contract and broker. The broker class would manage the other two. It would behave something like this:
For each feed name, see if it is one of the field names in marketFeeds.
If it exists, add contractHandle to list of handles maintained by the corresponding listener ( i.e., listenerHandle.addContract(marketFeeds.feedName.contractHandle)).
If it doesn't exist, add a new subscriber object and a listener for that object. Give the handle of the contract to the listener.
When a listener is told of a change in the market feed, it calls each of the handles with some command like contractHandle.reactToMarketChange(feedName,NewValue).
When the contract is terminated, send its handle to broker.deleteContract, which then does all the necessary actions on marketFeeds.
2 个评论
Andrew Newell
2011-3-28
@Joan, I'm glad it worked out! Unlike most of my answers, I couldn't test this one, so I wasn't sure if it would really solve your problem.
更多回答(2 个)
Andrew Newell
2011-3-20
I would think that the easiest way would be to define an event property in for each event, like this:
classdef complicatedClass < handle
...
events
event1
...
eventN
end
methods
function obj = longEvent1(obj,varargin)
...
notify(obj,'event1');
end
...
end
and then have a listener class, e.g., respondToEvent1, for each event. Then start them all at once and you can be sure something is listening without a lot of bookkeeping.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Environment and Settings 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!