});
return items;
+ },
+
+ find: function(item_id) {
+ var item;
+
+ console.log("MediaItem.find: entered.");
+ app.Store.getMediaItem(item_id).done(function(data) {
+ item = app.MediaItem.create(data);
+ });
+
+ return item;
}
});
}
},
+ // TODO: make the next two functions into one, to be DRY.
getMediaItems: function(deferred) {
var self = this,
d = deferred || new $.Deferred();
}
return d.promise();
+ },
+
+ getMediaItem: function(item_id, deferred) {
+ var self = this,
+ d = deferred || new $.DeferreD();
+
+ console.log("Store.getMediaItem: entered.");
+ if (self.scanCompleted) {
+ console.log("Store.getMediaITem: scan is completed, resolving promise.");
+ d.resolve(app.library.item(item_id);
+ } else {
+ // If the scan is not completed, we must be still scanning.
+ console.log("Store.getMediaItem: scan still pending. Trying again later.");
+ setTimeout(function() {
+ return self.getMediaItem(item_id, deferred);
+ }, 100);
+ }
}
};
// For readability:
this.initialized = false;
+ // Whether items should be resolved on MusicBrainz
+ this.resolve = false;
+
this.fetchCount = 100;
this.fetchOffset = 0;
this.oneTime = false;
"UNION", [this.audioTypeFilter, this.videoTypeFilter]);
};
- this.scan = function(count) {
+ this.scan = function(options) {
+ var opts = options || {};
+ if (opts.resolve !== undefined) {
+ this.resolve = opts.resolve;
+ }
+
this.deferred = new $.Deferred();
this.initialize()
this.mediaItems = [];
this.fetchOffset = 0;
- if (count !== undefined) {
- this.fetchCount = count;
+ if (opts.count !== undefined) {
+ this.fetchCount = opts.count;
this.oneTime = true;
}
items.forEach(function(item, index, items) {
self.mediaItems.push(item);
- win.MusicBrainz.getArtist(item.artists[0]).done(function(data) {
- console.log(
- "HoofbeatsLibrary.findCB: " +
- "item resolved on MusicBrainz: " +
- data.name);
- });
+ if (self.resolve) {
+ win.MusicBrainz.getArtist(item.artists[0]).done(function(data) {
+ console.log(
+ "HoofbeatsLibrary.findCB: " +
+ "item resolved on MusicBrainz: " +
+ data.name);
+ });
+ }
});
if (items.length == this.fetchCount && !this.oneTime) {
self.deferred.resolve();
}
};
+
+ this.item = function(id) {
+ var ret;
+
+ this.mediaItems.forEach(function(item, index, items) {
+ if (item.id == id)
+ ret = item;
+ });
+
+ return ret;
+ };
};
library.prototype = {
});
});
+ test("successful scan without resolving", function() {
+ var lib = new HoofbeatsLibrary();
+ stop();
+ lib.scan({resolve: false}).then(function() {
+ ok(lib.resolve == false, "lib.resolve is false");
+ start();
+ });
+ });
+
test("scan with count", function() {
var lib = new HoofbeatsLibrary();
stop();
- lib.scan(1).then(function() {
+ lib.scan({count: 1}).then(function() {
ok(lib.size == 1, "there is one item in the library");
start();
});
});
+
+ test("get one item", function() {
+ var lib = new HoofbeatsLibrary(),
+ item_id = 'e7e7023b-54b3-41d5-b4a1-aa24498e0572';
+
+ stop();
+ lib.scan({count: 1}).then(function() {
+ var item = lib.item(item_id);
+ ok(item !== undefined, "item was found");
+ ok(item.id == item_id, "correct item was found");
+ start();
+ });
+ });
});