4
Once
- Root
- Once
A Once class creates only one instance.
CLASS METHODS
- import { defclass, sendmsg, supersend } from 'So-o';
- defclass('Once', null, 1,
- ['instance'],
- null,
The Once class inherits from the Root class. The class property instance keeps the unique instance of the class. The Once class redefines the class message new.
new
SYNOPSIS
sendmsg(c, 'new'[, arg ...])
DESCRIPTION
The first time new is called, it returns a new instance of c
.
The following times, it returns this same instance of c
.
new passes all the arg
parameters of the message to init.
CODE
- { 'new':
- (self, ...args) => {
- let i = sendmsg(self, 'get', 'instance');
- if (!i) {
- i = supersend(Once, self, 'new', ...args);
- sendmsg(self, 'set', 'instance', i);
- }
- return i;
- }
- },
- null
- );
new
retrieves the class property instance.
If this property has no value yet, new
creates a new instance by calling the new
method inherited from the Root class with all the arguments of the message and saves the new instance in the class property instance.
new
returns the instance which was saved or the new instance.
Comments