refactor: declare KeyWeakMap<K, V> returned by createIDWeakMap() / createDoubleIDWeakMap() (#21171)

This commit is contained in:
Milan Burda 2019-11-21 13:32:31 +01:00 committed by Alexey Kuzmin
parent 92ff39c168
commit 3f2cb91a35
4 changed files with 15 additions and 7 deletions

View file

@ -23,7 +23,7 @@ const FUNCTION_PROPERTIES = [
// The remote functions in renderer processes. // The remote functions in renderer processes.
// id => Function // id => Function
const rendererFunctions = v8Util.createDoubleIDWeakMap() const rendererFunctions = v8Util.createDoubleIDWeakMap<(...args: any[]) => void>()
type ObjectMember = { type ObjectMember = {
name: string, name: string,
@ -289,7 +289,7 @@ const unwrapArgs = function (sender: electron.WebContents, frameId: number, cont
case 'function': { case 'function': {
// Merge contextId and meta.id, since meta.id can be the same in // Merge contextId and meta.id, since meta.id can be the same in
// different webContents. // different webContents.
const objectId = [contextId, meta.id] const objectId: [string, number] = [contextId, meta.id]
// Cache the callbacks in renderer. // Cache the callbacks in renderer.
if (rendererFunctions.has(objectId)) { if (rendererFunctions.has(objectId)) {
@ -373,12 +373,12 @@ const logStack = function (contents: electron.WebContents, code: string, stack:
} }
handleRemoteCommand('ELECTRON_BROWSER_WRONG_CONTEXT_ERROR', function (event, contextId, passedContextId, id) { handleRemoteCommand('ELECTRON_BROWSER_WRONG_CONTEXT_ERROR', function (event, contextId, passedContextId, id) {
const objectId = [passedContextId, id] const objectId: [string, number] = [passedContextId, id]
if (!rendererFunctions.has(objectId)) { if (!rendererFunctions.has(objectId)) {
// Do nothing if the error has already been reported before. // Do nothing if the error has already been reported before.
return return
} }
removeRemoteListenersAndLogWarning(event.sender, rendererFunctions.get(objectId)) removeRemoteListenersAndLogWarning(event.sender, rendererFunctions.get(objectId)!)
}) })
handleRemoteCommand('ELECTRON_BROWSER_REQUIRE', function (event, contextId, moduleName, stack) { handleRemoteCommand('ELECTRON_BROWSER_REQUIRE', function (event, contextId, moduleName, stack) {

View file

@ -829,7 +829,7 @@ describe('Menu module', function () {
// Keep a weak reference to the menu. // Keep a weak reference to the menu.
const v8Util = process.electronBinding('v8_util') const v8Util = process.electronBinding('v8_util')
const map = (v8Util as any).createIDWeakMap() as any const map = v8Util.createIDWeakMap<Electron.Menu>()
map.set(0, menu) map.set(0, menu)
setTimeout(() => { setTimeout(() => {
@ -839,7 +839,7 @@ describe('Menu module', function () {
setTimeout(() => { setTimeout(() => {
// Try to receive menu from weak reference. // Try to receive menu from weak reference.
if (map.has(0)) { if (map.has(0)) {
map.get(0).closePopup() map.get(0)!.closePopup()
done() done()
} else { } else {
done('Menu is garbage-collected while popuping') done('Menu is garbage-collected while popuping')

View file

@ -29,7 +29,8 @@ declare namespace NodeJS {
setHiddenValue<T>(obj: any, key: string, value: T): void; setHiddenValue<T>(obj: any, key: string, value: T): void;
deleteHiddenValue(obj: any, key: string): void; deleteHiddenValue(obj: any, key: string): void;
requestGarbageCollectionForTesting(): void; requestGarbageCollectionForTesting(): void;
createDoubleIDWeakMap(): any; createIDWeakMap<V>(): ElectronInternal.KeyWeakMap<number, V>;
createDoubleIDWeakMap<V>(): ElectronInternal.KeyWeakMap<[string, number], V>;
setRemoteCallbackFreer(fn: Function, frameId: number, contextId: String, id: number, sender: any): void setRemoteCallbackFreer(fn: Function, frameId: number, contextId: String, id: number, sender: any): void
} }

View file

@ -112,6 +112,13 @@ declare namespace ElectronInternal {
appIcon: string | null; appIcon: string | null;
} }
interface KeyWeakMap<K, V> {
set(key: K, value: V): void;
get(key: K): V | undefined;
has(key: K): boolean;
remove(key: K): void;
}
// Internal IPC has _replyInternal and NO reply method // Internal IPC has _replyInternal and NO reply method
interface IpcMainInternalEvent extends Omit<Electron.IpcMainEvent, 'reply'> { interface IpcMainInternalEvent extends Omit<Electron.IpcMainEvent, 'reply'> {
_replyInternal(...args: any[]): void; _replyInternal(...args: any[]): void;