Skip to content

Commit 809fcfe

Browse files
authored
Merge pull request #546 from Countly/async-multi-fix
Multi instance fix
2 parents fd3716e + f65e1b1 commit 809fcfe

7 files changed

+141
-98
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 24.11.4
2+
3+
- Mitigated an issue where `content` and `feedback` interface methods would not have worked if async methods were used when multi instancing the SDK.
4+
15
## 24.11.3
26

37
- Added support for content resizing (Experimental!)

cypress/e2e/bridged_utils.cy.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function initMain(name, version) {
1515
}
1616

1717
const SDK_NAME = "javascript_native_web";
18-
const SDK_VERSION = "24.11.3";
18+
const SDK_VERSION = "24.11.4";
1919

2020
// tests
2121
describe("Bridged SDK Utilities Tests", () => {

cypress/fixtures/multi_instance.html

+4
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@
124124
Countly.q.push(["YOUR_APP_KEY3", "collect_from_forms"]);
125125
Countly.q.push(["YOUR_APP_KEY3", "collect_from_facebook"]);
126126
Countly.q.push(["YOUR_APP_KEY3", "opt_in"]);
127+
Countly.q.push(["YOUR_APP_KEY3", "feedback.showNPS"]);
128+
Countly.q.push(["YOUR_APP_KEY3", "content.enterContentZone"]);
127129

128130
//initialize fourth instance for another app asynchronously
129131
Countly.q.push(["init", {
@@ -158,6 +160,8 @@
158160
Countly.q.push(["YOUR_APP_KEY4", "collect_from_forms"]);
159161
Countly.q.push(["YOUR_APP_KEY4", "collect_from_facebook"]);
160162
Countly.q.push(["YOUR_APP_KEY4", "opt_in"]);
163+
Countly.q.push(["YOUR_APP_KEY4", "feedback.showNPS"]);
164+
Countly.q.push(["YOUR_APP_KEY4", "content.enterContentZone"]);
161165
</script>
162166
</head>
163167
</html>

lib/countly.js

+69-36
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@
209209
statusCode: "cly_hc_status_code",
210210
errorMessage: "cly_hc_error_message"
211211
});
212-
var SDK_VERSION = "24.11.3";
212+
var SDK_VERSION = "24.11.4";
213213
var SDK_NAME = "javascript_native_web";
214214

215215
// Using this on document.referrer would return an array with 17 elements in it. The 12th element (array[11]) would be the path we are looking for. Others would be things like password and such (use https://regex101.com/ to check more)
@@ -4386,8 +4386,22 @@
43864386
if (e) {
43874387
return;
43884388
}
4389-
_classPrivateFieldGet2(_log, _this).call(_this, logLevelEnums.DEBUG, "sendContentRequest, received content: [" + resp + "]");
4390-
_classPrivateFieldGet2(_displayContent, _this).call(_this, resp);
4389+
if (!resp) {
4390+
_classPrivateFieldGet2(_log, _this).call(_this, logLevelEnums.VERBOSE, "sendContentRequest, no content to display");
4391+
return;
4392+
}
4393+
try {
4394+
var response = JSON.parse(resp);
4395+
} catch (error) {
4396+
// verbose log
4397+
_classPrivateFieldGet2(_log, _this).call(_this, logLevelEnums.VERBOSE, "sendContentRequest, No content to display or an error while parsing content: " + error);
4398+
return;
4399+
}
4400+
if (!response.html || !response.geo) {
4401+
_classPrivateFieldGet2(_log, _this).call(_this, logLevelEnums.VERBOSE, "sendContentRequest, no html content or orientation to display");
4402+
return;
4403+
}
4404+
_classPrivateFieldGet2(_displayContent, _this).call(_this, response);
43914405
clearInterval(_classPrivateFieldGet2(_contentZoneTimer, _this)); // prevent multiple content requests while one is on
43924406
window.addEventListener('message', function (event) {
43934407
_classPrivateFieldGet2(_interpretContentMessage, _this).call(_this, event);
@@ -4399,6 +4413,9 @@
43994413
var width = window.innerWidth;
44004414
var height = window.innerHeight;
44014415
var iframe = document.getElementById(_classPrivateFieldGet2(_contentIframeID, _this));
4416+
if (!iframe) {
4417+
return;
4418+
}
44024419
iframe.contentWindow.postMessage({
44034420
type: 'resize',
44044421
width: width,
@@ -4408,72 +4425,75 @@
44084425
});
44094426
}, true);
44104427
});
4411-
_classPrivateFieldInitSpec(this, _displayContent, function (content) {
4412-
if (!content) {
4413-
_classPrivateFieldGet2(_log, _this).call(_this, logLevelEnums.DEBUG, "displayContent, no content to display");
4414-
return;
4428+
_classPrivateFieldInitSpec(this, _displayContent, function (response) {
4429+
try {
4430+
var iframe = document.createElement("iframe");
4431+
iframe.id = _classPrivateFieldGet2(_contentIframeID, _this);
4432+
iframe.src = response.html;
4433+
iframe.style.position = "absolute";
4434+
var dimensionToUse = response.geo.p;
4435+
var resInfo = _classPrivateFieldGet2(_getResolution, _this).call(_this, true);
4436+
if (resInfo.width >= resInfo.height) {
4437+
dimensionToUse = response.geo.l;
4438+
}
4439+
;
4440+
iframe.style.left = dimensionToUse.x + "px";
4441+
iframe.style.top = dimensionToUse.y + "px";
4442+
iframe.style.width = dimensionToUse.w + "px";
4443+
iframe.style.height = dimensionToUse.h + "px";
4444+
iframe.style.border = "none";
4445+
iframe.style.zIndex = "999999";
4446+
document.body.appendChild(iframe);
4447+
} catch (error) {
4448+
_classPrivateFieldGet2(_log, _this).call(_this, logLevelEnums.ERROR, "displayContent, Error while creating iframe for the content: " + error);
44154449
}
4416-
_classPrivateFieldGet2(_log, _this).call(_this, logLevelEnums.DEBUG, "displayContent, displaying content");
4417-
var response = JSON.parse(content);
4418-
var iframe = document.createElement("iframe");
4419-
iframe.id = _classPrivateFieldGet2(_contentIframeID, _this);
4420-
iframe.src = response.html;
4421-
iframe.style.position = "absolute";
4422-
var dimensionToUse = response.geo.p;
4423-
var resInfo = _classPrivateFieldGet2(_getResolution, _this).call(_this, true);
4424-
if (resInfo.width >= resInfo.height) {
4425-
dimensionToUse = response.geo.l;
4426-
}
4427-
iframe.style.left = dimensionToUse.x + "px";
4428-
iframe.style.top = dimensionToUse.y + "px";
4429-
iframe.style.width = dimensionToUse.w + "px";
4430-
iframe.style.height = dimensionToUse.h + "px";
4431-
iframe.style.border = "none";
4432-
iframe.style.zIndex = "999999";
4433-
document.body.appendChild(iframe);
44344450
});
44354451
_classPrivateFieldInitSpec(this, _interpretContentMessage, function (messageEvent) {
44364452
if (messageEvent.origin !== _this.url) {
4437-
// this.#log(logLevelEnums.ERROR, "sendContentRequest, Received message from invalid origin");
4453+
// this.#log(logLevelEnums.ERROR, "interpretContentMessage, Received message from invalid origin");
44384454
// silent ignore
44394455
return;
44404456
}
4441-
_classPrivateFieldGet2(_log, _this).call(_this, logLevelEnums.DEBUG, "sendContentRequest, Received message from: [" + messageEvent.origin + "] with data: [" + JSON.stringify(messageEvent.data) + "]");
4457+
_classPrivateFieldGet2(_log, _this).call(_this, logLevelEnums.DEBUG, "interpretContentMessage, Received message from: [" + messageEvent.origin + "] with data: [" + JSON.stringify(messageEvent.data) + "]");
44424458
var _messageEvent$data = messageEvent.data,
44434459
close = _messageEvent$data.close,
44444460
link = _messageEvent$data.link,
44454461
event = _messageEvent$data.event,
44464462
resize_me = _messageEvent$data.resize_me;
44474463
if (event) {
4448-
_classPrivateFieldGet2(_log, _this).call(_this, logLevelEnums.DEBUG, "sendContentRequest, Received event");
4464+
_classPrivateFieldGet2(_log, _this).call(_this, logLevelEnums.DEBUG, "interpretContentMessage, Received event");
44494465
if (close === 1) {
4450-
_classPrivateFieldGet2(_log, _this).call(_this, logLevelEnums.DEBUG, "sendContentRequest, Closing content frame for event");
4466+
_classPrivateFieldGet2(_log, _this).call(_this, logLevelEnums.DEBUG, "interpretContentMessage, Closing content frame for event");
44514467
_classPrivateFieldGet2(_closeContentFrame, _this).call(_this);
44524468
}
44534469
if (!Array.isArray(event)) {
44544470
if (_typeof(event) === "object") {
44554471
_readOnlyError("event");
44564472
} else {
4457-
_classPrivateFieldGet2(_log, _this).call(_this, logLevelEnums.ERROR, "sendContentRequest, Invalid event type: [" + _typeof(event) + "]");
4473+
_classPrivateFieldGet2(_log, _this).call(_this, logLevelEnums.ERROR, "interpretContentMessage, Invalid event type: [" + _typeof(event) + "]");
44584474
return;
44594475
}
44604476
}
44614477
// event is expected to be an array of events
44624478
for (var i = 0; i < event.length; i++) {
4463-
_classPrivateFieldGet2(_add_cly_events, _this).call(_this, event[i]);
4479+
_classPrivateFieldGet2(_add_cly_events, _this).call(_this, event[i]); // let this method handle the event
44644480
}
44654481
}
44664482
if (link) {
44674483
if (close === 1) {
4468-
_classPrivateFieldGet2(_log, _this).call(_this, logLevelEnums.DEBUG, "sendContentRequest, Closing content frame for link");
4484+
_classPrivateFieldGet2(_log, _this).call(_this, logLevelEnums.DEBUG, "interpretContentMessage, Closing content frame for link");
44694485
_classPrivateFieldGet2(_closeContentFrame, _this).call(_this);
44704486
}
44714487
window.open(link, "_blank");
4472-
_classPrivateFieldGet2(_log, _this).call(_this, logLevelEnums.DEBUG, "sendContentRequest, Opened link in new tab: [".concat(link, "]"));
4488+
_classPrivateFieldGet2(_log, _this).call(_this, logLevelEnums.DEBUG, "interpretContentMessage, Opened link in new tab: [".concat(link, "]"));
44734489
}
44744490
if (resize_me) {
4475-
_classPrivateFieldGet2(_log, _this).call(_this, logLevelEnums.DEBUG, "sendContentRequest, Resizing iframe");
4491+
_classPrivateFieldGet2(_log, _this).call(_this, logLevelEnums.DEBUG, "interpretContentMessage, Resizing iframe");
44764492
var resInfo = _classPrivateFieldGet2(_getResolution, _this).call(_this, true);
4493+
if (!resize_me.l || !resize_me.p || !resize_me.l.x || !resize_me.l.y || !resize_me.l.w || !resize_me.l.h || !resize_me.p.x || !resize_me.p.y || !resize_me.p.w || !resize_me.p.h) {
4494+
_classPrivateFieldGet2(_log, _this).call(_this, logLevelEnums.ERROR, "interpretContentMessage, Invalid resize object");
4495+
return;
4496+
}
44774497
var dimensionToUse = resize_me.p;
44784498
if (resInfo.width >= resInfo.height) {
44794499
dimensionToUse = resize_me.l;
@@ -4489,10 +4509,11 @@
44894509
}
44904510
});
44914511
_classPrivateFieldInitSpec(this, _closeContentFrame, function () {
4512+
// we might want to remove event listeners here too but with the current implementation, it seems unnecessary
44924513
var iframe = document.getElementById(_classPrivateFieldGet2(_contentIframeID, _this));
44934514
if (iframe) {
44944515
iframe.remove();
4495-
_classPrivateFieldGet2(_log, _this).call(_this, logLevelEnums.DEBUG, "sendContentRequest, removed iframe");
4516+
_classPrivateFieldGet2(_log, _this).call(_this, logLevelEnums.DEBUG, "interpretContentMessage, removed iframe");
44964517
if (_classPrivateFieldGet2(_inContentZone, _this)) {
44974518
// if user did not exit content zone, re-enter
44984519
_classPrivateFieldGet2(_enterContentZoneInternal, _this).call(_this, true);
@@ -4846,11 +4867,23 @@
48464867
}
48474868
if (typeof inst[req[arg]] === "function") {
48484869
inst[req[arg]].apply(inst, req.slice(arg + 1));
4849-
} else if (req[arg].indexOf("userData.") === 0) {
4870+
}
4871+
// Add interfaces you add to here for async queue to work
4872+
else if (req[arg].indexOf("userData.") === 0) {
48504873
var userdata = req[arg].replace("userData.", "");
48514874
if (typeof inst.userData[userdata] === "function") {
48524875
inst.userData[userdata].apply(inst, req.slice(arg + 1));
48534876
}
4877+
} else if (req[arg].indexOf("content.") === 0) {
4878+
var contentMethod = req[arg].replace("content.", "");
4879+
if (typeof inst.content[contentMethod] === "function") {
4880+
inst.content[contentMethod].apply(inst, req.slice(arg + 1));
4881+
}
4882+
} else if (req[arg].indexOf("feedback.") === 0) {
4883+
var feedbackMethod = req[arg].replace("feedback.", "");
4884+
if (typeof inst.feedback[feedbackMethod] === "function") {
4885+
inst.feedback[feedbackMethod].apply(inst, req.slice(arg + 1));
4886+
}
48544887
} else if (typeof Countly[req[arg]] === "function") {
48554888
Countly[req[arg]].apply(Countly, req.slice(arg + 1));
48564889
}

0 commit comments

Comments
 (0)