Skip to content

Commit

Permalink
Merge pull request #31 from canhorn/feature/child_argument_construction
Browse files Browse the repository at this point in the history
Convert Nested Arguments
  • Loading branch information
canhorn authored Jul 23, 2021
2 parents 8805a77 + fcbf299 commit 6ec4127
Showing 1 changed file with 34 additions and 11 deletions.
45 changes: 34 additions & 11 deletions EventHorizon.Blazor.Interop/wwwroot/interop-bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
const method = argValue["method"];
return function () {
return invokableReference.invokeMethod(method, ...convertCallbackArguments(arguments));
};
};
} else if (argValue[typeKey] && argValue[typeKey] === actionCallbackType) {
const invokableReference = argValue["invokableReference"];
const method = argValue["method"];
Expand All @@ -82,23 +82,46 @@
const args = [];
for (var i = 1; i < argumentArray.length; i++) {
const arg = convertArg(argumentArray[i]);
constructArgument(args, arg);
}
return args;
};

if (arg && typeof (arg) === "object" && !arg[cacheKey] && !Array.isArray(arg)) {
// Object literal: { prop: "hi", prop2: { ___type: "action_callback" } }
const newArg = {};
for (const key in arg) {
if (Object.prototype.hasOwnProperty.call(arg, key)) {
const element = arg[key];
/**
* Convert argument to cached entity, introspects and converts children as well.
*
* @param {any} args
* @param {any} arg
*/
const constructArgument = (args, arg) => {
if (arg && typeof (arg) === "object" && !arg[cacheKey] && !Array.isArray(arg)) {
// Object literal: { prop: "hi", prop2: { ___type: "action_callback" } }
const newArg = {};
for (const key in arg) {
if (Object.prototype.hasOwnProperty.call(arg, key)) {
const element = arg[key];
if (Array.isArray(element)) {
var arrayArgs = [];
constructArgument(arrayArgs, element);
newArg[key] = arrayArgs[0];
} else {
newArg[key] = convertArg(element);
}
}
args.push(newArg);
} else {
args.push(arg);
}
args.push(newArg);
} else if (arg && typeof (arg) === "object" && Array.isArray(arg)) {
// Array: [ { ___type: "action_callback" } ]
const newArg = [];
for (const item of arg) {
newArg.push(convertArg(item));
}
args.push(newArg);
} else {
args.push(arg);
}
return args;
};

/**
* Create a serialization safe set of arguments.
* Taking into account arrays and objects
Expand Down

0 comments on commit 6ec4127

Please sign in to comment.