cordova-electron icon indicating copy to clipboard operation
cordova-electron copied to clipboard

feat: keep callback

Open adrian-bueno opened this issue 3 years ago • 0 comments

Platforms affected

  • Electron

Motivation and Context

Android and iOS plugins have a "keep callback" option that let us send multiple responses like an observable.
In Electron this feature doesn't exist yet.

Android plugin example:

final PluginResult result = new PluginResult(PluginResult.Status.OK, "Hello");
result.setKeepCallback(true);
callbackContext.sendPluginResult(result);

I found this problem when I tried to build a plugin that sends events when a USB is connected/disconnected.

Description

I tried to find a solution that doesn't break existing plugins, and decided to add a new exec$ method.

The $ is used to identify which functions could return multiple values.

The exec$ method, instead of using ipcRenderer.invoke(), uses ipcRenderer.on() and ipcRenderer.send() with a new channel cdv-plugin-exec$. To identify requests, a unique ID is used Date.now() + Math.random() (this should be random enough).

Testing

I have run npm run test.

I have also added an example in this plugin tests/spec/fixtures/test-app-with-electron-plugin/plugins/cordova-plugin-sample.

I don't know if there is currently a way to automatically test code inside path tests/spec/fixtures.

Checklist

  • [x] I've run the tests to see all new and existing tests pass
  • [ ] I added automated test coverage as appropriate for this change
  • [x] Commit is prefixed with (platform) if this change only applies to one platform (e.g. (android))
  • [x] If this Pull Request resolves an issue, I linked to the issue in the text above (and used the correct keyword to close issues using keywords)
  • [ ] I've updated the documentation if necessary

adrian-bueno avatar Jan 23 '22 21:01 adrian-bueno