Skip to content

Commit e4792bf

Browse files
Using AppSync in a dev environment #39
1 parent 26f6d00 commit e4792bf

File tree

7 files changed

+26
-6
lines changed

7 files changed

+26
-6
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ import { AppSync, InstallMode, SyncStatus } from "nativescript-app-sync";
151151
import { isIOS } from "tns-core-modules/platform";
152152

153153
AppSync.sync({
154+
enabledWhenUsingHmr: false, // this is optional and by default false so AppSync and HMR don't fight over app updates
154155
deploymentKey: isIOS ? "your-ios-deployment-key" : "your-android-deployment-key",
155156
installMode: InstallMode.ON_NEXT_RESTART, // this is the default install mode; the app updates upon the next cold boot (unless the --mandatory flag was specified while pushing the update)
156157
mandatoryInstallMode: isIOS ? InstallMode.ON_NEXT_RESUME : InstallMode.IMMEDIATE, // the default is InstallMode.ON_NEXT_RESUME which doesn't bother the user as long as the app is in the foreground. InstallMode.IMMEDIATE shows an installation prompt. Don't use that for iOS AppStore distributions because Apple doesn't want you to, but if you have an Enterprise-distributed app, go right ahead!
@@ -182,6 +183,7 @@ var SyncStatus = require("nativescript-app-sync").SyncStatus;
182183
var platform = require("tns-core-modules/platform");
183184

184185
AppSync.sync({
186+
enabledWhenUsingHmr: false, // this is optional and by default false so AppSync and HMR don't fight over app updates
185187
deploymentKey: platform.isIOS ? "your-ios-deployment-key" : "your-android-deployment-key",
186188
installMode: InstallMode.ON_NEXT_RESTART,
187189
mandatoryInstallMode: platform.isIOS ? InstallMode.ON_NEXT_RESUME : InstallMode.IMMEDIATE,

demo/demoapp/app.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
@import '~nativescript-theme-core/css/core.light.css';
1+


demo/demoapp/main-view-model.ts

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export class HelloWorldModel extends Observable {
2929
application.on(application.resumeEvent, () => {
3030
this.syncWithAppSyncServer();
3131
});
32+
33+
this.set("message", "HMR enabled: " + (typeof (<any>global).hmrRefresh === "function"));
3234
}
3335

3436
public setAppSettings() {
@@ -70,6 +72,7 @@ export class HelloWorldModel extends Observable {
7072
private syncWithAppSyncServer(): void {
7173
this.set("message", "Querying AppSync..");
7274
AppSync.sync({
75+
enabledWhenUsingHmr: false,
7376
deploymentKey: isIOS ? HelloWorldModel.APPSYNC_IOS_STAGING_KEY : HelloWorldModel.APPSYNC_ANDROID_STAGING_KEY,
7477
installMode: InstallMode.ON_NEXT_RESTART, // default InstallMode.ON_NEXT_RESTART
7578
mandatoryInstallMode: isIOS ? InstallMode.ON_NEXT_RESUME : InstallMode.IMMEDIATE, // default InstallMode.ON_NEXT_RESUME
@@ -86,6 +89,8 @@ export class HelloWorldModel extends Observable {
8689
this.set("message", `AppSync: up to date: ${updateLabel}`);
8790
} else if (syncStatus === SyncStatus.UPDATE_INSTALLED) {
8891
this.set("message", `AppSync: update installed: ${updateLabel}`);
92+
} else {
93+
this.set("message", `AppSync: status changed to: ${syncStatus}`);
8994
}
9095
});
9196
}

demo/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"ci.tslint": "npm i && tslint --config '../tslint.json' 'demoapp/**/*.ts' --exclude '**/node_modules/**' --exclude '**/typings/**'"
3232
},
3333
"dependencies": {
34-
"nativescript-app-sync": "file:../publish/package/nativescript-app-sync-1.0.5.tgz",
34+
"nativescript-app-sync": "file:../publish/package/nativescript-app-sync-1.0.6.tgz",
3535
"nativescript-theme-core": "~1.0.4",
3636
"nativescript-unit-test-runner": "0.7.0",
3737
"tns-core-modules": "~6.0.0"

src/app-sync.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import * as appSettings from "application-settings";
44
import * as AppVersion from "nativescript-appversion";
5+
import * as application from "tns-core-modules/application";
56
import { device } from "tns-core-modules/platform";
67
import { confirm } from "tns-core-modules/ui/dialogs";
7-
import * as application from "tns-core-modules/application";
88
import { TNSAcquisitionManager } from "./TNSAcquisitionManager";
99
import { TNSLocalPackage } from "./TNSLocalPackage";
1010
import { TNSRemotePackage } from "./TNSRemotePackage";
@@ -50,6 +50,11 @@ export enum SyncStatus {
5050
*/
5151
ERROR = <any>"ERROR",
5252

53+
/**
54+
* Returned if HMR is enabled and not overridden by the user.
55+
*/
56+
SKIPPING_BECAUSE_HMR_ENABLED = <any>"SKIPPING_BECAUSE_HMR_ENABLED",
57+
5358
/**
5459
* There is an ongoing sync in progress, so this attempt to sync has been aborted.
5560
*/
@@ -95,10 +100,12 @@ export class AppSync {
95100
throw new Error("Missing deploymentKey, pass it as part of the first parameter of the 'sync' function: { deploymentKey: 'your-key' }");
96101
}
97102

98-
if (AppSync.syncInProgress) {
99-
syncCallback && syncCallback(SyncStatus.IN_PROGRESS);
103+
// skip AppSync when HMR is detected, unless it's explicitly allowed
104+
if (typeof (<any>global).hmrRefresh === "function" && !options.enabledWhenUsingHmr) {
105+
syncCallback && syncCallback(SyncStatus.SKIPPING_BECAUSE_HMR_ENABLED);
100106
return;
101107
}
108+
102109
AppSync.syncInProgress = true;
103110

104111
// by default, use our Cloud server

src/code-push-lib.d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,12 @@ interface SyncOptions extends InstallOptions {
334334
* Overrides the default server URL (https://appsync-server.nativescript.org/).
335335
*/
336336
serverUrl?: string;
337+
338+
/**
339+
* By default we ignore AppSync updates when running with HMR.
340+
* But if you feel adventurous you can override that behaviour by setting this to true.
341+
*/
342+
enabledWhenUsingHmr?: boolean;
337343
}
338344

339345
/**

src/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-app-sync",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"description": "Use AppSync to hot deploy updates to your app.",
55
"main": "app-sync",
66
"typings": "index.d.ts",

0 commit comments

Comments
 (0)