Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
pooyaj committed Oct 24, 2022
1 parent 7e0a87e commit 131e8f5
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 73 deletions.
4 changes: 3 additions & 1 deletion packages/browser/src/core/analytics/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
127 changes: 58 additions & 69 deletions packages/browser/src/core/user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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

Expand All @@ -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)
Expand All @@ -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) {
Expand All @@ -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
}
Expand All @@ -360,26 +365,20 @@ export class User {

if (id === undefined) {
const val =
this.universalStore.getAndSync<ID>(
this.anonKey,
this.identityStoreTypes
) ?? this.legacySIO()?.[0]
this.identityStore.getAndSync<ID>(this.anonKey) ?? this.legacySIO()?.[0]

if (val) {
return val
}
}

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 => {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}

Expand Down
11 changes: 8 additions & 3 deletions packages/browser/src/plugins/segmentio/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -109,7 +114,7 @@ function referrerId(
}

if (!disablePersistance) {
jar.set('s:context.referrer', JSON.stringify(ad), getCookieOptions())
storage.set('s:context.referrer', ad, ['cookie'])
}
}

Expand Down

0 comments on commit 131e8f5

Please sign in to comment.