Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: add init method and onReadyFromCacheCb param to storage factory [WIP] #831

Draft
wants to merge 3 commits into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/__tests__/offline/browser.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@ tape('Browser offline mode', function (assert) {
});

const sdkReadyFromCache = (client) => () => {
assert.equal(factory.settings.storage.type, 'MEMORY', 'In localhost mode, storage must fallback to memory storage');

const clientStatus = client.__getStatus();
assert.equal(clientStatus.isReadyFromCache, true, 'If ready from cache, READY_FROM_CACHE status must be true');
assert.equal(clientStatus.isReady, false, 'READY status must not be set before READY_FROM_CACHE');
Expand Down
7 changes: 2 additions & 5 deletions src/factory/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { InMemoryStorageCSFactory } from '@splitsoftware/splitio-commons/src/sto
import { sdkManagerFactory } from '@splitsoftware/splitio-commons/src/sdkManager';
import { sdkClientMethodCSFactory } from '@splitsoftware/splitio-commons/src/sdkClient/sdkClientMethodCS';
import { impressionObserverCSFactory } from '@splitsoftware/splitio-commons/src/trackers/impressionObserver/impressionObserverCS';
import { __InLocalStorageMockFactory } from '@splitsoftware/splitio-commons/src/utils/settingsValidation/storage/storageCS';
import { sdkFactory } from '@splitsoftware/splitio-commons/src/sdkFactory';
import { LOCALHOST_MODE, STORAGE_LOCALSTORAGE } from '@splitsoftware/splitio-commons/src/utils/constants';
import { createUserConsentAPI } from '@splitsoftware/splitio-commons/src/consent/sdkUserConsent';
Expand All @@ -19,10 +18,8 @@ const syncManagerOnlineCSFactory = syncManagerOnlineFactory(pollingManagerCSFact

function getStorage(settings) {
return settings.storage.type === STORAGE_LOCALSTORAGE ?
InLocalStorage(settings.storage)
: settings.storage.__originalType === STORAGE_LOCALSTORAGE ?
__InLocalStorageMockFactory
: InMemoryStorageCSFactory;
InLocalStorage(settings.storage) :
InMemoryStorageCSFactory;
}

/**
Expand Down
18 changes: 2 additions & 16 deletions src/settings/storage/browser.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,29 @@
import { isLocalStorageAvailable } from '@splitsoftware/splitio-commons/src/utils/env/isLocalStorageAvailable';
import { LOCALHOST_MODE, STORAGE_MEMORY } from '@splitsoftware/splitio-commons/src/utils/constants';
import { STORAGE_MEMORY } from '@splitsoftware/splitio-commons/src/utils/constants';

const STORAGE_LOCALSTORAGE = 'LOCALSTORAGE';

export function validateStorage(settings) {
let {
log,
mode,
storage: {
type,
options = {},
prefix
} = { type: STORAGE_MEMORY },
} = settings;
let __originalType;

const fallbackToMemory = () => {
__originalType = type;
type = STORAGE_MEMORY;
};

// In localhost mode, fallback to Memory storage and track original type to emit SDK_READY_FROM_CACHE if corresponds.
// ATM, other mode settings (e.g., 'consumer') are ignored in client-side API, and so treated as standalone.
if (mode === LOCALHOST_MODE && type === STORAGE_LOCALSTORAGE) {
fallbackToMemory();
}

// If an invalid storage type is provided OR we want to use LOCALSTORAGE and
// it's not available, fallback into MEMORY
if (type !== STORAGE_MEMORY && type !== STORAGE_LOCALSTORAGE ||
type === STORAGE_LOCALSTORAGE && !isLocalStorageAvailable()) {
fallbackToMemory();
type = STORAGE_MEMORY;
log.error('Invalid or unavailable storage. Fallback into MEMORY storage');
}

return {
type,
options,
prefix,
__originalType
};
}
Loading