Security News
NVD Backlog Tops 20,000 CVEs Awaiting Analysis as NIST Prepares System Updates
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
@thejonan/as-sys
Advanced tools
General purpose Entity-Component System implemented in JavaScript.
Traditional Object Oriented Programming has the inherent problem of enforcing tree-like distribution of functionality among active entities, which are the instances of different classes. A class is wrapping of certain functionality, which is made accessible to the "agent" in the memory, when an instance of this class, or any descendent, is created. This is limiting. If you need to have three types of instances:
Humans
which are capable of walking;Planes
which are capable of flying;Birds
which are capable of both flying and walking;You can inherit walking in Birds from Humans, but then you'll need to have separate implementation of flying for Planes. Alternatively, you can inherit flying in Birds from Planes, but then walking should be re-implemented in Humans. In either scenario one skill should be implemented twice. Multiple inheritance, found nowhere but in C++ is quite complicated solution, neither being recommended, nor actually solving many of the problems (e.g. - sharing of properties between different classes, in the same entity).
Additionally, some languages, like JavaScript, are not designed in a traditional OOP manner. Rather, upon instantiation of an object, it is given a prototype, which it is bound to, and given access to its properties.
So, if one has implementations of a set of skills, she can assemble them into a single prototype and all objects (which we call agents) instantiated from this prototype will have all the skills. This is exactly what asSys library is doing.
Let's have our Flying and Walking skills defined like this:
var Flying = function () { this.isFlying = false; }
Flying.prototype = {
takeOff: function () { this.isFlying = true; },
land: function() { this.isFlying = false; }
};
var Walking = function () { this.isWalking = false; }
Walking.prototype = {
go: function () { this.isWalking = true; },
stop: function() { this.isWalking = false; }
};
Not the most meaningful implementation, but enough for the example. So, what will it take to have Humans type objects (entities, agents) defined? Only this:
var Humans = a$(Walking);
Pay attention, that Humans
is not an instance in the normal sense. To have one we must do:
var aGuy = new Humans();
Of course, we haven't added too much, because we could freely do it this way:
var aGuy = new Walking();
It's the same. But, let's see how Birds are defined:
var Birds = a$(Walking, Flying);
Now, that is different. Again new actual birds are instantiated this way:
var aFalcon = new Birds();
And, of course, creating and instantiating Planes is no more difficult:
var Planes = a$(Flying);
var aJumbo = new Planes();
In this example Planes
, Birds
and Humans
are dynamically constructed functions which (upon instantiation) invoke all the passed skills's constructors (i.e. functions) in the same order, in which they are given. For example Birds
is a function, that invokes Walking()
then Flying()
.
The above functionality can easily be achieved with C++ multiple-inheritance as well.
Let's define the basic skills a bit differently:
var Flying = function () { this.isFlying = false; }
Flying.prototype = {
takeOff: function () { this.isFlying = this.isMoving = true; },
land: function() { this.isFlying = false; }
};
var Walking = function () { this.isWalking = false; }
Walking.prototype = {
go: function () { this.isWalking = this.isMoving = true; },
stop: function() { this.isWalking = false; }
};
We've added isMoving
property. Now we can check for it in all agents:
aGuy.go();
if (aGuy.isMoving) { alert("aGuy is moving!")}
aFalcon.takeOff();
if (aFalcon.isMoving) { alert("aFalcon is moving!")}
This is quite convenient! Actually sharing of properties and methods is vital for effective combination of skills. And if certain cooperation (between skills) is about to happen, there are some expectation arising. Let's say we want to wrap the isMoving
setup into a separate method like this:
var Flying = function () { this.isFlying = false; }
Flying.prototype = {
takeOff: function () { this.wakeUp(); this.isFlying = true; },
land: function() { this.isFlying = false; }
};
var Walking = function () { this.isWalking = false; }
Walking.prototype = {
go: function () { this.wakeUp(); this.isWalking = true; },
stop: function() { this.isWalking = false; }
};
That will fire an error, because no skill is providing wakeUp()
method. But, this error will occur on the first attempt to use it, which could be quite misleading. So asSys allows each skill to list methods that it expects to be present already:
var Flying = function () { this.isFlying = false; }
Flying.prototype = {
__expects: ["wakeUp"],
takeOff: function () { this.wakeUp(); this.isFlying = true; },
land: function() { this.isFlying = false; }
};
var Walking = function () { this.isWalking = false; }
Walking.prototype = {
__expects: ["wakeUp"],
go: function () { this.wakeUp(); this.isWalking = true; },
stop: function() { this.isWalking = false; }
};
Now, there will be an error again, but this time it'll happen in these (type of) lines:
var Humans = a$(Walking);
asSys will report missing expectation. So, listing a skill that provides it, is the solution:
var Being = function () { }
Being.prototype.wakeUp = function () { this.isMoving = true; }
var Humans = a$(Being, Walking);
var aGuy = new Humans();
aGuy.go();
Not assembling each agent on every instantiation, is intentional, because the number of different combinations of skills is quite limited, because it is (usually) manually provided, by the developer. However, number of agents could be quite large. Hence, the approach of constructing these dynamic functions and prototypes - this let's the actual object creating to utilize the normal JavaScript engine routines.
Also, attaching desired set of methods to each new instance will lead to unnecessary memory consumption, while having them wrapped in the prototype will make this more efficient as well.
act
tool with overlapping methodsCopyright © 2016-2018, Ivan (Jonan) Georgiev
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
An Agent-Skills (i.e. Entity-Component) System JavaScript library.
The npm package @thejonan/as-sys receives a total of 1 weekly downloads. As such, @thejonan/as-sys popularity was classified as not popular.
We found that @thejonan/as-sys demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.