-
Notifications
You must be signed in to change notification settings - Fork 1
/
mike001.js
68 lines (50 loc) · 1.51 KB
/
mike001.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
var firebase = require('firebase')
var config = {
apiKey: "AIzaSyCiJU36qJxNoVYmmfw7gUCeWRC0py-RSPw",
authDomain: "open-44d30.firebaseapp.com",
databaseURL: "https://open-44d30.firebaseio.com",
projectId: "open-44d30",
storageBucket: "open-44d30.appspot.com",
messagingSenderId: "799649165678"
}
var db = firebase.initializeApp(config).database().ref().child('/statebus_objs')
function encode_firebase_key(k) {
return encodeURIComponent(k).replace(/\./g, '%2E')
}
function decode_firebase_key(k) {
return decodeURIComponent(k.replace('%2E', '.'))
}
var bus = require('statebus').serve({
file_store : false
})
bus('/*').to_fetch = function (key, t) {
db.child(encode_firebase_key(key)).once('value', function (x) {
t.done(x.val() || {})
}, function (x) { console.log('firebase error:', x) })
}
bus('/*').on_save = function (o) {
db.child(encode_firebase_key(o.key)).set(o)
}
db.on('child_added', function (x) {
var obj = x.val() || {}
obj.key = decode_firebase_key(x.key)
bus.save(obj)
})
db.on('child_changed', function (x) {
var obj = x.val() || {}
obj.key = decode_firebase_key(x.key)
bus.save(obj)
})
db.on('child_removed', function (x) {
bus.del(decode_firebase_key(x.key))
})
bus(function () {
console.log('testing A')
var x = bus.fetch('/helllo')
console.log('x = ', x)
console.log('testing B')
})
bus.save({ key : '/blar', bloop : 'hoop' })
setTimeout(function () {
bus.save({ key : '/helllo', hihi : 80 })
}, 5000)