a tiny implementation to fulfil protocols (Interfaces) in nodejs.
A js object does not have any possibilities to implement a protocol (Interface) at language level. Well, that is very good in many ways, but get extrem unhandy when implementing a pluggable system. node-protocols is a tiny object extension to allow a class implements one or more protocols.
Create a myProtocol.js file:
module.exports.foo = function(){};
module.exports.bar = function(){};
module.exports.s = String();
Than implement a module like this:
var impl = module.exports = {
foo: function(){
console.log("I am foo");
}
}
To confirm that this implementation confirms to a protocol, node-protocols is required.
var protocol = require('node-protocols');
Finally a member named "protocol" must be set in your implementation that defines the protocol:
module.exports.protocol = protocol.implement(impl, 'myProtocol.js')
It is allowed to use any kind of type as a definition. Object(), Array(), function(), String()...
A special case is undefined
that is there to allow anything.
Implement does have 3 Parameters:
- module
- Protoccol-List
- (optional) throw exception
If the execution of the application should not be terminated when a protocol is not fulfilled, than pass a "false" to the implement function:
module.exports.protocol = protocol.implement(impl, 'myProtocol.js', false)
Returns a list of the protocols that the implementation successfully implements.
Returns a boolean indicator "true" if all protocols are successfully implemented.
Yes, please help us to improve node-protocols and tell us your thoughts about this module.
See https://www.ausdertechnik.de for more details. if you like this tool, spend a Satoshi to this address (We will buy more coffee from it):
1FaKEmpUz7S1g4v5qgmDWwQDzVYGBM6MLw