Skip to content

Commit 3876110

Browse files
authored
resolve harmony hot update fail issue (#483)
* fix harmony more than 2M issue * fix mtpush-react-native conflics * update harmony remote dependency flow * udpate * udpate * udpate * udpate * udpate * update * uddate * udpapte * adapter pushy for Expo * udpate * resolve harmony hot update fail issue * udpate * udpate * udpate * udpate * udpate * update * udpate
1 parent 93f2d51 commit 3876110

File tree

9 files changed

+91
-69
lines changed

9 files changed

+91
-69
lines changed

Example/harmony_use_pushy/harmony/entry/src/main/resources/rawfile/bundle.harmony.js

Lines changed: 27 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"pushy_build_time": "2025-02-14T09:43:25.648Z",
2+
"pushy_build_time": "2025-03-09T01:57:42.464Z",
33
"versionName": "1.0.0"
44
}

Example/harmony_use_pushy/yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5520,9 +5520,9 @@ mustache@^4.2.0:
55205520
integrity sha512-71ippSywq5Yb7/tVYyGbkBggbU8H3u5Rz56fH60jGFgr8uHwxs+aSKeqmluIVzM0m0kB7xQjKS6qPfd0b2ZoqQ==
55215521

55225522
nanoid@^3.3.3:
5523-
version "3.3.8"
5524-
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.3.8.tgz#b1be3030bee36aaff18bacb375e5cce521684baf"
5525-
integrity sha512-WNLf5Sd8oZxOm+TzppcYk8gVOgP+l58xNy58D0nbUnOxOWRWvlcCV4kUF7ltmI6PsrLl/BgKEyS4mqsGChFN0w==
5523+
version "3.3.9"
5524+
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-3.3.9.tgz#e0097d8e026b3343ff053e9ccd407360a03f503a"
5525+
integrity sha512-SppoicMGpZvbF1l3z4x7No3OlIjP7QJvC9XR7AhZr1kL133KHnKPztkKDc+Ir4aJ/1VhTySrtKhrsycmrMQfvg==
55265526

55275527
natural-compare-lite@^1.4.0:
55285528
version "1.4.0"
@@ -6003,7 +6003,7 @@ react-is@^17.0.1:
60036003
integrity sha512-w2GsyukL62IJnlaff/nRegPQR94C/XXamvMWmSHRJ4y7Ts/4ocGRmTHvOs8PSE6pB3dWOrD/nueuU5sduBsQ4w==
60046004

60056005
"react-native-update@file:../..":
6006-
version "10.19.6"
6006+
version "10.26.1"
60076007
dependencies:
60086008
nanoid "^3.3.3"
60096009
react-native-url-polyfill "^2.0.0"

harmony/src/main/ets/DownloadTask.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,13 @@ export class DownloadTask {
6565
0,
6666
params.targetFile.lastIndexOf('/'),
6767
);
68-
await fileIo.mkdir(targetDir);
68+
const exists = fileIo.accessSync(targetDir);
69+
if(!exists){
70+
await fileIo.mkdir(targetDir);
71+
}
6972
}
7073
} catch (error) {
74+
throw error;
7175
}
7276

7377
const response = await httpRequest.request(params.url, {
@@ -78,12 +82,11 @@ export class DownloadTask {
7882
'Content-Type': 'application/octet-stream',
7983
},
8084
});
81-
8285
if (response.responseCode > 299) {
8386
throw new Error(`Server error: ${response.responseCode}`);
8487
}
8588

86-
const contentLength = parseInt(response.header['Content-Length'] || '0');
89+
const contentLength = parseInt(response.header['content-length'] || '0');
8790
const writer = await fileIo.open(
8891
params.targetFile,
8992
fileIo.OpenMode.CREATE | fileIo.OpenMode.READ_WRITE,
@@ -102,8 +105,12 @@ export class DownloadTask {
102105
this.onProgressUpdate(received, contentLength);
103106
}
104107
await fileIo.close(writer);
105-
const stat = await fileIo.stat(params.targetFile);
106-
const fileSize = stat.size;
108+
const stats = await fileIo.stat(params.targetFile);
109+
const fileSize = stats.size;
110+
if (fileSize !== contentLength) {
111+
throw new Error(`Download incomplete: expected ${contentLength} bytes but got ${stats.size} bytes`);
112+
}
113+
107114
} catch (error) {
108115
console.error('Download failed:', error);
109116
throw error;
@@ -113,7 +120,7 @@ export class DownloadTask {
113120
}
114121

115122
private onProgressUpdate(received: number, total: number): void {
116-
this.eventHub.emit('downloadProgress', {
123+
this.eventHub.emit('RCTPushyDownloadProgress', {
117124
received,
118125
total,
119126
hash: this.hash,
@@ -288,8 +295,8 @@ export class DownloadTask {
288295
}
289296
}
290297

291-
if(entry.filename !== '.DS_Store'){
292-
await zip.decompressFile(entry.filename, params.unzipDirectory);
298+
if(fn !== '.DS_Store'){
299+
await zip.decompressFile(fn, params.unzipDirectory);
293300
}
294301
}
295302

@@ -491,4 +498,4 @@ export class DownloadTask {
491498
params.listener?.onDownloadFailed(error);
492499
}
493500
}
494-
}
501+
}

harmony/src/main/ets/EventHub.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ type EventCallback = (data: any) => void;
33
export class EventHub {
44
private static instance: EventHub;
55
private listeners: Map<string, Set<EventCallback>>;
6+
private rnInstance: any;
67

78
private constructor() {
89
this.listeners = new Map();
@@ -27,12 +28,12 @@ export class EventHub {
2728
}
2829

2930
public emit(event: string, data: any): void {
30-
this.listeners.get(event)?.forEach(callback => {
31-
try {
32-
callback(data);
33-
} catch (error) {
34-
console.error(`Error in event listener for ${event}:`, error);
35-
}
36-
});
31+
if (this.rnInstance) {
32+
this.rnInstance.emitDeviceEvent(event, data);
33+
}
34+
}
35+
36+
setRNInstance(instance: any) {
37+
this.rnInstance = instance;
3738
}
3839
}

harmony/src/main/ets/PushyTurboModule.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { BusinessError } from '@ohos.base';
77
import logger from './Logger';
88
import { UpdateModuleImpl } from './UpdateModuleImpl';
99
import { UpdateContext } from './UpdateContext';
10+
import { EventHub } from './EventHub';
1011

1112
const TAG = "PushyTurboModule"
1213

@@ -18,9 +19,8 @@ export class PushyTurboModule extends TurboModule {
1819
super(ctx);
1920
logger.debug(TAG, ",PushyTurboModule constructor");
2021
this.mUiCtx = ctx.uiAbilityContext
21-
let rnInstance = ctx.rnInstance
2222
this.context = new UpdateContext(this.mUiCtx)
23-
// rnInstance.emitDeviceEvent("Pushy",{code: err.code, message: err.message});
23+
EventHub.getInstance().setRNInstance(ctx.rnInstance)
2424
}
2525

2626

harmony/src/main/ets/UpdateContext.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,14 @@ export class UpdateContext {
3131
this.preferences = preferences.getPreferencesSync(this.context, {name:'update'});
3232
const packageVersion = this.getPackageVersion();
3333
const storedVersion = this.preferences.getSync('packageVersion', '');
34-
if (packageVersion !== storedVersion) {
35-
this.preferences.clear();
36-
this.preferences.putSync('packageVersion', packageVersion);
37-
this.preferences.flush();
38-
this.cleanUp();
34+
if(!storedVersion){
35+
this.preferences.putSync('packageVersion', packageVersion);
36+
this.preferences.flush();
37+
} else if (storedVersion && packageVersion !== storedVersion) {
38+
this.preferences.clear();
39+
this.preferences.putSync('packageVersion', packageVersion);
40+
this.preferences.flush();
41+
this.cleanUp();
3942
}
4043
} catch (e) {
4144
console.error('Failed to init preferences:', e);
@@ -137,8 +140,9 @@ export class UpdateContext {
137140
params.unzipDirectory = `${this.rootDir}/${hash}`;
138141

139142
const downloadTask = new DownloadTask(this.context);
140-
await downloadTask.execute(params);
143+
return await downloadTask.execute(params);
141144
} catch (e) {
145+
throw e;
142146
console.error('Failed to download APK patch:', e);
143147
}
144148
}
@@ -152,14 +156,13 @@ export class UpdateContext {
152156

153157
const lastVersion = this.getKv('currentVersion');
154158
this.setKv('currentVersion', hash);
155-
156159
if (lastVersion && lastVersion !== hash) {
157160
this.setKv('lastVersion', lastVersion);
158161
}
159162

160163
this.setKv('firstTime', 'true');
161164
this.setKv('firstTimeOk', 'false');
162-
this.setKv('rolledBackVersion', null);
165+
this.setKv('rolledBackVersion', "");
163166
} catch (e) {
164167
console.error('Failed to switch version:', e);
165168
}
@@ -211,7 +214,7 @@ export class UpdateContext {
211214
}
212215

213216
public getCurrentVersion() : string {
214-
const currentVersion = this.preferences.getSync('currentVersion', '') as string;
217+
const currentVersion = this.getKv('currentVersion');
215218
return currentVersion;
216219
}
217220

harmony/src/main/ets/UpdateModuleImpl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export class UpdateModuleImpl {
5656
options: { updateUrl: string; hash: string }
5757
): Promise<void> {
5858
try {
59-
await updateContext.downloadPatchFromPackage(options.updateUrl, options.hash, {
59+
return await updateContext.downloadPatchFromPackage(options.updateUrl, options.hash, {
6060
onDownloadCompleted: (params: DownloadTaskParams) => {
6161
return Promise.resolve();
6262
},

src/client.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
promiseAny,
1010
testUrls,
1111
} from './utils';
12-
import { EmitterSubscription, Platform } from 'react-native';
12+
import { EmitterSubscription, Platform, DeviceEventEmitter } from 'react-native';
1313
import { PermissionsAndroid } from './permissions';
1414
import {
1515
PushyModule,
@@ -350,14 +350,25 @@ export class Pushy {
350350
return;
351351
}
352352
if (onDownloadProgress) {
353-
Pushy.progressHandlers[hash] = pushyNativeEventEmitter.addListener(
354-
'RCTPushyDownloadProgress',
355-
progressData => {
356-
if (progressData.hash === hash) {
357-
onDownloadProgress(progressData);
358-
}
359-
},
360-
);
353+
if (Platform.OS === 'harmony') {
354+
Pushy.progressHandlers[hash] = DeviceEventEmitter.addListener(
355+
'RCTPushyDownloadProgress',
356+
progressData => {
357+
if (progressData.hash === hash) {
358+
onDownloadProgress(progressData);
359+
}
360+
},
361+
);
362+
} else {
363+
Pushy.progressHandlers[hash] = pushyNativeEventEmitter.addListener(
364+
'RCTPushyDownloadProgress',
365+
progressData => {
366+
if (progressData.hash === hash) {
367+
onDownloadProgress(progressData);
368+
}
369+
},
370+
);
371+
}
361372
}
362373
let succeeded = '';
363374
this.report({ type: 'downloading' });

0 commit comments

Comments
 (0)