UNPKG

5.22 kBTypeScriptView Raw
1/*!
2 * Copyright 2019 Google Inc. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16/// <reference types="node" />
17import { ServiceError } from 'google-gax';
18import { EventEmitter } from 'events';
19import { BatchPublishOptions, MessageBatch } from './message-batch';
20import { PublishError } from './publish-error';
21import { Publisher, PubsubMessage, PublishCallback } from './';
22export interface PublishDone {
23 (err: ServiceError | null): void;
24}
25/**
26 * Queues are used to manage publishing batches of messages.
27 *
28 * @private
29 *
30 * @param {Publisher} publisher The parent publisher.
31 */
32export declare abstract class MessageQueue extends EventEmitter {
33 batchOptions: BatchPublishOptions;
34 publisher: Publisher;
35 pending?: NodeJS.Timer;
36 constructor(publisher: Publisher);
37 /**
38 * Forces the queue to update its options from the publisher.
39 * The specific queue will need to do a bit more to pass the new
40 * values down into any MessageBatch.
41 *
42 * This is only for use by {@link Publisher}.
43 *
44 * @private
45 */
46 updateOptions(): void;
47 /**
48 * Adds a message to the queue.
49 *
50 * @abstract
51 *
52 * @param {object} message The message to publish.
53 * @param {PublishCallback} callback The publish callback.
54 */
55 abstract add(message: PubsubMessage, callback: PublishCallback): void;
56 /**
57 * Method to initiate publishing.
58 *
59 * @abstract
60 */
61 abstract publish(): void;
62 /**
63 * Accepts a batch of messages and publishes them to the API.
64 *
65 * @param {object[]} messages The messages to publish.
66 * @param {PublishCallback[]} callbacks The corresponding callback functions.
67 * @param {function} [callback] Callback to be fired when publish is done.
68 */
69 _publish(messages: PubsubMessage[], callbacks: PublishCallback[], callback?: PublishDone): void;
70}
71/**
72 * Standard message queue used for publishing messages.
73 *
74 * @private
75 * @extends MessageQueue
76 *
77 * @param {Publisher} publisher The publisher.
78 */
79export declare class Queue extends MessageQueue {
80 batch: MessageBatch;
81 constructor(publisher: Publisher);
82 updateOptions(): void;
83 /**
84 * Adds a message to the queue.
85 *
86 * @param {PubsubMessage} message The message to publish.
87 * @param {PublishCallback} callback The publish callback.
88 */
89 add(message: PubsubMessage, callback: PublishCallback): void;
90 /**
91 * Cancels any pending publishes and calls _publish immediately.
92 */
93 publish(callback?: PublishDone): void;
94}
95/**
96 * Queue for handling ordered messages. Unlike the standard queue, this
97 * ensures that batches are published one at a time and throws an exception in
98 * the event that any batch fails to publish.
99 *
100 * @private
101 * @extends MessageQueue
102 *
103 * @param {Publisher} publisher The publisher.
104 * @param {string} key The key used to order the messages.
105 */
106export declare class OrderedQueue extends MessageQueue {
107 batches: MessageBatch[];
108 inFlight: boolean;
109 error?: null | PublishError;
110 key: string;
111 constructor(publisher: Publisher, key: string);
112 updateOptions(): void;
113 /**
114 * Reference to the batch we're currently filling.
115 * @returns {MessageBatch}
116 */
117 get currentBatch(): MessageBatch;
118 /**
119 * Adds a message to a batch, creating a new batch if need be.
120 *
121 * @param {object} message The message to publish.
122 * @param {PublishCallback} callback The publish callback.
123 */
124 add(message: PubsubMessage, callback: PublishCallback): void;
125 /**
126 * Starts a timeout to publish any pending messages.
127 */
128 beginNextPublish(): void;
129 /**
130 * Creates a new {@link MessageBatch} instance.
131 *
132 * @returns {MessageBatch}
133 */
134 createBatch(): MessageBatch;
135 /**
136 * In the event of a publish failure, we need to cache the error in question
137 * and reject all pending publish calls, prompting the user to call
138 * {@link OrderedQueue#resumePublishing}.
139 *
140 * @param {Error} err The publishing error.
141 */
142 handlePublishFailure(err: ServiceError): void;
143 /**
144 * Publishes the messages. If successful it will prepare the next batch to be
145 * published immediately after. If an error occurs, it will reject all
146 * pending messages. In the event that no pending messages/batches are left,
147 * a "drain" event will be fired, indicating to the publisher that it is
148 * safe to delete this queue.
149 *
150 * @fires OrderedQueue#drain
151 */
152 publish(callback?: PublishDone): void;
153 /**
154 * Tells the queue it is ok to continue publishing messages.
155 */
156 resumePublishing(): void;
157}