Skip to content

Instantly share code, notes, and snippets.

@wtrocki
Last active April 10, 2017 13:25
Show Gist options
  • Save wtrocki/950b2cd7e139f0df6cf702da0241e5f6 to your computer and use it in GitHub Desktop.
Save wtrocki/950b2cd7e139f0df6cf702da0241e5f6 to your computer and use it in GitHub Desktop.
Modrain - Modular architecture framework for modular client and server applications written in javascript/typescript.

ModRain Build Status

img

ModRain (Module Rain) modular architecture framework for modular client and server applications written in javascript/typescript. Use Modrain to implement microservices architecture for client and server side javascript apps.

Idea

ModRain

  • allows to write fully modularized javascript applications.
  • can be used for large scale frontend and node.js backend applications were reusability is expected.
  • prevents from npm version hell (multiple modules require each other)
  • allows to loosely couple modules and swap implementations at runtime

Quick start

Create new module (node.js app)

mkdir mymodule
cd mymodule
npm init
npm install modrain --save
touch index.js

Register module and create module interface.

var modrain = require("modrain");

var MODULE_NAME = "user";
var ACTION = "create";
// Register new module in namespace
var userModule = modrain.registerModule(MODULE_NAME);
// We can unregister module as well
//var userModule = modrain.unregisterModule(MODULE_NAME);
// Using method interface
userModule.registerHandler(ACTION, function(userObject, callback){
  // Implementation to save userObject
  // ...
  callback(null, "success");
  // In case of error
  callback("error");
});

TODO Change API to register module with entire module interface passed as second parameter.

Consuming module

var modrain = require("modrain");
modrain.getModule(user).call("create", userObject, function(err, result){
    // impl
});

// if global mount option is enabled
modrain.user.create(userObject, function(err, result){
     // impl
});

Consuming module - promises interface

var modrain = require("modrain");
modrain.getModule(user).call("create",userObject).then(function(err, result){
    // impl
});

Architecture

ModRain allows us to register modules into module registry and then define complete set of the actions on each module.

Interfaces

  • Callbacks
  • Promises

Classical callback interface is supported by default To enable promises use:

  npm install --save bluebird
  modrain.usePromises(true);

Modules

Modules can expose functionality to our application.

Transports

ModRain defines abstraction layer for module communications. Modules can interact with each other using transport in the background. Currently library supports following transports:

  • Direct method calls (globals)
  • Observer (ReactiveComponents)
  • Publish/Subscribe (using mediator)
  • HTTP (TODO)

Promises

Using promise library

Publish/Subscribe

Using mediator pattern and topics to register for some actions

API

ModRain is under active development. API may change.

TODO

@paolobueno
Copy link

The module registering seems nice, is it based on angular's dep injection?

Still currently there's not that much difference from the current api in capability and semantics, only in structure.
For a second iteration of the pub-sub bus I'd like to see a way to do data validation at this layer, either at runtime or leveraging typescript type checking in the future.

@wtrocki
Copy link
Author

wtrocki commented Mar 30, 2017

@paolobueno
It has nothing to do with angular at the moment, but I'm working to support other tools.
For this model angular dependency injection would be just defined as new transport.

Still currently there's not that much difference from the current api in capability and semantics, only in structure.

Difference is an transport abstraction so you can switch between transports without making changes in all modules.

@wtrocki

@paolobueno
Copy link

@wtrocki Apart from using a network protocol there's no advantage to switching between in-process communication mechanism if you're using the same api.
For instance switching between direct method calls and RxJS in a way that's transparent to client code just changes the performance profile since you can't leverage RxJS' extra features without using a specific api.

modrain.usePromises(true);

There's no need to use a flag and having a runtime switch between different interfaces would break code.
I personally vote for going full-promises, there's no need to support both.
Libraries that do so usually do something like this (very simplified):

function supportBoth(param, cb) {
  if(typeof cb === 'function') return cb(null, doWork(param));
  return Promise.resolve(doWork(param));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment