Skip to content

Commit 0149228

Browse files
committed
preparation for realtime firebase
1 parent da95856 commit 0149228

6 files changed

+80
-30
lines changed

my-mind.js

+40-15
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,10 @@ MM.Item.prototype.setText = function(text) {
445445
return this.update();
446446
}
447447

448+
MM.Item.prototype.getId = function() {
449+
return this._id;
450+
}
451+
448452
MM.Item.prototype.getText = function() {
449453
return this._dom.text.innerHTML.replace(/<br\s*\/?>/g, "\n");
450454
}
@@ -805,7 +809,6 @@ MM.Map = function(options) {
805809
this._root = null;
806810
this._visible = false;
807811
this._position = [0, 0];
808-
this._id = MM.generateId();
809812

810813
this._setRoot(new MM.Item().setText(o.root).setLayout(o.layout));
811814
}
@@ -815,15 +818,13 @@ MM.Map.fromJSON = function(data) {
815818
}
816819

817820
MM.Map.prototype.fromJSON = function(data) {
818-
if (data.id) { this._id = data.id; }
819821
this._setRoot(MM.Item.fromJSON(data.root));
820822
return this;
821823
}
822824

823825
MM.Map.prototype.toJSON = function() {
824826
var data = {
825-
root: this._root.toJSON(),
826-
id: this._id
827+
root: this._root.toJSON()
827828
};
828829
return data;
829830
}
@@ -954,7 +955,7 @@ MM.Map.prototype.getName = function() {
954955
}
955956

956957
MM.Map.prototype.getId = function() {
957-
return this._id;
958+
return this._root.getId();
958959
}
959960

960961
MM.Map.prototype.pick = function(item, direction) {
@@ -3006,7 +3007,8 @@ MM.Backend.File.load = function() {
30063007
MM.Backend.Firebase = Object.create(MM.Backend, {
30073008
label: {value: "Firebase"},
30083009
id: {value: "firebase"},
3009-
ref: {value:null, writable:true}
3010+
ref: {value:null, writable:true},
3011+
listenRef: {value:null, writable:true}
30103012
});
30113013

30123014
MM.Backend.Firebase.connect = function(server, auth) {
@@ -3019,9 +3021,7 @@ MM.Backend.Firebase.connect = function(server, auth) {
30193021
if (auth) {
30203022
return this._login(auth);
30213023
} else {
3022-
var promise = new Promise();
3023-
promise.fulfill();
3024-
return promise;
3024+
return new Promise().fulfill();
30253025
}
30263026
}
30273027

@@ -3030,13 +3030,15 @@ MM.Backend.Firebase.save = function(data, id, name) {
30303030

30313031
try {
30323032
this.ref.child("names/" + id).set(name);
3033-
this.ref.child("data/" + id).set(data, function(result) {
3033+
var ref = this.ref.child("data/" + id);
3034+
ref.set(data, function(result) {
30343035
if (result) {
30353036
promise.reject(result);
30363037
} else {
30373038
promise.fulfill();
3039+
this._listenStart(ref);
30383040
}
3039-
});
3041+
}.bind(this));
30403042
} catch (e) {
30413043
promise.reject(e);
30423044
}
@@ -3046,14 +3048,16 @@ MM.Backend.Firebase.save = function(data, id, name) {
30463048
MM.Backend.Firebase.load = function(id) {
30473049
var promise = new Promise();
30483050

3049-
this.ref.child("data/" + id).once("value", function(snap) {
3051+
var ref = this.ref.child("data/" + id);
3052+
ref.once("value", function(snap) {
30503053
var data = snap.val();
30513054
if (data) {
30523055
promise.fulfill(data);
3056+
this._listenStart(ref);
30533057
} else {
30543058
promise.reject(new Error("There is no such saved map"));
30553059
}
3056-
});
3060+
}.bind(this));
30573061
return promise;
30583062
}
30593063

@@ -3076,6 +3080,25 @@ MM.Backend.Firebase.remove = function(id) {
30763080
return promise;
30773081
}
30783082

3083+
MM.Backend.Firebase.reset = function() {
3084+
this._listenStop(); /* do not monitor current firebase ref for changes */
3085+
}
3086+
3087+
MM.Backend.Firebase._listenStart = function(ref) {
3088+
if (this.listenRef && this.listenRef.toString() == ref.toString()) { return; }
3089+
3090+
this._listenStop();
3091+
this.listenRef = ref;
3092+
ref.on("value", function() { /* console.log("!"); */ });
3093+
}
3094+
3095+
MM.Backend.Firebase._listenStop = function() {
3096+
if (this.listenRef) {
3097+
this.listenRef.off("value");
3098+
this.listenRef = null;
3099+
}
3100+
}
3101+
30793102
MM.Backend.Firebase._login = function(type) {
30803103
var promise = new Promise();
30813104

@@ -3693,6 +3716,9 @@ MM.UI.IO.prototype._syncBackend = function() {
36933716
this._backends[this._backend.value].show(this._mode);
36943717
}
36953718

3719+
/**
3720+
* @param {MM.UI.Backend} backend
3721+
*/
36963722
MM.UI.IO.prototype._setCurrentBackend = function(backend) {
36973723
if (this._currentBackend && this._currentBackend != backend) { this._currentBackend.reset(); }
36983724

@@ -4075,7 +4101,7 @@ MM.UI.Backend.Firebase.setState = function(data) {
40754101
this._connect(data.s, data.a).then(
40764102
this._load.bind(this, data.id),
40774103
this._error.bind(this)
4078-
)
4104+
);
40794105
}
40804106

40814107
MM.UI.Backend.Firebase.getState = function() {
@@ -4198,7 +4224,6 @@ MM.UI.Backend.Firebase._sync = function() {
41984224
if (this._mode == "load" && !this._list.value) { this._go.disabled = true; }
41994225
this._go.innerHTML = this._mode.charAt(0).toUpperCase() + this._mode.substring(1);
42004226
}
4201-
42024227
MM.UI.Backend.GDrive = Object.create(MM.UI.Backend, {
42034228
id: {value: "gdrive"}
42044229
});

src/backend.firebase.js

+30-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
MM.Backend.Firebase = Object.create(MM.Backend, {
22
label: {value: "Firebase"},
33
id: {value: "firebase"},
4-
ref: {value:null, writable:true}
4+
ref: {value:null, writable:true},
5+
listenRef: {value:null, writable:true}
56
});
67

78
MM.Backend.Firebase.connect = function(server, auth) {
@@ -14,9 +15,7 @@ MM.Backend.Firebase.connect = function(server, auth) {
1415
if (auth) {
1516
return this._login(auth);
1617
} else {
17-
var promise = new Promise();
18-
promise.fulfill();
19-
return promise;
18+
return new Promise().fulfill();
2019
}
2120
}
2221

@@ -25,13 +24,15 @@ MM.Backend.Firebase.save = function(data, id, name) {
2524

2625
try {
2726
this.ref.child("names/" + id).set(name);
28-
this.ref.child("data/" + id).set(data, function(result) {
27+
var ref = this.ref.child("data/" + id);
28+
ref.set(data, function(result) {
2929
if (result) {
3030
promise.reject(result);
3131
} else {
3232
promise.fulfill();
33+
this._listenStart(ref);
3334
}
34-
});
35+
}.bind(this));
3536
} catch (e) {
3637
promise.reject(e);
3738
}
@@ -41,14 +42,16 @@ MM.Backend.Firebase.save = function(data, id, name) {
4142
MM.Backend.Firebase.load = function(id) {
4243
var promise = new Promise();
4344

44-
this.ref.child("data/" + id).once("value", function(snap) {
45+
var ref = this.ref.child("data/" + id);
46+
ref.once("value", function(snap) {
4547
var data = snap.val();
4648
if (data) {
4749
promise.fulfill(data);
50+
this._listenStart(ref);
4851
} else {
4952
promise.reject(new Error("There is no such saved map"));
5053
}
51-
});
54+
}.bind(this));
5255
return promise;
5356
}
5457

@@ -71,6 +74,25 @@ MM.Backend.Firebase.remove = function(id) {
7174
return promise;
7275
}
7376

77+
MM.Backend.Firebase.reset = function() {
78+
this._listenStop(); /* do not monitor current firebase ref for changes */
79+
}
80+
81+
MM.Backend.Firebase._listenStart = function(ref) {
82+
if (this.listenRef && this.listenRef.toString() == ref.toString()) { return; }
83+
84+
this._listenStop();
85+
this.listenRef = ref;
86+
ref.on("value", function() { /* console.log("!"); */ });
87+
}
88+
89+
MM.Backend.Firebase._listenStop = function() {
90+
if (this.listenRef) {
91+
this.listenRef.off("value");
92+
this.listenRef = null;
93+
}
94+
}
95+
7496
MM.Backend.Firebase._login = function(type) {
7597
var promise = new Promise();
7698

src/item.js

+4
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ MM.Item.prototype.setText = function(text) {
157157
return this.update();
158158
}
159159

160+
MM.Item.prototype.getId = function() {
161+
return this._id;
162+
}
163+
160164
MM.Item.prototype.getText = function() {
161165
return this._dom.text.innerHTML.replace(/<br\s*\/?>/g, "\n");
162166
}

src/map.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ MM.Map = function(options) {
77
this._root = null;
88
this._visible = false;
99
this._position = [0, 0];
10-
this._id = MM.generateId();
1110

1211
this._setRoot(new MM.Item().setText(o.root).setLayout(o.layout));
1312
}
@@ -17,15 +16,13 @@ MM.Map.fromJSON = function(data) {
1716
}
1817

1918
MM.Map.prototype.fromJSON = function(data) {
20-
if (data.id) { this._id = data.id; }
2119
this._setRoot(MM.Item.fromJSON(data.root));
2220
return this;
2321
}
2422

2523
MM.Map.prototype.toJSON = function() {
2624
var data = {
27-
root: this._root.toJSON(),
28-
id: this._id
25+
root: this._root.toJSON()
2926
};
3027
return data;
3128
}
@@ -156,7 +153,7 @@ MM.Map.prototype.getName = function() {
156153
}
157154

158155
MM.Map.prototype.getId = function() {
159-
return this._id;
156+
return this._root.getId();
160157
}
161158

162159
MM.Map.prototype.pick = function(item, direction) {

src/ui.backend.firebase.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ MM.UI.Backend.Firebase.setState = function(data) {
2424
this._connect(data.s, data.a).then(
2525
this._load.bind(this, data.id),
2626
this._error.bind(this)
27-
)
27+
);
2828
}
2929

3030
MM.UI.Backend.Firebase.getState = function() {
@@ -147,4 +147,3 @@ MM.UI.Backend.Firebase._sync = function() {
147147
if (this._mode == "load" && !this._list.value) { this._go.disabled = true; }
148148
this._go.innerHTML = this._mode.charAt(0).toUpperCase() + this._mode.substring(1);
149149
}
150-

src/ui.io.js

+3
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ MM.UI.IO.prototype._syncBackend = function() {
116116
this._backends[this._backend.value].show(this._mode);
117117
}
118118

119+
/**
120+
* @param {MM.UI.Backend} backend
121+
*/
119122
MM.UI.IO.prototype._setCurrentBackend = function(backend) {
120123
if (this._currentBackend && this._currentBackend != backend) { this._currentBackend.reset(); }
121124

0 commit comments

Comments
 (0)