HeroEngine Forums
Welcome, Guest. Please login or Register for HeroCloud Account.

Author Topic: [Resolved] ObsSubject bug?  (Read 1312 times)

cheyennecloud

  • General Accounts
  • *
  • Posts: 10
    • View Profile
[Resolved] ObsSubject bug?
« on: Jul 18, 10, 01:03:28 AM »

In the addListener method of ObsSubjectClassMethods there is the following code:

Code: [Select]
method addListener( listener as NodeRef )
// Listeners that are not otherwise base_hard_associated are considered to be "owned" by the subject
//   which means the subject will clean then up when appropriate
//
  assert( HasMethod( listener, "EventRaised" ), "The listener node must implement $QEventRaised$Q method.")
 
  if HasMethod( listener, "IsAlreadyListening" )
    if listener.IsAlreadyListening( me )
      listener.destroyListener()
      return
    .
  .

If the listener is already listening to the subject why is it then destroyed?  Should it not just say return?


Also, why is code in ObsSubject calling listener.IsAlreadyListening() when IsAlreadyListening() calls right back to ObsSubject to find out the answer?  Wouldn't it be more efficient for ObsSubject to just call it's own method itself and save a couple back and forth calls or does inheritance make this a necessary evil.
« Last Edit: Nov 02, 12, 10:02:35 PM by HE-Cooper »
Logged

HE-CHRISTOPHER

  • HeroEngine
  • *****
  • Posts: 424
    • View Profile
Re: ObsSubject bug?
« Reply #1 on: Jul 20, 10, 02:42:36 PM »

In the addListener method of ObsSubjectClassMethods there is the following code:

Code: [Select]
method addListener( listener as NodeRef )
// Listeners that are not otherwise base_hard_associated are considered to be "owned" by the subject
//   which means the subject will clean then up when appropriate
//
  assert( HasMethod( listener, "EventRaised" ), "The listener node must implement $QEventRaised$Q method.")
  
  if HasMethod( listener, "IsAlreadyListening" )
    if listener.IsAlreadyListening( me )
      listener.destroyListener()
      return
    .
  .

If the listener is already listening to the subject why is it then destroyed?  Should it not just say return?


Also, why is code in ObsSubject calling listener.IsAlreadyListening() when IsAlreadyListening() calls right back to ObsSubject to find out the answer?  Wouldn't it be more efficient for ObsSubject to just call it's own method itself and save a couple back and forth calls or does inheritance make this a necessary evil.


Some types of listeners (such as "script" listeners which function as a proxy for a script in this observer implementation) are more complicated than a simple if new_listener = existing_listener check can express.  The listener encapsulates the logic required to determine if it is "already listening" so the subject does not have to know about the inner workings of the listeners.

As far as the default destruction of a listener that is added when it is already listening goes, the intent was to address the more abstract types of listeners like the script listener to avoid multiple callbacks for the same event.  In the case of a script listener, it evaluates to isalreadylistening=true if another listener for the subject is already sending events to the same script.  Destruction of the newly added listener, leaves the original in tact.

In the case of more concrete listeners, repeatedly adding the same listener to a subject is probably an indication of faulty design or logic errors.
Logged
Christopher Larsen
CTO
HeroEngine