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

Make hprose aware of CommonJS #4

Closed
IngwiePhoenix opened this issue May 3, 2015 · 10 comments
Closed

Make hprose aware of CommonJS #4

IngwiePhoenix opened this issue May 3, 2015 · 10 comments

Comments

@IngwiePhoenix
Copy link

I am planning on writing an RPC layer that is capable of running on "almost" all frameworks. In order to properly support this, hprose-js needs to learn to export. This is simple. As you know NodeJS, this will look very familiar to you:

module.exports = hprose;

The idea is, to have hprose initialize on a local variable first (hprose in my case). Then we can do something like jQuery does here: https://github.com/jquery/jquery/blob/2.1.4/dist/jquery.js#L15-L38

Do you think this is possible?

Kind regards,
Ingwie

@andot
Copy link
Member

andot commented May 3, 2015

CommonJS is very useful on:

  • Server-side JavaScript applications
  • Command line tools
  • Desktop GUI-based applications
  • Hybrid applications (Titanium, Adobe AIR)

But hprose-js can only work on browser-applications, it can't work on server-side.

hprose-nodejs is a node.js (CommonJS) module, it can work on everywhere except browser.

hprose-html5 is another hprose implementation for browser-applications, and it also can work on Hybrid applications (Titanium, Adobe AIR, cocos2d-js jsb applications). I added requirejs and seajs support for this version.

I think to add requirejs and seajs support for hprose-js is more useful than making hprose-js aware of CommonJS. Do you think so?

Best regards,
Ma Bingyao

@IngwiePhoenix
Copy link
Author

RequireJS - which basically is CommonJS - is becoming very, very popular these days. In fact, jQuery, for instance, handles both styles, AMD and CommonJS at once, and allows to be ran inside NodeJS if a window-like object can be specified, so they say.

What I would suggest for hprose-js is that it implements at least CommonJS. It gives developers the option to smoothly move their code to another platform without losing much features or having a big burden of converting their code.

Actually I don't know what the difference between hprose-js and hprose-html5 is. It looks very similar from a short glance.

So my opinion is, that you should add RequireJS support, since that is pretty much CommonJS.

Here is a little template that can maybe help you with getting this going:

(function(root, construct){
    if(typeof module != "undefined" && typeof exports == "object") {
        module.exports = construct();
    } else {
        root.hprose = construct();
    }
})(this, function(){

    // hprose code goes here. Here is an example:
    var hprose = {};
    hprose.foo = function() {}

    return hprose;
    // A module that require()'d this file now gets the hprose object.

});

Kind regards, Ingwie.

@andot
Copy link
Member

andot commented May 4, 2015

features hprose-js hprose-html5 hprose-nodejs
binary data
browser applications
old browser
cross domain flash & cors cors
http client
tcp client
unix socket client
websocket client
Server-side applications maybe
Command line tools maybe
Desktop GUI-based applications maybe
Hybrid applications (Titanium, Adobe AIR) maybe
http service/server
tcp service/server
unix socket service/server
websocket service/server
Completer/Future async todo
RequireJS(AMD) todo or not
SeaJS(CMD) todo or not
CommonJS
global HproseXXX (such as HproseClient)
hprose.XXX (such as hprose.Client)

hprose-js and hprose-html5 have different API interface, because we want they can work together without conflict.

The significance of the existence of hprose-js is only in order to be compatible with old browsers.

hprose-nodejs is compatible with the above two kinds of API interface. so if the developers want to move the hprose-js or hprose-html5 application to another platform, they don't need to converting any code, only to change the hprose-js or hprose-html5 to hprose-nodejs. and there is no feature losing, instead, they will get more features.

jQuery need to handle AMD and CommonJS at once, because it only have one implementation.

Hprose don't need to handle AMD and CommonJS at once, because hprose have three implementation.

But I can add CommonJS support to hprose-html5, because it is easy to do and more useful than hprose-js.

Best regards,
Ma Bingyao

@IngwiePhoenix
Copy link
Author

Oh I see! So if hprose-js is ment for older browsers, then I am likely better off when using hprose-html5.

In the long run, you should consider if it maybe isnt easier to merge the two together instead of maintaining two similar, but not similar, projects at the same time.

For now, I'd suggest adding the CommonJS/AMD support since many people seem to use it. One great usage is WebPack which I am successfuly using in my BIRD3 project - it makes many things much easier.

@andot
Copy link
Member

andot commented May 4, 2015

I will add CommonJS support for hprose-html5, tonight.

@IngwiePhoenix
Copy link
Author

IngwiePhoenix commented May 4, 2015 via email

@andot
Copy link
Member

andot commented May 4, 2015

Completer/Future async is come from dart language.

It is very similar to Promises/A+.

But I simplify its implementation. I only reserved the following methods and properties:

class method
Completer constructor
Completer complete
Completer completeError
Future then
Future catchError
class property
Completer future

for example:

    var completer = new Completer();
    var client = new hprose.Client.create('http://hprose.com/example/');
    client.then(function(stub) {
        stub.hello('World')
        .then(function(result) {
            completer.complete(result);
        })
        .catchError(function(e) {
            completer.completeError(e);
        })
    })
    .catchError(function(e) {
        console.error(e);
    })
...
    var future = completer.future;
    future.then(function(result) {
        console.info(result);
    })
    .catchError(function(e) {
        console.error(e);
    })

and I will add this feature in PHP, too, with this feature, we can publish an async service.

@andot
Copy link
Member

andot commented May 4, 2015

I already have added CommonJS support for hprose-html5, just now.

@andot
Copy link
Member

andot commented Feb 28, 2016

hprose for javascript is update.

features hprose-js hprose-html5 hprose-nodejs
binary data
browser applications
old browser
cross domain flash & cors cors
http client
tcp client
unix socket client
websocket client
Server-side applications maybe maybe
Command line tools maybe maybe
Desktop GUI-based applications
Hybrid applications (Titanium, Adobe AIR)
http service/server
tcp service/server
unix socket service/server
websocket service/server
Completer/Future async
RequireJS(AMD)
SeaJS(CMD)
CommonJS
global HproseXXX (such as HproseClient)
hprose.XXX (such as hprose.Client)

@IngwiePhoenix
Copy link
Author

IngwiePhoenix commented Feb 28, 2016 via email

@andot andot closed this as completed Mar 13, 2016
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

No branches or pull requests

2 participants