in src/core.js [602:760]
connect.core.initSharedWorker = function (params) {
connect.core.checkNotInitialized();
if (connect.core.initialized) {
return;
}
connect.assertNotNull(params, 'params');
var sharedWorkerUrl = connect.assertNotNull(params.sharedWorkerUrl, 'params.sharedWorkerUrl');
var authToken = connect.assertNotNull(params.authToken, 'params.authToken');
var refreshToken = connect.assertNotNull(params.refreshToken, 'params.refreshToken');
var authTokenExpiration = connect.assertNotNull(params.authTokenExpiration, 'params.authTokenExpiration');
var region = connect.assertNotNull(params.region, 'params.region');
var endpoint = params.endpoint || null;
var authorizeEndpoint = params.authorizeEndpoint;
if (!authorizeEndpoint) {
authorizeEndpoint = connect.core.isLegacyDomain()
? LEGACY_AUTHORIZE_ENDPOINT
: AUTHORIZE_ENDPOINT;
}
var agentAppEndpoint = params.agentAppEndpoint || null;
var authCookieName = params.authCookieName || null;
try {
// Initialize the event bus and agent data providers.
connect.core.eventBus = new connect.EventBus({ logEvents: true });
connect.core.agentDataProvider = new AgentDataProvider(connect.core.getEventBus());
connect.core.mediaFactory = new connect.MediaFactory(params);
// Create the shared worker and upstream conduit.
var worker = new SharedWorker(sharedWorkerUrl, "ConnectSharedWorker");
var conduit = new connect.Conduit("ConnectSharedWorkerConduit",
new connect.PortStream(worker.port),
new connect.WindowIOStream(window, parent));
// Set the global upstream conduit for external use.
connect.core.upstream = conduit;
connect.core.webSocketProvider = new WebSocketProvider();
// Close our port to the shared worker before the window closes.
global.onunload = function () {
conduit.sendUpstream(connect.EventType.CLOSE);
worker.port.close();
};
connect.getLog().scheduleUpstreamLogPush(conduit);
connect.getLog().scheduleDownstreamClientSideLogsPush();
// Bridge all upstream messages into the event bus.
conduit.onAllUpstream(connect.core.getEventBus().bridge());
// Pass all upstream messages (from shared worker) downstream (to CCP consumer).
conduit.onAllUpstream(conduit.passDownstream());
if (connect.isFramed()) {
// Bridge all downstream messages into the event bus.
conduit.onAllDownstream(connect.core.getEventBus().bridge());
// Pass all downstream messages (from CCP consumer) upstream (to shared worker).
conduit.onAllDownstream(conduit.passUpstream());
}
// Send configuration up to the shared worker.
conduit.sendUpstream(connect.EventType.CONFIGURE, {
authToken: authToken,
authTokenExpiration: authTokenExpiration,
endpoint: endpoint,
refreshToken: refreshToken,
region: region,
authorizeEndpoint: authorizeEndpoint,
agentAppEndpoint: agentAppEndpoint,
authCookieName: authCookieName
});
conduit.onUpstream(connect.EventType.ACKNOWLEDGE, function (data) {
connect.getLog().info("Acknowledged by the ConnectSharedWorker!").sendInternalLogToServer();
connect.core.initialized = true;
connect.core.portStreamId = data.id;
this.unsubscribe();
});
// Add all upstream log entries to our own logger.
conduit.onUpstream(connect.EventType.LOG, function (logEntry) {
if (logEntry.loggerId !== connect.getLog().getLoggerId()) {
connect.getLog().addLogEntry(connect.LogEntry.fromObject(logEntry));
}
});
// Get worker logs
conduit.onUpstream(connect.EventType.SERVER_BOUND_INTERNAL_LOG, function (logEntry) {
connect.getLog().sendInternalLogEntryToServer(connect.LogEntry.fromObject(logEntry));
});
// Get outer context logs
conduit.onDownstream(connect.EventType.SERVER_BOUND_INTERNAL_LOG, function (logs) {
if (connect.isFramed() && Array.isArray(logs)) {
logs.forEach(function (log) {
connect.getLog().sendInternalLogEntryToServer(connect.LogEntry.fromObject(log));
});
}
});
// Get log from outer context
conduit.onDownstream(connect.EventType.LOG, function (log) {
if (connect.isFramed() && log.loggerId !== connect.getLog().getLoggerId()) {
connect.getLog().addLogEntry(connect.LogEntry.fromObject(log));
}
});
// Reload the page if the shared worker detects an API auth failure.
conduit.onUpstream(connect.EventType.AUTH_FAIL, function (logEntry) {
location.reload();
});
connect.getLog().info("User Agent: " + navigator.userAgent).sendInternalLogToServer();
connect.getLog().info("isCCPv2: " + true).sendInternalLogToServer();
connect.getLog().info("isFramed: " + connect.isFramed()).sendInternalLogToServer();
connect.core.upstream.onDownstream(connect.EventType.OUTER_CONTEXT_INFO, function (data) {
var streamsVersion = data.streamsVersion;
connect.getLog().info("StreamsJS Version: " + streamsVersion).sendInternalLogToServer();
});
conduit.onUpstream(connect.EventType.UPDATE_CONNECTED_CCPS, function (data) {
connect.getLog().info("Number of connected CCPs updated: " + data.length).sendInternalLogToServer();
connect.numberOfConnectedCCPs = data.length;
});
connect.core.client = new connect.UpstreamConduitClient(conduit);
connect.core.masterClient = new connect.UpstreamConduitMasterClient(conduit);
// Pass the TERMINATE request upstream to the shared worker.
connect.core.getEventBus().subscribe(connect.EventType.TERMINATE,
conduit.passUpstream());
// Refresh the page when we receive the TERMINATED response from the
// shared worker.
connect.core.getEventBus().subscribe(connect.EventType.TERMINATED, function () {
window.location.reload(true);
});
worker.port.start();
conduit.onUpstream(connect.VoiceIdEvents.UPDATE_DOMAIN_ID, function (data) {
if (data && data.domainId) {
connect.core.voiceIdDomainId = data.domainId;
}
});
// try fetching voiceId's domainId once the agent is initialized
connect.agent(function () {
var voiceId = new connect.VoiceId();
voiceId.getDomainId()
.then(function(domainId) {
connect.getLog().info("voiceId domainId successfully fetched at agent initialization: " + domainId).sendInternalLogToServer();
})
.catch(function(err) {
connect.getLog().info("voiceId domainId not fetched at agent initialization").withObject({ err: err }).sendInternalLogToServer();
});
});
// Attempt to get permission to show notifications.
var nm = connect.core.getNotificationManager();
nm.requestPermission();
} catch (e) {
connect.getLog().error("Failed to initialize the API shared worker, we're dead!")
.withException(e).sendInternalLogToServer();
}
};