From 131e8f5ebe48a9f5c876e70baab715ec4821917d Mon Sep 17 00:00:00 2001 From: Pooya Jaferian Date: Mon, 24 Oct 2022 13:26:51 -0700 Subject: [PATCH] refactor --- packages/browser/src/core/analytics/index.ts | 4 +- packages/browser/src/core/user/index.ts | 127 ++++++++---------- .../src/plugins/segmentio/normalize.ts | 11 +- 3 files changed, 69 insertions(+), 73 deletions(-) diff --git a/packages/browser/src/core/analytics/index.ts b/packages/browser/src/core/analytics/index.ts index 0c3579351..42ebf54f2 100644 --- a/packages/browser/src/core/analytics/index.ts +++ b/packages/browser/src/core/analytics/index.ts @@ -121,7 +121,9 @@ export class Analytics queue ?? createDefaultQueue(options?.retryQueue, disablePersistance) this._universalStorage = UniversalStorage.getUniversalStorage( - disablePersistance !== false, + disablePersistance !== false + ? ['cookie', 'localStorage', 'memory'] + : ['memory'], cookieOptions ) diff --git a/packages/browser/src/core/user/index.ts b/packages/browser/src/core/user/index.ts index 68c4a543a..40cebb675 100644 --- a/packages/browser/src/core/user/index.ts +++ b/packages/browser/src/core/user/index.ts @@ -238,20 +238,22 @@ export class UniversalStorage { } static getUniversalStorage( - hasBrowserStorage: boolean, + defaultTargets: StoreType[] = ['cookie', 'localStorage', 'memory'], cookieOptions?: CookieOptions ): UniversalStorage { const stores = [] - if (hasBrowserStorage && Cookie.available()) { + if (defaultTargets.includes('cookie') && Cookie.available()) { stores.push(new Cookie(cookieOptions)) } - if (hasBrowserStorage && LocalStorage.available()) { + if (defaultTargets.includes('localStorage') && LocalStorage.available()) { stores.push(new LocalStorage()) } - stores.push(new Store()) + if (defaultTargets.includes('memory')) { + stores.push(new Store()) + } return new UniversalStorage(stores) } @@ -264,19 +266,14 @@ export class User { private traitsKey: string private anonKey: string private cookieOptions?: CookieOptions - private universalStore: UniversalStorage - private legacyUserStoreTargets: StoreType[] = [] - private traitsStoreTargets: StoreType[] = [] - private identityStoreTypes: StoreType[] = [] + private legacyUserStore: UniversalStorage + private traitsStore: UniversalStorage + private identityStore: UniversalStorage options: UserOptions = {} - constructor( - options: UserOptions = defaults, - cookieOptions?: CookieOptions, - universalStore?: UniversalStorage - ) { + constructor(options: UserOptions = defaults, cookieOptions?: CookieOptions) { this.options = options this.cookieOptions = cookieOptions @@ -287,24 +284,41 @@ export class User { const isDisabled = options.disable === true const shouldPersist = options.persist !== false - this.universalStore = - universalStore || - UniversalStorage.getUniversalStorage(shouldPersist, cookieOptions) - - if (!isDisabled) { - this.identityStoreTypes.push('memory', 'cookie') - this.legacyUserStoreTargets.push('cookie') - this.traitsStoreTargets.push('memory') - if (!options.localStorageFallbackDisabled) { - this.traitsStoreTargets.push('localStorage') - this.identityStoreTypes.push('localStorage') - } + let defaultStorageTargets: StoreType[] = isDisabled + ? [] + : shouldPersist + ? ['cookie', 'localStorage', 'memory'] + : ['memory'] + + if (options.localStorageFallbackDisabled) { + defaultStorageTargets = defaultStorageTargets.filter( + (t) => t !== 'localStorage' + ) } - const legacyUser = this.universalStore.get<{ + this.identityStore = UniversalStorage.getUniversalStorage( + defaultStorageTargets, + cookieOptions + ) + + // using only cookies for legacy user store + this.legacyUserStore = UniversalStorage.getUniversalStorage( + defaultStorageTargets.filter( + (t) => t !== 'localStorage' && t !== 'memory' + ), + cookieOptions + ) + + // using only localStorage / memory for traits store + this.traitsStore = UniversalStorage.getUniversalStorage( + defaultStorageTargets.filter((t) => t !== 'cookie'), + cookieOptions + ) + + const legacyUser = this.legacyUserStore.get<{ id?: string traits?: Traits - }>(defaults.cookie.oldKey, this.legacyUserStoreTargets) + }>(defaults.cookie.oldKey) if (legacyUser) { legacyUser.id && this.id(legacyUser.id) legacyUser.traits && this.traits(legacyUser.traits) @@ -317,13 +331,10 @@ export class User { return null } - const prevId = this.universalStore.getAndSync( - this.idKey, - this.identityStoreTypes - ) + const prevId = this.identityStore.getAndSync(this.idKey) if (id !== undefined) { - this.universalStore.set(this.idKey, id, this.identityStoreTypes) + this.identityStore.set(this.idKey, id) const changingIdentity = id !== prevId && prevId !== null && id !== null if (changingIdentity) { @@ -332,20 +343,14 @@ export class User { } return ( - this.universalStore.getAndSync(this.idKey, this.identityStoreTypes) ?? - this.universalStore.get( - defaults.cookie.oldKey, - this.legacyUserStoreTargets - ) ?? + this.identityStore.getAndSync(this.idKey) ?? + this.legacyUserStore.get(defaults.cookie.oldKey) ?? null ) } private legacySIO(): [string, string] | null { - const val = this.universalStore.get( - '_sio', - this.legacyUserStoreTargets - ) as string + const val = this.legacyUserStore.get('_sio') as string if (!val) { return null } @@ -360,10 +365,7 @@ export class User { if (id === undefined) { const val = - this.universalStore.getAndSync( - this.anonKey, - this.identityStoreTypes - ) ?? this.legacySIO()?.[0] + this.identityStore.getAndSync(this.anonKey) ?? this.legacySIO()?.[0] if (val) { return val @@ -371,15 +373,12 @@ export class User { } if (id === null) { - this.universalStore.set(this.anonKey, null, this.identityStoreTypes) - return this.universalStore.getAndSync( - this.anonKey, - this.identityStoreTypes - ) + this.identityStore.set(this.anonKey, null) + return this.identityStore.getAndSync(this.anonKey) } - this.universalStore.set(this.anonKey, id ?? uuid(), this.identityStoreTypes) - return this.universalStore.getAndSync(this.anonKey, this.identityStoreTypes) + this.identityStore.set(this.anonKey, id ?? uuid()) + return this.identityStore.getAndSync(this.anonKey) } traits = (traits?: Traits | null): Traits | undefined => { @@ -392,16 +391,10 @@ export class User { } if (traits) { - this.universalStore.set( - this.traitsKey, - traits ?? {}, - this.traitsStoreTargets - ) + this.traitsStore.set(this.traitsKey, traits ?? {}) } - return ( - this.universalStore.get(this.traitsKey, this.traitsStoreTargets) ?? {} - ) + return this.traitsStore.get(this.traitsKey) ?? {} } identify(id?: ID, traits?: Traits): void { @@ -434,9 +427,9 @@ export class User { reset(): void { this.logout() - this.universalStore.clear(this.idKey, this.identityStoreTypes) - this.universalStore.clear(this.anonKey, this.identityStoreTypes) - this.universalStore.clear(this.traitsKey, this.traitsStoreTargets) + this.identityStore.clear(this.idKey) + this.identityStore.clear(this.anonKey) + this.traitsStore.clear(this.traitsKey) } load(): User { @@ -459,12 +452,8 @@ const groupDefaults: UserOptions = { } export class Group extends User { - constructor( - options: UserOptions = groupDefaults, - cookie?: CookieOptions, - store?: UniversalStorage - ) { - super(options, cookie, store) + constructor(options: UserOptions = groupDefaults, cookie?: CookieOptions) { + super(options, cookie) autoBind(this) } diff --git a/packages/browser/src/plugins/segmentio/normalize.ts b/packages/browser/src/plugins/segmentio/normalize.ts index 3ba42dfef..e6ef15cf2 100644 --- a/packages/browser/src/plugins/segmentio/normalize.ts +++ b/packages/browser/src/plugins/segmentio/normalize.ts @@ -7,6 +7,7 @@ import { tld } from '../../core/user/tld' import { SegmentFacade } from '../../lib/to-facade' import { SegmentioSettings } from './index' import { version } from '../../generated/version' +import { UniversalStorage } from '../../core/user' let cookieOptions: jar.CookieAttributes | undefined function getCookieOptions(): jar.CookieAttributes { @@ -94,10 +95,14 @@ function referrerId( ctx: SegmentEvent['context'], disablePersistance: boolean ): void { - let stored = jar.get('s:context.referrer') + const storage = UniversalStorage.getUniversalStorage( + ['cookie'], + getCookieOptions() + ) + + const stored = storage.get('s:context.referrer', ['cookie']) let ad = ads(query) - stored = stored ? JSON.parse(stored) : undefined ad = ad ?? (stored as Ad | undefined) if (!ad) { @@ -109,7 +114,7 @@ function referrerId( } if (!disablePersistance) { - jar.set('s:context.referrer', JSON.stringify(ad), getCookieOptions()) + storage.set('s:context.referrer', ad, ['cookie']) } }