Skip to content

Commit 2b894f1

Browse files
committed
chore: CR issues
1 parent 735b9b0 commit 2b894f1

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed

src/client/WebsocketClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ function getAuthentication(options: WebsocketClientOptions) {
172172
return { 'x-api-key': options.authentication.apiKey };
173173
}
174174

175-
return null;
175+
return {};
176176
}
177177

178178
export type EventTypeProject =

src/utils/pullWatch/watchHandler.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,21 @@ export async function startWatching(
2727
info('Watching for translation changes... Press Ctrl+C to stop.');
2828

2929
let pulling = false;
30+
31+
/**
32+
* Pending handles the situation when changes are detected while the pull is
33+
* already in progress.
34+
*/
35+
let pending = false;
3036
let debounceTimer: NodeJS.Timeout | undefined;
3137
let pollingTimer: NodeJS.Timeout | undefined;
3238
let lastExecutionTime = 0;
3339

3440
const executePull = async (lastModified?: string) => {
35-
if (pulling) return;
41+
if (pulling) {
42+
pending = true;
43+
return;
44+
}
3645
pulling = true;
3746
lastExecutionTime = Date.now();
3847
try {
@@ -45,6 +54,11 @@ export async function startWatching(
4554
error('Error during pull: ' + e.message);
4655
debug(e);
4756
} finally {
57+
// If there was a pending pull (data changed when pulling), execute it now
58+
if (pending) {
59+
pending = false;
60+
void executePull();
61+
}
4862
pulling = false;
4963
}
5064
};
@@ -68,7 +82,9 @@ export async function startWatching(
6882

6983
// Polling mechanism as backup
7084
const startPolling = () => {
71-
clearInterval(pollingTimer);
85+
if (pollingTimer) {
86+
clearInterval(pollingTimer);
87+
}
7288
const poll = async () => {
7389
if (pulling) return;
7490
debug('Polling for changes...');
@@ -83,7 +99,6 @@ export async function startWatching(
8399
serverUrl: new URL(apiUrl).origin,
84100
authentication: { apiKey: apiKey },
85101
onError: (error) => {
86-
console.error('Error: handling auth error');
87102
AuthErrorHandler(client)
88103
.handleAuthErrors(error, shutdown)
89104
.catch((err: any) => {
@@ -99,7 +114,7 @@ export async function startWatching(
99114

100115
const channel = `/projects/${projectId}/translation-data-modified` as const;
101116

102-
let unsubscribe: () => void | undefined;
117+
let unsubscribe: (() => void) | undefined;
103118

104119
function subscribe() {
105120
unsubscribe = wsClient.subscribe(channel, () => {

test/e2e/pullWatch.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { setupTemporaryFolder, TMP_FOLDER } from './utils/tmp.js';
2-
import { run } from './utils/run.js';
2+
import { run, runWithKill } from './utils/run.js';
33
import './utils/toMatchContentsOf.js';
44
import {
55
createPak,
@@ -31,7 +31,7 @@ describe('Pull watch', () => {
3131
it('pulls strings from Tolgee with watch', async () => {
3232
await util.changeLocalizationData('A key');
3333

34-
run(
34+
const { kill } = runWithKill(
3535
['pull', '--api-key', pak, '--path', TMP_FOLDER, '--watch', '--verbose'],
3636
undefined,
3737
300000
@@ -46,5 +46,6 @@ describe('Pull watch', () => {
4646
// Tests that it pulls after change...
4747
await util.changeLocalizationData('Another key');
4848
await util.waitFilesystemDataUpdated('Another key');
49+
kill('SIGTERM');
4950
});
5051
});

test/e2e/utils/run.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,15 @@ export async function run(
103103
const cliProcess = spawn(args, false, env);
104104
return runProcess(cliProcess, timeout);
105105
}
106+
107+
export function runWithKill(
108+
args: string[],
109+
env?: Record<string, string>,
110+
timeout = 10e3
111+
) {
112+
const cliProcess = spawn(args, false, env);
113+
return {
114+
promise: runProcess(cliProcess, timeout),
115+
kill: (signal: NodeJS.Signals) => cliProcess.kill(signal),
116+
};
117+
}

0 commit comments

Comments
 (0)