Skip to content

Commit 8c86eb2

Browse files
committed
feature: keep socket always open
1 parent 0de063d commit 8c86eb2

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

test-app/app/src/debug/java/com/tns/NativeScriptSyncService.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -131,19 +131,19 @@ public LiveSyncWorker(LocalSocket systemSocket) throws IOException {
131131
}
132132

133133
public void run() {
134-
boolean exceptionWhileLivesyncing = false;
135134
try {
136135

137136
output.write(context.getPackageName().getBytes());
138137

139138
} catch (IOException e) {
140139
logger.write(String.format("Error while LiveSyncing: Client socket might be closed!", e.toString()));
141-
exceptionWhileLivesyncing = true;
142140
e.printStackTrace();
143141
}
144142
try {
145143
do {
146144
int operation = getOperation();
145+
146+
System.out.println("Operation: " + input.available());
147147
if (operation == DELETE_FILE_OPERATION) {
148148

149149
String fileName = getFileName();
@@ -155,20 +155,20 @@ public void run() {
155155
byte[] content = getFileContent(fileName);
156156
createOrOverrideFile(fileName, content);
157157

158-
159158
} else if (operation == DEFAULT_OPERATION) {
160159
logger.write("LiveSync: input stream is empty!");
161160
break;
162161
} else {
163162
throw new IllegalArgumentException(String.format("\nLiveSync: Operation not recognised. %s", LIVESYNC_ERROR_SUGGESTION));
164163
}
165164

166-
} while (this.input.available() > 0);
165+
//TODO: do debouncing
166+
runtime.runScript(new File(NativeScriptSyncService.this.context.getFilesDir(), "internal/livesync.js"));
167+
} while (true);
167168

168169
} catch (Exception e) {
169170
logger.write(String.format("Error while LiveSyncing: %s", e.toString()));
170171
e.printStackTrace();
171-
exceptionWhileLivesyncing = true;
172172
flushInputStream();
173173
} catch (Throwable e) {
174174
logger.write(String.format("%s(%s): Error while LiveSyncing.\nOriginal Exception: %s", Thread.currentThread().getName(), Thread.currentThread().getId(), e.toString()));
@@ -179,14 +179,11 @@ public void run() {
179179
}
180180
}
181181

182-
if (!exceptionWhileLivesyncing) {
183-
runtime.runScript(new File(NativeScriptSyncService.this.context.getFilesDir(), "internal/livesync.js"));
184-
}
185182
}
186183

187184
private void flushInputStream() {
188185
try {
189-
this.input.skip(1000000);
186+
this.input.skip(Long.MAX_VALUE);
190187
} catch (IOException e) {
191188
}
192189
}
@@ -205,8 +202,6 @@ private int getOperation() {
205202
}
206203
operation = Integer.parseInt(new String(operationBuff));
207204

208-
} catch (NumberFormatException e) {
209-
throw new IllegalStateException(String.format("\nLiveSync: failed to parse %s. %s\nOriginal Exception: %s", OPERATION, LIVESYNC_ERROR_SUGGESTION, e.toString()));
210205
} catch (Exception e) {
211206
throw new IllegalStateException(String.format("\nLiveSync: failed to parse %s. %s\nOriginal Exception: %s", OPERATION, LIVESYNC_ERROR_SUGGESTION, e.toString()));
212207
}

0 commit comments

Comments
 (0)