-
Notifications
You must be signed in to change notification settings - Fork 0
/
HNFirebase.js
147 lines (121 loc) · 3.57 KB
/
HNFirebase.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
var HNFirebase = function (baseURL)
{
this.ITEM_LIMIT = 10;
this.fireRef = new Firebase(baseURL);
this.newsRef = this.fireRef.child("topstories");
this.onNewItem = this.defaultOnNewItem;
this.onInitLoad = this.defaultOnInit;
};
HNFirebase.prototype.addListeners = function ()
{
// Reads entire content of Firebase database reference,
// even if only single item has changed
this.newsRef
.limitToFirst(this.ITEM_LIMIT)
.once("value", this.onValue, this.onError, this);
// Triggered once for each existing child
this.newsRef
.limitToFirst(this.ITEM_LIMIT)
.on("child_added", this.onChildAdd, this.onError, this);
this.newsRef
.limitToFirst(this.ITEM_LIMIT)
.on("child_changed", this.onChildChange, this.onError, this);
this.newsRef
.limitToFirst(this.ITEM_LIMIT)
.on("child_removed", this.onChildRemove, this.onError, this);
};
HNFirebase.prototype.removeListeners = function ()
{
this.newsRef.off();
this.fireRef.off();
};
HNFirebase.prototype.onValue = function (snapshot)
{
console.log("[FIRE] Value change", snapshot.val());
var itemIDs = snapshot.val();
if (!Array.isArray(itemIDs))
{
return;
}
// Generate promises
var promises = itemIDs.map(function (itemID)
{
return this.getItemByID(itemID);
}.bind(this));
// When all the requests are done
return Promise.all(promises)
.then(this._arrayToObject.bind(this))
.then(this.onInitLoad.bind(this));
};
HNFirebase.prototype.onChildAdd = function (snapshot, prevChildKey)
{
console.log("[FIRE] Added", snapshot.val(), prevChildKey);
this.getItemByID(snapshot.val())
.then(this.onNewItem.bind(this));
};
HNFirebase.prototype.onChildChange = function (childSnapshot, prevChildKey)
{
console.log("[FIRE] Child change", childSnapshot.val(), prevChildKey);
this.getItemByID(childSnapshot.val())
.then(this.onNewItem.bind(this));
};
HNFirebase.prototype.onChildRemove = function ()
{
console.log("[FIRE] Child remove", arguments);
};
HNFirebase.prototype.onError = function (err)
{
console.error("[FIRE] Firebase error", err);
};
HNFirebase.prototype.defaultOnNewItem = function (item)
{
console.log("[FIRE] Default action when new item received");
};
HNFirebase.prototype.defaultOnInit = function ()
{
console.log("[FIRE] Default init load action");
};
HNFirebase.prototype.getItemByID = function (itemID)
{
return new Promise(function (resolve)
{
this.fireRef
.child("item")
.child(itemID)
.once("value", function (S)
{
return resolve(S.val());
});
}.bind(this));
};
HNFirebase.prototype.getComments = function (itemID)
{
var load = function (item)
{
var commentIDs = item.kids || [];
// Lets load only the first level comments
var commentPromises = commentIDs.map(function (currentItemID)
{
return this.getItemByID(currentItemID);
}, this);
// Return until all the promies are fulfilled
return Promise.all(commentPromises);
}.bind(this);
return this.getItemByID(itemID)
.then(load)
.then(this._arrayToObject.bind(this));
};
/**
* Transform the array into object.
*
* @param {Array} array Make sure the array items has the 'id' property.
* @return {Object}
*/
HNFirebase.prototype._arrayToObject = function (array)
{
return array.reduce(function (prev, item)
{
prev[item.id] = item;
return prev;
}, {});
};