|
| 1 | +import { Kuzzle } from '../Kuzzle'; |
| 2 | +import { RealtimeDocument } from './RealtimeDocument'; |
| 3 | +import { Document, DocumentNotification, JSONObject } from '../types'; |
| 4 | +import { SearchResult } from './searchResult/SearchResultBase'; |
| 5 | +import { RealtimeDocumentSearchResult } from './searchResult/RealtimeDocument'; |
| 6 | + |
| 7 | +class CollectionSubscription extends Set<string> { |
| 8 | + public roomId: string = null; |
| 9 | + |
| 10 | + get filters () { |
| 11 | + return { |
| 12 | + ids: { values: Array.from(this.values()) } |
| 13 | + }; |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +type DocumentUrn = string; |
| 18 | +type CollectionUrn = string; |
| 19 | + |
| 20 | +function documentUrn (index: string, collection: string, id: string): DocumentUrn { |
| 21 | + return `${index}:${collection}:${id}`; |
| 22 | +} |
| 23 | + |
| 24 | +function collectionUrn (index: string, collection: string): CollectionUrn { |
| 25 | + return `${index}:${collection}`; |
| 26 | +} |
| 27 | + |
| 28 | +export class Observer { |
| 29 | + private collections = new Map<CollectionUrn, CollectionSubscription>(); |
| 30 | + private documents = new Map<DocumentUrn, RealtimeDocument>(); |
| 31 | + private sdk: Kuzzle; |
| 32 | + |
| 33 | + constructor (sdk: Kuzzle) { |
| 34 | + this.sdk = sdk; |
| 35 | + } |
| 36 | + |
| 37 | + stop (index: string, collection: string, document: { _id: string }) { |
| 38 | + const urn = documentUrn(index, collection, document._id); |
| 39 | + const rtDocument = this.documents.get(urn); |
| 40 | + |
| 41 | + if (! rtDocument) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + const subscription = this.collections.get(collectionUrn(index, collection)); |
| 46 | + |
| 47 | + subscription.delete(document._id); |
| 48 | + |
| 49 | + return this.resubscribe(index, collection); |
| 50 | + } |
| 51 | + |
| 52 | + dispose () { |
| 53 | + const promises = []; |
| 54 | + |
| 55 | + for (const subscription of this.collections.values()) { |
| 56 | + if (subscription.roomId) { |
| 57 | + promises.push(this.sdk.realtime.unsubscribe(subscription.roomId)); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + this.collections.clear(); |
| 62 | + this.documents.clear(); |
| 63 | + |
| 64 | + return Promise.all(promises); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Gets a realtime document |
| 69 | + * |
| 70 | + * @param index Index name |
| 71 | + * @param collection Collection name |
| 72 | + * @param _id Document ID |
| 73 | + * |
| 74 | + * @returns The realtime document |
| 75 | + */ |
| 76 | + get (index: string, collection: string, id: string): Promise<RealtimeDocument> { |
| 77 | + return this.sdk.document.get(index, collection, id) |
| 78 | + .then(document => this.observe(index, collection, document)); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * |
| 83 | + * Gets multiple realtime documents. |
| 84 | + * |
| 85 | + * @param index Index name |
| 86 | + * @param collection Collection name |
| 87 | + * @param ids Document IDs |
| 88 | + * |
| 89 | + * @returns An object containing 2 arrays: "successes" and "errors" |
| 90 | + */ |
| 91 | + mGet ( |
| 92 | + index: string, |
| 93 | + collection: string, |
| 94 | + ids: string[] |
| 95 | + ): Promise<{ |
| 96 | + /** |
| 97 | + * Array of successfully retrieved documents |
| 98 | + */ |
| 99 | + successes: RealtimeDocument[]; |
| 100 | + /** |
| 101 | + * Array of the IDs of not found documents. |
| 102 | + */ |
| 103 | + errors: string[]; |
| 104 | + }> { |
| 105 | + const rtDocuments = []; |
| 106 | + let _errors; |
| 107 | + |
| 108 | + return this.sdk.document.mGet(index, collection, ids) |
| 109 | + .then(({ successes, errors }) => { |
| 110 | + _errors = errors; |
| 111 | + |
| 112 | + for (const document of successes) { |
| 113 | + rtDocuments.push(this.addDocument(index, collection, document)); |
| 114 | + } |
| 115 | + |
| 116 | + return this.resubscribe(index, collection); |
| 117 | + }) |
| 118 | + .then(() => ({ successes: rtDocuments, errors: _errors })); |
| 119 | + } |
| 120 | + |
| 121 | + search ( |
| 122 | + index: string, |
| 123 | + collection: string, |
| 124 | + searchBody: JSONObject = {}, |
| 125 | + options: { |
| 126 | + from?: number; |
| 127 | + size?: number; |
| 128 | + scroll?: string; |
| 129 | + lang?: string; |
| 130 | + verb?: string; |
| 131 | + timeout?: number; |
| 132 | + } = {} |
| 133 | + ): Promise<SearchResult<RealtimeDocument>> { |
| 134 | + let result; |
| 135 | + |
| 136 | + return this.sdk.document['_search'](index, collection, searchBody, options) |
| 137 | + .then(({ response, request, opts }) => { |
| 138 | + result = new RealtimeDocumentSearchResult( |
| 139 | + this.sdk, |
| 140 | + request, |
| 141 | + opts, |
| 142 | + response.result, |
| 143 | + this); |
| 144 | + |
| 145 | + const rtDocuments = []; |
| 146 | + for (const hit of result.hits) { |
| 147 | + rtDocuments.push(this.addDocument(index, collection, hit)); |
| 148 | + } |
| 149 | + result.hits = rtDocuments; |
| 150 | + |
| 151 | + return this.resubscribe(index, collection) |
| 152 | + }) |
| 153 | + .then(() => result); |
| 154 | + } |
| 155 | + |
| 156 | + observe ( |
| 157 | + index: string, |
| 158 | + collection: string, |
| 159 | + document: Document, |
| 160 | + ): Promise<RealtimeDocument> { |
| 161 | + const rtDocument = this.addDocument(index, collection, document); |
| 162 | + |
| 163 | + return this.resubscribe(index, collection) |
| 164 | + .then(() => rtDocument); |
| 165 | + } |
| 166 | + |
| 167 | + addDocument (index: string, collection: string, document: Document): RealtimeDocument { |
| 168 | + const rtDocument = new RealtimeDocument(document); |
| 169 | + |
| 170 | + const urn = collectionUrn(index, collection); |
| 171 | + |
| 172 | + if (! this.collections.has(urn)) { |
| 173 | + this.collections.set(urn, new CollectionSubscription()); |
| 174 | + } |
| 175 | + |
| 176 | + const subscription = this.collections.get(urn); |
| 177 | + |
| 178 | + subscription.add(document._id); |
| 179 | + |
| 180 | + this.documents.set(documentUrn(index, collection, document._id), rtDocument); |
| 181 | + |
| 182 | + return rtDocument; |
| 183 | + } |
| 184 | + |
| 185 | + resubscribe (index: string, collection: string) { |
| 186 | + let _roomId; |
| 187 | + |
| 188 | + const subscription = this.collections.get(collectionUrn(index, collection)); |
| 189 | + |
| 190 | + return this.sdk.realtime.subscribe( |
| 191 | + index, |
| 192 | + collection, |
| 193 | + subscription.filters, |
| 194 | + this.notificationHandler.bind(this) |
| 195 | + ) |
| 196 | + .then(roomId => { |
| 197 | + _roomId = roomId; |
| 198 | + |
| 199 | + if (subscription.roomId) { |
| 200 | + return this.sdk.realtime.unsubscribe(subscription.roomId); |
| 201 | + } |
| 202 | + |
| 203 | + return null; |
| 204 | + }) |
| 205 | + .then(() => { |
| 206 | + subscription.roomId = _roomId; |
| 207 | + }); |
| 208 | + } |
| 209 | + |
| 210 | + private notificationHandler (notification: DocumentNotification) { |
| 211 | + const { index, collection, result } = notification; |
| 212 | + |
| 213 | + const urn = documentUrn(index, collection, result._id); |
| 214 | + const rtDocument = this.documents.get(urn); |
| 215 | + |
| 216 | + // On "write", mutate document with changes |
| 217 | + // On "publish", nothing |
| 218 | + if (notification.event !== 'delete') { |
| 219 | + if (notification.event === 'write') { |
| 220 | + Object.assign(rtDocument._source, result._source); |
| 221 | + } |
| 222 | + |
| 223 | + return Promise.resolve(); |
| 224 | + } |
| 225 | + |
| 226 | + rtDocument.deleted = true; |
| 227 | + this.documents.delete(rtDocument._id); |
| 228 | + |
| 229 | + return this.resubscribe(index, collection); |
| 230 | + } |
| 231 | +} |
0 commit comments