Add support for running code at start of publication process

- Add lbry.getFileInfoWhenListed()
 - Modify publish() to take an optional callback for when the file is
   listed.
This commit is contained in:
Alex Liebowitz 2016-09-16 11:17:12 -04:00
parent 4d34bb1599
commit f23d654a6b

View file

@ -169,14 +169,42 @@ lbry.revealFile = function(path, callback) {
lbry.call('reveal', { path: path }, callback); lbry.call('reveal', { path: path }, callback);
} }
lbry.publish = function(params, callback, errorCallback) { lbry.getFileInfoWhenListed = function(name, callback, timeoutCallback, tryNum=0) {
// Calls callback with file info when it appears in the list of files returned by lbry.getFilesInfo().
// If timeoutCallback is provided, it will be called if the file fails to appear.
lbry.getFilesInfo(function(filesInfo) {
for (var fileInfo of filesInfo) {
if (fileInfo.lbry_uri == name) {
callback(fileInfo);
return;
}
}
if (tryNum <= 200) {
setTimeout(function() { lbry.getFileInfoWhenListed(name, callback, timeoutCallback, tryNum + 1) }, 250);
} else if (timeoutCallback) {
timeoutCallback();
}
});
}
lbry.publish = function(params, fileListedCallback, publishedCallback, errorCallback) {
// Publishes a file.
// The optional fileListedCallback is called when the file becomes available in
// lbry.getFilesInfo() during the publish process.
// Use ES6 named arguments instead of directly passing param dict? // Use ES6 named arguments instead of directly passing param dict?
lbry.call('publish', params, callback, (errorInfo) => { lbry.call('publish', params, publishedCallback, (errorInfo) => {
errorCallback({ errorCallback({
name: fault.fault, name: fault.fault,
message: fault.faultString, message: fault.faultString,
}); });
}); });
if (fileListedCallback) {
lbry.getFileInfoWhenListed(params.name, function(fileInfo) {
fileListedCallback(fileInfo);
});
}
} }
lbry.getVersionInfo = function(callback) { lbry.getVersionInfo = function(callback) {