pending offline rework

This commit is contained in:
Natsumi
2024-11-04 16:32:13 +13:00
parent e69787e989
commit 7f08dc0350
+46 -53
View File
@@ -3712,8 +3712,6 @@ speechSynthesis.getVoices();
$app.data.debugGameLog = false; $app.data.debugGameLog = false;
$app.data.debugFriendState = false; $app.data.debugFriendState = false;
$app.data.APILastOnline = new Map();
$app.methods.notifyMenu = function (index) { $app.methods.notifyMenu = function (index) {
var { menu } = this.$refs; var { menu } = this.$refs;
if (menu.activeIndex !== index) { if (menu.activeIndex !== index) {
@@ -4346,6 +4344,8 @@ speechSynthesis.getVoices();
no: ++this.friendsNo, no: ++this.friendsNo,
memo: '', memo: '',
pendingOffline: false, pendingOffline: false,
pendingOfflineTime: '',
pendingState: '',
$nickName: '' $nickName: ''
}; };
if (this.friendLogInitStatus) { if (this.friendLogInitStatus) {
@@ -4405,8 +4405,6 @@ speechSynthesis.getVoices();
} }
}; };
$app.data.updateFriendInProgress = new Map();
$app.methods.updateFriend = function (ctx) { $app.methods.updateFriend = function (ctx) {
var { id, state, fromGetCurrentUser } = ctx; var { id, state, fromGetCurrentUser } = ctx;
var stateInput = state; var stateInput = state;
@@ -4414,9 +4412,19 @@ speechSynthesis.getVoices();
if (typeof ctx === 'undefined') { if (typeof ctx === 'undefined') {
return; return;
} }
if (stateInput) {
ctx.pendingState = stateInput;
if (typeof ref !== 'undefined') {
ctx.ref.state = stateInput;
}
}
if (stateInput === 'online') { if (stateInput === 'online') {
this.APILastOnline.set(id, Date.now()); if (this.debugFriendState && ctx.pendingOffline) {
var time = (Date.now() - ctx.pendingOfflineTime) / 1000;
console.log(`${ctx.name} pendingOfflineCancelTime ${time}`);
}
ctx.pendingOffline = false; ctx.pendingOffline = false;
ctx.pendingOfflineTime = '';
} }
var ref = API.cachedUsers.get(id); var ref = API.cachedUsers.get(id);
var isVIP = this.localFavoriteFriends.has(id); var isVIP = this.localFavoriteFriends.has(id);
@@ -4498,34 +4506,35 @@ speechSynthesis.getVoices();
if (typeof ref !== 'undefined') { if (typeof ref !== 'undefined') {
ctx.name = ref.displayName; ctx.name = ref.displayName;
} }
// delayed second check to prevent status flapping // prevent status flapping
var date = this.updateFriendInProgress.get(id); if (ctx.pendingOffline) {
if (date && date > Date.now() - this.pendingOfflineDelay + 5000) {
// check if already waiting
if (this.debugFriendState) { if (this.debugFriendState) {
console.log( console.log(ctx.name, 'pendingOfflineAlreadyWaiting');
ctx.name,
new Date().toJSON(),
'pendingOfflineCheck',
stateInput,
ctx.state
);
} }
return; return;
} }
ctx.pendingOffline = true; ctx.pendingOffline = true;
this.updateFriendInProgress.set(id, Date.now()); ctx.pendingOfflineTime = Date.now();
// wait 2minutes then check if user came back online // wait 2minutes then check if user came back online
workerTimers.setTimeout(() => { workerTimers.setTimeout(() => {
if (!ctx.pendingOffline) {
if (this.debugFriendState) {
console.log(ctx.name, 'pendingOfflineAlreadyCancelled');
}
return;
}
ctx.pendingOffline = false; ctx.pendingOffline = false;
this.updateFriendInProgress.delete(id); ctx.pendingOfflineTime = '';
this.updateFriendDelayedCheck( if (ctx.pendingState === ctx.state) {
id, if (this.debugFriendState) {
ctx, console.log(
stateInput, ctx.name,
location, 'pendingOfflineCancelledStateMatched'
$location_at );
); }
return;
}
this.updateFriendDelayedCheck(ctx, location, $location_at);
}, this.pendingOfflineDelay); }, this.pendingOfflineDelay);
} else { } else {
ctx.ref = ref; ctx.ref = ref;
@@ -4533,41 +4542,26 @@ speechSynthesis.getVoices();
if (typeof ref !== 'undefined') { if (typeof ref !== 'undefined') {
ctx.name = ref.displayName; ctx.name = ref.displayName;
} }
this.updateFriendDelayedCheck( this.updateFriendDelayedCheck(ctx, location, $location_at);
id,
ctx,
stateInput,
location,
$location_at
);
} }
}; };
$app.methods.updateFriendDelayedCheck = async function ( $app.methods.updateFriendDelayedCheck = async function (
id,
ctx, ctx,
newState,
location, location,
$location_at $location_at
) { ) {
var date = this.APILastOnline.get(id); var id = ctx.id;
if ( var newState = ctx.pendingState;
ctx.state === 'online' &&
(newState === 'active' || newState === 'offline') &&
date &&
date > Date.now() - 120000
) {
if (this.debugFriendState) {
console.log(
`falsePositiveOffline ${ctx.name} currentState:${ctx.state} expectedState:${newState}`
);
}
return;
}
if (this.debugFriendState) { if (this.debugFriendState) {
console.log( console.log(
`${ctx.name} updateFriendState ${newState} -> ${ctx.state}` `${ctx.name} updateFriendState ${ctx.state} -> ${newState}`
); );
if (location !== ctx.ref?.location) {
console.log(
`${ctx.name} pendingOfflineLocation ${location} -> ${ctx.ref.location}`
);
}
} }
var isVIP = this.localFavoriteFriends.has(id); var isVIP = this.localFavoriteFriends.has(id);
var ref = ctx.ref; var ref = ctx.ref;
@@ -4657,7 +4651,6 @@ speechSynthesis.getVoices();
this.updateOnlineFriendCoutner(); this.updateOnlineFriendCoutner();
} }
ctx.state = newState; ctx.state = newState;
ctx.ref.state = newState;
ctx.name = ref.displayName; ctx.name = ref.displayName;
ctx.isVIP = isVIP; ctx.isVIP = isVIP;
}; };
@@ -5490,9 +5483,9 @@ speechSynthesis.getVoices();
time = 0; time = 0;
} }
} }
if ($app.debugFriendState) { if ($app.debugFriendState && previousLocation) {
console.log( console.log(
`${ref.displayName} ${previousLocation} -> ${newLocation}` `${ref.displayName} GPS ${previousLocation} -> ${newLocation}`
); );
} }
if (!previousLocation) { if (!previousLocation) {
@@ -5500,7 +5493,7 @@ speechSynthesis.getVoices();
if ($app.debugFriendState) { if ($app.debugFriendState) {
console.log( console.log(
ref.displayName, ref.displayName,
'no previous location', 'Ignoring GPS, no previous location',
newLocation newLocation
); );
} }