@microsoft/applicationinsights-core-js 2.7.5-nightly.2203-01 → 2.7.5-nightly.2204-03

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (160) hide show
  1. package/browser/applicationinsights-core-js.integrity.json +9 -9
  2. package/browser/applicationinsights-core-js.js +1999 -793
  3. package/browser/applicationinsights-core-js.js.map +1 -1
  4. package/browser/applicationinsights-core-js.min.js +2 -2
  5. package/browser/applicationinsights-core-js.min.js.map +1 -1
  6. package/dist/applicationinsights-core-js.api.json +15679 -8835
  7. package/dist/applicationinsights-core-js.api.md +425 -48
  8. package/dist/applicationinsights-core-js.d.ts +784 -123
  9. package/dist/applicationinsights-core-js.js +1999 -793
  10. package/dist/applicationinsights-core-js.js.map +1 -1
  11. package/dist/applicationinsights-core-js.min.js +2 -2
  12. package/dist/applicationinsights-core-js.min.js.map +1 -1
  13. package/dist/applicationinsights-core-js.rollup.d.ts +784 -123
  14. package/dist-esm/JavaScriptSDK/AppInsightsCore.js +3 -69
  15. package/dist-esm/JavaScriptSDK/AppInsightsCore.js.map +1 -1
  16. package/dist-esm/JavaScriptSDK/BaseCore.js +496 -99
  17. package/dist-esm/JavaScriptSDK/BaseCore.js.map +1 -1
  18. package/dist-esm/JavaScriptSDK/BaseTelemetryPlugin.js +120 -21
  19. package/dist-esm/JavaScriptSDK/BaseTelemetryPlugin.js.map +1 -1
  20. package/dist-esm/JavaScriptSDK/ChannelController.js +202 -87
  21. package/dist-esm/JavaScriptSDK/ChannelController.js.map +1 -1
  22. package/dist-esm/JavaScriptSDK/Constants.js +1 -1
  23. package/dist-esm/JavaScriptSDK/CookieMgr.js +4 -4
  24. package/dist-esm/JavaScriptSDK/CookieMgr.js.map +1 -1
  25. package/dist-esm/JavaScriptSDK/CoreUtils.js +7 -148
  26. package/dist-esm/JavaScriptSDK/CoreUtils.js.map +1 -1
  27. package/dist-esm/JavaScriptSDK/DataCacheHelper.js +93 -0
  28. package/dist-esm/JavaScriptSDK/DataCacheHelper.js.map +1 -0
  29. package/dist-esm/JavaScriptSDK/DbgExtensionUtils.js +1 -1
  30. package/dist-esm/JavaScriptSDK/DiagnosticLogger.js +25 -12
  31. package/dist-esm/JavaScriptSDK/DiagnosticLogger.js.map +1 -1
  32. package/dist-esm/JavaScriptSDK/EnvUtils.js +13 -12
  33. package/dist-esm/JavaScriptSDK/EnvUtils.js.map +1 -1
  34. package/dist-esm/JavaScriptSDK/EventHelpers.js +477 -0
  35. package/dist-esm/JavaScriptSDK/EventHelpers.js.map +1 -0
  36. package/dist-esm/JavaScriptSDK/HelperFuncs.js +89 -86
  37. package/dist-esm/JavaScriptSDK/HelperFuncs.js.map +1 -1
  38. package/dist-esm/JavaScriptSDK/InstrumentHooks.js +3 -1
  39. package/dist-esm/JavaScriptSDK/InstrumentHooks.js.map +1 -1
  40. package/dist-esm/JavaScriptSDK/InternalConstants.js +28 -0
  41. package/dist-esm/JavaScriptSDK/InternalConstants.js.map +1 -0
  42. package/dist-esm/JavaScriptSDK/NotificationManager.js +37 -42
  43. package/dist-esm/JavaScriptSDK/NotificationManager.js.map +1 -1
  44. package/dist-esm/JavaScriptSDK/PerfManager.js +1 -1
  45. package/dist-esm/JavaScriptSDK/ProcessTelemetryContext.js +430 -119
  46. package/dist-esm/JavaScriptSDK/ProcessTelemetryContext.js.map +1 -1
  47. package/dist-esm/JavaScriptSDK/RandomHelper.js +30 -4
  48. package/dist-esm/JavaScriptSDK/RandomHelper.js.map +1 -1
  49. package/dist-esm/JavaScriptSDK/TelemetryHelpers.js +60 -14
  50. package/dist-esm/JavaScriptSDK/TelemetryHelpers.js.map +1 -1
  51. package/dist-esm/JavaScriptSDK/TelemetryInitializerPlugin.js +81 -0
  52. package/dist-esm/JavaScriptSDK/TelemetryInitializerPlugin.js.map +1 -0
  53. package/dist-esm/JavaScriptSDK/UnloadHandlerContainer.js +33 -0
  54. package/dist-esm/JavaScriptSDK/UnloadHandlerContainer.js.map +1 -0
  55. package/dist-esm/JavaScriptSDK.Enums/EventsDiscardedReason.js +1 -1
  56. package/dist-esm/JavaScriptSDK.Enums/LoggingEnums.js +90 -96
  57. package/dist-esm/JavaScriptSDK.Enums/LoggingEnums.js.map +1 -1
  58. package/dist-esm/JavaScriptSDK.Enums/SendRequestReason.js +1 -1
  59. package/dist-esm/JavaScriptSDK.Enums/TelemetryUnloadReason.js +8 -0
  60. package/dist-esm/JavaScriptSDK.Enums/TelemetryUnloadReason.js.map +1 -0
  61. package/dist-esm/JavaScriptSDK.Enums/TelemetryUpdateReason.js +8 -0
  62. package/dist-esm/JavaScriptSDK.Enums/TelemetryUpdateReason.js.map +1 -0
  63. package/dist-esm/JavaScriptSDK.Interfaces/IAppInsightsCore.js +1 -2
  64. package/dist-esm/JavaScriptSDK.Interfaces/IAppInsightsCore.js.map +1 -1
  65. package/dist-esm/JavaScriptSDK.Interfaces/IChannelControls.js +1 -1
  66. package/dist-esm/JavaScriptSDK.Interfaces/IConfiguration.js +1 -1
  67. package/dist-esm/JavaScriptSDK.Interfaces/ICookieMgr.js +1 -1
  68. package/dist-esm/JavaScriptSDK.Interfaces/IDbgExtension.js +1 -1
  69. package/dist-esm/JavaScriptSDK.Interfaces/IDiagnosticLogger.js +1 -1
  70. package/dist-esm/JavaScriptSDK.Interfaces/IInstrumentHooks.js +1 -1
  71. package/dist-esm/JavaScriptSDK.Interfaces/INotificationListener.js +1 -1
  72. package/dist-esm/JavaScriptSDK.Interfaces/INotificationManager.js +1 -1
  73. package/dist-esm/JavaScriptSDK.Interfaces/IPerfEvent.js +1 -1
  74. package/dist-esm/JavaScriptSDK.Interfaces/IPerfManager.js +1 -1
  75. package/dist-esm/JavaScriptSDK.Interfaces/IProcessTelemetryContext.js +1 -1
  76. package/dist-esm/JavaScriptSDK.Interfaces/ITelemetryInitializers.js +6 -0
  77. package/dist-esm/JavaScriptSDK.Interfaces/ITelemetryInitializers.js.map +1 -0
  78. package/dist-esm/JavaScriptSDK.Interfaces/ITelemetryItem.js +1 -1
  79. package/dist-esm/JavaScriptSDK.Interfaces/ITelemetryPlugin.js +1 -1
  80. package/dist-esm/JavaScriptSDK.Interfaces/ITelemetryPluginChain.js +1 -2
  81. package/dist-esm/JavaScriptSDK.Interfaces/ITelemetryPluginChain.js.map +1 -1
  82. package/dist-esm/JavaScriptSDK.Interfaces/ITelemetryUnloadState.js +8 -0
  83. package/dist-esm/JavaScriptSDK.Interfaces/ITelemetryUnloadState.js.map +1 -0
  84. package/dist-esm/JavaScriptSDK.Interfaces/ITelemetryUpdateState.js +8 -0
  85. package/dist-esm/JavaScriptSDK.Interfaces/ITelemetryUpdateState.js.map +1 -0
  86. package/dist-esm/JavaScriptSDK.Interfaces/IUnloadableComponent.js +6 -0
  87. package/dist-esm/JavaScriptSDK.Interfaces/IUnloadableComponent.js.map +1 -0
  88. package/dist-esm/applicationinsights-core-js.js +12 -7
  89. package/dist-esm/applicationinsights-core-js.js.map +1 -1
  90. package/package.json +2 -2
  91. package/src/JavaScriptSDK/AppInsightsCore.ts +2 -101
  92. package/src/JavaScriptSDK/BaseCore.ts +696 -120
  93. package/src/JavaScriptSDK/BaseTelemetryPlugin.ts +205 -31
  94. package/src/JavaScriptSDK/ChannelController.ts +242 -103
  95. package/src/JavaScriptSDK/CookieMgr.ts +7 -5
  96. package/src/JavaScriptSDK/CoreUtils.ts +8 -172
  97. package/src/JavaScriptSDK/DataCacheHelper.ts +106 -0
  98. package/src/JavaScriptSDK/DiagnosticLogger.ts +27 -12
  99. package/src/JavaScriptSDK/EnvUtils.ts +13 -12
  100. package/src/JavaScriptSDK/EventHelpers.ts +550 -0
  101. package/src/JavaScriptSDK/HelperFuncs.ts +123 -97
  102. package/src/JavaScriptSDK/InstrumentHooks.ts +4 -2
  103. package/src/JavaScriptSDK/InternalConstants.ts +26 -0
  104. package/src/JavaScriptSDK/NotificationManager.ts +34 -37
  105. package/src/JavaScriptSDK/PerfManager.ts +4 -7
  106. package/src/JavaScriptSDK/ProcessTelemetryContext.ts +543 -134
  107. package/src/JavaScriptSDK/RandomHelper.ts +35 -3
  108. package/src/JavaScriptSDK/TelemetryHelpers.ts +83 -21
  109. package/src/JavaScriptSDK/TelemetryInitializerPlugin.ts +111 -0
  110. package/src/JavaScriptSDK/UnloadHandlerContainer.ts +46 -0
  111. package/src/JavaScriptSDK.Enums/LoggingEnums.ts +184 -87
  112. package/src/JavaScriptSDK.Enums/SendRequestReason.ts +5 -0
  113. package/src/JavaScriptSDK.Enums/TelemetryUnloadReason.ts +27 -0
  114. package/src/JavaScriptSDK.Enums/TelemetryUpdateReason.ts +27 -0
  115. package/src/JavaScriptSDK.Interfaces/IAppInsightsCore.ts +82 -2
  116. package/src/JavaScriptSDK.Interfaces/IChannelControls.ts +16 -5
  117. package/src/JavaScriptSDK.Interfaces/IInstrumentHooks.ts +16 -6
  118. package/src/JavaScriptSDK.Interfaces/IProcessTelemetryContext.ts +94 -14
  119. package/src/JavaScriptSDK.Interfaces/ITelemetryInitializers.ts +16 -0
  120. package/src/JavaScriptSDK.Interfaces/ITelemetryPlugin.ts +28 -10
  121. package/src/JavaScriptSDK.Interfaces/ITelemetryPluginChain.ts +9 -11
  122. package/src/JavaScriptSDK.Interfaces/ITelemetryUnloadState.ts +10 -0
  123. package/src/JavaScriptSDK.Interfaces/ITelemetryUpdateState.ts +34 -0
  124. package/src/JavaScriptSDK.Interfaces/IUnloadableComponent.ts +17 -0
  125. package/types/JavaScriptSDK/AppInsightsCore.d.ts +0 -21
  126. package/types/JavaScriptSDK/BaseCore.d.ts +81 -3
  127. package/types/JavaScriptSDK/BaseTelemetryPlugin.d.ts +53 -10
  128. package/types/JavaScriptSDK/ChannelController.d.ts +16 -17
  129. package/types/JavaScriptSDK/CoreUtils.d.ts +1 -48
  130. package/types/JavaScriptSDK/DataCacheHelper.d.ts +13 -0
  131. package/types/JavaScriptSDK/DiagnosticLogger.d.ts +10 -0
  132. package/types/JavaScriptSDK/EventHelpers.d.ts +154 -0
  133. package/types/JavaScriptSDK/HelperFuncs.d.ts +33 -32
  134. package/types/JavaScriptSDK/InternalConstants.d.ts +20 -0
  135. package/types/JavaScriptSDK/PerfManager.d.ts +1 -1
  136. package/types/JavaScriptSDK/ProcessTelemetryContext.d.ts +69 -6
  137. package/types/JavaScriptSDK/RandomHelper.d.ts +6 -0
  138. package/types/JavaScriptSDK/TelemetryHelpers.d.ts +23 -3
  139. package/types/JavaScriptSDK/TelemetryInitializerPlugin.d.ts +16 -0
  140. package/types/JavaScriptSDK/UnloadHandlerContainer.d.ts +11 -0
  141. package/types/JavaScriptSDK.Enums/LoggingEnums.d.ts +91 -2
  142. package/types/JavaScriptSDK.Enums/SendRequestReason.d.ts +4 -0
  143. package/types/JavaScriptSDK.Enums/TelemetryUnloadReason.d.ts +21 -0
  144. package/types/JavaScriptSDK.Enums/TelemetryUpdateReason.d.ts +20 -0
  145. package/types/JavaScriptSDK.Interfaces/IAppInsightsCore.d.ts +72 -1
  146. package/types/JavaScriptSDK.Interfaces/IChannelControls.d.ts +16 -5
  147. package/types/JavaScriptSDK.Interfaces/IInstrumentHooks.d.ts +9 -0
  148. package/types/JavaScriptSDK.Interfaces/IProcessTelemetryContext.d.ts +82 -11
  149. package/types/JavaScriptSDK.Interfaces/ITelemetryInitializers.d.ts +13 -0
  150. package/types/JavaScriptSDK.Interfaces/ITelemetryPlugin.d.ts +25 -8
  151. package/types/JavaScriptSDK.Interfaces/ITelemetryPluginChain.d.ts +9 -10
  152. package/types/JavaScriptSDK.Interfaces/ITelemetryUnloadState.d.ts +6 -0
  153. package/types/JavaScriptSDK.Interfaces/ITelemetryUpdateState.d.ts +22 -0
  154. package/types/JavaScriptSDK.Interfaces/IUnloadableComponent.d.ts +13 -0
  155. package/types/applicationinsights-core-js.d.ts +18 -9
  156. package/types/tsdoc-metadata.json +1 -1
  157. package/dist-esm/JavaScriptSDK/TelemetryPluginChain.js +0 -76
  158. package/dist-esm/JavaScriptSDK/TelemetryPluginChain.js.map +0 -1
  159. package/src/JavaScriptSDK/TelemetryPluginChain.ts +0 -120
  160. package/types/JavaScriptSDK/TelemetryPluginChain.d.ts +0 -32
@@ -1,135 +1,274 @@
1
1
  // Copyright (c) Microsoft Corporation. All rights reserved.
2
- // Licensed under the MIT License.
3
- "use strict";
2
+ // // Licensed under the MIT License.
4
3
 
5
- import dynamicProto from "@microsoft/dynamicproto-js";
6
- import { IAppInsightsCore } from "../JavaScriptSDK.Interfaces/IAppInsightsCore"
7
- import { IConfiguration } from "../JavaScriptSDK.Interfaces/IConfiguration";
4
+ import { SendRequestReason } from "../JavaScriptSDK.Enums/SendRequestReason";
5
+ import { TelemetryUnloadReason } from "../JavaScriptSDK.Enums/TelemetryUnloadReason";
6
+ import { TelemetryUpdateReason } from "../JavaScriptSDK.Enums/TelemetryUpdateReason";
7
+ import { IAppInsightsCore } from "../JavaScriptSDK.Interfaces/IAppInsightsCore";
8
8
  import { IChannelControls } from "../JavaScriptSDK.Interfaces/IChannelControls";
9
- import { IPlugin, ITelemetryPlugin } from "../JavaScriptSDK.Interfaces/ITelemetryPlugin";
10
- import { ITelemetryPluginChain } from "../JavaScriptSDK.Interfaces/ITelemetryPluginChain";
9
+ import { IConfiguration } from "../JavaScriptSDK.Interfaces/IConfiguration";
10
+ import { IBaseProcessingContext, IProcessTelemetryContext, IProcessTelemetryUnloadContext, IProcessTelemetryUpdateContext } from "../JavaScriptSDK.Interfaces/IProcessTelemetryContext";
11
11
  import { ITelemetryItem } from "../JavaScriptSDK.Interfaces/ITelemetryItem";
12
- import { IProcessTelemetryContext } from "../JavaScriptSDK.Interfaces/IProcessTelemetryContext";
13
- import { _InternalLogMessage } from "./DiagnosticLogger";
14
- import { BaseTelemetryPlugin } from "./BaseTelemetryPlugin";
15
- import { ProcessTelemetryContext } from "./ProcessTelemetryContext";
12
+ import { IPlugin } from "../JavaScriptSDK.Interfaces/ITelemetryPlugin";
13
+ import { ITelemetryPluginChain } from "../JavaScriptSDK.Interfaces/ITelemetryPluginChain";
14
+ import { ITelemetryUnloadState } from "../JavaScriptSDK.Interfaces/ITelemetryUnloadState";
15
+ import { ITelemetryUpdateState } from "../JavaScriptSDK.Interfaces/ITelemetryUpdateState";
16
+ import { arrForEach, isArray, objFreeze, throwError } from "./HelperFuncs";
17
+ import { strPause, strProcessNext, strResume, strTeardown } from "./InternalConstants";
18
+ import { createProcessTelemetryContext, createTelemetryProxyChain } from "./ProcessTelemetryContext";
16
19
  import { initializePlugins } from "./TelemetryHelpers";
17
- import { arrForEach, objDefineAccessors, throwError } from "./HelperFuncs";
18
20
 
19
- const ChannelControllerPriority = 500;
20
- const ChannelValidationMessage = "Channel has invalid priority";
21
+ export const ChannelControllerPriority = 500;
22
+ const ChannelValidationMessage = "Channel has invalid priority - ";
21
23
 
22
- export class ChannelController extends BaseTelemetryPlugin {
24
+ export interface IChannelController extends IChannelControls {
25
+ flush(isAsync: boolean, callBack: (flushComplete?: boolean) => void, sendReason: SendRequestReason, cbTimeout?: number): void;
23
26
 
24
- identifier: string = "ChannelControllerPlugin";
25
- priority: number = ChannelControllerPriority; // in reserved range 100 to 200
27
+ getChannel<T extends IPlugin = IPlugin>(pluginIdentifier: string): T;
28
+ }
26
29
 
27
- public setNextPlugin: (next: ITelemetryPlugin | ITelemetryPluginChain) => void;
30
+ export interface IInternalChannelController extends IChannelController {
31
+ _setQueue: (channels: _IInternalChannels[]) => void;
32
+ }
28
33
 
29
- constructor() {
30
- super();
31
- let _channelQueue: IChannelControls[][];
34
+ export interface _IInternalChannels {
35
+ queue: IChannelControls[];
36
+ chain: ITelemetryPluginChain;
37
+ }
32
38
 
33
- dynamicProto(ChannelController, this, (_self, _base) => {
34
- _self.setNextPlugin = (next: ITelemetryPlugin | ITelemetryPluginChain) => {
35
- // The Channel controller is last in pipeline
36
- };
39
+ function _addChannelQueue(channelQueue: _IInternalChannels[], queue: IChannelControls[], config: IConfiguration, core: IAppInsightsCore) {
40
+ if (queue && isArray(queue) && queue.length > 0) {
41
+ queue = queue.sort((a, b) => { // sort based on priority within each queue
42
+ return a.priority - b.priority;
43
+ });
37
44
 
38
- _self.processTelemetry = (item: ITelemetryItem, itemCtx: IProcessTelemetryContext) => {
39
- if (_channelQueue) {
40
- arrForEach(_channelQueue, queues => {
41
- // pass on to first item in queue
42
- if (queues.length > 0) {
43
- // Copying the item context as we could have mutiple chains that are executing asynchronously
44
- // and calling _getDefTelCtx as it's possible that the caller doesn't pass any context
45
- let chainCtx = this._getTelCtx(itemCtx).createNew(queues);
46
- chainCtx.processNext(item);
47
- }
48
- });
49
- }
50
- };
51
-
52
- _self.getChannelControls = (): IChannelControls[][] => {
53
- return _channelQueue;
54
- };
55
-
56
- _self.initialize = (config: IConfiguration, core: IAppInsightsCore, extensions: IPlugin[]) => {
57
- if (_self.isInitialized()) {
58
- // already initialized
59
- return;
60
- }
45
+ arrForEach(queue, queueItem => {
46
+ if (queueItem.priority < ChannelControllerPriority) {
47
+ throwError(ChannelValidationMessage + queueItem.identifier);
48
+ }
49
+ });
50
+
51
+ channelQueue.push({
52
+ queue: objFreeze(queue),
53
+ chain: createTelemetryProxyChain(queue, config, core)
54
+ });
55
+ }
56
+ }
57
+
58
+ export function createChannelControllerPlugin(channelQueue: _IInternalChannels[], core: IAppInsightsCore): IChannelController {
59
+
60
+ function _getTelCtx() {
61
+ return createProcessTelemetryContext(null, core.config, core, null)
62
+ }
61
63
 
62
- _base.initialize(config, core, extensions);
64
+ function _processChannelQueue<T extends IBaseProcessingContext>(theChannels: _IInternalChannels[], itemCtx: T, processFn: (chainCtx: T) => void, onComplete: (() => void) | null) {
65
+ let waiting = theChannels ? (theChannels.length + 1) : 1;
63
66
 
64
- _createChannelQueues((config||{}).channels, extensions);
65
-
66
- // Initialize the Queues
67
- arrForEach(_channelQueue, queue => initializePlugins(new ProcessTelemetryContext(queue, config, core), extensions));
67
+ function _runChainOnComplete() {
68
+ waiting --;
69
+
70
+ if (waiting === 0) {
71
+ onComplete && onComplete();
72
+ onComplete = null;
68
73
  }
74
+ }
75
+
76
+ if (waiting > 0) {
77
+ arrForEach(theChannels, (channels) => {
78
+ // pass on to first item in queue
79
+ if (channels && channels.queue.length > 0) {
80
+ let channelChain = channels.chain;
81
+ let chainCtx = itemCtx.createNew(channelChain) as T;
82
+ chainCtx.onComplete(_runChainOnComplete);
83
+
84
+ // Cause this chain to start processing
85
+ processFn(chainCtx);
86
+ } else {
87
+ waiting --;
88
+ }
89
+ });
90
+ }
91
+
92
+ _runChainOnComplete();
93
+ }
94
+
95
+ function _doUpdate(updateCtx: IProcessTelemetryUpdateContext, updateState: ITelemetryUpdateState) {
96
+ let theUpdateState: ITelemetryUpdateState = updateState || {
97
+ reason: TelemetryUpdateReason.Unknown
98
+ };
99
+
100
+ _processChannelQueue(channelQueue, updateCtx, (chainCtx: IProcessTelemetryUpdateContext) => {
101
+ chainCtx[strProcessNext](theUpdateState);
102
+ }, () => {
103
+ updateCtx[strProcessNext](theUpdateState);
104
+ });
105
+
106
+ return true;
107
+ }
108
+
109
+ function _doTeardown(unloadCtx: IProcessTelemetryUnloadContext, unloadState: ITelemetryUnloadState) {
110
+ let theUnloadState: ITelemetryUnloadState = unloadState || {
111
+ reason: TelemetryUnloadReason.ManualTeardown,
112
+ isAsync: false
113
+ };
114
+
115
+ _processChannelQueue(channelQueue, unloadCtx, (chainCtx: IProcessTelemetryUnloadContext) => {
116
+ chainCtx[strProcessNext](theUnloadState);
117
+ }, () => {
118
+ unloadCtx[strProcessNext](theUnloadState);
119
+ isInitialized = false;
69
120
  });
70
121
 
71
- function _checkQueuePriority(queue:IChannelControls[]) {
72
- arrForEach(queue, queueItem => {
73
- if (queueItem.priority < ChannelControllerPriority) {
74
- throwError(ChannelValidationMessage + queueItem.identifier);
122
+ return true;
123
+ }
124
+
125
+ function _getChannel<T extends IPlugin = IPlugin>(pluginIdentifier: string): T {
126
+ let thePlugin: T = null;
127
+
128
+ if (channelQueue && channelQueue.length > 0) {
129
+ arrForEach(channelQueue, (channels) => {
130
+ // pass on to first item in queue
131
+ if (channels && channels.queue.length > 0) {
132
+ arrForEach(channels.queue, (ext: any) => {
133
+ if (ext.identifier === pluginIdentifier) {
134
+ thePlugin = ext;
135
+ // Cause arrForEach to stop iterating
136
+ return -1;
137
+ }
138
+ });
139
+
140
+ if (thePlugin) {
141
+ // Cause arrForEach to stop iterating
142
+ return -1;
143
+ }
75
144
  }
76
145
  });
77
146
  }
78
-
79
- function _addChannelQueue(queue:IChannelControls[]) {
80
- if (queue && queue.length > 0) {
81
- queue = queue.sort((a, b) => { // sort based on priority within each queue
82
- return a.priority - b.priority;
147
+
148
+ return thePlugin;
149
+ }
150
+
151
+ let isInitialized = false;
152
+ let channelController: IInternalChannelController = {
153
+ identifier: "ChannelControllerPlugin",
154
+ priority: ChannelControllerPriority,
155
+ initialize: (config: IConfiguration, core: IAppInsightsCore, extensions: IPlugin[], pluginChain?: ITelemetryPluginChain) => {
156
+ isInitialized = true;
157
+ arrForEach(channelQueue, (channels) => {
158
+ if (channels && channels.queue.length > 0) {
159
+ initializePlugins(createProcessTelemetryContext(channels.chain, config, core), extensions);
160
+ }
161
+ });
162
+ },
163
+ isInitialized: () => {
164
+ return isInitialized
165
+ },
166
+ processTelemetry: (item: ITelemetryItem, itemCtx: IProcessTelemetryContext) => {
167
+ _processChannelQueue(channelQueue, itemCtx || _getTelCtx(), (chainCtx: IProcessTelemetryContext) => {
168
+ chainCtx[strProcessNext](item);
169
+ }, () => {
170
+ itemCtx[strProcessNext](item);
171
+ });
172
+ },
173
+ update: _doUpdate,
174
+ [strPause]: () => {
175
+ _processChannelQueue(channelQueue, _getTelCtx(), (chainCtx: IProcessTelemetryContext) => {
176
+ chainCtx.iterate<IChannelControls>((plugin) => {
177
+ plugin[strPause] && plugin[strPause]();
83
178
  });
84
-
85
- _checkQueuePriority(queue);
86
- _channelQueue.push(queue);
87
- }
88
- }
89
-
90
- function _createChannelQueues(channels:IChannelControls[][], extensions: IPlugin[]) {
91
- _channelQueue = [];
92
-
93
- if (channels) {
94
- // Add and sort the configuration channel queues
95
- arrForEach(channels, queue => _addChannelQueue(queue));
179
+ }, null);
180
+ },
181
+ [strResume]: () => {
182
+ _processChannelQueue(channelQueue, _getTelCtx(), (chainCtx: IProcessTelemetryContext) => {
183
+ chainCtx.iterate<IChannelControls>((plugin) => {
184
+ plugin[strResume] && plugin[strResume]();
185
+ });
186
+ }, null);
187
+ },
188
+ [strTeardown]: _doTeardown,
189
+ getChannel: _getChannel,
190
+ flush: (isAsync: boolean, callBack: (flushComplete?: boolean) => void, sendReason: SendRequestReason, cbTimeout?: number) => {
191
+ // Setting waiting to one so that we don't call the callBack until we finish iterating
192
+ let waiting = 1;
193
+ let doneIterating = false;
194
+ let cbTimer: any = null;
195
+
196
+ cbTimeout = cbTimeout || 5000;
197
+
198
+ function doCallback() {
199
+ waiting--;
200
+ if (doneIterating && waiting === 0) {
201
+ if (cbTimer) {
202
+ clearTimeout(cbTimer);
203
+ cbTimer = null;
204
+ }
205
+
206
+ callBack && callBack(doneIterating);
207
+ callBack = null;
208
+ }
96
209
  }
97
-
98
- if (extensions) {
99
- // Create a new channel queue for any extensions with a priority > the ChannelControllerPriority
100
- let extensionQueue:IChannelControls[] = [];
101
- arrForEach(extensions as IChannelControls[], plugin => {
102
- if (plugin.priority > ChannelControllerPriority) {
103
- extensionQueue.push(plugin);
210
+
211
+ _processChannelQueue(channelQueue, _getTelCtx(), (chainCtx: IProcessTelemetryContext) => {
212
+ chainCtx.iterate<IChannelControls>((plugin) => {
213
+ if (plugin.flush) {
214
+ waiting ++;
215
+
216
+ let handled = false;
217
+ // Not all channels will call this callback for every scenario
218
+ if (!plugin.flush(isAsync, () => {
219
+ handled = true;
220
+ doCallback();
221
+ }, sendReason)) {
222
+ if (!handled) {
223
+ // If any channel doesn't return true and it didn't call the callback, then we should assume that the callback
224
+ // will never be called, so use a timeout to allow the channel(s) some time to "finish" before triggering any
225
+ // followup function (such as unloading)
226
+ if (isAsync && cbTimer == null) {
227
+ cbTimer = setTimeout(() => {
228
+ cbTimer = null;
229
+ doCallback();
230
+ }, cbTimeout);
231
+ } else {
232
+ doCallback();
233
+ }
234
+ }
235
+ }
104
236
  }
105
237
  });
106
-
107
- _addChannelQueue(extensionQueue);
108
- }
238
+ }, () => {
239
+ doneIterating = true;
240
+ doCallback();
241
+ });
242
+
243
+ return true;
244
+ },
245
+ _setQueue: (queue: _IInternalChannels[]) => {
246
+ channelQueue = queue;
109
247
  }
110
- }
248
+ };
111
249
 
112
- public processTelemetry(item: ITelemetryItem, itemCtx: IProcessTelemetryContext) {
113
- // @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
114
- }
250
+ return channelController;
251
+ }
252
+
253
+ export function createChannelQueues(channels: IChannelControls[][], extensions: IPlugin[], config: IConfiguration, core: IAppInsightsCore) {
254
+ let channelQueue: _IInternalChannels[] = [];
115
255
 
116
- public getChannelControls(): IChannelControls[][] {
117
- // @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
118
- return null;
256
+ if (channels) {
257
+ // Add and sort the configuration channel queues
258
+ arrForEach(channels, queue => _addChannelQueue(channelQueue, queue, config, core));
119
259
  }
120
260
 
121
- public initialize(config: IConfiguration, core: IAppInsightsCore, extensions: IPlugin[]) {
122
- // @DynamicProtoStub -- DO NOT add any code as this will be removed during packaging
261
+ if (extensions) {
262
+ // Create a new channel queue for any extensions with a priority > the ChannelControllerPriority
263
+ let extensionQueue: IChannelControls[] = [];
264
+ arrForEach(extensions as IChannelControls[], plugin => {
265
+ if (plugin.priority > ChannelControllerPriority) {
266
+ extensionQueue.push(plugin);
267
+ }
268
+ });
269
+
270
+ _addChannelQueue(channelQueue, extensionQueue, config, core);
123
271
  }
124
272
 
125
- /**
126
- * Static constructor, attempt to create accessors
127
- */
128
- // tslint:disable-next-line
129
- private static _staticInit = (() => {
130
- let proto = ChannelController.prototype;
131
- // Dynamically create get/set property accessors
132
- objDefineAccessors(proto, "ChannelControls", proto.getChannelControls);
133
- objDefineAccessors(proto, "channelQueue", proto.getChannelControls);
134
- })();
273
+ return channelQueue;
135
274
  }
@@ -2,7 +2,7 @@
2
2
  // Licensed under the MIT License.
3
3
  import { IDiagnosticLogger } from "../JavaScriptSDK.Interfaces/IDiagnosticLogger";
4
4
  import { ICookieMgr, ICookieMgrConfig } from "../JavaScriptSDK.Interfaces/ICookieMgr";
5
- import { _InternalMessageId, LoggingSeverity } from "../JavaScriptSDK.Enums/LoggingEnums";
5
+ import { _eInternalMessageId, eLoggingSeverity } from "../JavaScriptSDK.Enums/LoggingEnums";
6
6
  import { dumpObj, getDocument, getLocation, getNavigator, isIE } from "./EnvUtils";
7
7
  import {
8
8
  arrForEach, dateNow, getExceptionName, isFunction, isNotNullOrUndefined, isNullOrUndefined, isString, isTruthy, isUndefined,
@@ -10,6 +10,8 @@ import {
10
10
  } from "./HelperFuncs";
11
11
  import { IConfiguration } from "../JavaScriptSDK.Interfaces/IConfiguration";
12
12
  import { IAppInsightsCore } from "../JavaScriptSDK.Interfaces/IAppInsightsCore";
13
+ import { strEmpty } from "./InternalConstants";
14
+ import { _throwInternal } from "./DiagnosticLogger";
13
15
 
14
16
  const strToGMTString = "toGMTString";
15
17
  const strToUTCString = "toUTCString";
@@ -19,7 +21,6 @@ const strEnabled = "enabled";
19
21
  const strIsCookieUseDisabled = "isCookieUseDisabled";
20
22
  const strDisableCookiesUsage = "disableCookiesUsage";
21
23
  const strConfigCookieMgr = "_ckMgr";
22
- const strEmpty = "";
23
24
 
24
25
  let _supportsCookies: boolean = null;
25
26
  let _allowUaSameSite: boolean = null;
@@ -252,9 +253,10 @@ export function areCookiesSupported(logger?: IDiagnosticLogger): any {
252
253
  let doc = _doc || {} as Document;
253
254
  _supportsCookies = doc[strCookie] !== undefined;
254
255
  } catch (e) {
255
- logger && logger.throwInternal(
256
- LoggingSeverity.WARNING,
257
- _InternalMessageId.CannotAccessCookie,
256
+ _throwInternal(
257
+ logger,
258
+ eLoggingSeverity.WARNING,
259
+ _eInternalMessageId.CannotAccessCookie,
258
260
  "Cannot access document.cookie - " + getExceptionName(e),
259
261
  { exception: dumpObj(e) });
260
262
  }
@@ -6,158 +6,21 @@ import { IConfiguration } from "../JavaScriptSDK.Interfaces/IConfiguration";
6
6
  import { ICookieMgr } from "../JavaScriptSDK.Interfaces/ICookieMgr";
7
7
  import { IDiagnosticLogger } from "../JavaScriptSDK.Interfaces/IDiagnosticLogger";
8
8
  import { _gblCookieMgr } from "./CookieMgr";
9
- import { getWindow, getDocument, getPerformance, isIE } from "./EnvUtils";
9
+ import { getPerformance, isIE } from "./EnvUtils";
10
10
  import {
11
- arrForEach, arrIndexOf, arrMap, arrReduce, attachEvent, dateNow, detachEvent, hasOwnProperty,
11
+ arrForEach, arrIndexOf, arrMap, arrReduce, dateNow, hasOwnProperty,
12
12
  isArray, isBoolean, isDate, isError, isFunction, isNullOrUndefined, isNumber, isObject, isString, isTypeof,
13
13
  isUndefined, objDefineAccessors, objKeys, strTrim, toISOString
14
14
  } from "./HelperFuncs";
15
- import { randomValue, random32, mwcRandomSeed, mwcRandom32 } from "./RandomHelper";
16
-
17
- const strVisibilityChangeEvt: string = "visibilitychange";
18
- const strPageHide: string = "pagehide";
19
- const strPageShow: string = "pageshow";
15
+ import { addEventHandler, attachEvent, detachEvent } from "./EventHelpers";
16
+ import { randomValue, random32, mwcRandomSeed, mwcRandom32, newId } from "./RandomHelper";
17
+ import { strEmpty } from "./InternalConstants";
20
18
 
21
19
  let _cookieMgrs: ICookieMgr[] = null;
22
20
  let _canUseCookies: boolean; // legacy supported config
23
21
 
24
22
  // Added to help with minfication
25
23
  export const Undefined = strShimUndefined;
26
-
27
- /**
28
- * Trys to add an event handler for the specified event to the window, body and document
29
- * @param eventName {string} - The name of the event
30
- * @param callback {any} - The callback function that needs to be executed for the given event
31
- * @return {boolean} - true if the handler was successfully added
32
- */
33
- export function addEventHandler(eventName: string, callback: any): boolean {
34
- let result = false;
35
- let w = getWindow();
36
- if (w) {
37
- result = attachEvent(w, eventName, callback);
38
- result = attachEvent(w["body"], eventName, callback) || result;
39
- }
40
-
41
- let doc = getDocument();
42
- if (doc) {
43
- result = attachEvent(doc, eventName, callback) || result;
44
- }
45
-
46
- return result;
47
- }
48
-
49
- /**
50
- * Bind the listener to the array of events
51
- * @param events An string array of event names to bind the listener to
52
- * @param listener The event callback to call when the event is triggered
53
- * @param excludeEvents - [Optional] An array of events that should not be hooked (if possible), unless no other events can be.
54
- * @returns true - when at least one of the events was registered otherwise false
55
- */
56
- export function addEventListeners(events: string[], listener: any, excludeEvents?: string[]): boolean {
57
- let added = false;
58
-
59
- if (listener && events && isArray(events)) {
60
- let excluded: string[] = [];
61
- arrForEach(events, (name) => {
62
- if (isString(name)) {
63
- if (!excludeEvents || arrIndexOf(excludeEvents, name) === -1) {
64
- added = addEventHandler(name, listener) || added;
65
- } else {
66
- excluded.push(name);
67
- }
68
- }
69
- });
70
-
71
- if (!added && excluded.length > 0) {
72
- // Failed to add any listeners and we excluded some, so just attempt to add the excluded events
73
- added = addEventListeners(excluded, listener);
74
- }
75
- }
76
-
77
- return added;
78
- }
79
-
80
- /**
81
- * Listen to the 'beforeunload', 'unload' and 'pagehide' events which indicates a page unload is occurring,
82
- * this does NOT listen to the 'visibilitychange' event as while it does indicate that the page is being hidden
83
- * it does not *necessarily* mean that the page is being completely unloaded, it can mean that the user is
84
- * just navigating to a different Tab and may come back (without unloading the page). As such you may also
85
- * need to listen to the 'addPageHideEventListener' and 'addPageShowEventListener' events.
86
- * @param listener - The event callback to call when a page unload event is triggered
87
- * @param excludeEvents - [Optional] An array of events that should not be hooked, unless no other events can be.
88
- * @returns true - when at least one of the events was registered otherwise false
89
- */
90
- export function addPageUnloadEventListener(listener: any, excludeEvents?: string[]): boolean {
91
- // Hook the unload event for the document, window and body to ensure that the client events are flushed to the server
92
- // As just hooking the window does not always fire (on chrome) for page navigation's.
93
- return addEventListeners(["beforeunload", "unload", "pagehide"], listener, excludeEvents);
94
- }
95
-
96
- /**
97
- * Listen to the pagehide and visibility changing to 'hidden' events
98
- * @param listener - The event callback to call when a page hide event is triggered
99
- * @param excludeEvents - [Optional] An array of events that should not be hooked (if possible), unless no other events can be.
100
- * Suggestion: pass as true if you are also calling addPageUnloadEventListener as that also hooks pagehide
101
- * @returns true - when at least one of the events was registered otherwise false
102
- */
103
- export function addPageHideEventListener(listener: any, excludeEvents?: string[]): boolean {
104
-
105
- function _handlePageVisibility(evt: any) {
106
- let doc = getDocument();
107
- if (listener && doc && doc.visibilityState === "hidden") {
108
- listener(evt);
109
- }
110
- }
111
-
112
- let pageUnloadAdded = false;
113
- if (!excludeEvents || arrIndexOf(excludeEvents, strPageHide) === -1) {
114
- pageUnloadAdded = addEventHandler(strPageHide, listener);
115
- }
116
-
117
- if (!excludeEvents || arrIndexOf(excludeEvents, strVisibilityChangeEvt) === -1) {
118
- pageUnloadAdded = addEventHandler(strVisibilityChangeEvt, _handlePageVisibility) || pageUnloadAdded;
119
- }
120
-
121
- if (!pageUnloadAdded && excludeEvents) {
122
- // Failed to add any listeners and we where requested to exclude some, so just call again without excluding anything
123
- pageUnloadAdded = addPageHideEventListener(listener);
124
- }
125
-
126
- return pageUnloadAdded;
127
- }
128
-
129
- /**
130
- * Listen to the pageshow and visibility changing to 'visible' events
131
- * @param listener - The event callback to call when a page is show event is triggered
132
- * @param excludeEvents - [Optional] An array of events that should not be hooked (if possible), unless no other events can be.
133
- * @returns true - when at least one of the events was registered otherwise false
134
- */
135
- export function addPageShowEventListener(listener: any, excludeEvents?: string[]): boolean {
136
-
137
- function _handlePageVisibility(evt: any) {
138
- let doc = getDocument();
139
- if (listener && doc && doc.visibilityState === "visible") {
140
- listener(evt);
141
- }
142
- }
143
-
144
- let pageShowAdded = false;
145
- if (!excludeEvents || arrIndexOf(excludeEvents, strPageShow) === -1) {
146
- pageShowAdded = addEventHandler(strPageShow, listener);
147
- }
148
-
149
- if (!excludeEvents || arrIndexOf(excludeEvents, strVisibilityChangeEvt) === -1) {
150
- pageShowAdded = addEventHandler(strVisibilityChangeEvt, _handlePageVisibility) || pageShowAdded;
151
- }
152
-
153
- if (!pageShowAdded && excludeEvents) {
154
- // Failed to add any listeners and we where requested to exclude some, so just call again without excluding anything
155
- pageShowAdded = addPageShowEventListener(listener);
156
- }
157
-
158
- return pageShowAdded;
159
- }
160
-
161
24
  export function newGuid(): string {
162
25
  function randomHexDigit() {
163
26
  return randomValue(15); // Get a random value from 0..15
@@ -182,33 +45,6 @@ export function perfNow(): number {
182
45
  return dateNow();
183
46
  }
184
47
 
185
- /**
186
- * Generate random base64 id string.
187
- * The default length is 22 which is 132-bits so almost the same as a GUID but as base64 (the previous default was 5)
188
- * @param maxLength - Optional value to specify the length of the id to be generated, defaults to 22
189
- */
190
- export function newId(maxLength = 22): string {
191
- const base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
192
-
193
- // Start with an initial random number, consuming the value in reverse byte order
194
- let number = random32() >>> 0; // Make sure it's a +ve number
195
- let chars = 0;
196
- let result = "";
197
- while (result.length < maxLength) {
198
- chars ++;
199
- result += base64chars.charAt(number & 0x3F);
200
- number >>>= 6; // Zero fill with right shift
201
- if (chars === 5) {
202
- // 5 base64 characters === 30 bits so we don't have enough bits for another base64 char
203
- // So add on another 30 bits and make sure it's +ve
204
- number = (((random32() << 2) & 0xFFFFFFFF) | (number & 0x03)) >>> 0;
205
- chars = 0; // We need to reset the number every 5 chars (30 bits)
206
- }
207
- }
208
-
209
- return result;
210
- }
211
-
212
48
  /**
213
49
  * The strEndsWith() method determines whether a string ends with the characters of a specified string, returning true or false as appropriate.
214
50
  * @param value - The value to check whether it ends with the search value.
@@ -232,7 +68,7 @@ export function generateW3CId(): string {
232
68
  const hexValues = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"];
233
69
 
234
70
  // rfc4122 version 4 UUID without dashes and with lowercase letters
235
- let oct = "", tmp;
71
+ let oct = strEmpty, tmp;
236
72
  for (let a = 0; a < 4; a++) {
237
73
  tmp = random32();
238
74
  oct +=
@@ -407,7 +243,7 @@ export interface ICoreUtils {
407
243
  * @param callback {any} - The callback function that needs to be executed for the given event
408
244
  * @return {boolean} - true if the handler was successfully added
409
245
  */
410
- addEventHandler: (eventName: string, callback: any) => boolean;
246
+ addEventHandler: (eventName: string, callback: any, evtNamespace?: string | string[]) => boolean;
411
247
 
412
248
  /**
413
249
  * Return the current time via the Date now() function (if available) and falls back to (new Date()).getTime() if now() is unavailable (IE8 or less)
@@ -574,7 +410,7 @@ export const EventHelper: IEventHelper = {
574
410
  * @param logger
575
411
  * @returns
576
412
  */
577
- export function _legacyCookieMgr(config?: IConfiguration, logger?: IDiagnosticLogger): ICookieMgr {
413
+ export function _legacyCookieMgr(config?: IConfiguration, logger?: IDiagnosticLogger): ICookieMgr {
578
414
  let cookieMgr = _gblCookieMgr(config, logger);
579
415
  let legacyCanUseCookies = (CoreUtils as any)._canUseCookies;
580
416