1 | var ShareDBError = require('./error');
|
2 |
|
3 | module.exports = ReadSnapshotsRequest;
|
4 |
|
5 | /**
|
6 | * Context object passed to "readSnapshots" middleware functions
|
7 | *
|
8 | * @param {string} collection
|
9 | * @param {Snapshot[]} snapshots - snapshots being read
|
10 | * @param {keyof Backend.prototype.SNAPSHOT_TYPES} snapshotType - the type of snapshot read being
|
11 | * performed
|
12 | */
|
13 | function ReadSnapshotsRequest(collection, snapshots, snapshotType) {
|
14 | this.collection = collection;
|
15 | this.snapshots = snapshots;
|
16 | this.snapshotType = snapshotType;
|
17 |
|
18 | // Added by Backend#trigger
|
19 | this.action = null;
|
20 | this.agent = null;
|
21 | this.backend = null;
|
22 |
|
23 | /**
|
24 | * Map of doc id to error: `{[docId: string]: string | Error}`
|
25 | */
|
26 | this._idToError = null;
|
27 | }
|
28 |
|
29 | /**
|
30 | * Rejects the read of a specific snapshot. A rejected snapshot read will not have that snapshot's
|
31 | * data sent down to the client.
|
32 | *
|
33 | * If the error has a `code` property of `"ERR_SNAPSHOT_READ_SILENT_REJECTION"`, then the Share
|
34 | * client will not pass the error to user code, but will still do things like cancel subscriptions.
|
35 | * The `#rejectSnapshotReadSilent(snapshot, errorMessage)` method can also be used for convenience.
|
36 | *
|
37 | * @param {Snapshot} snapshot
|
38 | * @param {string | Error} error
|
39 | *
|
40 | * @see #rejectSnapshotReadSilent
|
41 | * @see ShareDBError.CODES.ERR_SNAPSHOT_READ_SILENT_REJECTION
|
42 | * @see ShareDBError.CODES.ERR_SNAPSHOT_READS_REJECTED
|
43 | */
|
44 | ReadSnapshotsRequest.prototype.rejectSnapshotRead = function(snapshot, error) {
|
45 | if (!this._idToError) {
|
46 | this._idToError = {};
|
47 | }
|
48 | this._idToError[snapshot.id] = error;
|
49 | };
|
50 |
|
51 | /**
|
52 | * Rejects the read of a specific snapshot. A rejected snapshot read will not have that snapshot's
|
53 | * data sent down to the client.
|
54 | *
|
55 | * This method will set a special error code that causes the Share client to not pass the error to
|
56 | * user code, though it will still do things like cancel subscriptions.
|
57 | *
|
58 | * @param {Snapshot} snapshot
|
59 | * @param {string} errorMessage
|
60 | */
|
61 | ReadSnapshotsRequest.prototype.rejectSnapshotReadSilent = function(snapshot, errorMessage) {
|
62 | this.rejectSnapshotRead(snapshot, this.silentRejectionError(errorMessage));
|
63 | };
|
64 |
|
65 | ReadSnapshotsRequest.prototype.silentRejectionError = function(errorMessage) {
|
66 | return new ShareDBError(ShareDBError.CODES.ERR_SNAPSHOT_READ_SILENT_REJECTION, errorMessage);
|
67 | };
|
68 |
|
69 | /**
|
70 | * Returns whether this trigger of "readSnapshots" has had a snapshot read rejected.
|
71 | */
|
72 | ReadSnapshotsRequest.prototype.hasSnapshotRejection = function() {
|
73 | return this._idToError != null;
|
74 | };
|
75 |
|
76 | /**
|
77 | * Returns an overall error from "readSnapshots" based on the snapshot-specific errors.
|
78 | *
|
79 | * - If there's exactly one snapshot and it has an error, then that error is returned.
|
80 | * - If there's more than one snapshot and at least one has an error, then an overall
|
81 | * "ERR_SNAPSHOT_READS_REJECTED" is returned, with an `idToError` property.
|
82 | */
|
83 | ReadSnapshotsRequest.prototype.getReadSnapshotsError = function() {
|
84 | var snapshots = this.snapshots;
|
85 | var idToError = this._idToError;
|
86 | // If there are 0 snapshots, there can't be any snapshot-specific errors.
|
87 | if (snapshots.length === 0) {
|
88 | return;
|
89 | }
|
90 |
|
91 | // Single snapshot with error is treated as a full error.
|
92 | if (snapshots.length === 1) {
|
93 | var snapshotError = idToError[snapshots[0].id];
|
94 | if (snapshotError) {
|
95 | return snapshotError;
|
96 | } else {
|
97 | return;
|
98 | }
|
99 | }
|
100 |
|
101 | // Errors in specific snapshots result in an overall ERR_SNAPSHOT_READS_REJECTED.
|
102 | //
|
103 | // fetchBulk and subscribeBulk know how to handle that special error by sending a doc-by-doc
|
104 | // success/failure to the client. Other methods that don't or can't handle partial failures
|
105 | // will treat it as a full rejection.
|
106 | var err = new ShareDBError(ShareDBError.CODES.ERR_SNAPSHOT_READS_REJECTED);
|
107 | err.idToError = idToError;
|
108 | return err;
|
109 | };
|