Responder
- Root
- 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
- import { defclass, sendmsg, supersend } from 'So-o';
- defclass('Responder', null, 1,
- null,
- ['nextResponders'],
- 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
- { '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
- '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
- 'addNextResponder':
- (self, r) => {
- let responders = sendmsg(self, 'get', 'nextResponders');
- if (!responders)
- sendmsg(self, 'set', 'nextResponders', [r]);
- else {
- if (responders.indexOf(r) == -1)
- responders.push(r);
- }
- return self;
- },
removeNextResponder
SYNOPSIS
sendmsg(self, 'removeNextResponder', r)
DESCRIPTION
removeNextResponder
removes r
from the list of responders of self
.
CODE
- 'removeNextResponder':
- (self, r) => {
- let responders = sendmsg(self, 'get', 'nextResponders');
- if (responders) {
- let i = responders.indexOf(r);
- if (i != -1)
- responders.splice(i, 1);
- }
- return self;
- },
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
.
- 'respondTo':
- (self, msg, ...args) => {
- if (sendmsg(self, 'respondsTo', msg) && sendmsg(self, msg, ...args))
- return self;
- let responders = sendmsg(self, 'get', 'nextResponders');
- if (responders) {
- for (let r of responders)
- sendmsg(r, 'respondTo', msg, ...args);
- }
- return self;
- }
- }
- );
EXAMPLE
An instance of Button notifies the message clicked when it receives the message click:
- import { defclass, sendmsg } from 'So-o';
- import 'Responder';
- defclass('Button', Responder, 1,
- null,
- null,
- null,
- { 'click': (self) => {
- console.log(self + ' clicked');
- sendmsg(self, 'respondTo', 'clicked', self);
- },
- }
- );
click
displays a trace message then notifies the message clicked.
An instance of Action responds to clicked:
- import { defclass, sendmsg } from 'So-o';
- import 'Responder';
- defclass('Action', Responder, 1,
- null,
- null,
- null,
- { 'clicked': (self, sender) => {
- console.log(self + ' clicked from ' + sender);
- return false;
- }
- }
- );
An instance of AnotherAction responds to clicked
too but returns true
:
- import { defclass, sendmsg } from 'So-o';
- import 'Responder';
- defclass('AnotherAction', Responder, 1,
- null,
- null,
- null,
- { 'clicked': (self, sender) => {
- console.log(self + ' clicked from ' + sender);
- return true;
- }
- }
- );
- import { sendmsg } from 'So-o';
- import 'Button';
- var btn = sendmsg(Button, 'new');
- sendmsg(btn, 'click');
Creates an instance of Button and sends it the message click.
clicked is displayed by btn
.
- import 'Action';
- var act1 = sendmsg(Action, 'new');
- sendmsg(btn, 'addNextResponder', act1);
- 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
- import 'AnotherAction';
- var act2 = sendmsg(AnotherAction, 'new');
- sendmsg(btn, 'addNextResponder', act2);
- 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
.
- sendmsg(btn, 'removeNextResponder', act2);
- 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.
- sendmsg(act1, 'addNextResponder', act2);
- 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
.
- sendmsg(btn, 'removeNextResponder', act1);
- sendmsg(act1, 'removeNextResponder', act2);
- 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.
- sendmsg(act2, 'addNextResponder', act1);
- sendmsg(btn, 'addNextResponder', act2);
- 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