@todesktop/client-util
Advanced tools
Comparing version
/** | ||
* Barebones EventEmitter implementation for API namespaces. | ||
*/ | ||
import type { BrowserWindow, WebContents } from "@todesktop/client-electron-types"; | ||
import type { BrowserWindow, WebContents, Tray, NativeImage, NativeTheme, Menu, MenuItem, BrowserView } from "@todesktop/client-electron-types"; | ||
/** | ||
@@ -20,9 +20,30 @@ * Setup the ability to register an event handler for the given `eventMappings`. | ||
export declare type MethodFunc = (...args: any[]) => unknown; | ||
export declare type ConstructorFunc = new (...args: any) => any; | ||
export interface NamespacesInstancesV1 { | ||
BrowserWindow: BrowserWindow; | ||
WebContents: WebContents; | ||
Tray: Tray; | ||
NativeImage: NativeImage; | ||
NativeTheme: NativeTheme; | ||
Menu: Menu; | ||
MenuItem: MenuItem; | ||
BrowserView: BrowserView; | ||
} | ||
export interface NamespacesStaticV1 { | ||
BrowserWindow: typeof BrowserWindow; | ||
Tray: typeof Tray; | ||
Menu: typeof Menu; | ||
MenuItem: typeof MenuItem; | ||
BrowserView: typeof BrowserView; | ||
} | ||
export interface InstanceRefObject { | ||
id: string; | ||
namespace: string; | ||
type: string; | ||
} | ||
declare type ChangeInstancesToInstanceRefsSingle<T> = T extends NamespacesInstancesV1[keyof NamespacesInstancesV1] ? InstanceRefObject : T; | ||
declare type ChangeInstancesToInstanceRefsInParams<Obj> = { | ||
[K in keyof Obj]: ChangeInstancesToInstanceRefsSingle<Obj[K]>; | ||
}; | ||
declare type ChangeInstancesToInstanceRefs<T> = T extends any[] ? ChangeInstancesToInstanceRefsInParams<T> : ChangeInstancesToInstanceRefsSingle<T>; | ||
/** | ||
@@ -45,3 +66,3 @@ * @internal | ||
*/ | ||
args: Parameters<Func>; | ||
args: ChangeInstancesToInstanceRefs<Parameters<Func>>; | ||
/** | ||
@@ -52,2 +73,17 @@ * A refernce to the instance on which to invoke the method | ||
} | ||
export interface InvokeConstructorCommand<NS, Func extends ConstructorFunc> { | ||
/** | ||
* The namespace containing the class you wish to construct. | ||
*/ | ||
namespace: NS; | ||
/** | ||
* Any arguments required by the constructor. | ||
*/ | ||
args: ChangeInstancesToInstanceRefs<ConstructorParameters<Func>>; | ||
} | ||
export interface InvokeConstructorCommandWithPlaceholderMethod<NS, Func extends ConstructorFunc> { | ||
namespace: NS; | ||
args: ChangeInstancesToInstanceRefs<ConstructorParameters<Func>>; | ||
method: ":new"; | ||
} | ||
export interface InvokePropCommand<NS, M, IR> { | ||
@@ -79,2 +115,8 @@ /** | ||
} | ||
export interface InvokeConstructorPayload<NS, Func> { | ||
/** | ||
* The command to execute. | ||
*/ | ||
command: Func extends ConstructorFunc ? InvokeConstructorCommand<NS, Func> : never; | ||
} | ||
/** | ||
@@ -112,3 +154,3 @@ * @internal | ||
} | ||
export interface InvokeOptions<NS, M, Func, IR> { | ||
export interface InvokeOptions<T> { | ||
metadata: { | ||
@@ -123,3 +165,3 @@ package: { | ||
}; | ||
payload: InvokePayload<NS, M, Func, IR>; | ||
payload: T; | ||
} | ||
@@ -136,2 +178,6 @@ /** | ||
*/ | ||
export declare function createInvokeWithMetadata(name: string, version: string): <NS = NamespacesStaticV1 & NamespacesInstancesV1, NSName extends keyof NS = keyof NS, MethodName extends keyof NS[NSName] = keyof NS[NSName], Func = NS[NSName][MethodName], InstanceRef = void | Record<string, unknown>>(command: Func extends MethodFunc ? InvokeMethodCommand<NSName, MethodName, Func, InstanceRef> : InvokePropCommand<NSName, MethodName, InstanceRef>) => Promise<Func extends MethodFunc ? ReturnType<Func> : Func>; | ||
export declare function createInvokeWithMetadata(name: string, version: string): { | ||
invoke: <NS = NamespacesStaticV1 & NamespacesInstancesV1, NSName extends keyof NS = keyof NS, MethodName extends keyof NS[NSName] = keyof NS[NSName], Func = NS[NSName][MethodName], InstanceRef = void | InstanceRefObject>(command: Func extends MethodFunc ? InvokeMethodCommand<NSName, MethodName, Func, InstanceRef> : InvokePropCommand<NSName, MethodName, InstanceRef>) => Promise<Func extends MethodFunc ? ChangeInstancesToInstanceRefs<ReturnType<Func>> : Func>; | ||
construct: <NS_1 = NamespacesStaticV1, NSName_1 extends keyof NS_1 = keyof NS_1, Func_1 = NS_1[NSName_1]>(command: Func_1 extends ConstructorFunc ? InvokeConstructorCommand<NSName_1, Func_1> : never) => Promise<InstanceRefObject>; | ||
}; | ||
export {}; |
22
index.js
@@ -39,8 +39,9 @@ /** | ||
export function createInvokeWithMetadata(name, version) { | ||
function _invoke(command) { | ||
const metadata = { | ||
package: { name, version }, | ||
invoke: { version: "1.0.0" }, | ||
}; | ||
function invoke(command) { | ||
const options = { | ||
metadata: { | ||
package: { name, version }, | ||
invoke: { version: "1.0.0" }, | ||
}, | ||
metadata, | ||
payload: { | ||
@@ -52,3 +53,12 @@ command, | ||
} | ||
return _invoke; | ||
function construct(command) { | ||
const options = { | ||
metadata, | ||
payload: { | ||
command: Object.assign(Object.assign({}, command), { method: ":new" }), | ||
}, | ||
}; | ||
return window["todesktop"]["invoke"](options); | ||
} | ||
return { invoke, construct }; | ||
} |
{ | ||
"name": "@todesktop/client-util", | ||
"version": "1.0.0-alpha3", | ||
"version": "1.0.0-alpha4", | ||
"description": "Utility functions for ToDesktop client libraries", | ||
@@ -22,3 +22,3 @@ "main": "index.js", | ||
"agadoo": "^2.0.0", | ||
"typescript": "^4.3.4" | ||
"typescript": "^4.5.5" | ||
}, | ||
@@ -25,0 +25,0 @@ "dependencies": { |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
247
29.32%9009
-96.28%4
-20%