fix(userInfo): fixed infinite loop sync in firefox

This commit is contained in:
molvqingtai 2023-12-06 03:12:13 +08:00
parent fef6a46cb7
commit 9fca355c99

View file

@ -30,14 +30,26 @@ const UserInfoDomain = Remesh.domain({
const UpdateUserInfoCommand = domain.command({ const UpdateUserInfoCommand = domain.command({
name: 'UserInfo.UpdateUserInfoCommand', name: 'UserInfo.UpdateUserInfoCommand',
impl: (_, userInfo: UserInfo | null) => { impl: (_, userInfo: UserInfo | null) => {
return [UserInfoState().new(userInfo), UpdateUserInfoEvent()] return [UserInfoState().new(userInfo), UpdateUserInfoEvent(userInfo), SyncToStorageEvent(userInfo)]
} }
}) })
const UpdateUserInfoEvent = domain.event({ const UpdateUserInfoEvent = domain.event<UserInfo | null>({
name: 'UserInfo.UpdateUserInfoEvent', name: 'UserInfo.UpdateUserInfoEvent'
impl: ({ get }) => { })
return get(UserInfoQuery())
const SyncToStorageEvent = domain.event<UserInfo | null>({
name: 'UserInfo.SyncToStorageEvent'
})
const SyncToStateEvent = domain.event<UserInfo | null>({
name: 'UserInfo.SyncToStateEvent'
})
const SyncToStateCommand = domain.command({
name: 'UserInfo.SyncToStateCommand',
impl: (_, userInfo: UserInfo | null) => {
return [UserInfoState().new(userInfo), UpdateUserInfoEvent(userInfo), SyncToStateEvent(userInfo)]
} }
}) })
@ -59,9 +71,9 @@ const UserInfoDomain = Remesh.domain({
!isNullish(userInfo.createTime) && !isNullish(userInfo.createTime) &&
!isNullish(userInfo.themeMode) !isNullish(userInfo.themeMode)
) { ) {
return UpdateUserInfoCommand(userInfo as UserInfo) return SyncToStateCommand(userInfo as UserInfo)
} else { } else {
return UpdateUserInfoCommand(null) return SyncToStateCommand(null)
} }
}) })
) )
@ -71,7 +83,7 @@ const UserInfoDomain = Remesh.domain({
domain.effect({ domain.effect({
name: 'FormStateToStorageEffect', name: 'FormStateToStorageEffect',
impl: ({ fromEvent }) => { impl: ({ fromEvent }) => {
const changeUserInfo$ = fromEvent(UpdateUserInfoEvent).pipe( const changeUserInfo$ = fromEvent(SyncToStorageEvent).pipe(
tap(async (userInfo) => { tap(async (userInfo) => {
return await Promise.all([ return await Promise.all([
storage.set<UserInfo['id'] | null>(storageKeys.USER_INFO_ID, userInfo?.id ?? null), storage.set<UserInfo['id'] | null>(storageKeys.USER_INFO_ID, userInfo?.id ?? null),
@ -90,7 +102,7 @@ const UserInfoDomain = Remesh.domain({
}) })
domain.effect({ domain.effect({
name: 'WatchStorageEffect', name: 'WatchStorageToStateEffect',
impl: () => { impl: () => {
return storageToObservable(storage).pipe( return storageToObservable(storage).pipe(
switchMap(() => { switchMap(() => {
@ -109,9 +121,9 @@ const UserInfoDomain = Remesh.domain({
!isNullish(userInfo.createTime) && !isNullish(userInfo.createTime) &&
!isNullish(userInfo.themeMode) !isNullish(userInfo.themeMode)
) { ) {
return UpdateUserInfoCommand(userInfo as UserInfo) return SyncToStateCommand(userInfo as UserInfo)
} else { } else {
return UpdateUserInfoCommand(null) return SyncToStateCommand(null)
} }
}) })
) )
@ -128,6 +140,8 @@ const UserInfoDomain = Remesh.domain({
UpdateUserInfoCommand UpdateUserInfoCommand
}, },
event: { event: {
SyncToStateEvent,
SyncToStorageEvent,
UpdateUserInfoEvent UpdateUserInfoEvent
} }
} }