更新同步协商流程

pull/1583/head
lyswhut 2023-09-06 16:01:23 +08:00
parent 19049fb890
commit fcffcb5b59
8 changed files with 68 additions and 51 deletions

View File

@ -69,10 +69,16 @@ declare namespace LX {
isMobile: boolean
}
interface ListConfig {
skipSnapshot: boolean
}
interface DislikeConfig {
skipSnapshot: boolean
}
type ServerType = 'desktop-app' | 'server'
interface EnabledFeatures {
list: boolean
dislike: boolean
list?: false | ListConfig
dislike?: false | DislikeConfig
}
type SupportedFeatures = Partial<{ [k in keyof EnabledFeatures]: number }>
}

View File

@ -5,20 +5,30 @@
import { featureVersion } from '../modules'
export const getEnabledFeatures = async(socket: LX.Sync.Client.Socket, serverType: LX.Sync.ServerType, supportedFeatures: LX.Sync.SupportedFeatures): Promise<LX.Sync.EnabledFeatures> => {
const handler: Omit<LX.Sync.ClientSyncHandlerActions<LX.Sync.Client.Socket>, 'finished'> = {
async getEnabledFeatures(socket, serverType, supportedFeatures) {
// const userSpace = getUserSpace(socket.userInfo.name)
switch (serverType) {
case 'server':
return {
list: featureVersion.list == supportedFeatures.list,
dislike: featureVersion.dislike == supportedFeatures.dislike,
}
case 'desktop-app':
default:
return {
list: featureVersion.list == supportedFeatures.list,
dislike: featureVersion.dislike == supportedFeatures.dislike,
}
}
const features: LX.Sync.EnabledFeatures = {}
switch (serverType) {
case 'server':
if (featureVersion.list == supportedFeatures.list) {
features.list = { skipSnapshot: false }
}
if (featureVersion.dislike == supportedFeatures.dislike) {
features.dislike = { skipSnapshot: false }
}
return features
case 'desktop-app':
default:
if (featureVersion.list == supportedFeatures.list) {
features.list = { skipSnapshot: false }
}
if (featureVersion.dislike == supportedFeatures.dislike) {
features.dislike = { skipSnapshot: false }
}
return features
}
},
}
export default handler

View File

@ -1,4 +1,4 @@
import * as handler from './handler'
import handler from './handler'
import { callObj as _callObj } from '../modules'
export { modules } from '../modules'

View File

@ -196,14 +196,16 @@ const handleMergeListDataFromSnapshot = async(socket: LX.Sync.Server.Socket, sna
const syncDislike = async(socket: LX.Sync.Server.Socket) => {
// socket.data.snapshotFilePath = getSnapshotFilePath(socket.keyInfo)
// console.log(socket.keyInfo)
const user = getUserSpace(socket.userInfo.name)
const userCurrentDislikeInfoKey = await user.dislikeManage.getDeviceCurrentSnapshotKey(socket.keyInfo.clientId)
if (userCurrentDislikeInfoKey) {
const listData = await user.dislikeManage.snapshotDataManage.getSnapshot(userCurrentDislikeInfoKey)
if (listData) {
console.log('handleMergeDislikeDataFromSnapshot')
await handleMergeListDataFromSnapshot(socket, listData)
return
if (!(socket.feature.dislike as LX.Sync.DislikeConfig).skipSnapshot) {
const user = getUserSpace(socket.userInfo.name)
const userCurrentDislikeInfoKey = await user.dislikeManage.getDeviceCurrentSnapshotKey(socket.keyInfo.clientId)
if (userCurrentDislikeInfoKey) {
const listData = await user.dislikeManage.snapshotDataManage.getSnapshot(userCurrentDislikeInfoKey)
if (listData) {
console.log('handleMergeDislikeDataFromSnapshot')
await handleMergeListDataFromSnapshot(socket, listData)
return
}
}
}
await handleSyncList(socket)

View File

@ -398,14 +398,16 @@ const handleMergeListDataFromSnapshot = async(socket: LX.Sync.Server.Socket, sna
const syncList = async(socket: LX.Sync.Server.Socket) => {
// socket.data.snapshotFilePath = getSnapshotFilePath(socket.keyInfo)
// console.log(socket.keyInfo)
const user = getUserSpace(socket.userInfo.name)
const userCurrentListInfoKey = await user.listManage.getDeviceCurrentSnapshotKey(socket.keyInfo.clientId)
if (userCurrentListInfoKey) {
const listData = await user.listManage.snapshotDataManage.getSnapshot(userCurrentListInfoKey)
if (listData) {
console.log('handleMergeListDataFromSnapshot')
await handleMergeListDataFromSnapshot(socket, listData)
return
if (!(socket.feature.list as LX.Sync.ListConfig).skipSnapshot) {
const user = getUserSpace(socket.userInfo.name)
const userCurrentListInfoKey = await user.listManage.getDeviceCurrentSnapshotKey(socket.keyInfo.clientId)
if (userCurrentListInfoKey) {
const listData = await user.listManage.snapshotDataManage.getSnapshot(userCurrentListInfoKey)
if (listData) {
console.log('handleMergeListDataFromSnapshot')
await handleMergeListDataFromSnapshot(socket, listData)
return
}
}
}
await handleSyncList(socket)

View File

@ -3,21 +3,19 @@
import { FeaturesList } from '../../../../../../common/constants_sync'
import { modules } from '../../modules'
const handler: LX.Sync.ServerSyncHandlerActions<LX.Sync.Server.Socket> = {
async onFeatureChanged(socket, feature) {
// const userSpace = getUserSpace(socket.userInfo.name)
const beforeFeature = socket.feature
export const onFeatureChanged = async(socket: LX.Sync.Server.Socket, feature: LX.Sync.EnabledFeatures) => {
// const userSpace = getUserSpace(socket.userInfo.name)
const beforeFeature = socket.feature
for (const name of FeaturesList) {
if (feature[name] == beforeFeature[name]) continue
if (feature[name]) {
await modules[name].sync(socket).then(() => {
beforeFeature[name] = true
}).catch(_ => _)
} else {
for (const name of FeaturesList) {
const newStatus = feature[name]
if (newStatus == null) continue
beforeFeature[name] = feature[name]
socket.moduleReadys[name] = false
beforeFeature[name] = false
if (feature[name]) await modules[name].sync(socket).catch(_ => _)
}
}
},
}
export default handler

View File

@ -1,4 +1,4 @@
import * as handler from './handler'
import handler from './handler'
import { callObj as _callObj } from '../../modules'
export { sync } from './sync'
export { modules } from '../../modules'

View File

@ -12,9 +12,8 @@ export const sync = async(socket: LX.Sync.Server.Socket) => {
if (disconnected) throw new Error('disconnected')
for (const moduleName of FeaturesList) {
if (enabledFeatures[moduleName]) {
await modules[moduleName].sync(socket).then(() => {
socket.feature[moduleName] = true
}).catch(_ => _)
socket.feature[moduleName] = enabledFeatures[moduleName]
await modules[moduleName].sync(socket).catch(_ => _)
}
if (disconnected) throw new Error('disconnected')
}