@microsoft/dotnet-js-interop
Advanced tools
Comparing version 6.0.0-preview.5.21301.17 to 6.0.0-preview.6.21355.2
@@ -87,2 +87,8 @@ export declare module DotNet { | ||
endInvokeJSFromDotNet(callId: number, succeeded: boolean, resultOrError: any): void; | ||
/** | ||
* Invoked by the runtime to transfer a byte array from JS to .NET. | ||
* @param id The identifier for the byte array used during revival. | ||
* @param data The byte array being transferred for eventual revival. | ||
*/ | ||
sendByteArray(id: number, data: Uint8Array): void; | ||
} | ||
@@ -134,2 +140,8 @@ /** | ||
endInvokeDotNetFromJS: (asyncCallId: string, success: boolean, resultJsonOrExceptionMessage: string) => void; | ||
/** | ||
* Receives notification that a byte array is being transferred from .NET to JS. | ||
* @param id The identifier for the byte array used during revival. | ||
* @param data The byte array being transferred for eventual revival. | ||
*/ | ||
receiveByteArray: (id: number, data: Uint8Array) => void; | ||
}; | ||
@@ -136,0 +148,0 @@ function findJSFunction(identifier: string, targetInstanceId: number): Function; |
@@ -6,2 +6,3 @@ // This is a single-file self-contained module to avoid the need for a Webpack build | ||
const jsonRevivers = []; | ||
const byteArraysToBeRevived = new Map(); | ||
class JSObject { | ||
@@ -150,3 +151,3 @@ constructor(_jsObject) { | ||
if (dispatcher.invokeDotNetFromJS) { | ||
const argsJson = JSON.stringify(args, argReplacer); | ||
const argsJson = stringifyArgs(args); | ||
const resultJson = dispatcher.invokeDotNetFromJS(assemblyName, methodIdentifier, dotNetObjectId, argsJson); | ||
@@ -168,3 +169,3 @@ return resultJson ? parseJsonWithRevivers(resultJson) : null; | ||
try { | ||
const argsJson = JSON.stringify(args, argReplacer); | ||
const argsJson = stringifyArgs(args); | ||
getRequiredDispatcher().beginInvokeDotNetFromJS(asyncCallId, assemblyName, methodIdentifier, dotNetObjectId, argsJson); | ||
@@ -237,3 +238,3 @@ } | ||
? null | ||
: JSON.stringify(result, argReplacer); | ||
: stringifyArgs(result); | ||
}, | ||
@@ -260,3 +261,3 @@ /** | ||
// Not using "await" because it codegens a lot of boilerplate | ||
promise.then(result => getRequiredDispatcher().endInvokeJSFromDotNet(asyncHandle, true, JSON.stringify([asyncHandle, true, createJSCallResult(result, resultType)], argReplacer)), error => getRequiredDispatcher().endInvokeJSFromDotNet(asyncHandle, false, JSON.stringify([asyncHandle, false, formatError(error)]))); | ||
promise.then(result => getRequiredDispatcher().endInvokeJSFromDotNet(asyncHandle, true, stringifyArgs([asyncHandle, true, createJSCallResult(result, resultType)])), error => getRequiredDispatcher().endInvokeJSFromDotNet(asyncHandle, false, JSON.stringify([asyncHandle, false, formatError(error)]))); | ||
} | ||
@@ -275,2 +276,10 @@ }, | ||
completePendingCall(parseInt(asyncCallId), success, resultOrError); | ||
}, | ||
/** | ||
* Receives notification that a byte array is being transferred from .NET to JS. | ||
* @param id The identifier for the byte array used during revival. | ||
* @param data The byte array being transferred for eventual revival. | ||
*/ | ||
receiveByteArray: (id, data) => { | ||
byteArraysToBeRevived.set(id, data); | ||
} | ||
@@ -317,19 +326,26 @@ }; | ||
const dotNetObjectRefKey = '__dotNetObject'; | ||
attachReviver(function reviveDotNetObject(key, value) { | ||
if (value && typeof value === 'object' && value.hasOwnProperty(dotNetObjectRefKey)) { | ||
return new DotNetObject(value.__dotNetObject); | ||
} | ||
// Unrecognized - let another reviver handle it | ||
return value; | ||
}); | ||
attachReviver(function reviveJSObjectReference(key, value) { | ||
if (value && typeof value === 'object' && value.hasOwnProperty(jsObjectIdKey)) { | ||
const id = value[jsObjectIdKey]; | ||
const jsObject = cachedJSObjectsById[id]; | ||
if (jsObject) { | ||
return jsObject.getWrappedObject(); | ||
const byteArrayRefKey = '__byte[]'; | ||
attachReviver(function reviveReference(key, value) { | ||
if (value && typeof value === 'object') { | ||
if (value.hasOwnProperty(dotNetObjectRefKey)) { | ||
return new DotNetObject(value.__dotNetObject); | ||
} | ||
else { | ||
throw new Error(`JS object instance with ID ${id} does not exist (has it been disposed?).`); | ||
else if (value.hasOwnProperty(jsObjectIdKey)) { | ||
const id = value[jsObjectIdKey]; | ||
const jsObject = cachedJSObjectsById[id]; | ||
if (jsObject) { | ||
return jsObject.getWrappedObject(); | ||
} | ||
else { | ||
throw new Error(`JS object instance with Id '${id}' does not exist. It may have been disposed.`); | ||
} | ||
} | ||
else if (value.hasOwnProperty(byteArrayRefKey)) { | ||
const index = value[byteArrayRefKey]; | ||
const byteArray = byteArraysToBeRevived.get(index); | ||
if (byteArray === undefined) { | ||
throw new Error(`Byte array index '${index}' does not exist.`); | ||
} | ||
return byteArray; | ||
} | ||
} | ||
@@ -349,6 +365,20 @@ // Unrecognized - let another reviver handle it | ||
} | ||
let nextByteArrayIndex = 0; | ||
function stringifyArgs(args) { | ||
nextByteArrayIndex = 0; | ||
return JSON.stringify(args, argReplacer); | ||
} | ||
function argReplacer(key, value) { | ||
return value instanceof DotNetObject ? value.serializeAsArg() : value; | ||
if (value instanceof DotNetObject) { | ||
return value.serializeAsArg(); | ||
} | ||
else if (value instanceof Uint8Array) { | ||
dotNetDispatcher.sendByteArray(nextByteArrayIndex, value); | ||
const jsonValue = { [byteArrayRefKey]: nextByteArrayIndex }; | ||
nextByteArrayIndex++; | ||
return jsonValue; | ||
} | ||
return value; | ||
} | ||
})(DotNet || (DotNet = {})); | ||
//# sourceMappingURL=Microsoft.JSInterop.js.map |
{ | ||
"name": "@microsoft/dotnet-js-interop", | ||
"version": "6.0.0-preview.5.21301.17", | ||
"version": "6.0.0-preview.6.21355.2", | ||
"description": "Provides abstractions and features for interop between .NET and JavaScript code.", | ||
@@ -5,0 +5,0 @@ "main": "dist/Microsoft.JSInterop.js", |
Sorry, the diff of this file is not supported yet
37042
525