Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added basic browser support #6

Closed
wants to merge 3 commits into from
Closed

Added basic browser support #6

wants to merge 3 commits into from

Conversation

larrybahr
Copy link

Added basic browser support so no error is thrown in browser. Currently I have to manually remove this plugin from Cordova before I can build for browser. This will fix the issue.

Larry Bahr added 2 commits April 10, 2017 17:53
Added basic browser support so no error is thrown in browser
@larrybahr
Copy link
Author

After looking at pull request #2 I agree that the plug-in should not just crash on a platform that is not supported. It makes more sense to just include dependencies on platforms that are supported and do nothing on ones that are not supported. I have updated my pull request to include those changes.

@becvert
Copy link
Owner

becvert commented Apr 12, 2017

I was about to let you know about #2.
I rather move js-module into each supported platform too than add a browser platform.
It does not make much sense to me to add the browser platform to this plugin, so I'll just do the above.

@larrybahr
Copy link
Author

That works for me. I updated the pull request with the changes so you can merge them.

@becvert
Copy link
Owner

becvert commented Apr 19, 2017

@pke
Do we need to add the js-module to windows too?

<platform name="windows">    
 +        <js-module src="www/device-name.js" name="DeviceName">
 +            <clobbers target="cordova.plugins.deviceName" />
 +        </js-module>
          <js-module src="src/windows/DeviceNameProxy.js" name="DeviceNameProxy">
              <runs />
          </js-module>

@pke
Copy link
Contributor

pke commented Apr 19, 2017

@becvert yes

I still wonder why the browser platform would crash with the module in the global section?!

@larrybahr
Copy link
Author

I found that the device-name.js was not able to be loaded. It makes sense that cordova would not add any cordova-plugin-device-name files to browser builds if browser was not supported by the plug-in.

@becvert
Copy link
Owner

becvert commented Apr 20, 2017

To sum up:
<js-module> is added to each installed platform regardless of the supported <platform> in plugin.xml.
In browser, an alert box pops up [DeviceName] Error initializing Cordova: Missing Command Error from an errorCallback in device-name.js
Cordova logs Error: exec proxy not found for :: DeviceName :: get in the console.
In consequence the onDeviceNameReady channel is not fired, then deviceready has not fired after 5 seconds.

  • option number 1: move js-module to each supported platform
  • option number 2: silence the error and always fire deviceNameReady
  • option number 3: just provide the async get() method and remove the initialization of deviceName.name

note that even with the first option, on failure (very unlikely though), the app will crash.

What do you think?

@larrybahr
Copy link
Author

I do not get any alert box because device-name.js does not exist. That is my issue; device-name.js is not bundled with the browser build so no error handling is taking place. I do get the Cordova logs in my console and as a result of the errors Cordova does not fire any events.

  • Option number 1 is the best solution because you load files based on what is needed by each platform.
  • Option number 2 is not a solution because it is masking the error. The file would still not be found, but you fire the event as if everything was fine.
  • Option number 3 is an okay option, but you would have to add every supported platform (e.g. blackberry). Plug-ins should not cause an app to break if it can be avoided even if the platform is not supported.

@becvert
Copy link
Owner

becvert commented Apr 20, 2017

I do not get any alert box because device-name.js does not exist

I suppose <js-module> was moved to each platform?
In that case indeed there is no alert box and the deviceready event is fired correctly.
But you should not use cordova.plugins.deviceName in the browser because it isnt supported.
That's the default behavior.
Use cordova-plugin-device to identify on which platform your code is running.

You get an alert box when <js-module> is global.
That's the current implementation of this plugin and the current issue at hand I'm trying to resolve.
I'd like to keep js-module global somehow withouth crashing unsupported platforms

@pke
Copy link
Contributor

pke commented Apr 20, 2017

@becvert Why not create a simple proxy file like the one for Windows and just return "Browser" as the name?

module.exports = {
    get: function getDeviceName(success, fail) {
        return success({
            name: "Browser"
        });
    }
};
require("cordova/exec/proxy").add("DeviceName", module.exports);

Should solve the problem effectively and without hackery.

Or, in a second step return the device name based on the browser user agent? I'm sure there is a lib out there that can vaguely decipher user agents like google analytics does.

@becvert
Copy link
Owner

becvert commented Apr 21, 2017

@pke, in that case we need to support all other platforms too. I do not want to do that.

The cordova-plugin-device is already deciphering the user-agent I think.

I'll move the js-module to the supported platforms and then it's the developer's responsability to know if they can call the api in their app depending on the platform it's running on.

@becvert
Copy link
Owner

becvert commented Apr 25, 2017

release 1.2.0
thank you all

@becvert becvert closed this Apr 25, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants