Skip to content

Commit

Permalink
fix: optimize localstorage key access
Browse files Browse the repository at this point in the history
  • Loading branch information
saikumarrs committed Mar 21, 2024
1 parent 52e01b7 commit ddc9cef
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 42 deletions.
1 change: 1 addition & 0 deletions packages/analytics-js-common/src/types/Store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export interface IStore {

export interface IStorage extends Storage {
configure?(options: StorageOptions): void;
keys(): string[];
isEnabled?: boolean;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -705,45 +705,31 @@ class RetryQueue implements IQueue<QueueItemData> {
};
const findOtherQueues = (name: string): IStore[] => {
const res: IStore[] = [];
const storage = this.store.getOriginalEngine();

for (let i = 0; i < storage.length; i++) {
const k = storage.key(i);
const parts: string[] = k ? k.split('.') : [];

if (parts.length !== 3) {
// eslint-disable-next-line no-continue
continue;
}

if (parts[0] !== name) {
// eslint-disable-next-line no-continue
continue;
}

if (parts[2] !== QueueStatuses.ACK) {
// eslint-disable-next-line no-continue
continue;
const storageKeys = this.store.getOriginalEngine().keys();
storageKeys.forEach((k: string) => {
const keyParts: string[] = k ? k.split('.') : [];

if (
keyParts.length >= 3 &&
keyParts[0] === name &&
keyParts[1] !== this.id &&
keyParts[2] === QueueStatuses.ACK
) {
res.push(
this.storeManager.setStore({
id: keyParts[1] as string,
name,
validKeys: QueueStatuses,
type: LOCAL_STORAGE,
}),
);
}

res.push(
this.storeManager.setStore({
id: parts[1] as string,
name,
validKeys: QueueStatuses,
type: LOCAL_STORAGE,
}),
);
}
});

return res;
};

findOtherQueues(this.name).forEach(store => {
if (store.id === this.id) {
return;
}

if (this.schedule.now() - store.get(QueueStatuses.ACK) < this.timeouts.reclaimTimeout) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,14 @@ class CookieStorage implements IStorage {
// better to explicitly clear specific ones if needed
}

// This cannot be implemented for cookies
// eslint-disable-next-line class-methods-use-this
key(index: number): Nullable<string> {
const cookies = cookie();
const cookieNames = Object.keys(cookies);
return isUndefined(cookieNames[index]) ? null : (cookieNames[index] as string);
const curKeys = this.keys();
return curKeys[index] ?? null;
}

// eslint-disable-next-line class-methods-use-this
keys(): string[] {
return Object.keys(cookie());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ class InMemoryStorage implements IStorage {
}

key(index: number): Nullable<string> {
return Object.keys(this.data)[index] ? (Object.keys(this.data)[index] as string) : null;
const curKeys = this.keys();
return curKeys[index] ?? null;
}

keys(): string[] {
return Object.keys(this.data);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,14 @@ class LocalStorage implements IStorage {
this.length = 0;
}

// eslint-disable-next-line class-methods-use-this
key(index: number): Nullable<string> {
return store.keys()[index] ? (store.keys()[index] as string) : null;
const curKeys = this.keys();
return curKeys[index] ?? null;
}

// eslint-disable-next-line class-methods-use-this
keys(): string[] {
return store.keys();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { ILogger } from '@rudderstack/analytics-js-common/types/Logger';
import { mergeDeepRight } from '@rudderstack/analytics-js-common/utilities/object';
import { isUndefined } from '@rudderstack/analytics-js-common/utilities/checks';
import { SESSION_STORAGE } from '@rudderstack/analytics-js-common/constants/storages';
import type { Nullable } from '@rudderstack/analytics-js-common/types/Nullable';
import { isStorageAvailable } from '../../../components/capabilitiesManager/detection';
import { defaultLogger } from '../../Logger';
import { getDefaultSessionStorageOptions } from './defaultOptions';
Expand Down Expand Up @@ -54,9 +55,20 @@ class SessionStorage implements IStorage {
this.length = 0;
}

key(index: number): string | null {
key(index: number): Nullable<string> {
return this.store.key(index);
}

keys(): string[] {
const keys: string[] = [];
for (let i = 0; i < this.store.length; i += 1) {
const key = this.store.key(i);

Check warning on line 65 in packages/analytics-js/src/services/StoreManager/storages/sessionStorage.ts

View check run for this annotation

Codecov / codecov/patch

packages/analytics-js/src/services/StoreManager/storages/sessionStorage.ts#L63-L65

Added lines #L63 - L65 were not covered by tests
if (key !== null) {
keys.push(key);

Check warning on line 67 in packages/analytics-js/src/services/StoreManager/storages/sessionStorage.ts

View check run for this annotation

Codecov / codecov/patch

packages/analytics-js/src/services/StoreManager/storages/sessionStorage.ts#L67

Added line #L67 was not covered by tests
}
}
return keys;

Check warning on line 70 in packages/analytics-js/src/services/StoreManager/storages/sessionStorage.ts

View check run for this annotation

Codecov / codecov/patch

packages/analytics-js/src/services/StoreManager/storages/sessionStorage.ts#L70

Added line #L70 was not covered by tests
}
}

const defaultSessionStorage = new SessionStorage({}, defaultLogger);
Expand Down

0 comments on commit ddc9cef

Please sign in to comment.