Skip to content

Commit 6c91623

Browse files
committed
fix(harmonyos): preserve newer presence during device refresh
A delayed device-list response could replace online or offline status received while the request was in flight. Apply the newer presence snapshot to the response while retaining refreshed device metadata; later HTTP reads can still recover from missed events.
1 parent ac5558b commit 6c91623

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

‎src/apps/mobile/harmonyos/entry/src/main/ets/pages/viewmodel/SettingsController.ets‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export class SettingsController {
7575
private accountDevices: CloudAccountDevice[] = [];
7676
private lastAccountDevicesAt: number = 0;
7777
private stopPresence?: () => void;
78+
private latestPresence?: string[];
7879
private presenceGeneration: number = 0;
7980
private presenceRefreshing: boolean = false;
8081
private targetProbeTimer: number = -1;
@@ -293,6 +294,7 @@ export class SettingsController {
293294
this.stopPresence = binding.client.observePresence(binding.relayUrl, binding.session,
294295
(ids: string[]) => {
295296
if (epoch !== this.presenceGeneration) return;
297+
this.latestPresence = ids.slice();
296298
const previousTarget = this.accountDevices.find((device: CloudAccountDevice) => device.deviceId === this.requireCloud().remoteState.controlTargetDeviceId);
297299
this.accountDevices = this.accountDevices.map((device: CloudAccountDevice): CloudAccountDevice => ({
298300
deviceId: device.deviceId, deviceName: device.deviceName, deviceKind: device.deviceKind,
@@ -325,6 +327,7 @@ export class SettingsController {
325327

326328
stopPresencePolling(): void {
327329
this.presenceGeneration++;
330+
this.latestPresence = undefined;
328331
this.stopPresence?.(); this.stopPresence = undefined;
329332
if (this.targetProbeTimer !== -1) clearTimeout(this.targetProbeTimer);
330333
this.targetProbeTimer = -1;
@@ -438,9 +441,18 @@ export class SettingsController {
438441
if (!session || this.cloudRelayUrl.length === 0) {
439442
return [];
440443
}
444+
const presenceAtStart = this.latestPresence;
441445
try {
442446
const devices = await cloud.client.listDevices(this.cloudRelayUrl, session, cloud.hooks.deviceId());
443447
if (this.cloudSession !== session) throw new Error('Account changed while loading devices');
448+
// A presence snapshot received during this request is newer than its
449+
// HTTP answer. Keep that liveness while accepting refreshed metadata.
450+
const presence = this.latestPresence;
451+
if (presence !== undefined && presence !== presenceAtStart) {
452+
for (const device of devices) {
453+
device.online = presence.includes(device.deviceId);
454+
}
455+
}
444456
this.accountDevices = devices;
445457
this.lastAccountDevicesAt = Date.now();
446458
const target = devices.find((device: CloudAccountDevice): boolean =>

‎src/apps/mobile/harmonyos/tools/tests/account-restore.test.cjs‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,41 @@ test('directory refresh and presence retain aliases in the selected and preferre
102102
assert.equal(state.controlTargetDeviceId, 'desktop');
103103
controller.stopPresencePolling();
104104
});
105+
106+
for (const online of [true, false]) {
107+
test(`a delayed device list retains newer ${online ? 'online' : 'offline'} presence and refreshes metadata`, async () => {
108+
const device = { deviceId: 'desktop', deviceName: 'Desktop', online: !online };
109+
let onPresence;
110+
let published;
111+
const client = {
112+
listDevices: async () => [{ ...device }],
113+
observePresence: (_relay, _session, listener) => { onPresence = listener; return () => {}; },
114+
};
115+
const controller = new exported.SettingsController({ client, remoteState: {}, hooks: {
116+
deviceId: () => 'phone', onAccountDevices: devices => { published = devices; },
117+
} });
118+
controller.cloudSession = { token: 'token', userId: 'user' };
119+
controller.cloudRelayUrl = official;
120+
await controller.listCloudAccountDevices();
121+
controller.startPresencePolling(0);
122+
123+
let resolve;
124+
client.listDevices = () => new Promise(yes => { resolve = yes; });
125+
const pending = controller.listCloudAccountDevices();
126+
onPresence(online ? ['desktop'] : []);
127+
assert.equal(published[0].online, online);
128+
resolve([{ ...device, deviceAlias: 'Office', compatible: false }]);
129+
const result = await pending;
130+
assert.equal(result[0].online, online);
131+
assert.equal(published[0].online, online);
132+
assert.equal(controller.knownAccountDevice('desktop').online, online);
133+
assert.equal(published[0].deviceAlias, 'Office');
134+
assert.equal(published[0].compatible, false);
135+
136+
// A later explicit read must still recover from a missed presence event.
137+
client.listDevices = async () => [{ ...device }];
138+
await controller.listCloudAccountDevices();
139+
assert.equal(published[0].online, !online);
140+
controller.stopPresencePolling();
141+
});
142+
}

0 commit comments

Comments
 (0)