@microsoft/dotnet-js-interop
Advanced tools
Comparing version 6.0.0-preview.2.21154.6 to 6.0.0-preview.3.21201.13
export declare module DotNet { | ||
type JsonReviver = ((key: any, value: any) => any); | ||
export type JsonReviver = ((key: any, value: any) => any); | ||
/** | ||
@@ -9,3 +9,3 @@ * Sets the specified .NET call dispatcher as the current instance so that it will be used | ||
*/ | ||
function attachDispatcher(dispatcher: DotNetCallDispatcher): void; | ||
export function attachDispatcher(dispatcher: DotNetCallDispatcher): void; | ||
/** | ||
@@ -15,3 +15,3 @@ * Adds a JSON reviver callback that will be used when parsing arguments received from .NET. | ||
*/ | ||
function attachReviver(reviver: JsonReviver): void; | ||
export function attachReviver(reviver: JsonReviver): void; | ||
/** | ||
@@ -26,3 +26,3 @@ * Invokes the specified .NET public method synchronously. Not all hosting scenarios support | ||
*/ | ||
function invokeMethod<T>(assemblyName: string, methodIdentifier: string, ...args: any[]): T; | ||
export function invokeMethod<T>(assemblyName: string, methodIdentifier: string, ...args: any[]): T; | ||
/** | ||
@@ -36,3 +36,3 @@ * Invokes the specified .NET public method asynchronously. | ||
*/ | ||
function invokeMethodAsync<T>(assemblyName: string, methodIdentifier: string, ...args: any[]): Promise<T>; | ||
export function invokeMethodAsync<T>(assemblyName: string, methodIdentifier: string, ...args: any[]): Promise<T>; | ||
/** | ||
@@ -45,3 +45,3 @@ * Creates a JavaScript object reference that can be passed to .NET via interop calls. | ||
*/ | ||
function createJSObjectReference(jsObject: any): any; | ||
export function createJSObjectReference(jsObject: any): any; | ||
/** | ||
@@ -52,3 +52,3 @@ * Disposes the given JavaScript object reference. | ||
*/ | ||
function disposeJSObjectReference(jsObjectReference: any): void; | ||
export function disposeJSObjectReference(jsObjectReference: any): void; | ||
/** | ||
@@ -59,7 +59,7 @@ * Parses the given JSON string using revivers to restore args passed from .NET to JS. | ||
*/ | ||
function parseJsonWithRevivers(json: string): any; | ||
export function parseJsonWithRevivers(json: string): any; | ||
/** | ||
* Represents the type of result expected from a JS interop call. | ||
*/ | ||
enum JSCallResultType { | ||
export enum JSCallResultType { | ||
Default = 0, | ||
@@ -71,3 +71,3 @@ JSObjectReference = 1 | ||
*/ | ||
interface DotNetCallDispatcher { | ||
export interface DotNetCallDispatcher { | ||
/** | ||
@@ -105,3 +105,3 @@ * Optional. If implemented, invoked by the runtime to perform a synchronous call to a .NET method. | ||
*/ | ||
const jsCallDispatcher: { | ||
export const jsCallDispatcher: { | ||
/** | ||
@@ -151,2 +151,3 @@ * Finds the JavaScript function matching the specified identifier. | ||
function disposeJSObjectReferenceById(id: number): void; | ||
export {}; | ||
} |
// This is a single-file self-contained module to avoid the need for a Webpack build | ||
export var DotNet; | ||
(function (DotNet) { | ||
var _a; | ||
window.DotNet = DotNet; // Ensure reachable from anywhere | ||
var jsonRevivers = []; | ||
var JSObject = /** @class */ (function () { | ||
function JSObject(_jsObject) { | ||
const jsonRevivers = []; | ||
class JSObject { | ||
constructor(_jsObject) { | ||
this._jsObject = _jsObject; | ||
this._cachedFunctions = new Map(); | ||
} | ||
JSObject.prototype.findFunction = function (identifier) { | ||
var cachedFunction = this._cachedFunctions.get(identifier); | ||
findFunction(identifier) { | ||
const cachedFunction = this._cachedFunctions.get(identifier); | ||
if (cachedFunction) { | ||
return cachedFunction; | ||
} | ||
var result = this._jsObject; | ||
var lastSegmentValue; | ||
identifier.split('.').forEach(function (segment) { | ||
let result = this._jsObject; | ||
let lastSegmentValue; | ||
identifier.split('.').forEach(segment => { | ||
if (segment in result) { | ||
@@ -25,3 +24,3 @@ lastSegmentValue = result; | ||
else { | ||
throw new Error("Could not find '" + identifier + "' ('" + segment + "' was undefined)."); | ||
throw new Error(`Could not find '${identifier}' ('${segment}' was undefined).`); | ||
} | ||
@@ -35,17 +34,16 @@ }); | ||
else { | ||
throw new Error("The value '" + identifier + "' is not a function."); | ||
throw new Error(`The value '${identifier}' is not a function.`); | ||
} | ||
}; | ||
JSObject.prototype.getWrappedObject = function () { | ||
} | ||
getWrappedObject() { | ||
return this._jsObject; | ||
}; | ||
return JSObject; | ||
}()); | ||
var jsObjectIdKey = "__jsObjectId"; | ||
var pendingAsyncCalls = {}; | ||
var windowJSObjectId = 0; | ||
var cachedJSObjectsById = (_a = {}, | ||
_a[windowJSObjectId] = new JSObject(window), | ||
_a); | ||
cachedJSObjectsById[windowJSObjectId]._cachedFunctions.set('import', function (url) { | ||
} | ||
} | ||
const jsObjectIdKey = "__jsObjectId"; | ||
const pendingAsyncCalls = {}; | ||
const windowJSObjectId = 0; | ||
const cachedJSObjectsById = { | ||
[windowJSObjectId]: new JSObject(window), | ||
}; | ||
cachedJSObjectsById[windowJSObjectId]._cachedFunctions.set('import', (url) => { | ||
// In most cases developers will want to resolve dynamic imports relative to the base HREF. | ||
@@ -59,5 +57,5 @@ // However since we're the one calling the import keyword, they would be resolved relative to | ||
}); | ||
var nextAsyncCallId = 1; // Start at 1 because zero signals "no response needed" | ||
var nextJsObjectId = 1; // Start at 1 because zero is reserved for "window" | ||
var dotNetDispatcher = null; | ||
let nextAsyncCallId = 1; // Start at 1 because zero signals "no response needed" | ||
let nextJsObjectId = 1; // Start at 1 because zero is reserved for "window" | ||
let dotNetDispatcher = null; | ||
/** | ||
@@ -90,7 +88,3 @@ * Sets the specified .NET call dispatcher as the current instance so that it will be used | ||
*/ | ||
function invokeMethod(assemblyName, methodIdentifier) { | ||
var args = []; | ||
for (var _i = 2; _i < arguments.length; _i++) { | ||
args[_i - 2] = arguments[_i]; | ||
} | ||
function invokeMethod(assemblyName, methodIdentifier, ...args) { | ||
return invokePossibleInstanceMethod(assemblyName, methodIdentifier, null, args); | ||
@@ -107,7 +101,3 @@ } | ||
*/ | ||
function invokeMethodAsync(assemblyName, methodIdentifier) { | ||
var args = []; | ||
for (var _i = 2; _i < arguments.length; _i++) { | ||
args[_i - 2] = arguments[_i]; | ||
} | ||
function invokeMethodAsync(assemblyName, methodIdentifier, ...args) { | ||
return invokePossibleInstanceMethodAsync(assemblyName, methodIdentifier, null, args); | ||
@@ -124,8 +114,7 @@ } | ||
function createJSObjectReference(jsObject) { | ||
var _a; | ||
if (jsObject && typeof jsObject === 'object') { | ||
cachedJSObjectsById[nextJsObjectId] = new JSObject(jsObject); | ||
var result = (_a = {}, | ||
_a[jsObjectIdKey] = nextJsObjectId, | ||
_a); | ||
const result = { | ||
[jsObjectIdKey]: nextJsObjectId | ||
}; | ||
nextJsObjectId++; | ||
@@ -135,3 +124,3 @@ return result; | ||
else { | ||
throw new Error("Cannot create a JSObjectReference from the value '" + jsObject + "'."); | ||
throw new Error(`Cannot create a JSObjectReference from the value '${jsObject}'.`); | ||
} | ||
@@ -146,3 +135,3 @@ } | ||
function disposeJSObjectReference(jsObjectReference) { | ||
var id = jsObjectReference && jsObjectReference[jsObjectIdKey]; | ||
const id = jsObjectReference && jsObjectReference[jsObjectIdKey]; | ||
if (typeof id === 'number') { | ||
@@ -159,6 +148,6 @@ disposeJSObjectReferenceById(id); | ||
function parseJsonWithRevivers(json) { | ||
return json ? JSON.parse(json, function (key, initialValue) { | ||
return json ? JSON.parse(json, (key, initialValue) => { | ||
// Invoke each reviver in order, passing the output from the previous reviver, | ||
// so that each one gets a chance to transform the value | ||
return jsonRevivers.reduce(function (latestValue, reviver) { return reviver(key, latestValue); }, initialValue); | ||
return jsonRevivers.reduce((latestValue, reviver) => reviver(key, latestValue), initialValue); | ||
}) : null; | ||
@@ -168,6 +157,6 @@ } | ||
function invokePossibleInstanceMethod(assemblyName, methodIdentifier, dotNetObjectId, args) { | ||
var dispatcher = getRequiredDispatcher(); | ||
const dispatcher = getRequiredDispatcher(); | ||
if (dispatcher.invokeDotNetFromJS) { | ||
var argsJson = JSON.stringify(args, argReplacer); | ||
var resultJson = dispatcher.invokeDotNetFromJS(assemblyName, methodIdentifier, dotNetObjectId, argsJson); | ||
const argsJson = JSON.stringify(args, argReplacer); | ||
const resultJson = dispatcher.invokeDotNetFromJS(assemblyName, methodIdentifier, dotNetObjectId, argsJson); | ||
return resultJson ? parseJsonWithRevivers(resultJson) : null; | ||
@@ -181,10 +170,10 @@ } | ||
if (assemblyName && dotNetObjectId) { | ||
throw new Error("For instance method calls, assemblyName should be null. Received '" + assemblyName + "'."); | ||
throw new Error(`For instance method calls, assemblyName should be null. Received '${assemblyName}'.`); | ||
} | ||
var asyncCallId = nextAsyncCallId++; | ||
var resultPromise = new Promise(function (resolve, reject) { | ||
pendingAsyncCalls[asyncCallId] = { resolve: resolve, reject: reject }; | ||
const asyncCallId = nextAsyncCallId++; | ||
const resultPromise = new Promise((resolve, reject) => { | ||
pendingAsyncCalls[asyncCallId] = { resolve, reject }; | ||
}); | ||
try { | ||
var argsJson = JSON.stringify(args, argReplacer); | ||
const argsJson = JSON.stringify(args, argReplacer); | ||
getRequiredDispatcher().beginInvokeDotNetFromJS(asyncCallId, assemblyName, methodIdentifier, dotNetObjectId, argsJson); | ||
@@ -206,5 +195,5 @@ } | ||
if (!pendingAsyncCalls.hasOwnProperty(asyncCallId)) { | ||
throw new Error("There is no pending async call with ID " + asyncCallId + "."); | ||
throw new Error(`There is no pending async call with ID ${asyncCallId}.`); | ||
} | ||
var asyncCall = pendingAsyncCalls[asyncCallId]; | ||
const asyncCall = pendingAsyncCalls[asyncCallId]; | ||
delete pendingAsyncCalls[asyncCallId]; | ||
@@ -221,3 +210,3 @@ if (success) { | ||
*/ | ||
var JSCallResultType; | ||
let JSCallResultType; | ||
(function (JSCallResultType) { | ||
@@ -238,3 +227,3 @@ JSCallResultType[JSCallResultType["Default"] = 0] = "Default"; | ||
*/ | ||
findJSFunction: findJSFunction, | ||
findJSFunction, | ||
/** | ||
@@ -245,3 +234,3 @@ * Disposes the JavaScript object reference with the specified object ID. | ||
*/ | ||
disposeJSObjectReferenceById: disposeJSObjectReferenceById, | ||
disposeJSObjectReferenceById, | ||
/** | ||
@@ -256,5 +245,5 @@ * Invokes the specified synchronous JavaScript function. | ||
*/ | ||
invokeJSFromDotNet: function (identifier, argsJson, resultType, targetInstanceId) { | ||
var returnValue = findJSFunction(identifier, targetInstanceId).apply(null, parseJsonWithRevivers(argsJson)); | ||
var result = createJSCallResult(returnValue, resultType); | ||
invokeJSFromDotNet: (identifier, argsJson, resultType, targetInstanceId) => { | ||
const returnValue = findJSFunction(identifier, targetInstanceId).apply(null, parseJsonWithRevivers(argsJson)); | ||
const result = createJSCallResult(returnValue, resultType); | ||
return result === null || result === undefined | ||
@@ -273,7 +262,7 @@ ? null | ||
*/ | ||
beginInvokeJSFromDotNet: function (asyncHandle, identifier, argsJson, resultType, targetInstanceId) { | ||
beginInvokeJSFromDotNet: (asyncHandle, identifier, argsJson, resultType, targetInstanceId) => { | ||
// Coerce synchronous functions into async ones, plus treat | ||
// synchronous exceptions the same as async ones | ||
var promise = new Promise(function (resolve) { | ||
var synchronousResultOrPromise = findJSFunction(identifier, targetInstanceId).apply(null, parseJsonWithRevivers(argsJson)); | ||
const promise = new Promise(resolve => { | ||
const synchronousResultOrPromise = findJSFunction(identifier, targetInstanceId).apply(null, parseJsonWithRevivers(argsJson)); | ||
resolve(synchronousResultOrPromise); | ||
@@ -285,3 +274,3 @@ }); | ||
// Not using "await" because it codegens a lot of boilerplate | ||
promise.then(function (result) { return getRequiredDispatcher().endInvokeJSFromDotNet(asyncHandle, true, JSON.stringify([asyncHandle, true, createJSCallResult(result, resultType)], argReplacer)); }, function (error) { return getRequiredDispatcher().endInvokeJSFromDotNet(asyncHandle, false, JSON.stringify([asyncHandle, false, formatError(error)])); }); | ||
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)]))); | ||
} | ||
@@ -295,4 +284,4 @@ }, | ||
*/ | ||
endInvokeDotNetFromJS: function (asyncCallId, success, resultOrExceptionMessage) { | ||
var resultOrError = success ? resultOrExceptionMessage : new Error(resultOrExceptionMessage); | ||
endInvokeDotNetFromJS: (asyncCallId, success, resultOrExceptionMessage) => { | ||
const resultOrError = success ? resultOrExceptionMessage : new Error(resultOrExceptionMessage); | ||
completePendingCall(parseInt(asyncCallId), success, resultOrError); | ||
@@ -303,3 +292,3 @@ } | ||
if (error instanceof Error) { | ||
return error.message + "\n" + error.stack; | ||
return `${error.message}\n${error.stack}`; | ||
} | ||
@@ -311,3 +300,3 @@ else { | ||
function findJSFunction(identifier, targetInstanceId) { | ||
var targetInstance = cachedJSObjectsById[targetInstanceId]; | ||
let targetInstance = cachedJSObjectsById[targetInstanceId]; | ||
if (targetInstance) { | ||
@@ -317,3 +306,3 @@ return targetInstance.findFunction(identifier); | ||
else { | ||
throw new Error("JS object instance with ID " + targetInstanceId + " does not exist (has it been disposed?)."); | ||
throw new Error(`JS object instance with ID ${targetInstanceId} does not exist (has it been disposed?).`); | ||
} | ||
@@ -324,30 +313,21 @@ } | ||
} | ||
var DotNetObject = /** @class */ (function () { | ||
function DotNetObject(_id) { | ||
class DotNetObject { | ||
constructor(_id) { | ||
this._id = _id; | ||
} | ||
DotNetObject.prototype.invokeMethod = function (methodIdentifier) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
invokeMethod(methodIdentifier, ...args) { | ||
return invokePossibleInstanceMethod(null, methodIdentifier, this._id, args); | ||
}; | ||
DotNetObject.prototype.invokeMethodAsync = function (methodIdentifier) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
} | ||
invokeMethodAsync(methodIdentifier, ...args) { | ||
return invokePossibleInstanceMethodAsync(null, methodIdentifier, this._id, args); | ||
}; | ||
DotNetObject.prototype.dispose = function () { | ||
var promise = invokePossibleInstanceMethodAsync(null, '__Dispose', this._id, null); | ||
promise.catch(function (error) { return console.error(error); }); | ||
}; | ||
DotNetObject.prototype.serializeAsArg = function () { | ||
} | ||
dispose() { | ||
const promise = invokePossibleInstanceMethodAsync(null, '__Dispose', this._id, null); | ||
promise.catch(error => console.error(error)); | ||
} | ||
serializeAsArg() { | ||
return { __dotNetObject: this._id }; | ||
}; | ||
return DotNetObject; | ||
}()); | ||
var dotNetObjectRefKey = '__dotNetObject'; | ||
} | ||
} | ||
const dotNetObjectRefKey = '__dotNetObject'; | ||
attachReviver(function reviveDotNetObject(key, value) { | ||
@@ -362,4 +342,4 @@ if (value && typeof value === 'object' && value.hasOwnProperty(dotNetObjectRefKey)) { | ||
if (value && typeof value === 'object' && value.hasOwnProperty(jsObjectIdKey)) { | ||
var id = value[jsObjectIdKey]; | ||
var jsObject = cachedJSObjectsById[id]; | ||
const id = value[jsObjectIdKey]; | ||
const jsObject = cachedJSObjectsById[id]; | ||
if (jsObject) { | ||
@@ -369,3 +349,3 @@ return jsObject.getWrappedObject(); | ||
else { | ||
throw new Error("JS object instance with ID " + id + " does not exist (has it been disposed?)."); | ||
throw new Error(`JS object instance with ID ${id} does not exist (has it been disposed?).`); | ||
} | ||
@@ -383,3 +363,3 @@ } | ||
default: | ||
throw new Error("Invalid JS call result type '" + resultType + "'."); | ||
throw new Error(`Invalid JS call result type '${resultType}'.`); | ||
} | ||
@@ -386,0 +366,0 @@ } |
{ | ||
"name": "@microsoft/dotnet-js-interop", | ||
"version": "6.0.0-preview.2.21154.6", | ||
"version": "6.0.0-preview.3.21201.13", | ||
"description": "Provides abstractions and features for interop between .NET and JavaScript code.", | ||
"main": "dist/Microsoft.JSInterop.js", | ||
"types": "dist/Microsoft.JSInterop.d.ts", | ||
"type": "module", | ||
"scripts": { | ||
@@ -28,6 +29,6 @@ "preclean": "yarn install --mutex network", | ||
"devDependencies": { | ||
"rimraf": "^2.5.4", | ||
"tslint": "^5.9.1", | ||
"typescript": "^2.7.1" | ||
"rimraf": "^3.0.2", | ||
"tslint": "^6.1.3", | ||
"typescript": "^4.2.2" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Yes
34773
488