hprose / hprose-js

Hprose is a cross-language RPC. This project is Hprose 2.0 RPC for JavaScript
MIT License
135 stars 44 forks source link

Make hprose aware of CommonJS #4

Closed IngwiePhoenix closed 8 years ago

IngwiePhoenix commented 9 years ago

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 commented 9 years ago

CommonJS is very useful on:

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 commented 9 years ago

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

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 commented 9 years ago

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 commented 9 years ago

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

IngwiePhoenix commented 9 years ago

Awesome! I can then work with hprose in my app also in client side. :)

By the way, what is this Future-async stuff about?

andot commented 9 years ago

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 commented 9 years ago

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

andot commented 8 years ago

hprose for javascript is update.

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

Awesome, thanks for the update! :)