Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 08283ef

Browse files
author
Geoffrey Goh
committed
CR feedback
1 parent 34f1ca4 commit 08283ef

12 files changed

+104
-112
lines changed

windows/CodePush.csproj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,8 @@
109109
<ItemGroup>
110110
<Compile Include="CodePushReactPackage.cs" />
111111
<Compile Include="CodePushConstants.cs" />
112-
<Compile Include="CodePushResumeListener.cs" />
112+
<Compile Include="CodePushLifecycleEventListener.cs" />
113113
<Compile Include="InstallMode.cs" />
114-
<Compile Include="CodePushInvalidUpdateException.cs" />
115114
<Compile Include="CodePushNativeModule.cs" />
116115
<Compile Include="UpdateManager.cs" />
117116
<Compile Include="UpdateState.cs" />

windows/CodePushInvalidUpdateException.cs

Lines changed: 0 additions & 12 deletions
This file was deleted.

windows/CodePushResumeListener.cs renamed to windows/CodePushLifecycleEventListener.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33

44
namespace CodePush.ReactNative
55
{
6-
internal class CodePushResumeListener : ILifecycleEventListener
6+
internal class CodePushLifecycleEventListener : ILifecycleEventListener
77
{
88
private DateTime? _lastSuspendDate = null;
9-
private CodePushNativeModule _codePushNativeModule;
9+
private Action _loadBundleAction;
1010

1111
internal int MinimumBackgroundDuration { get; set; }
1212

13-
internal CodePushResumeListener(CodePushNativeModule codePushNativeModule, int minimumBackgroundDuration)
13+
internal CodePushLifecycleEventListener(Action loadBundleAction, int minimumBackgroundDuration)
1414
{
15-
_codePushNativeModule = codePushNativeModule;
15+
_loadBundleAction = loadBundleAction;
1616
MinimumBackgroundDuration = minimumBackgroundDuration;
1717
}
1818

@@ -29,12 +29,7 @@ public void OnResume()
2929
double durationInBackground = (new DateTime() - (DateTime)_lastSuspendDate).TotalSeconds;
3030
if (durationInBackground >= MinimumBackgroundDuration)
3131
{
32-
Action loadBundleAction = async () =>
33-
{
34-
await _codePushNativeModule.LoadBundle();
35-
};
36-
37-
_codePushNativeModule.Context.RunOnNativeModulesQueueThread(loadBundleAction);
32+
_loadBundleAction.Invoke();
3833
}
3934
}
4035
}

windows/CodePushNativeModule.cs

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using ReactNative.Modules.Core;
55
using System;
66
using System.Collections.Generic;
7+
using System.IO;
78
using System.Reflection;
89
using System.Threading.Tasks;
910
using Windows.Web.Http;
@@ -12,7 +13,7 @@ namespace CodePush.ReactNative
1213
{
1314
internal class CodePushNativeModule : ReactContextNativeModuleBase
1415
{
15-
private CodePushResumeListener _codePushLifecycleEventListener = null;
16+
private CodePushLifecycleEventListener _codePushLifecycleEventListener = null;
1617
private ReactContext _reactContext;
1718
private CodePushReactPackage _codePush;
1819

@@ -36,12 +37,12 @@ public override IReadOnlyDictionary<string, object> Constants
3637
{
3738
return new Dictionary<string, object>
3839
{
39-
{ "codePushInstallModeImmediate", InstallMode.ON_NEXT_RESTART },
40-
{ "codePushInstallModeOnNextResume", InstallMode.ON_NEXT_RESUME },
41-
{ "codePushInstallModeOnNextRestart", InstallMode.ON_NEXT_RESTART },
42-
{ "codePushUpdateStateRunning", UpdateState.RUNNING },
43-
{ "codePushUpdateStatePending", UpdateState.PENDING },
44-
{ "codePushUpdateStateLatest", UpdateState.LATEST },
40+
{ "codePushInstallModeImmediate", InstallMode.Immediate },
41+
{ "codePushInstallModeOnNextResume", InstallMode.OnNextResume },
42+
{ "codePushInstallModeOnNextRestart", InstallMode.OnNextRestart },
43+
{ "codePushUpdateStateRunning", UpdateState.Running },
44+
{ "codePushUpdateStatePending", UpdateState.Pending },
45+
{ "codePushUpdateStateLatest", UpdateState.Lastest },
4546
};
4647
}
4748
}
@@ -70,9 +71,12 @@ await _codePush.UpdateManager.DownloadPackage(
7071
return;
7172
}
7273

73-
var downloadProgress = new JObject();
74-
downloadProgress["totalBytes"] = progress.TotalBytesToReceive;
75-
downloadProgress["receivedBytes"] = progress.BytesReceived;
74+
var downloadProgress = new JObject()
75+
{
76+
{ "totalBytes", progress.TotalBytesToReceive },
77+
{ "receivedBytes", progress.BytesReceived }
78+
};
79+
7680
_reactContext
7781
.GetJavaScriptModule<RCTDeviceEventEmitter>()
7882
.emit(CodePushConstants.DownloadProgressEventName, downloadProgress);
@@ -83,7 +87,7 @@ await _codePush.UpdateManager.DownloadPackage(
8387
JObject newPackage = await _codePush.UpdateManager.GetPackage((string)updatePackage[CodePushConstants.PackageHashKey]);
8488
promise.Resolve(newPackage);
8589
}
86-
catch (CodePushInvalidUpdateException e)
90+
catch (InvalidDataException e)
8791
{
8892
CodePushUtils.Log(e.ToString());
8993
SettingsManager.SaveFailedUpdate(updatePackage);
@@ -105,9 +109,9 @@ public void getConfiguration(IPromise promise)
105109
var config = new JObject
106110
{
107111
{ "appVersion", _codePush.AppVersion },
108-
{ "deploymentKey", _codePush.DeploymentKey },
109-
{ "serverUrl", CodePushConstants.CodePushServerUrl },
110112
{ "clientUniqueId", CodePushUtils.GetDeviceId() },
113+
{ "deploymentKey", _codePush.DeploymentKey },
114+
{ "serverUrl", CodePushConstants.CodePushServerUrl }
111115
};
112116

113117
// TODO generate binary hash
@@ -136,16 +140,16 @@ public void getUpdateMetadata(int updateState, IPromise promise)
136140
if (currentPackage[CodePushConstants.PackageHashKey] != null)
137141
{
138142
var currentHash = (string)currentPackage[CodePushConstants.PackageHashKey];
139-
currentUpdateIsPending = _codePush.IsPendingUpdate(currentHash);
143+
currentUpdateIsPending = SettingsManager.IsPendingUpdate(currentHash);
140144
}
141145

142-
if (updateState == (int)UpdateState.PENDING && !currentUpdateIsPending)
146+
if (updateState == (int)UpdateState.Pending && !currentUpdateIsPending)
143147
{
144148
// The caller wanted a pending update
145149
// but there isn't currently one.
146150
promise.Resolve("");
147151
}
148-
else if (updateState == (int)UpdateState.RUNNING && currentUpdateIsPending)
152+
else if (updateState == (int)UpdateState.Running && currentUpdateIsPending)
149153
{
150154
// The caller wants the running update, but the current
151155
// one is pending, so we need to grab the previous.
@@ -187,15 +191,23 @@ public void installUpdate(JObject updatePackage, int installMode, int minimumBac
187191
{
188192
Action installUpdateAction = async () =>
189193
{
190-
await _codePush.UpdateManager.InstallPackage(updatePackage, _codePush.IsPendingUpdate(null));
194+
await _codePush.UpdateManager.InstallPackage(updatePackage, SettingsManager.IsPendingUpdate(null));
191195
var pendingHash = (string)updatePackage[CodePushConstants.PackageHashKey];
192196
SettingsManager.SavePendingUpdate(pendingHash, /* isLoading */false);
193-
if (installMode == (int)InstallMode.ON_NEXT_RESUME)
197+
if (installMode == (int)InstallMode.OnNextResume)
194198
{
195199
if (_codePushLifecycleEventListener == null)
196200
{
197201
// Ensure we do not add the listener twice.
198-
_codePushLifecycleEventListener = new CodePushResumeListener(this, minimumBackgroundDuration);
202+
Action loadBundleAction = () =>
203+
{
204+
Context.RunOnNativeModulesQueueThread(async () =>
205+
{
206+
await LoadBundle();
207+
});
208+
};
209+
210+
_codePushLifecycleEventListener = new CodePushLifecycleEventListener(loadBundleAction, minimumBackgroundDuration);
199211
_reactContext.AddLifecycleEventListener(_codePushLifecycleEventListener);
200212
}
201213
else
@@ -213,7 +225,7 @@ public void installUpdate(JObject updatePackage, int installMode, int minimumBac
213225
[ReactMethod]
214226
public void isFailedUpdate(string packageHash, IPromise promise)
215227
{
216-
promise.Resolve(_codePush.IsFailedHash(packageHash));
228+
promise.Resolve(SettingsManager.IsFailedHash(packageHash));
217229
}
218230

219231
[ReactMethod]
@@ -245,7 +257,7 @@ public void restartApp(bool onlyIfUpdateIsPending)
245257
{
246258
// If this is an unconditional restart request, or there
247259
// is current pending update, then reload the app.
248-
if (!onlyIfUpdateIsPending || _codePush.IsPendingUpdate(null))
260+
if (!onlyIfUpdateIsPending || SettingsManager.IsPendingUpdate(null))
249261
{
250262
await LoadBundle();
251263
}

windows/CodePushReactPackage.cs

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public CodePushReactPackage(string deploymentKey, ReactPage mainPage)
4848
CurrentInstance = this;
4949
}
5050

51-
// Public methods
51+
#region Public methods
5252
public IReadOnlyList<Type> CreateJavaScriptModulesConfig()
5353
{
5454
return new List<Type>();
@@ -131,8 +131,9 @@ public async Task<string> GetJavaScriptBundleFileAsync(string assetsBundleFileNa
131131
return binaryJsBundleUrl;
132132
}
133133
}
134+
#endregion
134135

135-
// Internal methods
136+
#region Internal methods
136137
internal async Task<long> GetBinaryResourcesModifiedTime()
137138
{
138139
var assetJSBundleFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(CodePushConstants.AssetsBundlePrefix + AssetsBundleFileName));
@@ -169,40 +170,15 @@ internal void InitializeUpdateAfterRestart()
169170
}
170171
}
171172

172-
internal bool IsFailedHash(string packageHash)
173-
{
174-
JArray failedUpdates = SettingsManager.GetFailedUpdates();
175-
if (packageHash != null)
176-
{
177-
foreach (var failedPackage in failedUpdates)
178-
{
179-
var failedPackageHash = (string)failedPackage[CodePushConstants.PackageHashKey];
180-
if (packageHash.Equals(failedPackageHash))
181-
{
182-
return true;
183-
}
184-
}
185-
}
186-
187-
return false;
188-
}
189-
190-
internal bool IsPendingUpdate(string packageHash)
191-
{
192-
JObject pendingUpdate = SettingsManager.GetPendingUpdate();
193-
return pendingUpdate != null &&
194-
!(bool)pendingUpdate[CodePushConstants.PendingUpdateIsLoadingKey] &&
195-
(packageHash == null || ((string)pendingUpdate[CodePushConstants.PendingUpdateHashKey]).Equals(packageHash));
196-
}
197-
198173
internal async Task ClearUpdates()
199174
{
200175
await UpdateManager.ClearUpdates();
201176
SettingsManager.RemovePendingUpdate();
202177
SettingsManager.RemoveFailedUpdates();
203178
}
179+
#endregion
204180

205-
// Private methods
181+
#region Private methods
206182
private async Task ClearReactDevBundleCache()
207183
{
208184
var devBundleCacheFile = (StorageFile) await ApplicationData.Current.LocalFolder.TryGetItemAsync(CodePushConstants.ReactDevBundleCacheFileName);
@@ -219,5 +195,6 @@ private async Task RollbackPackage()
219195
await UpdateManager.RollbackPackage();
220196
SettingsManager.RemovePendingUpdate();
221197
}
198+
#endregion
222199
}
223200
}

windows/CodePushUtils.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ internal static string GetDeviceId()
4242
{
4343
HardwareToken token = HardwareIdentification.GetPackageSpecificToken(null);
4444
IBuffer hardwareId = token.Id;
45-
DataReader dataReader = DataReader.FromBuffer(hardwareId);
45+
var dataReader = DataReader.FromBuffer(hardwareId);
4646

47-
byte[] bytes = new byte[hardwareId.Length];
47+
var bytes = new byte[hardwareId.Length];
4848
dataReader.ReadBytes(bytes);
4949

5050
return BitConverter.ToString(bytes);

windows/FileUtils.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace CodePush.ReactNative
66
{
77
internal class FileUtils
88
{
9-
internal async static Task MergeDirectories(StorageFolder source, StorageFolder target)
9+
internal async static Task MergeFolders(StorageFolder source, StorageFolder target)
1010
{
1111
foreach (StorageFile sourceFile in await source.GetFilesAsync())
1212
{
@@ -16,7 +16,7 @@ internal async static Task MergeDirectories(StorageFolder source, StorageFolder
1616
foreach (StorageFolder sourceDirectory in await source.GetFoldersAsync())
1717
{
1818
StorageFolder nextTargetSubDir = await target.CreateFolderAsync(sourceDirectory.Name, CreationCollisionOption.OpenIfExists);
19-
await MergeDirectories(sourceDirectory, nextTargetSubDir);
19+
await MergeFolders(sourceDirectory, nextTargetSubDir);
2020
}
2121
}
2222
}

windows/InstallMode.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
{
33
enum InstallMode
44
{
5-
IMMEDIATE = 0,
6-
ON_NEXT_RESTART = 1,
7-
ON_NEXT_RESUME = 2
5+
Immediate,
6+
OnNextRestart,
7+
OnNextResume
88
}
99
}

0 commit comments

Comments
 (0)