Code Monkey home page Code Monkey logo

js-bus's Introduction

jsBus is no more maintained. It was moved to @layer/bus.

jsBus

Build Status npm version Coverage Status Known Vulnerabilities Greenkeeper badge CodeFactor BCH compliance License

This is an experimental library inspired by MessageBus for PHP.

Motivations

This is just an experimental library to decoupling UI/UX code and the business logic. It is intended to be used with Angular2 projects. Anyway, it is written in TypeScript and it can be used with any TypeScript application without limitations.

Install

NPM

npm install js-bus

Yarn

yarn add js-bus

Development

We use NodeJS and NVM to handle the node versions. So, first of all:

  • Git clone this repo.
  • Install nvm if you don't have it already: curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash. This installs version v0.32.1, but any other should be the same.
  • Install the version of node we use in the project. In the project root perform cat .nvmrc to see this version. Let's say it is v6.5.0. Afterwards, run nvm install v6.5.0.
  • Install yarn dependency manager. npm install -g yarn.
  • Type yarn install to install all dependencies.
  • Good to go!

Link for development (optional)

If you want to link the library to an existing project before pushing your changes. You can go to the library root and type:

yarn link

then in the project root:

yarn link js-bus

If you want to remove the dependency, just type the same command using unlink instead of link.

Contributing

We use conventional commit so install commitizen.

npm install -g commitizen

Afterwards, add the conventional changelog adapter. Go in the library root and type:

npm run cz:init

You are set! Each time you want to commit something use git cz instead of git commit and follow the wizard to create the commit message.

js-bus's People

Contributors

greenkeeper[bot] avatar tafax avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

js-bus's Issues

Setup multiple handlers for a message

Currently a message can be bound to a single handler, e.g.

export const MESSAGE_HANDLER_PAIRS: MessageHandlerPair[] = [
  { message: ProcessPaymentCommand, handler: ProcessPaymentCommandHandler }
];

We'd like to have the chance to connect multiple handlers to a single message. A possible interface would be:

export const MESSAGE_HANDLER_PAIRS: MessageHandlerPair[] = [
  { message: ProcessPaymentCommand, handlers: [ ProcessPaymentCommandHandler ] }
];

NOTES:

Going to submit a PR soon πŸ˜„

Improving the build to create multiple bundles

We want to improve the building process in order to provide these artifacts:

  • UMD
  • ESM5
  • ESM2015
  • FESM5
  • FESM2015

Define these entries in package.json:

{
  "name": "js-bus", 
  "module": "./fesm5/js-bus.js", 
  "es2015": "./fesm2015/js-bus.js", 
  "esm5": "./esm5/js-bus.js",
  "esm2015": "./esm2015/js-bus.js",
  "fesm5": "./fesm5/js-bus.js",
  "fesm2015": "./fesm2015/js-bus.js",
  "main": "bundles/js-bus.umd.js", 
  "typings": "js-bus.d.ts",
  "sideEffects": false
}

TsConfig to module type:

  • target es5 => umd
  • target es5 => esm5/fesm5
  • target es2018 => esm2015/fesm2015

References:

An in-range update of rollup is breaking the build 🚨

The devDependency rollup was updated from 1.27.9 to 1.27.10.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

rollup is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build failed (Details).

Release Notes for v1.27.10

2019-12-11

Bug Fixes

  • Keep track of function return values in more situations (#3278)

Pull Requests

Commits

The new version differs by 3 commits.

  • a562ec5 1.27.10
  • 6a2b3f8 Update changelog
  • 31747e3 Avoid some unnecessary value tracking deoptimizations (#3278)

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of rollup is breaking the build 🚨

The devDependency rollup was updated from 1.14.2 to 1.14.3.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

rollup is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).

Release Notes for v1.14.3

2019-06-06

Bug Fixes

  • Generate correct external imports when importing from a directory that would be above the root of the current working directory (#2902)

Pull Requests

Commits

The new version differs by 4 commits.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of @types/node is breaking the build 🚨

The devDependency @types/node was updated from 12.12.13 to 12.12.14.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

@types/node is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

An in-range update of mocha is breaking the build 🚨

The devDependency mocha was updated from 6.0.2 to 6.1.0.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

mocha is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build failed (Details).

Release Notes for v6.1.0

6.1.0 / 2019-04-07

πŸ”’ Security Fixes

  • #3845: Update dependency "js-yaml" to v3.13.0 per npm security advisory (@plroebuck)

πŸŽ‰ Enhancements

  • #3766: Make reporter constructor support optional options parameter (@plroebuck)
  • #3760: Add support for config files with .jsonc extension (@sstephant)

πŸ“  Deprecations

These are soft-deprecated, and will emit a warning upon use. Support will be removed in (likely) the next major version of Mocha:

πŸ› Fixes

  • #3829: Use cwd-relative pathname to load config file (@plroebuck)
  • #3745: Fix async calls of this.skip() in "before each" hooks (@juergba)
  • #3669: Enable --allow-uncaught for uncaught exceptions thrown inside hooks (@givanse)

and some regressions:

πŸ“– Documentation

πŸ”© Other

  • #3830: Replace dependency "findup-sync" with "find-up" for faster startup (@cspotcode)
  • #3799: Update devDependencies to fix many npm vulnerabilities (@XhmikosR)
Commits

The new version differs by 28 commits.

  • f4fc95a Release v6.1.0
  • bd29dbd update CHANGELOG for v6.1.0 [ci skip]
  • aaf2b72 Use cwd-relative pathname to load config file (#3829)
  • b079d24 upgrade deps as per npm audit fix; closes #3854
  • e87c689 Deprecate this.skip() for "after all" hooks (#3719)
  • 81cfa90 Copy Suite property "root" when cloning; closes #3847 (#3848)
  • 8aa2fc4 Fix issue 3714, hide pound icon showing on hover header on docs page (#3850)
  • 586bf78 Update JS-YAML to address security issue (#3845)
  • d1024a3 Update doc examples "tests.html" (#3811)
  • 1d570e0 Delete "/docs/example/chai.js"
  • ade8b90 runner.js: "self.test" undefined in Browser (#3835)
  • 0098147 Replace findup-sync with find-up for faster startup (#3830)
  • d5ba121 Remove "package" flag from sample config file because it can only be passes as CLI arg (#3793)
  • a3089ad update package-lock
  • 75430ec Upgrade yargs-parser dependency to avoid loading 2 copies of yargs

There are 28 commits in total.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Provide a more sophisticated approach for handling the service locator

We provide the ability the handler resolution through a service locator.

const resolver = new ServiceLocatorAwareCallableResolver(aFunction);

aFunction is a function object that is able to get an identifier (the same you use for the mapping) and provide a well-defined handler that is a handler that has a method called handle.

Even if you can create your own resolver using the provided interface CallableResolverInterface, the service locator resolver is very useful for integrating the library in many frameworks.

We'll provide a way to avoid passing a function so you can avoid handling the function scope. Instead, we'll introduce an adapter that you can use to decoupling you specific service locator to what the library expects to use.

const myFrameworkLocator = ...;

const jsBusLocatoAdapter = new JsBusLocatorAdapter(myFrameworkLocator);
const resolver = new ServiceLocatorAwareCallableResolver(jsBusLocatoAdapter);

JsBusLocatorAdapter will be a class conform to a specific interface that the consumer needs to create based on its own service locator.

We'll leave the ability to use the function as before, but will become deprecated.

An in-range update of rollup is breaking the build 🚨

The devDependency rollup was updated from 1.9.2 to 1.9.3.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

rollup is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • ❌ continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).

Release Notes for v1.9.3

2019-04-10

Bug Fixes

  • Simplify return expressions that are evaluated before the surrounding function is bound (#2803)

Pull Requests

  • #2803: Handle out-of-order binding of identifiers to improve tree-shaking (@lukastaegert)
Commits

The new version differs by 3 commits.

  • 516a06d 1.9.3
  • a5526ea Update changelog
  • c3d73ff Handle out-of-order binding of identifiers to improve tree-shaking (#2803)

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Avoid to use function binding for handlers

Once the handlers are resolved we use function binding to handle it.
It is originally implemented to make the library as agnostic as possible. It doesn't work very well with TypeScript and in general, the binding is a bad technique.

// Uses the bind to maintain the scope of the object.
return handler.handle.bind(handler);

So, what we can do is returning an object instead of a binding function. To do that we have to introduce an internal interface to identify a handler.

Given that doesn't change the need to have a handle method in the handler object, for sure.

Please update return type of MessageBusAllowMiddleware.handle method and the NPM repo URL

After upgrading to TypeScript 2.4.2, I experience an issue related to the handle method of MessageBusAllowMiddleware here https://github.com/tafax/js-bus/blob/master/src/Bus/MessageBusInterface.ts#L11
As usual, I'd like to handle the result of it as an Observable and to be able to apply RxJS operators.
Looks like the type checking became stricter and we need to explicitly mark the return type of handle as Observable now.

Also, since this repo was moved to GItHub, please update the NPM repo URL in the following places
https://github.com/tafax/js-bus/blob/master/package.json#L19
https://github.com/tafax/js-bus/blob/master/package.json#L29

Handle message handler mapping duplications

If you map the same message to two different handles, right now, just the first mapping is used to resolve the handler.

const mapping = [
   { message: 'my message', handler: 'my handler' },
   { message: 'my message', handler: 'my other handler' },
];

When you execute the message you will get the my handler execution.

// Will execute `my handler` and will ignore `my other handler`.
messageBus.execute('my message').subscribe();

Anyway, it is not deterministic since the resolver will iterate the mapping and it will use the first one. To avoid this type of situations will introduce a specific error that is thrown when you try to map the same message to another handler.

// Will throw an error.
const mapping = [
   { message: 'my message', handler: 'my handler' },
   { message: 'my message', handler: 'my other handler' },
];

Allow to execute a message handler without a subscription

Right now, if you pass a message to the message bus you have to subscribe to the returned observable.

// Not executed.
messageBus.execute('message');

// Executed.
messageBus.execute('message').subscribe();

in order to execute the handler mapped to that message.

We want to allow consumers to subscribe only if they need to handle the subscription callbacks.

// Executed.
messageBus.execute('message');

// Executed.
messageBus.execute('message').subscribe();

Your message will be executed twice if you call execute twice.

// Executed.
const execution$ = messageBus.execute('message');

// Just subscribe to execution observable.
execution$.subscribe();

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.