@@ -69,7 +69,26 @@ const getAICore = async (key: string, vscodeAPI: typeof vscode, xhrOverride?: IX
69
69
* @param xhrOverride An optional override to use for requests instead of the XHTMLRequest object. Useful for node environments
70
70
*/
71
71
export const oneDataSystemClientFactory = async ( key : string , vscodeAPI : typeof vscode , xhrOverride ?: IXHROverride ) : Promise < BaseTelemetryClient > => {
72
- const appInsightsCore = await getAICore ( key , vscodeAPI , xhrOverride ) ;
72
+ let appInsightsCore : AppInsightsCore | undefined = await getAICore ( key , vscodeAPI , xhrOverride ) ;
73
+ const flushOneDS = async ( ) => {
74
+ try {
75
+ const flushPromise = new Promise < void > ( ( resolve , reject ) => {
76
+ if ( ! appInsightsCore ) {
77
+ resolve ( ) ;
78
+ return ;
79
+ }
80
+ appInsightsCore . flush ( true , ( completedFlush ) => {
81
+ if ( ! completedFlush ) {
82
+ reject ( "Failed to flush app 1DS!" ) ;
83
+ return ;
84
+ }
85
+ } ) ;
86
+ } ) ;
87
+ return flushPromise ;
88
+ } catch ( e : any ) {
89
+ throw new Error ( "Failed to flush 1DS!\n" + e . message ) ;
90
+ }
91
+ } ;
73
92
// Shape the app insights core from 1DS into a standard format
74
93
const telemetryClient : BaseTelemetryClient = {
75
94
logEvent : ( eventName : string , data ?: SenderData ) => {
@@ -82,28 +101,21 @@ export const oneDataSystemClientFactory = async (key: string, vscodeAPI: typeof
82
101
throw new Error ( "Failed to log event to app insights!\n" + e . message ) ;
83
102
}
84
103
} ,
85
- flush : async ( ) => {
86
- try {
87
- const flushPromise = new Promise < void > ( ( resolve , reject ) => {
88
- if ( ! appInsightsCore ) {
89
- resolve ( ) ;
90
- return ;
91
- }
92
- appInsightsCore . flush ( true , ( completedFlush ) => {
93
- if ( ! completedFlush ) {
94
- reject ( "Failed to flush app 1DS!" ) ;
95
- return ;
96
- }
97
- appInsightsCore . unload ( true , ( ) => {
98
- resolve ( ) ;
99
- return ;
100
- } ) ;
101
- } ) ;
104
+ flush : flushOneDS ,
105
+ dispose : async ( ) => {
106
+ await flushOneDS ( ) ;
107
+ const disposePromise = new Promise < void > ( ( resolve ) => {
108
+ if ( ! appInsightsCore ) {
109
+ resolve ( ) ;
110
+ return ;
111
+ }
112
+ appInsightsCore . unload ( true , ( ) => {
113
+ resolve ( ) ;
114
+ appInsightsCore = undefined ;
115
+ return ;
102
116
} ) ;
103
- return flushPromise ;
104
- } catch ( e : any ) {
105
- throw new Error ( "Failed to flush 1DS!\n" + e . message ) ;
106
- }
117
+ } ) ;
118
+ return disposePromise ;
107
119
}
108
120
} ;
109
121
return telemetryClient ;
0 commit comments