HomePhabricator
Diffusion Comm 4cb87f3b1daf

[native] implement method to run asynchronous cancellable task in…

Description

[native] implement method to run asynchronous cancellable task in CommCoreModule and return error via promise

Summary:
context: ENG-2139

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

In this scenario an error could not be thrown because it will kill the database thread, it has to be returned via promise and handled in js.

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 {
  await commCoreModule.printMessageAndWaitAsync('async task 1');
  await commCoreModule.printMessageAndWaitAsync('async task 2');
} catch (e) {
  console.log('caught: ', e);
}

try {
  await commCoreModule.cancelTasks();
} catch (e) {
  console.log('caught: ', e);
}

try {
  await commCoreModule.printMessageAndWaitAsync('async task 3');
  await commCoreModule.printMessageAndWaitAsync('async task 4');
} catch (e) {
  console.log('caught: ', e);
}

try {
  await commCoreModule.runTasks();
} catch (e) {
  console.log('caught: ', e);
}

try {
  await commCoreModule.printMessageAndWaitAsync('async task 5');
} catch (e) {
  console.log('caught: ', e);
}

result:

2022-11-09 14:24:10.079178+0100 Comm[88845:5799175] COMM: Starting async task: async task 1
2022-11-09 14:24:10.084171+0100 Comm[88845:5799175] COMM: Ending async task: async task 1
2022-11-09 14:24:10.097871+0100 Comm[88845:5799175] COMM: Starting async task: async task 2
2022-11-09 14:24:10.102069+0100 Comm[88845:5799175] COMM: Ending async task: async task 2
2022-11-09 14:24:10.114216+0100 Comm[88845:5799137] [javascript] 'caught: ', { message: 'TASK_CANCELLED' }
2022-11-09 14:24:10.115071+0100 Comm[88845:5799175] COMM: Starting async task: async task 5
2022-11-09 14:24:10.117837+0100 Comm[88845:5799175] COMM: Ending async task: async task 5

which demonstrates that we can not add a new task when taskCancelled: true, additionally we can remove await while calling commCoreModule.printMessageAndWaitAsync which will result that almost all (depending if the task will manage to execute) will throw an error that task was canceled - it's a scenario when tasks are in the queue but are removed without executing.

Reviewers: marcin, jon, tomek, atul

Reviewed By: marcin, tomek

Subscribers: ashoat, tomek, atul, abosh

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