songlocator-api
Version:
SongLocator resolvers exposed as WebSocket API
111 lines (97 loc) • 3.11 kB
JavaScript
//@ sourceMappingURL=songlocator-api-client.map
// Generated by CoffeeScript 1.6.1
/*
songlocator-base
2013 (c) Andrey Popp <8mayday@gmail.com>
*/
var __hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
(function(root, factory) {
var SongLocator;
if (typeof exports === 'object') {
SongLocator = require('songlocator-base');
return module.exports = factory(SongLocator);
} else if (typeof define === 'function' && define.amd) {
return define(function(require) {
SongLocator = require('songlocator-base');
return root.SongLocator.API = factory(SongLocator);
});
} else {
return root.SongLocator.API = factory(window.SongLocator);
}
})(this, function(_arg) {
var BaseResolver, Client, extend;
BaseResolver = _arg.BaseResolver, extend = _arg.extend;
Client = (function(_super) {
__extends(Client, _super);
function Client(options) {
this.options = options;
this.queue = [];
this.initSocket();
}
Client.prototype.initSocket = function() {
var _this = this;
this.isOpenned = false;
this.sock = new WebSocket(this.options.uri);
this.sock.onopen = function() {
var data, method, _i, _len, _ref, _ref1;
_this.isOpenned = true;
if (_this.queue.length > 0) {
_ref = _this.queue;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
_ref1 = _ref[_i], method = _ref1.method, data = _ref1.data;
_this.call(method, data);
}
return _this.queue = [];
}
};
this.sock.onerror = function() {
return _this.initSocket();
};
return this.sock.onmessage = this.onMessage.bind(this);
};
Client.prototype.onMessage = function(e) {
var data;
data = (function() {
try {
return JSON.parse(e.data);
} catch (e) {
return void 0;
}
})();
if (!(((data != null ? data.qid : void 0) != null) && ((data != null ? data.results : void 0) != null))) {
return;
}
return this.results(data.qid, data.results);
};
Client.prototype.call = function(method, data) {
if (this.isOpenned) {
data.method = method;
return this.sock.send(JSON.stringify(data));
} else {
return this.queue.push({
method: method,
data: data
});
}
};
Client.prototype.search = function(qid, query) {
return this.call('search', {
qid: qid,
query: query
});
};
Client.prototype.resolve = function(qid, title, artist, album) {
return this.call('resolve', {
qid: qid,
title: title,
artist: artist,
album: album
});
};
return Client;
})(BaseResolver);
return {
Client: Client
};
});