Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Commit

Permalink
fix: reset anonymousId for users who have persisted incorrect value (s…
Browse files Browse the repository at this point in the history
  • Loading branch information
oscb authored Feb 17, 2022
1 parent 26c8263 commit 37aeb81
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion packages/core/src/storage/sovranStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,31 @@ export class SovranStorage implements Storage {
this.userInfoStore = createStore(
{ userInfo: INITIAL_VALUES.userInfo },
{
persist: { storeId: `${this.storeId}-userInfo` },
persist: {
storeId: `${this.storeId}-userInfo`,
},
}
);

this.fixAnonymousId();
}

/**
* This is a fix for users that have started the app with the anonymousId set to 'anonymousId' bug
*/
private fixAnonymousId = () => {
const fixUnsubscribe = this.userInfoStore.subscribe((store) => {
if (store.userInfo.anonymousId === 'anonymousId') {
this.userInfoStore.dispatch((state) => {
return {
userInfo: { ...state.userInfo, anonymousId: getUUID() },
};
});
}
fixUnsubscribe();
});
};

readonly isReady = {
get: () => true,
onChange: (_callback: (value: boolean) => void) => {
Expand Down Expand Up @@ -119,6 +139,7 @@ export class SovranStorage implements Storage {
});
},
};

readonly userInfo = {
get: () => this.userInfoStore.getState().userInfo,
onChange: (callback: (value: UserInfoState) => void) =>
Expand Down

0 comments on commit 37aeb81

Please sign in to comment.