-
Notifications
You must be signed in to change notification settings - Fork 444
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
624 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,9 @@ | |
# Emacs | ||
\#*\# | ||
|
||
# VS Code | ||
.vscode/ | ||
|
||
# Logs | ||
logs | ||
*.log | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
var Snapshot = require('../snapshot'); | ||
var util = require('../util'); | ||
var emitter = require('../emitter'); | ||
|
||
module.exports = SnapshotRequest; | ||
|
||
function SnapshotRequest(connection, requestId, collection, id, version, callback) { | ||
emitter.EventEmitter.call(this); | ||
|
||
if (typeof callback !== 'function') { | ||
throw new Error('Callback is required for SnapshotRequest'); | ||
} | ||
|
||
if (!this.isValidVersion(version)) { | ||
throw new Error('Snapshot version must be a positive integer or null'); | ||
} | ||
|
||
this.requestId = requestId; | ||
this.connection = connection; | ||
this.id = id; | ||
this.collection = collection; | ||
this.version = version; | ||
this.callback = callback; | ||
|
||
this.sent = false; | ||
} | ||
emitter.mixin(SnapshotRequest); | ||
|
||
SnapshotRequest.prototype.isValidVersion = function (version) { | ||
if (version === null) { | ||
return true; | ||
} | ||
|
||
if (!util.isInteger(version)) { | ||
return false; | ||
} | ||
|
||
return version >= 0; | ||
} | ||
|
||
SnapshotRequest.prototype.send = function () { | ||
if (!this.connection.canSend) { | ||
return; | ||
} | ||
|
||
var message = { | ||
a: 'nf', | ||
id: this.requestId, | ||
c: this.collection, | ||
d: this.id, | ||
v: this.version, | ||
}; | ||
|
||
this.connection.send(message); | ||
this.sent = true; | ||
}; | ||
|
||
SnapshotRequest.prototype._onConnectionStateChanged = function () { | ||
if (this.connection.canSend && !this.sent) { | ||
this.send(); | ||
} else if (!this.connection.canSend) { | ||
this.sent = false; | ||
} | ||
}; | ||
|
||
SnapshotRequest.prototype._handleResponse = function (error, message) { | ||
this.emit('ready'); | ||
|
||
if (error) { | ||
return this.callback(error); | ||
} | ||
|
||
var snapshot = new Snapshot(this.id, message.v, message.type, message.data, null); | ||
this.callback(null, snapshot); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module.exports = Snapshot; | ||
function Snapshot(id, version, type, data, meta) { | ||
this.id = id; | ||
this.v = version; | ||
this.type = type; | ||
this.data = data; | ||
this.m = meta; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.