added user events

This commit is contained in:
Kevin Jahns 2015-04-28 11:31:43 +02:00
parent 729d7ed3aa
commit 80f1cfd21b
11 changed files with 429 additions and 158 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -38,7 +38,11 @@ module.exports = {
this.connections = {};
this.current_sync_target = null;
this.sent_hb_to_all_users = false;
return this.is_initialized = true;
this.is_initialized = true;
return this.connections_listeners = [];
},
onUserEvent: function(f) {
return this.connections_listeners.push(f);
},
isRoleMaster: function() {
return this.role === "master";
@ -65,11 +69,22 @@ module.exports = {
return null;
},
userLeft: function(user) {
var f, i, len, ref, results;
delete this.connections[user];
return this.findNewSyncTarget();
this.findNewSyncTarget();
ref = this.connections_listeners;
results = [];
for (i = 0, len = ref.length; i < len; i++) {
f = ref[i];
results.push(f({
action: "userLeft",
user: user
}));
}
return results;
},
userJoined: function(user, role) {
var base;
var base, f, i, len, ref, results;
if (role == null) {
throw new Error("Internal: You must specify the role of the joined user! E.g. userJoined('uid:3939','slave')");
}
@ -79,11 +94,22 @@ module.exports = {
this.connections[user].is_synced = false;
if ((!this.is_synced) || this.syncMethod === "syncAll") {
if (this.syncMethod === "syncAll") {
return this.performSync(user);
this.performSync(user);
} else if (role === "master") {
return this.performSyncWithMaster(user);
this.performSyncWithMaster(user);
}
}
ref = this.connections_listeners;
results = [];
for (i = 0, len = ref.length; i < len; i++) {
f = ref[i];
results.push(f({
action: "userJoined",
user: user,
role: role
}));
}
return results;
},
whenSynced: function(args) {
if (args.constructore === Function) {

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -45,6 +45,10 @@ module.exports =
@current_sync_target = null
@sent_hb_to_all_users = false
@is_initialized = true
@connections_listeners = []
onUserEvent: (f)->
@connections_listeners.push f
isRoleMaster: ->
@role is "master"
@ -66,6 +70,12 @@ module.exports =
userLeft: (user)->
delete @connections[user]
@findNewSyncTarget()
for f in @connections_listeners
f {
action: "userLeft"
user: user
}
userJoined: (user, role)->
if not role?
@ -81,6 +91,12 @@ module.exports =
# TODO: What if there are two masters? Prevent sending everything two times!
@performSyncWithMaster user
for f in @connections_listeners
f {
action: "userJoined"
user: user
role: role
}
#
# Execute a function _when_ we are connected. If not connected, wait until connected.

File diff suppressed because one or more lines are too long

4
y.js

File diff suppressed because one or more lines are too long