Skip to content

Commit 3b41dde

Browse files
committed
Added check for valid callback function in Peaks.init()
See #546
1 parent 8144d61 commit 3b41dde

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

src/main.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ function checkContainerElements(options) {
337337
* Creates and initialises a new Peaks instance with the given options.
338338
*
339339
* @param {Object} opts Configuration options
340+
* @param {Function} callback
340341
*
341342
* @return {Peaks}
342343
*/
@@ -346,6 +347,10 @@ Peaks.init = function(opts, callback) {
346347

347348
let err = instance._setOptions(opts);
348349

350+
if (!callback) {
351+
instance._logger('Peaks.init(): Missing callback function');
352+
}
353+
349354
if (!err) {
350355
err = checkContainerElements(instance.options);
351356
}
@@ -448,7 +453,9 @@ Peaks.init = function(opts, callback) {
448453
instance.emit('peaks.ready');
449454
}, 0);
450455

451-
callback(null, instance);
456+
if (callback) {
457+
callback(null, instance);
458+
}
452459
});
453460
})
454461
.catch(function(err) {
@@ -608,6 +615,14 @@ Peaks.prototype.getWaveformData = function() {
608615
return this._waveformData;
609616
};
610617

618+
Peaks.prototype.on = function(type, listener, options) {
619+
if (type === 'peaks.ready') {
620+
this._logger('Peaks.on(): The peaks.ready event is deprecated');
621+
}
622+
623+
EventEmitter.prototype.on.call(this, type, listener, options);
624+
};
625+
611626
/**
612627
* Cleans up a Peaks instance after use.
613628
*/

test/api-spec.js

+33-1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ describe('Peaks', function() {
7474
});
7575

7676
it('should emit a peaks.ready event when initialised', function(done) {
77+
const logger = sinon.spy();
78+
7779
Peaks.init({
7880
overview: {
7981
container: document.getElementById('overview-container')
@@ -82,19 +84,49 @@ describe('Peaks', function() {
8284
container: document.getElementById('zoomview-container')
8385
},
8486
mediaElement: document.getElementById('media'),
85-
dataUri: { arraybuffer: '/base/test/data/sample.dat' }
87+
dataUri: { arraybuffer: '/base/test/data/sample.dat' },
88+
logger: logger
8689
},
8790
function(err, instance) {
8891
expect(err).to.equal(null);
8992
expect(instance).to.be.an.instanceOf(Peaks);
9093

9194
instance.on('peaks.ready', function() {
95+
expect(logger.callCount).to.equal(1);
96+
expect(logger).calledWithMatch(/deprecated/);
9297
expect(instance.getWaveformData()).to.be.an.instanceOf(WaveformData);
9398
done();
9499
});
95100
});
96101
});
97102

103+
it('should emit a peaks.ready event when initialised without a callback', function(done) {
104+
const logger = sinon.spy();
105+
106+
const instance = Peaks.init({
107+
overview: {
108+
container: document.getElementById('overview-container')
109+
},
110+
zoomview: {
111+
container: document.getElementById('zoomview-container')
112+
},
113+
mediaElement: document.getElementById('media'),
114+
dataUri: { arraybuffer: '/base/test/data/sample.dat' },
115+
logger: logger
116+
});
117+
118+
expect(instance).to.be.an.instanceOf(Peaks);
119+
120+
expect(logger.callCount).to.equal(1);
121+
122+
instance.on('peaks.ready', function() {
123+
expect(logger.callCount).to.equal(2);
124+
expect(logger.getCall(0).args[0]).to.match(/callback/);
125+
expect(logger.getCall(1).args[0]).to.match(/deprecated/);
126+
done();
127+
});
128+
});
129+
98130
context('with zoomview and overview options', function() {
99131
it('should construct a Peaks object with overview and zoomable waveforms', function(done) {
100132
Peaks.init({

0 commit comments

Comments
 (0)