UNPKG

4.41 kBJavaScriptView Raw
1"use strict";
2/*!
3 * Copyright 2017 Google Inc. All Rights Reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.Snapshot = void 0;
19const subscription_1 = require("./subscription");
20const util_1 = require("./util");
21/**
22 * A Snapshot object will give you access to your Cloud Pub/Sub snapshot.
23 *
24 * Snapshots are sometimes retrieved when using various methods:
25 *
26 * - {@link PubSub#getSnapshots}
27 * - {@link PubSub#getSnapshotsStream}
28 * - {@link PubSub#snapshot}
29 *
30 * Snapshots may be created with:
31 *
32 * - {@link Subscription#createSnapshot}
33 *
34 * You can use snapshots to seek a subscription to a specific point in time.
35 *
36 * - {@link Subscription#seek}
37 *
38 * @class
39 *
40 * @example
41 * ```
42 * //-
43 * // From {@link PubSub#getSnapshots}:
44 * //-
45 * pubsub.getSnapshots((err, snapshots) => {
46 * // `snapshots` is an array of Snapshot objects.
47 * });
48 *
49 * //-
50 * // From {@link PubSub#getSnapshotsStream}:
51 * //-
52 * pubsub.getSnapshotsStream()
53 * .on('error', console.error)
54 * .on('data', (snapshot) => {
55 * // `snapshot` is a Snapshot object.
56 * });
57 *
58 * //-
59 * // From {@link PubSub#snapshot}:
60 * //-
61 * const snapshot = pubsub.snapshot('my-snapshot');
62 * // snapshot is a Snapshot object.
63 *
64 * //-
65 * // Create a snapshot with {module:pubsub/subscription#createSnapshot}:
66 * //-
67 * const subscription = pubsub.subscription('my-subscription');
68 *
69 * subscription.createSnapshot('my-snapshot', (err, snapshot) => {
70 * if (!err) {
71 * // `snapshot` is a Snapshot object.
72 * }
73 * });
74 *
75 * //-
76 * // Seek to your snapshot:
77 * //-
78 * const subscription = pubsub.subscription('my-subscription');
79 *
80 * subscription.seek('my-snapshot', (err) => {
81 * if (err) {
82 * // Error handling omitted.
83 * }
84 * });
85 * ```
86 */
87class Snapshot {
88 constructor(parent, name) {
89 this.parent = parent;
90 this.name = Snapshot.formatName_(parent.projectId, name);
91 }
92 delete(callback) {
93 const reqOpts = {
94 snapshot: this.name,
95 };
96 this.parent.request({
97 client: 'SubscriberClient',
98 method: 'deleteSnapshot',
99 reqOpts,
100 }, callback);
101 }
102 /*@
103 * Format the name of a snapshot. A snapshot's full name is in the format of
104 * projects/{projectId}/snapshots/{snapshotName}
105 *
106 * @private
107 */
108 static formatName_(projectId, name) {
109 return 'projects/' + projectId + '/snapshots/' + name.split('/').pop();
110 }
111 create(optsOrCallback, callback) {
112 if (!(this.parent instanceof subscription_1.Subscription)) {
113 throw new Error('This is only available if you accessed this object through Subscription#snapshot');
114 }
115 const options = typeof optsOrCallback === 'object' ? optsOrCallback : {};
116 callback = typeof optsOrCallback === 'function' ? optsOrCallback : callback;
117 return this.parent.createSnapshot(this.name, options, (err, snapshot, resp) => {
118 if (err) {
119 callback(err, null, resp);
120 return;
121 }
122 Object.assign(this, snapshot);
123 callback(null, this, resp);
124 });
125 }
126 seek(gaxOpts, callback) {
127 if (!(this.parent instanceof subscription_1.Subscription)) {
128 throw new Error('This is only available if you accessed this object through Subscription#snapshot');
129 }
130 return this.parent.seek(this.name, gaxOpts, callback);
131 }
132}
133exports.Snapshot = Snapshot;
134/*! Developer Documentation
135 *
136 * Existing async methods (except for streams) will return a Promise in the event
137 * that a callback is omitted. Future methods will not allow for a callback.
138 * (Use .then() on the returned Promise instead.)
139 */
140util_1.promisifySome(Snapshot, Snapshot.prototype, ['delete', 'create', 'seek']);
141//# sourceMappingURL=snapshot.js.map
\No newline at end of file