Skip to content

Commit

Permalink
Match types for User#callFunction implementation across platforms
Browse files Browse the repository at this point in the history
The unifies the call signature of User#callFunction across all documentation
and platforms.
`callFunction` now has the signature:
```javascript
  user.callFunction('func', 1, 2, 3);
```
  • Loading branch information
takameyer committed Aug 31, 2022
1 parent f75302f commit 8652c05
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 9 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
x.x.x Release notes (yyyy-MM-dd)
=============================================================
### Breaking change
* Unify the call signature documentation of `User#callFunction` ([#3733](https://github.com/realm/realm-js/issues/3733))
* Example:
```javascript
user.callFunction("sum", 1, 2, 3); // Valid
user.callFunction("sum", [1, 2, 3]); // Invalid
```
### Enhancements
* None.

Expand Down
4 changes: 2 additions & 2 deletions docs/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -871,10 +871,10 @@ class User {
/**
* Calls the named server function as this user.
* @param {string} name - name of the function to call
* @param {any[]} args = [] - list of arguments to pass
* @param {...*} [args] - arguments to pass to the function
* @return {Promise<any>} - resolves when the function terminates.
*/
callFunction(name, args) {}
callFunction(name, ...args) {}

/**
* Convenience wrapper around `callFunction(name, [args])`
Expand Down
12 changes: 8 additions & 4 deletions lib/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,14 @@ const instanceMethods = {
return promisify((cb) => this._logOut(cb));
},

async callFunction(name, args = [], service = undefined) {
async callFunction(name, ...args) {
this._callFunctionOnService(name, undefined, ...args);
},

async _callFunctionOnService(name, serviceName, ...args) {
const cleanedArgs = cleanArguments(args);
const stringifiedArgs = EJSON.stringify(cleanedArgs, { relaxed: false });
const result = await promisify((cb) => this._callFunction(name, stringifiedArgs, service, cb));
const result = await promisify((cb) => this._callFunction(name, stringifiedArgs, serviceName, cb));
return EJSON.parse(result);
},

Expand Down Expand Up @@ -98,15 +102,15 @@ const instanceMethods = {
},

// Internal helpers.
_functionsOnService(service) {
_functionsOnService(serviceName) {
const user = this;
return new Proxy(
{},
{
get(target, name, receiver) {
if (typeof name === "string" && name != "inspect") {
return (...args) => {
return user.callFunction(name, args, service);
return user._callFunctionOnService(name, serviceName, args);
};
} else {
return Reflect.get(target, name, receiver);
Expand Down
2 changes: 1 addition & 1 deletion tests/js/user-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ module.exports = {
let user = await app.logIn(credentials);

TestCase.assertEqual(await user.callFunction("sumFunc"), 0);
TestCase.assertEqual(await user.callFunction("sumFunc", [123]), 123);
TestCase.assertEqual(await user.callFunction("sumFunc", 123), 123);
TestCase.assertEqual(await user.functions.sumFunc(123), 123);
TestCase.assertEqual(await user.functions["sumFunc"](123), 123);

Expand Down
4 changes: 2 additions & 2 deletions types/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ declare namespace Realm {
class User<
FunctionsFactoryType = DefaultFunctionsFactory,
CustomDataType = SimpleObject,
UserProfileDataType = DefaultUserProfileData,
UserProfileDataType = DefaultUserProfileData
> {
/**
* The automatically-generated internal ID of the user.
Expand Down Expand Up @@ -490,7 +490,7 @@ declare namespace Realm {
*
* @example
* // These are all equivalent:
* await user.callFunction("doThing", [a1, a2, a3]);
* await user.callFunction("doThing", a1, a2, a3);
* await user.functions.doThing(a1, a2, a3);
* await user.functions["doThing"](a1, a2, a3);
* @example
Expand Down

0 comments on commit 8652c05

Please sign in to comment.