Added wallet_* calls to lbry.js with some refactoring

This commit is contained in:
Akinwale Ariwodola 2018-05-02 01:09:00 +01:00
parent 3d06d1474e
commit b5e777ef91
2 changed files with 78 additions and 63 deletions

84
dist/bundle.js vendored
View file

@ -1808,36 +1808,59 @@ function apiCall(method /*: string*/, params /*: ?{}*/, resolve /*: Function*/,
}).catch(reject);
}
var daemonCallWithResult = function daemonCallWithResult(name) {
var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return new Promise(function (resolve, reject) {
apiCall(name, params, function (result) {
resolve(result);
}, reject);
});
};
// core
Lbry.status = function () {
var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return new Promise(function (resolve, reject) {
apiCall('status', params, function (status) {
resolve(status);
}, reject);
});
return daemonCallWithResult('status', params);
};
Lbry.version = function () {
return new Promise(function (resolve, reject) {
apiCall('version', {}, function (versionInfo) {
resolve(versionInfo);
}, reject);
});
return daemonCallWithResult('version', {});
};
Lbry.file_delete = function () {
var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return new Promise(function (resolve, reject) {
apiCall('file_delete', params, resolve, reject);
});
return daemonCallWithResult('file_delete', params);
};
Lbry.file_set_status = function () {
var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return new Promise(function (resolve, reject) {
apiCall('file_set_status', params, resolve, reject);
});
return daemonCallWithResult('file_set_status', params);
};
// wallet
Lbry.wallet_balance = function () {
var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return daemonCallWithResult('wallet_balance', params);
};
Lbry.wallet_is_address_mine = function () {
var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return daemonCallWithResult('wallet_is_address_mine', params);
};
Lbry.wallet_new_address = function () {
var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return daemonCallWithResult('wallet_new_address', params);
};
Lbry.wallet_send = function () {
var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return daemonCallWithResult('wallet_send', params);
};
Lbry.wallet_encrypt = function () {
var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return daemonCallWithResult('wallet_encrypt', params);
};
Lbry.wallet_decrypt = function () {
return daemonCallWithResult('wallet_decrypt', {});
};
Lbry.wallet_unlock = function () {
var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
return daemonCallWithResult('wallet_unlock', params);
};
Lbry.connectPromise = null;
@ -1869,20 +1892,15 @@ Lbry.getMediaType = function (contentType, fileName) {
return (/^[^/]+/.exec(contentType)[0]
);
} else if (fileName) {
var dotIndex = fileName.lastIndexOf('.');
if (dotIndex === -1) {
return 'unknown';
}
var ext = fileName.substr(dotIndex + 1);
if (/^mp4|m4v|webm|flv|f4v|ogv$/i.test(ext)) {
return 'video';
} else if (/^mp3|m4a|aac|wav|flac|ogg|opus$/i.test(ext)) {
return 'audio';
} else if (/^html|htm|xml|pdf|odf|doc|docx|md|markdown|txt|epub|org$/i.test(ext)) {
return 'document';
}
return 'unknown';
var formats = [[/^.+\.(mp4|m4v|webm|flv|f4v|ogv)$/i, 'video'], [/^.+\.(mp3|m4a|aac|wav|flac|ogg|opus)$/i, 'audio'], [/^.+\.(html|htm|xml|pdf|odf|doc|docx|md|markdown|txt|epub|org)$/i, 'document']];
var res = formats.reduce(function extensionMatch(ret, testpair) {
switch (testpair[0].test(ret)) {
case true:
return testpair[1];
default:
return ret;}
}, fileName);
return res === fileName ? 'unknown' : res;
}
return 'unknown';
};

View file

@ -49,40 +49,33 @@ function apiCall(method: string, params: ?{}, resolve: Function, reject: Functio
.catch(reject);
}
// core
Lbry.status = (params = {}) =>
const daemonCallWithResult = (name, params = {}) =>
new Promise((resolve, reject) => {
apiCall(
'status',
name,
params,
status => {
resolve(status);
result => {
resolve(result);
},
reject
);
});
Lbry.version = () =>
new Promise((resolve, reject) => {
apiCall(
'version',
{},
versionInfo => {
resolve(versionInfo);
},
reject
);
});
// core
Lbry.status = (params = {}) => daemonCallWithResult('status', params);
Lbry.version = () => daemonCallWithResult('version', {});
Lbry.file_delete = (params = {}) => daemonCallWithResult('file_delete', params);
Lbry.file_set_status = (params = {}) => daemonCallWithResult('file_set_status', params);
Lbry.file_delete = (params = {}) =>
new Promise((resolve, reject) => {
apiCall('file_delete', params, resolve, reject);
});
Lbry.file_set_status = (params = {}) =>
new Promise((resolve, reject) => {
apiCall('file_set_status', params, resolve, reject);
});
// wallet
Lbry.wallet_balance = (params = {}) => daemonCallWithResult('wallet_balance', params);
Lbry.wallet_is_address_mine = (params = {}) =>
daemonCallWithResult('wallet_is_address_mine', params);
Lbry.wallet_new_address = (params = {}) => daemonCallWithResult('wallet_new_address', params);
Lbry.wallet_send = (params = {}) => daemonCallWithResult('wallet_send', params);
Lbry.wallet_encrypt = (params = {}) => daemonCallWithResult('wallet_encrypt', params);
Lbry.wallet_decrypt = () => daemonCallWithResult('wallet_decrypt', {});
Lbry.wallet_unlock = (params = {}) => daemonCallWithResult('wallet_unlock', params);
Lbry.connectPromise = null;
Lbry.connect = () => {
@ -117,12 +110,16 @@ Lbry.getMediaType = (contentType, fileName) => {
const formats = [
[/^.+\.(mp4|m4v|webm|flv|f4v|ogv)$/i, 'video'],
[/^.+\.(mp3|m4a|aac|wav|flac|ogg|opus)$/i, 'audio'],
[/^.+\.(html|htm|xml|pdf|odf|doc|docx|md|markdown|txt|epub|org)$/i, 'document']];
const res = formats.reduce(function extensionMatch(ret, testpair) {
[/^.+\.(html|htm|xml|pdf|odf|doc|docx|md|markdown|txt|epub|org)$/i, 'document'],
];
const res = formats.reduce((ret, testpair) => {
switch (testpair[0].test(ret)) {
case true: return testpair[1];
default: return ret; }
}, fileName);
case true:
return testpair[1];
default:
return ret;
}
}, fileName);
return res === fileName ? 'unknown' : res;
}
return 'unknown';