Skip to content

Commit

Permalink
added custom storage
Browse files Browse the repository at this point in the history
  • Loading branch information
turtledreams committed Nov 20, 2023
1 parent 8541a46 commit 5896889
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
15 changes: 14 additions & 1 deletion examples/worker.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
importScripts("../lib/countly.js");

const STORE={}; // in-memory storage for worker

Countly.init({
app_key: "YOUR_APP_KEY",
url: "https://try.count.ly",
debug: true
debug: true,
storage: {
getItem: function (key) {
return STORE[key];
},
setItem: function (key, value) {
STORE[key] = value;
},
removeItem: function (key) {
delete STORE[key];
}
}
});

onmessage = function (e) {
Expand Down
43 changes: 29 additions & 14 deletions lib/countly.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@
this.ignore_visitor = getConfig("ignore_visitor", ob, false);
this.require_consent = getConfig("require_consent", ob, false);
this.track_domains = !isBrowser ? undefined : getConfig("track_domains", ob, true);
this.storage = !isBrowser ? "none" : getConfig("storage", ob, "default");
this.storage = getConfig("storage", ob, "default");
this.enableOrientationTracking = !isBrowser ? undefined : getConfig("enable_orientation_tracking", ob, true);
this.maxKeyLength = getConfig("max_key_length", ob, configurationDefaultValues.MAX_KEY_LENGTH);
this.maxValueSize = getConfig("max_value_size", ob, configurationDefaultValues.MAX_VALUE_SIZE);
Expand Down Expand Up @@ -4419,9 +4419,9 @@
* @returns {Varies} values stored for key
*/
function getValueFromStorage(key, useLocalStorage, useRawKey) {
// check if we should use storage at all
if (self.storage === "none") {
log(logLevelEnums.WARNING, "Storage is disabled. Value with key: " + key + " won't be retrieved");
// check if we should use storage at all. If in worker context but no storage is available, return early
if (self.storage === "none" || (typeof self.storage !== "object" && !isBrowser)) {
log(logLevelEnums.DEBUG, "Storage is disabled. Value with key: [" + key + "] won't be retrieved");
return;
}

Expand All @@ -4433,11 +4433,16 @@
}
}

var data;
if (typeof self.storage.getItem === "function") {
data = self.storage.getItem(key);
return key.endsWith("cly_id") ? data : self.deserialize(data);
}

// developer set values takes priority
if (useLocalStorage === undefined) {
useLocalStorage = lsSupport;
}
var data;

// Get value
if (useLocalStorage) { // Native support
Expand Down Expand Up @@ -4465,8 +4470,8 @@
*/
function setValueInStorage(key, value, useLocalStorage, useRawKey) {
// check if we should use storage options at all
if (self.storage === "none") {
log(logLevelEnums.WARNING, "Storage is disabled. Value with key: " + key + " won't be stored");
if (self.storage === "none" || (typeof self.storage !== "object" && !isBrowser)) {
log(logLevelEnums.DEBUG, "Storage is disabled. Value with key: " + key + " won't be stored");
return;
}

Expand All @@ -4478,12 +4483,17 @@
}
}

// developer set values takes priority
if (useLocalStorage === undefined) {
useLocalStorage = lsSupport;
}

if (typeof value !== "undefined" && value !== null) {
if (typeof self.storage.setItem === "function") {
self.storage.setItem(key, value);
return;
}

// developer set values takes priority
if (useLocalStorage === undefined) {
useLocalStorage = lsSupport;
}

value = self.serialize(value);
// Set the store
if (useLocalStorage) { // Native support
Expand All @@ -4504,8 +4514,8 @@
*/
function removeValueFromStorage(key, useLocalStorage, useRawKey) {
// check if we should use storage options at all
if (self.storage === "none") {
log(logLevelEnums.WARNING, "Storage is disabled. Value with key: " + key + " won't be removed");
if (self.storage === "none" || (typeof self.storage !== "object" && !isBrowser)) {
log(logLevelEnums.DEBUG, "Storage is disabled. Value with key: " + key + " won't be removed");
return;
}

Expand All @@ -4517,6 +4527,11 @@
}
}

if (typeof self.storage.removeItem === "function") {
self.storage.removeItem(key);
return;
}

// developer set values takes priority
if (useLocalStorage === undefined) {
useLocalStorage = lsSupport;
Expand Down

0 comments on commit 5896889

Please sign in to comment.