Skip to content

Commit

Permalink
Added check for valid callback function in Peaks.init()
Browse files Browse the repository at this point in the history
See #546
  • Loading branch information
chrisn committed Sep 5, 2024
1 parent 8144d61 commit 3b41dde
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
17 changes: 16 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ function checkContainerElements(options) {
* Creates and initialises a new Peaks instance with the given options.
*
* @param {Object} opts Configuration options
* @param {Function} callback
*
* @return {Peaks}
*/
Expand All @@ -346,6 +347,10 @@ Peaks.init = function(opts, callback) {

let err = instance._setOptions(opts);

if (!callback) {
instance._logger('Peaks.init(): Missing callback function');
}

if (!err) {
err = checkContainerElements(instance.options);
}
Expand Down Expand Up @@ -448,7 +453,9 @@ Peaks.init = function(opts, callback) {
instance.emit('peaks.ready');
}, 0);

callback(null, instance);
if (callback) {
callback(null, instance);
}
});
})
.catch(function(err) {
Expand Down Expand Up @@ -608,6 +615,14 @@ Peaks.prototype.getWaveformData = function() {
return this._waveformData;
};

Peaks.prototype.on = function(type, listener, options) {
if (type === 'peaks.ready') {
this._logger('Peaks.on(): The peaks.ready event is deprecated');
}

EventEmitter.prototype.on.call(this, type, listener, options);
};

/**
* Cleans up a Peaks instance after use.
*/
Expand Down
34 changes: 33 additions & 1 deletion test/api-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ describe('Peaks', function() {
});

it('should emit a peaks.ready event when initialised', function(done) {
const logger = sinon.spy();

Peaks.init({
overview: {
container: document.getElementById('overview-container')
Expand All @@ -82,19 +84,49 @@ describe('Peaks', function() {
container: document.getElementById('zoomview-container')
},
mediaElement: document.getElementById('media'),
dataUri: { arraybuffer: '/base/test/data/sample.dat' }
dataUri: { arraybuffer: '/base/test/data/sample.dat' },
logger: logger
},
function(err, instance) {
expect(err).to.equal(null);
expect(instance).to.be.an.instanceOf(Peaks);

instance.on('peaks.ready', function() {
expect(logger.callCount).to.equal(1);
expect(logger).calledWithMatch(/deprecated/);
expect(instance.getWaveformData()).to.be.an.instanceOf(WaveformData);
done();
});
});
});

it('should emit a peaks.ready event when initialised without a callback', function(done) {
const logger = sinon.spy();

const instance = Peaks.init({
overview: {
container: document.getElementById('overview-container')
},
zoomview: {
container: document.getElementById('zoomview-container')
},
mediaElement: document.getElementById('media'),
dataUri: { arraybuffer: '/base/test/data/sample.dat' },
logger: logger
});

expect(instance).to.be.an.instanceOf(Peaks);

expect(logger.callCount).to.equal(1);

instance.on('peaks.ready', function() {
expect(logger.callCount).to.equal(2);
expect(logger.getCall(0).args[0]).to.match(/callback/);
expect(logger.getCall(1).args[0]).to.match(/deprecated/);
done();
});
});

context('with zoomview and overview options', function() {
it('should construct a Peaks object with overview and zoomable waveforms', function(done) {
Peaks.init({
Expand Down

0 comments on commit 3b41dde

Please sign in to comment.