HomePhabricator
Diffusion Comm afc218bf615f

[native] implement method to run synchronous cancellable task in CommCoreModule

Description

[native] implement method to run synchronous cancellable task in CommCoreModule

Summary:
context: ENG-2139

Introduced scheduleOrRunCancellable which will allow scheduling a task from the synchronous call that could be canceled while atomic flag in database singleton tasksCancelled will be set to true.

Using the error message as constant TASK_CANCELLED not a meaningful string because later in javascript we will need to indicate an error only by detecting if its message is equal to a given string or contain a given string.

Test Plan:

  1. Build app for both iOS and Android
  2. Add changes with CommCoreModule methods which will simplify testing: D5582
  3. Run this code to check if this works as supposed to:
try {
  commCoreModule.printMessageAndWaitSync('sync task 1');
} catch (e) {
  console.log('caught: ', e);
}
try {
  commCoreModule.printMessageAndWaitSync('sync task 2');
} catch (e) {
  console.log('caught: ', e);
}
try {
  await commCoreModule.cancelTasks();
} catch (e) {
  console.log('caught: ', e);
}
try {
  commCoreModule.printMessageAndWaitSync('sync task 3');
} catch (e) {
  console.log('caught: ', e);
}
try {
  commCoreModule.printMessageAndWaitSync('sync task 4');
} catch (e) {
  console.log('caught: ', e);
}
try {
  await commCoreModule.runTasks();
} catch (e) {
  console.log('caught: ', e);
}
try {
  commCoreModule.printMessageAndWaitSync('sync task 5');
} catch (e) {
  console.log('caught: ', e);
}

result:

2022-11-09 14:17:50.568369+0100 Comm[88845:5799175] COMM: Starting sync task: sync task 1
2022-11-09 14:17:50.572780+0100 Comm[88845:5799175] COMM: Ending sync task: sync task 1
2022-11-09 14:17:50.579646+0100 Comm[88845:5799175] COMM: Starting sync task: sync task 2
2022-11-09 14:17:50.582453+0100 Comm[88845:5799175] COMM: Ending sync task: sync task 2
2022-11-09 14:17:50.631433+0100 Comm[88845:5799137] [javascript] 'caught: ', [Error: Exception in HostFunction: TASK_CANCELLED]
2022-11-09 14:17:50.631909+0100 Comm[88845:5799137] [javascript] 'caught: ', [Error: Exception in HostFunction: TASK_CANCELLED]
2022-11-09 14:17:51.078927+0100 Comm[88845:5799175] COMM: Starting sync task: sync task 5
2022-11-09 14:17:51.082612+0100 Comm[88845:5799175] COMM: Ending sync task: sync task 5

Reviewers: marcin, jon, tomek, atul

Reviewed By: marcin, tomek

Subscribers: ashoat, tomek, atul, abosh

Differential Revision: https://phab.comm.dev/D5577