|
| 1 | +/** |
| 2 | + * Copyright (c) 2002-2019 "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 RxSession from '../../types/session-rx' |
| 21 | +import { TransactionConfig } from '../../types/session' |
| 22 | +import RxTransaction from '../../types/transaction-rx' |
| 23 | +import Record from '../../types/record' |
| 24 | +import RxResult from '../../types/result-rx' |
| 25 | +import ResultSummary from '../../types/result-summary' |
| 26 | +import Integer from '../../types/integer' |
| 27 | +import { Observable, of, Observer, throwError } from 'rxjs' |
| 28 | +import { concat, finalize, catchError } from 'rxjs/operators' |
| 29 | + |
| 30 | +const dummy: any = null |
| 31 | +const intValue: Integer = Integer.fromInt(42) |
| 32 | + |
| 33 | +const keysObserver: Observer<string[]> = { |
| 34 | + next: value => console.log(`keys: ${value}`), |
| 35 | + complete: () => console.log('keys complete'), |
| 36 | + error: error => console.log(`keys error: ${error}`) |
| 37 | +} |
| 38 | + |
| 39 | +const recordsObserver: Observer<Record> = { |
| 40 | + next: value => console.log(`record: ${value}`), |
| 41 | + complete: () => console.log('records complete'), |
| 42 | + error: error => console.log(`records error: ${error}`) |
| 43 | +} |
| 44 | + |
| 45 | +const summaryObserver: Observer<ResultSummary> = { |
| 46 | + next: value => console.log(`summary: ${value}`), |
| 47 | + complete: () => console.log('summary complete'), |
| 48 | + error: error => console.log(`summary error: ${error}`) |
| 49 | +} |
| 50 | + |
| 51 | +const rxSession: RxSession = dummy |
| 52 | + |
| 53 | +const txConfig1: TransactionConfig = {} |
| 54 | +const txConfig2: TransactionConfig = { timeout: 5000 } |
| 55 | +const txConfig3: TransactionConfig = { timeout: intValue } |
| 56 | +const txConfig4: TransactionConfig = { metadata: {} } |
| 57 | +const txConfig5: TransactionConfig = { |
| 58 | + metadata: { |
| 59 | + key1: 'value1', |
| 60 | + key2: 5, |
| 61 | + key3: { a: 'a', b: 'b' }, |
| 62 | + key4: [1, 2, 3] |
| 63 | + } |
| 64 | +} |
| 65 | +const txConfig6: TransactionConfig = { |
| 66 | + timeout: 2000, |
| 67 | + metadata: { key1: 'value1', key2: 2 } |
| 68 | +} |
| 69 | +const txConfig7: TransactionConfig = { |
| 70 | + timeout: intValue, |
| 71 | + metadata: { key1: 'value1', key2: 2 } |
| 72 | +} |
| 73 | + |
| 74 | +const tx1: Observable<RxTransaction> = rxSession.beginTransaction() |
| 75 | +const bookmark: null | string = <null>rxSession.lastBookmark() |
| 76 | + |
| 77 | +const observable1: Observable<number> = rxSession.readTransaction( |
| 78 | + (tx: RxTransaction) => { |
| 79 | + return of(10) |
| 80 | + } |
| 81 | +) |
| 82 | + |
| 83 | +const observable2: Observable<string> = rxSession.readTransaction( |
| 84 | + (tx: RxTransaction) => { |
| 85 | + return of('42') |
| 86 | + } |
| 87 | +) |
| 88 | + |
| 89 | +const observable3: Observable<number> = rxSession.writeTransaction( |
| 90 | + (tx: RxTransaction) => { |
| 91 | + return of(10) |
| 92 | + } |
| 93 | +) |
| 94 | + |
| 95 | +const observable4: Observable<string> = rxSession.writeTransaction( |
| 96 | + (tx: RxTransaction) => { |
| 97 | + return of('42') |
| 98 | + } |
| 99 | +) |
| 100 | + |
| 101 | +const close1: Observable<void> = rxSession.close() |
| 102 | +const close2: Observable<void> = rxSession |
| 103 | + .close() |
| 104 | + .pipe(finalize(() => 'session closed')) |
| 105 | + |
| 106 | +const result1: RxResult = rxSession.run('RETURN 1') |
| 107 | +result1.keys().subscribe(keysObserver) |
| 108 | +result1.records().subscribe(recordsObserver) |
| 109 | +result1 |
| 110 | + .summary() |
| 111 | + .pipe( |
| 112 | + concat(close1), |
| 113 | + catchError(err => close1.pipe(concat(throwError(err)))) |
| 114 | + ) |
| 115 | + .subscribe(summaryObserver) |
| 116 | + |
| 117 | +const result2: RxResult = rxSession.run('RETURN $value', { value: '42' }) |
| 118 | +result2.keys().subscribe(keysObserver) |
| 119 | +result2.records().subscribe(recordsObserver) |
| 120 | +result2 |
| 121 | + .summary() |
| 122 | + .pipe( |
| 123 | + concat(close1), |
| 124 | + catchError(err => close1.pipe(concat(throwError(err)))) |
| 125 | + ) |
| 126 | + .subscribe(summaryObserver) |
| 127 | + |
| 128 | +const result3: RxResult = rxSession.run( |
| 129 | + 'RETURN $value', |
| 130 | + { value: '42' }, |
| 131 | + txConfig1 |
| 132 | +) |
| 133 | +result3.keys().subscribe(keysObserver) |
| 134 | +result3.records().subscribe(recordsObserver) |
| 135 | +result3 |
| 136 | + .summary() |
| 137 | + .pipe( |
| 138 | + concat(close1), |
| 139 | + catchError(err => close1.pipe(concat(throwError(err)))) |
| 140 | + ) |
| 141 | + .subscribe(summaryObserver) |
| 142 | + |
| 143 | +const tx2: Observable<RxTransaction> = rxSession.beginTransaction(txConfig2) |
| 144 | +const observable5: Observable<string> = rxSession.readTransaction( |
| 145 | + (tx: RxTransaction) => of(''), |
| 146 | + txConfig3 |
| 147 | +) |
| 148 | +const observable6: Observable<number> = rxSession.writeTransaction( |
| 149 | + (tx: RxTransaction) => of(42), |
| 150 | + txConfig4 |
| 151 | +) |
0 commit comments