27

Responder

  1. Root
    1. Responder

A Responder instance automatically sends notification messages to one or several instances. Responder instances can be linked so notifications are automatically transmitted from one responder to another.

INSTANCE METHODS

  1. import { defclass, sendmsg, supersend } from 'So-o';
  2.  
  3. defclass('Responder', null, 1,
  4.     null,
  5.     ['nextResponders'],
  6.     null,

The Responder class inherits from the Root class. The instance property nextResponders holds the list of instances which have asked to be notified when an event occurs. The Responder class adds the instance messages nextResponders, setNextResponders, addNextResponder, removeNextResponder and respondTo.

nextResponders
SYNOPSIS

sendmsg(self, 'nextResponders')

DESCRIPTION

nextResponders returns the list of responders of self, an array of instances.

CODE
  1.     {   'nextResponders':       (self) => sendmsg(self, 'get', 'nextResponders'),
setNextResponders
SYNOPSIS

sendmsg(self, 'setNextResponders', responders)

DESCRIPTION

setNextResponders initializes the list of responders of self to responders. responders is an array of instances.

CODE
  1.         'setNextResponders':    (self, responders) => sendmsg(self, 'set', 'nextResponders', responders),
addNextResponder
SYNOPSIS

sendmsg(self, 'addNextResponder', r)

DESCRIPTION

addNextResponder adds r to the list of responders of self.

CODE
  1.         'addNextResponder':
  2.             (self, r) => {
  3.                 let responders = sendmsg(self, 'get', 'nextResponders');
  4.  
  5.                 if (!responders)
  6.                     sendmsg(self, 'set', 'nextResponders', [r]);
  7.                 else {
  8.                     if (responders.indexOf(r) == -1)
  9.                         responders.push(r);
  10.                 }
  11.  
  12.                 return self;
  13.             },
removeNextResponder
SYNOPSIS

sendmsg(self, 'removeNextResponder', r)

DESCRIPTION

removeNextResponder removes r from the list of responders of self.

CODE
  1.         'removeNextResponder':
  2.             (self, r) => {
  3.                 let responders = sendmsg(self, 'get', 'nextResponders');
  4.  
  5.                 if (responders) {
  6.                     let i = responders.indexOf(r);
  7.                    
  8.                     if (i != -1)
  9.                         responders.splice(i, 1);
  10.                 }
  11.    
  12.                 return self;
  13.             },
respondTo
SYNOPSIS

sendmsg(self, 'respondTo', msg[, arg ...])

DESCRIPTION
CODE

If self responds to msg, respondTo sends msg and its parameters arg to self. If self doesn't respond to msg or if the execution of the message returns false, respondTo transmits the message and its parameters to all the instances of the list of responders of self.

  1.         'respondTo':
  2.             (self, msg, ...args) => {
  3.                 if (sendmsg(self, 'respondsTo', msg) && sendmsg(self, msg, ...args))
  4.                     return self;
  5.  
  6.                 let responders = sendmsg(self, 'get', 'nextResponders');
  7.                
  8.                 if (responders) {
  9.                     for (let r of responders)
  10.                         sendmsg(r, 'respondTo', msg, ...args);
  11.                 }
  12.  
  13.                 return self;
  14.             }
  15.     }
  16. );
EXAMPLE

An instance of Button notifies the message clicked when it receives the message click:

  1. import { defclass, sendmsg } from 'So-o';
  2.  
  3. import 'Responder';
  4.  
  5. defclass('Button', Responder, 1,
  6.     null,
  7.     null,
  8.     null,
  9.     {   'click':    (self) => {
  10.                         console.log(self + ' clicked');
  11.  
  12.                         sendmsg(self, 'respondTo', 'clicked', self);
  13.                     },
  14.     }
  15. );

click displays a trace message then notifies the message clicked.

An instance of Action responds to clicked:

  1. import { defclass, sendmsg } from 'So-o';
  2.  
  3. import 'Responder';
  4.  
  5. defclass('Action', Responder, 1,
  6.     null,
  7.     null,
  8.     null,
  9.     {   'clicked':  (self, sender) => {
  10.                         console.log(self + ' clicked from ' + sender);
  11.  
  12.                         return false;
  13.                     }
  14.     }
  15. );

An instance of AnotherAction responds to clicked too but returns true:

  1. import { defclass, sendmsg } from 'So-o';
  2.  
  3. import 'Responder';
  4.  
  5. defclass('AnotherAction', Responder, 1,
  6.     null,
  7.     null,
  8.     null,
  9.     {   'clicked':  (self, sender) => {
  10.                         console.log(self + ' clicked from ' + sender);
  11.  
  12.                         return true;
  13.                     }
  14.     }
  15. );
  1. import { sendmsg } from 'So-o';
  2.  
  3. import 'Button';
  4.  
  5. var btn = sendmsg(Button, 'new');
  6.  
  7. sendmsg(btn, 'click');

Creates an instance of Button and sends it the message click.

clicked is displayed by btn.

  1. import 'Action';
  2.  
  3. var act1 = sendmsg(Action, 'new');
  4.  
  5. sendmsg(btn, 'addNextResponder', act1);
  6. sendmsg(btn, 'click');

Creates and instance of Action and adds it as a responder to the instance of Button. Sends the message click to the button.

clicked is displayed by btn which sends to act1

  1. import 'AnotherAction';
  2.  
  3. var act2 = sendmsg(AnotherAction, 'new');
  4.  
  5. sendmsg(btn, 'addNextResponder', act2);
  6. sendmsg(btn, 'click');

Creates and instance of anotherAction and adds it as a responder to the instance of Button. Sends the message click to the button.

clicked is sent by btn to act1 then to act2.

  1. sendmsg(btn, 'removeNextResponder', act2);
  2. sendmsg(btn, 'click');

Removes the instance of AnotherAction from the list of responders of the instance of Button. Sends the message click to the button.

act2 doesn't receive the message clicked anymore.

  1. sendmsg(act1, 'addNextResponder', act2);
  2. sendmsg(btn, 'click');

Adds the instance of AnotherAction to the list of responders of the instance of Action. Sends the message click to the button.

clicked is sent to the responder of btn then to the responder of act1.

  1. sendmsg(btn, 'removeNextResponder', act1);
  2. sendmsg(act1, 'removeNextResponder', act2);
  3. sendmsg(btn, 'click');

Removes the instance of Action from the list of responders of the instance of Button. Removes the instance of AnotherAction from the list of responders of the instance of Action. Sends the message click to the button.

  1. sendmsg(act2, 'addNextResponder', act1);
  2. sendmsg(btn, 'addNextResponder', act2);
  3. sendmsg(btn, 'click');

Adds the instance of Action to the list of responders of the instance of AnotherAction. Adds the instance of AnotherAction to the list of responders of the instance of Button. Sends the message click to the button.

clicked is sent by btn to act2 which returns true and blocks sending the message to act1.

$ ln Responder.js node_modules/Responder.mjs
$ ln testResponder.js testResponder.mjs
$ nodejs --experimental-modules testResponder
object(Button) clicked
object(Button) clicked
object(Action) clicked from object(Button)
object(Button) clicked
object(Action) clicked from object(Button)
object(AnotherAction) clicked from object(Button)
object(Button) clicked
object(Action) clicked from object(Button)
object(Button) clicked
object(Action) clicked from object(Button)
object(AnotherAction) clicked from object(Button)
object(Button) clicked
object(Button) clicked
object(AnotherAction) clicked from object(Button)

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].