Skip to content

Commit 713a817

Browse files
fix(preview): remove prepareReadyEvent handler correctly on preview
`tns preview` (and using SK) should remove its handler for prepareControllers' `prepareReadyEvent` handler. However, this does not happend due to two reasons: - we try to remove the handler from prepareControllers' event, but this handler is attached to `preparController`, so it actually remains and causes multiple issues in long living processes, when CLI is used as a library - the event handler is not cached correctly as we attach its bound version, but cache it without the binding. So you cannot remove it. This causes issues when using preview in Sidekick, closing the current app, open it again and try to livesync a change - we have many handlers for prepareReadyEvent and this causes multiple livesync operations.
1 parent b7a32da commit 713a817

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

lib/controllers/preview-app-controller.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export class PreviewAppController extends EventEmitter implements IPreviewAppCon
4343
this.$previewSdkService.stop();
4444
this.$previewDevicesService.updateConnectedDevices([]);
4545
if (this.prepareReadyEventHandler) {
46-
this.removeListener(PREPARE_READY_EVENT_NAME, this.prepareReadyEventHandler);
46+
this.$prepareController.removeListener(PREPARE_READY_EVENT_NAME, this.prepareReadyEventHandler);
4747
this.prepareReadyEventHandler = null;
4848
}
4949
}
@@ -83,10 +83,12 @@ export class PreviewAppController extends EventEmitter implements IPreviewAppCon
8383
await this.$previewAppPluginsService.comparePluginsOnDevice(data, device);
8484

8585
if (!this.prepareReadyEventHandler) {
86-
this.prepareReadyEventHandler = async (currentPrepareData: IFilesChangeEventData) => {
86+
const handler = async (currentPrepareData: IFilesChangeEventData) => {
8787
await this.handlePrepareReadyEvent(data, currentPrepareData);
8888
};
89-
this.$prepareController.on(PREPARE_READY_EVENT_NAME, this.prepareReadyEventHandler.bind(this));
89+
90+
this.prepareReadyEventHandler = handler.bind(this);
91+
this.$prepareController.on(PREPARE_READY_EVENT_NAME, this.prepareReadyEventHandler);
9092
}
9193

9294
data.env = data.env || {};

0 commit comments

Comments
 (0)