|
| 1 | +/** |
| 2 | + * Copyright (c) "Neo4j" |
| 3 | + * Neo4j Sweden AB [http://neo4j.com] |
| 4 | + * |
| 5 | + * This file is part of Neo4j. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import Transaction from "./transaction" |
| 21 | +import { |
| 22 | + ConnectionHolder |
| 23 | +} from './internal/connection-holder' |
| 24 | + |
| 25 | +import { Bookmarks } from './internal/bookmarks' |
| 26 | +import { TxConfig } from "./internal/tx-config"; |
| 27 | + |
| 28 | +/** |
| 29 | + * Represents a {@link Promise<Transaction>} object and a {@link Transaction} object. |
| 30 | + * |
| 31 | + * Resolving this object promise verifies the result of the transaction begin and returns the {@link Transaction} object in case of success. |
| 32 | + * |
| 33 | + * The object can still also used as {@link Transaction} for convenience. The result of begin will be checked |
| 34 | + * during the next API calls in the object as it is in the transaction. |
| 35 | + * |
| 36 | + * @access public |
| 37 | + */ |
| 38 | +class TransactionPromise extends Transaction implements Promise<Transaction>{ |
| 39 | + [Symbol.toStringTag]: string = "TransactionPromise" |
| 40 | + private _beginError?: Error; |
| 41 | + private _beginMetadata?: any; |
| 42 | + private _beginPromise?: Promise<Transaction>; |
| 43 | + private _reject?: (error: Error) => void; |
| 44 | + private _resolve?: (value?: Transaction | PromiseLike<Transaction> | undefined) => void; |
| 45 | + |
| 46 | + /** |
| 47 | + * @constructor |
| 48 | + * @param {ConnectionHolder} connectionHolder - the connection holder to get connection from. |
| 49 | + * @param {function()} onClose - Function to be called when transaction is committed or rolled back. |
| 50 | + * @param {function(bookmarks: Bookmarks)} onBookmarks callback invoked when new bookmark is produced. |
| 51 | + * * @param {function()} onConnection - Function to be called when a connection is obtained to ensure the conneciton |
| 52 | + * is not yet released. |
| 53 | + * @param {boolean} reactive whether this transaction generates reactive streams |
| 54 | + * @param {number} fetchSize - the record fetch size in each pulling batch. |
| 55 | + * @param {string} impersonatedUser - The name of the user which should be impersonated for the duration of the session. |
| 56 | + */ |
| 57 | + constructor({ |
| 58 | + connectionHolder, |
| 59 | + onClose, |
| 60 | + onBookmarks, |
| 61 | + onConnection, |
| 62 | + reactive, |
| 63 | + fetchSize, |
| 64 | + impersonatedUser, |
| 65 | + highRecordWatermark, |
| 66 | + lowRecordWatermark |
| 67 | + }: { |
| 68 | + connectionHolder: ConnectionHolder |
| 69 | + onClose: () => void |
| 70 | + onBookmarks: (bookmarks: Bookmarks) => void |
| 71 | + onConnection: () => void |
| 72 | + reactive: boolean |
| 73 | + fetchSize: number |
| 74 | + impersonatedUser?: string, |
| 75 | + highRecordWatermark: number, |
| 76 | + lowRecordWatermark: number |
| 77 | + }) { |
| 78 | + super({ |
| 79 | + connectionHolder, |
| 80 | + onClose, |
| 81 | + onBookmarks, |
| 82 | + onConnection, |
| 83 | + reactive, |
| 84 | + fetchSize, |
| 85 | + impersonatedUser, |
| 86 | + highRecordWatermark, |
| 87 | + lowRecordWatermark |
| 88 | + }) |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Waits for the begin to complete. |
| 93 | + * |
| 94 | + * @param {function(transaction: Transaction)} onFulfilled - function to be called when finished. |
| 95 | + * @param {function(error: {message:string, code:string})} onRejected - function to be called upon errors. |
| 96 | + * @return {Promise} promise. |
| 97 | + */ |
| 98 | + then<TResult1 = Transaction, TResult2 = never>( |
| 99 | + onfulfilled?: |
| 100 | + ((value: Transaction) => TResult1 | PromiseLike<TResult1>) |
| 101 | + | null, |
| 102 | + onrejected?: |
| 103 | + ((reason: any) => TResult2 | PromiseLike<TResult2>) |
| 104 | + | null |
| 105 | + ): Promise<TResult1 | TResult2> { |
| 106 | + return this._getOrCreateBeginPromise().then(onfulfilled, onrejected); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Catch errors when using promises. |
| 111 | + * |
| 112 | + * @param {function(error: Neo4jError)} onRejected - Function to be called upon errors. |
| 113 | + * @return {Promise} promise. |
| 114 | + */ |
| 115 | + catch<TResult = never>(onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null): Promise<any> { |
| 116 | + return this._getOrCreateBeginPromise().catch(onrejected); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Called when finally the begin is done |
| 121 | + * |
| 122 | + * @param {function()|null} onfinally - function when the promise finished |
| 123 | + * @return {Promise} promise. |
| 124 | + */ |
| 125 | + finally(onfinally?: (() => void) | null): Promise<Transaction> { |
| 126 | + return this._getOrCreateBeginPromise().finally(onfinally); |
| 127 | + } |
| 128 | + |
| 129 | + private _getOrCreateBeginPromise(): Promise<Transaction> { |
| 130 | + if (!this._beginPromise) { |
| 131 | + this._beginPromise = new Promise((resolve, reject) => { |
| 132 | + this._resolve = resolve; |
| 133 | + this._reject = reject; |
| 134 | + if (this._beginError) { |
| 135 | + reject(this._beginError); |
| 136 | + } |
| 137 | + if (this._beginMetadata) { |
| 138 | + resolve(this._toTransaction()); |
| 139 | + } |
| 140 | + }); |
| 141 | + } |
| 142 | + return this._beginPromise; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * @access private |
| 147 | + */ |
| 148 | + private _toTransaction(): Transaction { |
| 149 | + //@ts-ignore |
| 150 | + return { |
| 151 | + ...this, |
| 152 | + run: super.run.bind(this), |
| 153 | + commit: super.commit.bind(this), |
| 154 | + rollback: super.rollback.bind(this), |
| 155 | + close: super.close.bind(this), |
| 156 | + isOpen: super.isOpen.bind(this), |
| 157 | + _begin: this._begin.bind(this), |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * @access private |
| 163 | + */ |
| 164 | + _begin(bookmarks: string | Bookmarks | string[], txConfig: TxConfig): void { |
| 165 | + return super._begin(bookmarks, txConfig, { |
| 166 | + onError: this._onBeginError.bind(this), |
| 167 | + onComplete: this._onBeginMetadata.bind(this) |
| 168 | + }); |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * @access private |
| 173 | + */ |
| 174 | + private _onBeginError(error: Error): void { |
| 175 | + this._beginError = error; |
| 176 | + if (this._reject) { |
| 177 | + this._reject(error); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * @access private |
| 183 | + */ |
| 184 | + private _onBeginMetadata(metadata: any): void { |
| 185 | + this._beginMetadata = metadata || {}; |
| 186 | + if (this._resolve) { |
| 187 | + this._resolve(this._toTransaction()); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | +} |
| 192 | + |
| 193 | +export default TransactionPromise |
0 commit comments