Application
- Root
- Once
- Application
- Once
An Application class has only one instance. An Application instance can have a name. It can be associated to another instance which will interpret all the messages it receives but which it doesn't implement.
INSTANCE METHODS
- import { defclass, sendmsg, supersend } from 'So-o';
- import 'Once';
- defclass('Application', Once, 1,
- null,
- ['appName', 'app'],
- null,
The Application class inherits from the Once class. The instance properties appName and app contain the name of an Application instance and the reference to its proxy. The Application class redefines the instance messages init and doesNotRecognize. It adds the instance message appName.
init
SYNOPSIS
sendmsg($self, 'init', appName = null, app = null)
DESCRIPTION
init
initializes the name and the proxy of self
with appName
and app
.
CODE
- { 'init':
- (self, appName = null, app = null) => {
- supersend(Application, self, 'init');
- if (appName) {
- sendmsg(self, 'set', 'appName', appName);
- if (app)
- sendmsg(self, 'set', 'app', app);
- }
- return self;
- },
Runs the init
method of the superclass of the Application class.
Initializes the instance properties appName and app with the parameters appName
and app
.
appName
SYNOPSIS
sendmsg(self, 'appName')
DESCRIPTION
appName
returns the name of the Application instance self
.
If self
has no name, appName
returns null
.
CODE
- 'appName': (self) => sendmsg(self, 'get', 'appName'),
Returns the appName property of self
.
doesNotRecognize
SYNOPSIS
sendmsg(self, 'doesNotRecognize', msg[, arg...])
DESCRIPTION
An Application instance intercepts the message doesNotRecognize
which is automatically sent to a class or an instance which receives a message it doesn't implement.
doesNotRecognize
returns the result of forwarding msg
and its parameters to the proxy of self
, the instance which was passed as a parameter to the new message to create self
.
If self
has no proxy, doesNotRecognize
executes the message doesNotRecognize in the context of the superclass of the Application class, which will run the doesNotRecognize
method of the Root class, which displays an error message.
CODE
- 'doesNotRecognize':
- (self, msg, ...args) => {
- let app = sendmsg(self, 'get', 'app');
- if (!app)
- return supersend(Application, self, 'doesNotRecognize', msg);
- return sendmsg(app, 'perform', msg, args);
- }
- }
- );
Initializes app
to the value of the app property of self
.
If app
is null
, returns the result of executing the message doesNotRecognize by the superclass of the Application class.
Otherwise, if self
has a proxy, returns the result of sending msg
and its parameters to app
.
EXAMPLE
- import { sendmsg } from 'So-o';
- import 'Hello';
- var hello = sendmsg(Hello, 'new');
- sendmsg(hello, 'hello');
Creates an instance of Hello and sends it the message hello.
- import 'Application';
- var app = sendmsg(Application, 'new', 'Hello', hello);
- console.log(sendmsg(app, 'appName'));
Creates an instance of Application called Hello with hello
as proxy. Displays its name.
- sendmsg(app, 'hello');
Sends the message hello to app
which forwards to hello
.
- sendmsg(app, 'foobar');
Sends to app
a message unknown to the proxy.
- sendmsg(app, 'set', 'app', null);
- sendmsg(app, 'hello');
Removes the proxy from app
and sends the message hello to app
.
$ ln Hello.js node_modules/Hello.mjs
$ ln testApplication.js testApplication.mjs
$ nodejs --experimental-modules testApplication
Hello from So-o!
Hello
Hello from So-o!
Hello::foobar Invalid instance message
Application::hello Invalid instance message
Comments