Skip to content

Commit

Permalink
chore: improve type safety (#40)
Browse files Browse the repository at this point in the history
Co-authored-by: Milan Burda <miburda@microsoft.com>
  • Loading branch information
miniak and Milan Burda committed Mar 9, 2021
1 parent 73ed252 commit 9af5b2e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
File renamed without changes.
20 changes: 10 additions & 10 deletions src/main/objects-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class ObjectsRegistry {

// Register a new object and return its assigned ID. If the object is already
// registered then the already assigned ID would be returned.
add (webContents: WebContents, contextId: string, obj: any) {
add (webContents: WebContents, contextId: string, obj: any): number {
// Get or assign an ID to the object.
const id = this.saveToStorage(obj)

Expand All @@ -41,15 +41,15 @@ class ObjectsRegistry {
}

// Get an object according to its ID.
get (id: number) {
get (id: number): any {
const pointer = this.storage[id]
if (pointer != null) return pointer.object
}

// Dereference an object according to its ID.
// Note that an object may be double-freed (cleared when page is reloaded, and
// then garbage collected in old page).
remove (webContents: WebContents, contextId: string, id: number) {
remove (webContents: WebContents, contextId: string, id: number): void {
const ownerKey = getOwnerKey(webContents, contextId)
const owner = this.owners[ownerKey]
if (owner && owner.has(id)) {
Expand All @@ -69,7 +69,7 @@ class ObjectsRegistry {
}

// Clear all references to objects refrenced by the WebContents.
clear (webContents: WebContents, contextId: string) {
clear (webContents: WebContents, contextId: string): void {
const ownerKey = getOwnerKey(webContents, contextId)
const owner = this.owners[ownerKey]
if (!owner) return
Expand All @@ -79,8 +79,8 @@ class ObjectsRegistry {
delete this.owners[ownerKey]
}

// Private: Saves the object into storage and assigns an ID for it.
saveToStorage (object: any) {
// Saves the object into storage and assigns an ID for it.
private saveToStorage (object: any): number {
let id = this.electronIds.get(object)
if (!id) {
id = ++this.nextId
Expand All @@ -93,8 +93,8 @@ class ObjectsRegistry {
return id
}

// Private: Dereference the object from store.
dereference (id: number) {
// Dereference the object from store.
private dereference (id: number): void {
const pointer = this.storage[id]
if (pointer == null) {
return
Expand All @@ -106,8 +106,8 @@ class ObjectsRegistry {
}
}

// Private: Clear the storage when renderer process is destroyed.
registerDeleteListener (webContents: WebContents, contextId: string) {
// Clear the storage when renderer process is destroyed.
private registerDeleteListener (webContents: WebContents, contextId: string): void {
// contextId => ${processHostId}-${contextCount}
const processHostId = contextId.split('-')[0]
const listener = (_: any, deletedProcessHostId: string) => {
Expand Down
16 changes: 8 additions & 8 deletions src/renderer/callbacks-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ export class CallbacksRegistry {
private callbackIds = new WeakMap<Function, number>();
private locationInfo = new WeakMap<Function, string>();

add (callback: Function) {
add (callback: Function): number {
// The callback is already added.
let id = this.callbackIds.get(callback);
if (id != null) return id

id = this.nextId += 1
this.callbacks[id] = callback
this.callbackIds.set(callback, id);

// Capture the location of the function and put it in the ID string,
// so that release errors can be tracked down easily.
const regexp = /at (.*)/gi
const stackString = (new Error()).stack
if (!stackString) return
if (!stackString) return id;

let filenameAndLine: string;
let match
Expand All @@ -34,25 +36,23 @@ export class CallbacksRegistry {
break
}

this.callbacks[id] = callback
this.callbackIds.set(callback, id);
this.locationInfo.set(callback, filenameAndLine!);
return id
}

get (id: number) {
get (id: number): Function {
return this.callbacks[id] || function () {}
}

getLocation (callback: Function) {
getLocation (callback: Function): string | undefined {
return this.locationInfo.get(callback);
}

apply (id: number, ...args: any[]) {
apply (id: number, ...args: any[]): any {
return this.get(id).apply(global, ...args)
}

remove (id: number) {
remove (id: number): void {
const callback = this.callbacks[id]
if (callback) {
this.callbackIds.delete(callback);
Expand Down
10 changes: 5 additions & 5 deletions src/renderer/remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ process.on('exit', () => {
const IS_REMOTE_PROXY = Symbol('is-remote-proxy')

// Convert the arguments object into an array of meta data.
function wrapArgs (args: any[], visited = new Set()): any {
const valueToMeta = (value: any): any => {
function wrapArgs (args: any[], visited = new Set()): MetaTypeFromRenderer[] {
const valueToMeta = (value: any): MetaTypeFromRenderer => {
// Check for circular reference.
if (visited.has(value)) {
return {
Expand All @@ -64,7 +64,7 @@ function wrapArgs (args: any[], visited = new Set()): any {
return { type: 'nativeimage', value: serialize(value) }
} else if (Array.isArray(value)) {
visited.add(value)
const meta = {
const meta: MetaTypeFromRenderer = {
type: 'array',
value: wrapArgs(value, visited)
}
Expand All @@ -91,7 +91,7 @@ function wrapArgs (args: any[], visited = new Set()): any {
} else if (electronIds.has(value)) {
return {
type: 'remote-object',
id: electronIds.get(value)
id: electronIds.get(value)!
}
}

Expand All @@ -118,7 +118,7 @@ function wrapArgs (args: any[], visited = new Set()): any {
return {
type: 'function',
id: callbacksRegistry.add(value),
location: callbacksRegistry.getLocation(value),
location: callbacksRegistry.getLocation(value)!,
length: value.length
}
} else {
Expand Down

0 comments on commit 9af5b2e

Please sign in to comment.