更新同步协商流程

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 isMobile: boolean
} }
interface ListConfig {
skipSnapshot: boolean
}
interface DislikeConfig {
skipSnapshot: boolean
}
type ServerType = 'desktop-app' | 'server' type ServerType = 'desktop-app' | 'server'
interface EnabledFeatures { interface EnabledFeatures {
list: boolean list?: false | ListConfig
dislike: boolean dislike?: false | DislikeConfig
} }
type SupportedFeatures = Partial<{ [k in keyof EnabledFeatures]: number }> type SupportedFeatures = Partial<{ [k in keyof EnabledFeatures]: number }>
} }

View File

@ -5,20 +5,30 @@
import { featureVersion } from '../modules' import { featureVersion } from '../modules'
const handler: Omit<LX.Sync.ClientSyncHandlerActions<LX.Sync.Client.Socket>, 'finished'> = {
export const getEnabledFeatures = async(socket: LX.Sync.Client.Socket, serverType: LX.Sync.ServerType, supportedFeatures: LX.Sync.SupportedFeatures): Promise<LX.Sync.EnabledFeatures> => { async getEnabledFeatures(socket, serverType, supportedFeatures) {
// const userSpace = getUserSpace(socket.userInfo.name) // const userSpace = getUserSpace(socket.userInfo.name)
switch (serverType) { const features: LX.Sync.EnabledFeatures = {}
case 'server': switch (serverType) {
return { case 'server':
list: featureVersion.list == supportedFeatures.list, if (featureVersion.list == supportedFeatures.list) {
dislike: featureVersion.dislike == supportedFeatures.dislike, features.list = { skipSnapshot: false }
} }
case 'desktop-app': if (featureVersion.dislike == supportedFeatures.dislike) {
default: features.dislike = { skipSnapshot: false }
return { }
list: featureVersion.list == supportedFeatures.list, return features
dislike: featureVersion.dislike == supportedFeatures.dislike, 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' import { callObj as _callObj } from '../modules'
export { modules } 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) => { const syncDislike = async(socket: LX.Sync.Server.Socket) => {
// socket.data.snapshotFilePath = getSnapshotFilePath(socket.keyInfo) // socket.data.snapshotFilePath = getSnapshotFilePath(socket.keyInfo)
// console.log(socket.keyInfo) // console.log(socket.keyInfo)
const user = getUserSpace(socket.userInfo.name) if (!(socket.feature.dislike as LX.Sync.DislikeConfig).skipSnapshot) {
const userCurrentDislikeInfoKey = await user.dislikeManage.getDeviceCurrentSnapshotKey(socket.keyInfo.clientId) const user = getUserSpace(socket.userInfo.name)
if (userCurrentDislikeInfoKey) { const userCurrentDislikeInfoKey = await user.dislikeManage.getDeviceCurrentSnapshotKey(socket.keyInfo.clientId)
const listData = await user.dislikeManage.snapshotDataManage.getSnapshot(userCurrentDislikeInfoKey) if (userCurrentDislikeInfoKey) {
if (listData) { const listData = await user.dislikeManage.snapshotDataManage.getSnapshot(userCurrentDislikeInfoKey)
console.log('handleMergeDislikeDataFromSnapshot') if (listData) {
await handleMergeListDataFromSnapshot(socket, listData) console.log('handleMergeDislikeDataFromSnapshot')
return await handleMergeListDataFromSnapshot(socket, listData)
return
}
} }
} }
await handleSyncList(socket) 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) => { const syncList = async(socket: LX.Sync.Server.Socket) => {
// socket.data.snapshotFilePath = getSnapshotFilePath(socket.keyInfo) // socket.data.snapshotFilePath = getSnapshotFilePath(socket.keyInfo)
// console.log(socket.keyInfo) // console.log(socket.keyInfo)
const user = getUserSpace(socket.userInfo.name) if (!(socket.feature.list as LX.Sync.ListConfig).skipSnapshot) {
const userCurrentListInfoKey = await user.listManage.getDeviceCurrentSnapshotKey(socket.keyInfo.clientId) const user = getUserSpace(socket.userInfo.name)
if (userCurrentListInfoKey) { const userCurrentListInfoKey = await user.listManage.getDeviceCurrentSnapshotKey(socket.keyInfo.clientId)
const listData = await user.listManage.snapshotDataManage.getSnapshot(userCurrentListInfoKey) if (userCurrentListInfoKey) {
if (listData) { const listData = await user.listManage.snapshotDataManage.getSnapshot(userCurrentListInfoKey)
console.log('handleMergeListDataFromSnapshot') if (listData) {
await handleMergeListDataFromSnapshot(socket, listData) console.log('handleMergeListDataFromSnapshot')
return await handleMergeListDataFromSnapshot(socket, listData)
return
}
} }
} }
await handleSyncList(socket) await handleSyncList(socket)

View File

@ -3,21 +3,19 @@
import { FeaturesList } from '../../../../../../common/constants_sync' import { FeaturesList } from '../../../../../../common/constants_sync'
import { modules } from '../../modules' 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) => { for (const name of FeaturesList) {
// const userSpace = getUserSpace(socket.userInfo.name) const newStatus = feature[name]
const beforeFeature = socket.feature if (newStatus == null) continue
beforeFeature[name] = feature[name]
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 {
socket.moduleReadys[name] = false 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' import { callObj as _callObj } from '../../modules'
export { sync } from './sync' export { sync } from './sync'
export { modules } from '../../modules' 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') if (disconnected) throw new Error('disconnected')
for (const moduleName of FeaturesList) { for (const moduleName of FeaturesList) {
if (enabledFeatures[moduleName]) { if (enabledFeatures[moduleName]) {
await modules[moduleName].sync(socket).then(() => { socket.feature[moduleName] = enabledFeatures[moduleName]
socket.feature[moduleName] = true await modules[moduleName].sync(socket).catch(_ => _)
}).catch(_ => _)
} }
if (disconnected) throw new Error('disconnected') if (disconnected) throw new Error('disconnected')
} }