Suggestions for the new javascript api

Post here your questions about the HTML5 / JavaScript for SFS2X

Moderators: Lapo, Bax

User avatar
arodrigo
Posts: 24
Joined: 22 Jul 2011, 10:03
Location: Spain
Contact:

Suggestions for the new javascript api

Postby arodrigo » 09 Aug 2017, 10:21

Hi

After port all our code from the old javascript library to the new one we have some suggestions that could make easier the use of the library.

1. Add a function to map a SFSArray to an javascript array of one type

In many cases a SFSArray contains only SFSObjects or SFSArrays and usually we map then to other structures/classes. Now with the current implementation we do something like this:

Code: Select all

parseArray(dataArray) {

    const result = [];

    if (dataArray != null) {
        for (let i = 0; i < dataArray.size(); i++) {
            result.push(new Item(dataArray.getSFSObject(i)));
        }
    }

    return result;
}


It would be nice if the library provides functions like SFSArray.map(type: SFSDataType) that simply returns a javascript array with all the objects mapped as the SFSDataType specified. Then we can do things like

Code: Select all

const mappedObjects = dataArray.map(SFSDataType.SFS_ARRAY).map(mapingFunction);


2. Add the function 'contains' to SFSArray and SFSObject

Now to know if an object or an array contains an element we have to do a 'get' and check if it's not null. For an SFSObject this works perfect but, with the SFSArray, if we get a position that can be, for example, null or String we must use the 'get' without type before and then check if it's null. if we don't do that we will get an exception.

Code: Select all

//Bad code

const myString = sfsArray.getUtfString(0); //If this is null it would fail
if (myString) console.log(myString)

Code: Select all

//Good code
if (sfsArray.get(0)) {
   myString = sfsArray.getUtfString(0);
   console.log(myString)
}


The problem is that the documentation don't let clear that sfsArray.getUtfString(0) would throw an exception with null. it would be nice if the SFSArray and the SFSObject provides the function 'contains' that returns a boolean to make the code more clear and avoid future mistakes.

3. Make the method fromSFSArray public in all your objects like SFSRoom, SFSUser, ...

In all the other clients we can user the method fromSFSArray to create an SFSRoom object from the SFSArray produced by the function toSFSArray on the server side.

We have been using this for some special functionalities of the moderators, and now in the new library this method is private. We know that we don't have to create rooms, we only use it because it come in handy to send the information of the rooms in some special cases and this wasn't a problem until now.

4. Add to SFSobject and SFSArray the possibility to get the long values as plain string or HEX

With the number limitation on javascript the long java numbers can be a problem but in the cases when we need the number it would be nice (and i don't now if possible) that the SFSArray and the SFSObject contains the functions getLongAsString getLongAsHEX. For example:

Code: Select all

veryLongNumber = sfsObject.getLong('veryLongNumber'); //Show a log warning
console.log(veryLongNumber); //NaN
veryLongNumber = sfsObject.getLongString('veryLongNumber');
console.log(veryLongNumber); //'10201670634517191'
veryLongNumber = sfsObject.getLongHEX('veryLongNumber');
console.log(veryLongNumber); //'243E5D8A4932C7'


5. Provide a d.ts of your lib

https://www.typescriptlang.org/docs/han ... ction.html

With the d.ts most of IDE's autocomplete the code of your lib simplifying the development and also allows to use your lib in a TypeScript environments.

We attach the one that we use as an example.
https://drive.google.com/open?id=0B5Pvd ... 3FYS3JjWTg

That's it, most of the suggestions can be implemented on our end with prototypes and so on but we think that can make the live easier to any one who starts using the library.

I hope that the points are well explained.

Regards and thank you for your time.
User avatar
Lapo
Site Admin
Posts: 23008
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Suggestions for the new javascript api

Postby Lapo » 10 Aug 2017, 14:35

Thanks your feedback.
1. Add a function to map a SFSArray to an javascript array of one type

I am not sure I understand the goal of this.
In particular I find the premise to be puzzling:
    In many cases a SFSArray contains only SFSObjects or SFSArrays and usually we map then to other structures/classes. Now with the current implementation we do something like this:
in other words I don't think the assumption is justified. If you take out Javascript examples as a sample you will notice this is not the case and more in general this seems like a very particular use case.

Wouldn't it be easier to simply have a toNativeArray() method that converts the SFSArray to a JS array? If the original SFSArray contains uniform types you'll obtain a uniformly typed JS array anyway.
I am also to sure why a type parameter should be passed. Every object inside an SFSArray maintains its type, so the conversion is not going to break anything.

2. Add the function 'contains' to SFSArray and SFSObject

Agreed. This is indeed missing compared to other API implementations. We will add it.

3. Make the method fromSFSArray public in all your objects like SFSRoom, SFSUser, ...

Ok, we'll look into it.

4. Add to SFSobject and SFSArray the possibility to get the long values as plain string or HEX

I don't think this belongs to SFSObject/Array.
Converting a number to other bases is already provided natively in Javascript via the toString() method.
There's no need to add that kind of responsibility to SFSObject/Array, instead you can use this:

Code: Select all

var hexValue = sfsobj.getInt().toString(16);


5. Provide a d.ts of your lib

Ok, we'll look into it.

Thanks
Lapo
--
gotoAndPlay()
...addicted to flash games
User avatar
Rob
Posts: 53
Joined: 01 Jul 2017, 07:33

Re: Suggestions for the new javascript api

Postby Rob » 12 Aug 2017, 17:48

:idea: How about putting up the javascript client on GitHub? That way it would be easier to report issues and submit pull requests with improvements/fixes.

I recently had to tweak the client (to show more debug info for larger packets), something which others might have use for as well. Having the code in a public repository would make it easy to share things like that.
User avatar
Bax
Site Admin
Posts: 4609
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: Suggestions for the new javascript api

Postby Bax » 16 Aug 2017, 08:04

arodrigo wrote:2. Add the function 'contains' to SFSArray and SFSObject

Now to know if an object or an array contains an element we have to do a 'get' and check if it's not null. For an SFSObject this works perfect but, with the SFSArray, if we get a position that can be, for example, null or String we must use the 'get' without type before and then check if it's null. if we don't do that we will get an exception.

Code: Select all

//Bad code

const myString = sfsArray.getUtfString(0); //If this is null it would fail
if (myString) console.log(myString)

Code: Select all

//Good code
if (sfsArray.get(0)) {
   myString = sfsArray.getUtfString(0);
   console.log(myString)
}


The problem is that the documentation don't let clear that sfsArray.getUtfString(0) would throw an exception with null. it would be nice if the SFSArray and the SFSObject provides the function 'contains' that returns a boolean to make the code more clear and avoid future mistakes.

The array has no empty slots, so you just have to make sure the index you are getting doesn't exceed the SFSArray's size.
Paolo Bax
The SmartFoxServer Team
User avatar
Bax
Site Admin
Posts: 4609
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: Suggestions for the new javascript api

Postby Bax » 16 Aug 2017, 08:40

arodrigo wrote:3. Make the method fromSFSArray public in all your objects like SFSRoom, SFSUser, ...

In all the other clients we can user the method fromSFSArray to create an SFSRoom object from the SFSArray produced by the function toSFSArray on the server side.

We have been using this for some special functionalities of the moderators, and now in the new library this method is private. We know that we don't have to create rooms, we only use it because it come in handy to send the information of the rooms in some special cases and this wasn't a problem until now.

Even if not documented, the fromSFSArray method exists, so you can use it if you find it useful in your case.
Paolo Bax
The SmartFoxServer Team
User avatar
Bax
Site Admin
Posts: 4609
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: Suggestions for the new javascript api

Postby Bax » 16 Aug 2017, 09:31

arodrigo wrote:5. Provide a d.ts of your lib

https://www.typescriptlang.org/docs/han ... ction.html

With the d.ts most of IDE's autocomplete the code of your lib simplifying the development and also allows to use your lib in a TypeScript environments.

We attach the one that we use as an example.
https://drive.google.com/open?id=0B5Pvd ... 3FYS3JjWTg

That's it, most of the suggestions can be implemented on our end with prototypes and so on but we think that can make the live easier to any one who starts using the library.

I'm not allowed to download that file from Google Drive. Can you please send it by email, so we can take a look?
Can you also please the usage of the declaration file in your IDE of choice? Are you developing your project in TypeScript?
Thank you.
Paolo Bax
The SmartFoxServer Team
User avatar
Bax
Site Admin
Posts: 4609
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: Suggestions for the new javascript api

Postby Bax » 18 Aug 2017, 08:27

Bax wrote:
arodrigo wrote:5. Provide a d.ts of your lib

https://www.typescriptlang.org/docs/han ... ction.html

With the d.ts most of IDE's autocomplete the code of your lib simplifying the development and also allows to use your lib in a TypeScript environments.

We attach the one that we use as an example.
https://drive.google.com/open?id=0B5Pvd ... 3FYS3JjWTg

That's it, most of the suggestions can be implemented on our end with prototypes and so on but we think that can make the live easier to any one who starts using the library.

I'm not allowed to download that file from Google Drive. Can you please send it by email, so we can take a look?
Can you also please the usage of the declaration file in your IDE of choice? Are you developing your project in TypeScript?
Thank you.

I downloaded the file. Haw did you generate it? Manually based on the API doc? or somehow automatically?
And, again, what IDE are you using? Is your project developed in TypeScript or you just need the definitions for autocomplete?
Thanks.
Paolo Bax
The SmartFoxServer Team
User avatar
arodrigo
Posts: 24
Joined: 22 Jul 2011, 10:03
Location: Spain
Contact:

Re: Suggestions for the new javascript api

Postby arodrigo » 18 Aug 2017, 12:44

Hello Bax and Lapo

Sorry for the late response.

1. Add a function to map a SFSArray to an javascript array of one type
Wouldn't it be easier to simply have a toNativeArray() method that converts the SFSArray to a JS array? If the original SFSArray contains uniform types you'll obtain a uniformly typed JS array anyway.
I am also to sure why a type parameter should be passed. Every object inside an SFSArray maintains its type, so the conversion is not going to break anything.


Yep as always i didn't express myself correctly. A function like toNativeArray() its what i meant. With the native array the i can use easily the map, reduce, etc functions without extra code.

2. Add the function 'contains' to SFSArray and SFSObject

The array has no empty slots, so you just have to make sure the index you are getting doesn't exceed the SFSArray's size.


The array can contain a Null value and the only way to check if is null or other thing is with a get and check if it is not null.

3. Make the method fromSFSArray public in all your objects like SFSRoom, SFSUser, ...
Even if not documented, the fromSFSArray method exists, so you can use it if you find it useful in your case.


Yep the function is on the SFSRoom but its not exposed on the library. Right now we have to access to it from the prototype.

4. Add to SFSobject and SFSArray the possibility to get the long values as plain string or HEX

I don't think this belongs to SFSObject/Array.
Converting a number to other bases is already provided natively in Javascript via the toString() method.
There's no need to add that kind of responsibility to SFSObject/Array, instead you can use this:


Again i did explained it incorrectly, i don't want to do conversion of numbers to another bases. What i was proposing is only intended for the long numbers. If the number is greater than 2^53 getLong is going to be an incorrect value, but if you have a function like getLongHEX that process the byte array of the number converting it to an hex then the value is going to be valid and i could use a lib like https://github.com/MikeMcl/big.js/ to work with it.

5. Provide a d.ts of your lib

I'm not allowed to download that file from Google Drive. Can you please send it by email, so we can take a look?
Can you also please the usage of the declaration file in your IDE of choice? Are you developing your project in TypeScript?
Thank you.


Sorry i just changed the permission on the file.

Haw did you generate it? Manually based on the API doc?


We generate manually, but it can be generated automatically with grunt or gulp from the sorce code of any javascript code.

what IDE are you using?


Intellij and webstorm accept the d.ts as definition file for autocomplete.

Is your project developed in TypeScript or you just need the definitions for autocomplete?


We have the two types of project that are use your lib ones with typescript and other directly with javascript.

Extra
How about putting up the javascript client on GitHub? That way it would be easier to report issues and submit pull requests with improvements/fixes.

I recently had to tweak the client (to show more debug info for larger packets), something which others might have use for as well. Having the code in a public repository would make it easy to share things like that.


Our team would love work directly on the lib to to help with some features. So +++++

Thank you!
User avatar
Bax
Site Admin
Posts: 4609
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: Suggestions for the new javascript api

Postby Bax » 21 Aug 2017, 07:46

(1)
The toNativeArray() method you are asking for doesn't exist in the other APIs. As one of our biggest efforts is to keep the APIs consistent, adding it here would require the same on all other platforms. Currently this is not our priority. Maybe you can create your own utility class implementing that method and any other you need.

(2)
In our dev environment we already added the SFSObject.containsKey and SFSArray.contains methods. We will make them available as soon as we sort out all the required changes.
In any case SFSArray never contains null values. It can contain objects of type SFSDataType.NULL.

(3)
The fromSFSArray() method is static, so calling it on the prototype is the right way to go.

(4)
See 1.

(extra)
Thank you for offer to help, but again we need to keep the full control over our API to ensure consistency across the multiple platforms we support.
Paolo Bax
The SmartFoxServer Team
User avatar
arodrigo
Posts: 24
Joined: 22 Jul 2011, 10:03
Location: Spain
Contact:

Re: Suggestions for the new javascript api

Postby arodrigo » 21 Aug 2017, 15:26

(1)
ok it was a suggestion.

(2)
Ok, perfect.

(3)
The fromSFSArray() method is static, so calling it on the prototype is the right way to go.


I think that this points it's not clear. When i say that the method is not exposed i mean that i cannot access to it even as an static method. Please try to use it with the lib that you provide to download.

See the following image as an example
Captura.PNG
(8.1 KiB) Not downloaded yet


(4)
So to keep the APIs consistent, you prefer to don't support the possibility to work with long values that is possible in other platforms?

(extra)
Thank you for offer to help, but again we need to keep the full control over our API to ensure consistency across the multiple platforms we support.


You don't lost the control when the client is open source, you can control the developers that works over the project and only accept change request of other developers if you want. Is a win win scenario.

Regards
User avatar
Bax
Site Admin
Posts: 4609
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: Suggestions for the new javascript api

Postby Bax » 21 Aug 2017, 15:42

arodrigo wrote:(3)
The fromSFSArray() method is static, so calling it on the prototype is the right way to go.


I think that this points it's not clear. When i say that the method is not exposed i mean that i cannot access to it even as an static method. Please try to use it with the lib that you provide to download.

I'm sorry, you are right. I forgot that the SFSRoom class is not exposed, because developers never need to instantiate it directly.
Paolo Bax
The SmartFoxServer Team
Hamster
Posts: 4
Joined: 07 Sep 2017, 11:09

Re: Suggestions for the new javascript api

Postby Hamster » 08 Sep 2017, 09:45

Bax wrote:
Bax wrote:
arodrigo wrote:5. Provide a d.ts of your lib

https://www.typescriptlang.org/docs/han ... ction.html

With the d.ts most of IDE's autocomplete the code of your lib simplifying the development and also allows to use your lib in a TypeScript environments.

We attach the one that we use as an example.
https://drive.google.com/open?id=0B5Pvd ... 3FYS3JjWTg

That's it, most of the suggestions can be implemented on our end with prototypes and so on but we think that can make the live easier to any one who starts using the library.

I'm not allowed to download that file from Google Drive. Can you please send it by email, so we can take a look?
Can you also please the usage of the declaration file in your IDE of choice? Are you developing your project in TypeScript?
Thank you.

I downloaded the file. Haw did you generate it? Manually based on the API doc? or somehow automatically?
And, again, what IDE are you using? Is your project developed in TypeScript or you just need the definitions for autocomplete?
Thanks.


You should use the Typescript definition file (*.d.ts) from Definitelly Typed. The SmartFox definition file can be found here, but it should be corrected according to my post. There I mentioned also the idea of making clients or wrappers for Angular.

It can be installed with:

Code: Select all

npm install --save @types/smart-fox-server
User avatar
Bax
Site Admin
Posts: 4609
Joined: 29 Mar 2005, 09:50
Location: Italy
Contact:

Re: Suggestions for the new javascript api

Postby Bax » 08 Sep 2017, 09:58

Recently we investigated the request to add a definitions file to our API, but we couldn't locate tools to generate it automatically.
We don0t know who created the file you linked, or how it was created.
Paolo Bax
The SmartFoxServer Team

Return to “SFS2X HTML5 / JavaScript API”

Who is online

Users browsing this forum: Google [Bot] and 21 guests