summaryrefslogtreecommitdiff
path: root/public/vendor/ot/socketio-adapter.js
blob: 189a081bc71b63c698eb95abc479407e63c38847 (plain)
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
/*global ot */

ot.SocketIOAdapter = (function () {
    'use strict';

    function SocketIOAdapter(socket) {
        this.socket = socket;

        var self = this;
        socket.on('client_left', function (clientId) {
            self.trigger('client_left', clientId);
        });
        socket.on('set_name', function (clientId, name) {
            self.trigger('set_name', clientId, name);
        });
        socket.on('set_color', function (clientId, color) {
            self.trigger('set_color', clientId, color);
        });
        socket.on('ack', function (revision) {
            self.trigger('ack', revision);
        });
        socket.on('operation', function (clientId, revision, operation, selection) {
            self.trigger('operation', revision, operation);
            self.trigger('selection', clientId, selection);
        });
        socket.on('operations', function (head, operations) {
            self.trigger('operations', head, operations);
        });
        socket.on('selection', function (clientId, selection) {
            self.trigger('selection', clientId, selection);
        });
        socket.on('reconnect', function () {
            self.trigger('reconnect');
        });
    }

    SocketIOAdapter.prototype.sendOperation = function (revision, operation, selection) {
        this.socket.emit('operation', revision, operation, selection);
    };

    SocketIOAdapter.prototype.sendSelection = function (selection) {
        this.socket.emit('selection', selection);
    };

    SocketIOAdapter.prototype.getOperations = function (base, head) {
        this.socket.emit('get_operations', base, head);
    };

    SocketIOAdapter.prototype.registerCallbacks = function (cb) {
        this.callbacks = cb;
    };

    SocketIOAdapter.prototype.trigger = function (event) {
        var args = Array.prototype.slice.call(arguments, 1);
        var action = this.callbacks && this.callbacks[event];
        if (action) {
            action.apply(this, args);
        }
    };

    return SocketIOAdapter;

}());