Repository navigation
Expand file tree
/
Copy pathclient-sync.js
More file actions
72 lines (64 loc) · 2.68 KB
/
Copy pathclient-sync.js
File metadata and controls
72 lines (64 loc) · 2.68 KB
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
var assign = require('object-assign')
var BaseSync = require('./base-sync')
var DEFAULT_OPTIONS = {
fixTime: true,
timeout: 20000,
ping: 5000
}
/**
* Client node in synchronization pair.
*
* Instead of server node, it initializes synchronization
* and sends connect message.
*
* @param {string|number} nodeId Unique current machine name.
* @param {Log} log Logux log instance to sync with other node log.
* @param {Connection} connection Connection to other node.
* @param {object} [options] Synchronization options.
* @param {object} [options.credentials] Client credentials.
* For example, access token.
* @param {authCallback} [options.auth] Function to check server credentials.
* @param {boolean} [options.fixTime=true] Detect difference between client
* and server and fix time
* in synchronized actions.
* @param {number} [options.timeout=20000] Timeout in milliseconds
* to wait answer before disconnect.
* @param {number} [options.ping=5000] Milliseconds since last message to test
* connection by sending ping.
* @param {filter} [options.inFilter] Function to filter actions from server.
* Best place for permissions control.
* @param {mapper} [options.inMap] Map function to change other node’s action
* before put it to current log.
* @param {filter} [options.outFilter] Filter function to select actions
* to synchronization.
* @param {mapper} [options.outMap] Map function to change action
* before sending it to other client.
* @param {number[]} [options.subprotocol] Application subprotocol version.
* @param {string} [options.subprotocol] Application subprotocol version
* in SemVer format.
*
* @example
* import { ClientSync } from 'logux-sync'
* const connection = new BrowserConnection(url)
* const sync = new ClientSync(nodeId, log, connection)
*
* @extends BaseSync
* @class
*/
function ClientSync (nodeId, log, connection, options) {
options = assign({ }, DEFAULT_OPTIONS, options)
BaseSync.call(this, nodeId, log, connection, options)
}
ClientSync.prototype = {
onConnect: function onConnect () {
if (!this.connected) {
this.connected = true
var sync = this
this.initializing = this.initializing.then(function () {
sync.sendConnect()
})
}
}
}
ClientSync.prototype = assign({ }, BaseSync.prototype, ClientSync.prototype)
module.exports = ClientSync