8. Create the skeleton of the public interface
filipedeschamps opened this issue · 0 comments
I don't know why, but I find this the best part: write the public methods without any implementation details. Probably because this interface is the one all your users will use, nothing more, and most of them will not care about the implementation details.
Create the source file
Following what we've decided in the Create the path structure issue, create the source file inside the src
directory:
$ touch src/rss-feed-emitter.js
Event emitter interface
Awesome! Now, do you remember what was decided about our public interface in the Scratch the public interface of your module issue? There's one big detail in it which is our module will also expose an Event Emitter interface:
feeder.on( 'new-item', callback);
So it must not only receive events, but also emit
them, right?
To do so, we will make our class extend from an Event Emitter. We will use a tiny but battle tested module called tiny-emitter to extends from.
First, add it to your project dependencies:
$ npm install --save tiny-emitter
RssFeedEmitter class
Now it's time to edit our source file src/rss-feed-emitter.js
. Start by declaring strict mode to it, importing tiny-emitter dependency and extending from it:
'use strict';
import TinyEmitter from 'tiny-emitter';
class RssFeedEmitter extends TinyEmitter {
}
Constructor method
Great! Now since we're extending from another class, let's declare the constructor method and call the super()
method to give tiny-emitter a chance to make it's mambo-jambo.
'use strict';
import TinyEmitter from 'tiny-emitter';
class RssFeedEmitter extends TinyEmitter {
constructor() {
super();
}
}
Export
Let's not forget to make this class be exported by default when someone requires it, take a look on the last line:
'use strict';
import TinyEmitter from 'tiny-emitter';
class RssFeedEmitter extends TinyEmitter {
constructor() {
super();
}
}
export default RssFeedEmitter;
Public methods
The final step is to declare the public methods:
'use strict';
import TinyEmitter from 'tiny-emitter';
class RssFeedEmitter extends TinyEmitter {
constructor() {
super();
}
add() {
}
remove() {
}
list() {
}
destroy() {
}
}
export default RssFeedEmitter;
Conclusion
Now we have enough material to create our first automated test. But first, we need to transpile it to ES5 JavaScript, since this code will not work even in Node.js 5.x for the lack of import
feature.