This repository has been archived by the owner on Jun 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tracks.js
91 lines (73 loc) · 1.84 KB
/
tracks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
function tracks() {
var TrackPrototype = require('./track');
var config = require('config');
var storage = require('node-persist');
storage.initSync();
var tracksData = storage.getItem('tracks');
if (tracksData === undefined)
{
console.log("Pulling default tracks from config");
if (config.has('tracks') )
{
tracksData = config.get('tracks');
analytics.sendEvent("app", "load_defaults");
} else
{
console.log("Defaults not found, no tracks");
tracksData = [];
analytics.sendEvent("app", "load_empty");
}
}
var allTracks = [];
var tracksByHash = {};
var tracksByName = {};
for (var index = 0; index < tracksData.length; index++) {
var trackData = tracksData[index];
var track = new TrackPrototype(trackData);
allTracks.push(track);
tracksByHash[track.hash()] = track;
tracksByName[track.name] = track;
}
return {
all : allTracks,
numberOfTracks: function() {
return this.all.length;
},
trackAtIndex: function(index) {
var result = undefined;
if (index < this.numberOfTracks() )
{
result = this.all[index];
result.index = index;
} else
{
console.log("Maximum index for tracks is " + this.numberOfTracks() + ", attempt to access track at index " + index);
}
return result;
},
trackWithHash: function(hash) {
return tracksByHash[hash];
},
trackWithName: function(name) {
return tracksByName[name];
},
performOnEachTrack: function(action) {
//if (action typeof function)
{
for (var index = 0; index < this.numberOfTracks(); index ++) {
var track = this.trackAtIndex(index);
if (track != undefined)
{
action(track);
}
}
}
},
save: function() {
console.log("Saving to storage");
storage.setItem('tracks', allTracks);
analytics.sendEvent("app", "save");
}
};
}
module.exports = exports = new tracks();