test_pubsub v0.5.0
Hiring test based on pubsub pattern
PubSub
The PubSub class, lib/pubsub.js
, should be a basic implementation of the Publish<>Subscribe pattern. You can find out more about it here : Wikipedia :)
The idea is that something (a subscriber) can "subscribe" to events "published" by something else ( a publisher ). This events are represented by a name, with a hierarchical pattern in this case.
Goals
You have to code a PubSub class lib/pubsub.js
.
To help you in this task, you have a complete spec/test file, spec/lib/pubsub.spec.js
, that uses jest. This test file shows you how the PubSub class is supposed to work.
The best way to do the test is to launch the tests, and validate each test one by one, in a TDD approach.
Notes:
- Do not necessary try to do everything, but ...
- Complete the existing class and code as much methods as you can
- Validate as much specs/tests as you can
- Show us the Ninja in you instead of trying to finish the test
- Explain in a few words your idea/algorithm or what you think you have to code if you don't know how to do it
How to use this project
First, you need to install node.js for your environment, you should have done that before the test
Dependencies
To install the dependencies of the project, just run npm install command:
$ npm i
Execute tests
To execute the tests, just run the test
script of the project:
$ npm test
As you can see there is some failures, resolve as much as you can.
You can also use the dev
script when you develop, it will automaticaly relaunch the test
script when you make some changes to a file (watch mode):
$ npm run dev
Misc
Lodash
You can use lodash, or equivalent, if you want methods like each, merge, etc...
Example require of lodash in your project
const _ = require('lodash');
Code coverage
If you want to look at the code coverage of your PubSub class, you can use the coverage
script of the project:
$ npm run coverage
Typical use of the PubSub Class
See example.js
for a basic typical use case.
Specs explanation
The PubSub class must accept an options
object on its constructor.
There is two default properties that can be override:
var ps = new PubSub({
// Separator between each level of a topic name
// Example with ">" separator : user>update>name
topicChildSeparator: string,
// The default priority order of a subscriber, if no priority provided
defaultPriority: number
});
The PubSub class must provide two main methods, publish
and subscribe
.
// Callback method will be called when something publish to the given topic (or a child of this topic) in the given priority order
function subscribe(topic, callback, priority){}
// Send the given value to any subscriber who have subscribe to this topic (or parent of this topic)
function publish(topic, value){}
With default options, topic publishing order is like this :
var topic = 'parent>child>sub-child';
ps.publish(topic,'foo');
// Subscribers callbacks will be called with 'foo' as first argument in this order :
// 1 : "parent>child>sub-child" subscribers, according to their priority
// 2 : "parent>child" subscribers, according to their priority
// 3 : "parent" subscribers, according to their priority
When you subscribe to a topic, you should receive a subscriber object representing the subscription. The form of the subscription object is like this:
type SubscriptionType = {
id: any, // A unique identifier of the subscription
topic: string,
priority: number,
callback: function
};
This subscription object can be used later to unsubscribe
from the topic it was created for :
ps.unsubcribe(subscription)
2 years ago