apache / cordova-ios

Apache Cordova iOS
https://cordova.apache.org/
Apache License 2.0
2.15k stars 987 forks source link

Define exec proxy with js-module #1159

Open miroslavvojtus opened 2 years ago

miroslavvojtus commented 2 years ago

Feature Request

Motivation Behind Feature

I have some logic which is iOS specific but I do not want to write it in native source code. I would like just to write simple js-module which should after registration of relevant proxy be able to the iOS specific job done.

Right now any cordova/exec on iOS bypasses the registered cordova/exec/proxy registered proxy in case there is no need to implement the code natively.

Feature Description

I would like to define ios specific js-module like:

require('codrova/exec/proxy').add('MyService', {
  init: (success, fail) => {
    console.log('Do my job');
    success();
  }
})

In platfrom brige js-module code you could call:

require('cordova/exec')(() => console.loog('initialized'), console.error, 'MyService', 'init')

Currently I would get the following error from native iOS: "Plugin 'MyService' not found, or is not a CDVPlugin. Check your plugin mapping congfig.xml."

I would suggest one of:

Alternatives or Workarounds

I have to do the logic manually in my bridge code:

module.exports = {
  init: (success, fail) => {
    const proxyAction = require('cordova/exec/proxy').get('MyService', 'init');
    if (typeof proxyAction === 'function') {
      proxyAction(success, fail);
    } else {
      require('cordova/exec')(success, fail,  'MyService', 'init');
    }
  }
}
breautek commented 2 years ago

I'm failing to understand what you're really trying to do. It sounds like there might be a misunderstanding of the Cordova's architecture here.

I have some logic which is iOS specific but I do not want to write it in native source code.

The exec function is for communicating with native, if you don't need to call onto native, then don't use the function.

If you need platform-specific behaviour, you can use the pre-existing cordova.platform, or you can use the `cordova-plugin-device package for better insight of the environment, (e.g. os version) the code is currently in to determine when to enter platform-specific logic.

The exec method is mainly used by cordova plugins to call onto native side.

Can you explain why this is necessary?

miroslavvojtus commented 2 years ago

Well, it is more convenience than necessity. I write a plugin which has some parts implemented on broswer, electron, windows, ios. Some behavior does not make sense to implement native but makes native related decisions, thus having js proxy would be convenient.

Browser JS consumes exposed cordova API. Platform specific code regardless if native or not implements it.

In windows I am able to do that 😒 Either write and use WinJS solely or write runtimeComponent.

Iknow it is different from platfom to platform but now I either write common js module bridge to be able to pick a proxy first, or i write platform coditions dierectly in common js module what is discouraged.

But if you consider it as nonlegitimate I understand if you close this. Nevertheless I do not get why there is such an option on windows (and more or less on electron).

Dňa po 11. 10. 2021, 16:02 Norman Breau @.***> napísal(a):

I'm failing to understand what you're really trying to do. It sounds like there might be a misunderstanding of the Cordova's architecture here.

I have some logic which is iOS specific but I do not want to write it in native source code.

The exec function is for communicating with native, if you don't need to call onto native, then don't use the function.

If you need platform-specific behaviour, you can use the pre-existing cordova.platform, or you can use the `cordova-plugin-device https://cordova.apache.org/docs/en/10.x/reference/cordova-plugin-device/ package for better insight of the environment, (e.g. os version) the code is currently in to determine when to enter platform-specific logic.

The exec method is mainly used by cordova plugins to call onto native side.

Can you explain why this is necessary?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/apache/cordova-ios/issues/1159#issuecomment-940060140, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABS3MV4J7C3TMT4SWF35RTUGLU7FANCNFSM5FXZMB4Q . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.