This repository has been archived by the owner on Jun 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.js
121 lines (112 loc) · 3.37 KB
/
list.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
const {Component} = require('react')
const ORDER_BY_TYPES = [
'child',
'key',
'priority',
'value'
]
module.exports = (Firebase) => class extends Component {
constructor (props) {
super(props)
this.state = {
list: {}
}
this.setupListener = () => {
const {path, startAt, startAtKey, endAt, endAtKey, equalTo, equalToKey, limitToFirst, limitToLast} = this.props
let {orderBy, orderByChild} = this.props
this.reference = Firebase.database().ref(path)
if (!orderBy && orderByChild) {
orderBy = 'child'
}
if (orderBy) {
switch (orderBy) {
case 'child':
if (!orderByChild) {
throw new Error('orderByChild must be used when orderBy="child"')
}
this.reference = this.reference.orderByChild(orderByChild)
break
case 'key':
this.reference = this.reference.orderByKey()
break
case 'priority':
this.reference = this.reference.orderByPriority()
break
case 'value':
this.reference = this.reference.orderByValue()
break
default:
throw new Error(`orderBy must be one of [${ORDER_BY_TYPES.join(', ')}] but got ${orderBy}`)
}
}
if (startAt) {
this.reference = this.reference.startAt(startAt, startAtKey)
}
if (endAt) {
this.reference = this.reference.endAt(endAt, endAtKey)
}
if (equalTo) {
this.reference = this.reference.equalTo(equalTo, equalToKey)
}
if (limitToFirst) {
this.reference = this.reference.limitToFirst(Number.parseInt(limitToFirst, 10))
}
if (limitToLast) {
this.reference = this.reference.limitToLast(Number.parseInt(limitToLast, 10))
}
this.onChildAddedListener = this.reference.on('child_added', (snapshot) => {
this.setState({
list: Object.assign({}, this.state.list, {
[snapshot.key]: snapshot.val()
})
})
})
this.onChildRemovedListener = this.reference.on('child_removed', (snapshot) => {
const nextList = {}
for (const key in this.state.list) {
if (key !== snapshot.key) {
nextList[key] = this.state.list[key]
}
}
this.setState({
list: nextList
})
})
this.onChildChangedListener = this.reference.on('child_changed', (snapshot) => {
this.setState({
list: Object.assign({}, this.state.list, {
[snapshot.key]: snapshot.val()
})
})
})
}
this.cleanUpListener = () => {
if (!this.reference) {
return
}
this.reference.off('child_added', this.onChildAddedListener)
this.reference.off('child_removed', this.onChildRemovedListener)
this.reference.off('child_changed', this.onChildChangedListener)
this.reference = undefined
}
}
componentDidMount () {
this.setupListener()
}
componentDidUpdate ({path: prevPath, limit: prevLimit = 16}) {
const {path, limit = 16} = this.props
if (path !== prevPath || limit !== prevLimit) {
this.cleanUpListener()
this.setState({
list: {}
})
this.setupListener()
}
}
componentWillUnmount () {
this.cleanUpListener()
}
render () {
return this.props.children(this.state.list)
}
}