@internal
(timeoutMS?: number, error?: Error)
| 985 | |
| 986 | /** @internal */ |
| 987 | private async cleanup(timeoutMS?: number, error?: Error) { |
| 988 | this.abortListener?.[kDispose](); |
| 989 | this.isClosed = true; |
| 990 | const timeoutContextForKillCursors = (): CursorTimeoutContext | undefined => { |
| 991 | if (timeoutMS != null) { |
| 992 | this.timeoutContext?.clear(); |
| 993 | return new CursorTimeoutContext( |
| 994 | TimeoutContext.create({ |
| 995 | serverSelectionTimeoutMS: this.client.s.options.serverSelectionTimeoutMS, |
| 996 | timeoutMS |
| 997 | }), |
| 998 | this |
| 999 | ); |
| 1000 | } else { |
| 1001 | return this.timeoutContext?.refreshed(); |
| 1002 | } |
| 1003 | }; |
| 1004 | |
| 1005 | const withEmitClose = async (fn: () => Promise<void>) => { |
| 1006 | try { |
| 1007 | await fn(); |
| 1008 | } finally { |
| 1009 | this.emitClose(); |
| 1010 | } |
| 1011 | }; |
| 1012 | |
| 1013 | const close = async () => { |
| 1014 | // if no session has been defined on the cursor, the cursor was never initialized |
| 1015 | // or the cursor was re-wound and never re-iterated. In either case, we |
| 1016 | // 1. do not need to end the session (there is no session after all) |
| 1017 | // 2. do not need to kill the cursor server-side |
| 1018 | const session = this.cursorSession; |
| 1019 | if (!session) return; |
| 1020 | |
| 1021 | try { |
| 1022 | if ( |
| 1023 | !this.isKilled && |
| 1024 | this.cursorId && |
| 1025 | !this.cursorId.isZero() && |
| 1026 | this.cursorNamespace && |
| 1027 | this.selectedServer && |
| 1028 | !session.hasEnded |
| 1029 | ) { |
| 1030 | this.isKilled = true; |
| 1031 | const cursorId = this.cursorId; |
| 1032 | this.cursorId = Long.ZERO; |
| 1033 | |
| 1034 | await executeOperation( |
| 1035 | this.cursorClient, |
| 1036 | new KillCursorsOperation(cursorId, this.cursorNamespace, this.selectedServer, { |
| 1037 | session |
| 1038 | }), |
| 1039 | timeoutContextForKillCursors() |
| 1040 | ); |
| 1041 | } |
| 1042 | } catch (error) { |
| 1043 | squashError(error); |
| 1044 | } finally { |
no test coverage detected