Skip to content

Commit

Permalink
fix: node:events import
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmaddock committed Jan 7, 2025
1 parent 74898fe commit 3a7845e
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 11 deletions.
1 change: 0 additions & 1 deletion packages/electron-chrome-extensions/spec/crx-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as path from 'node:path'
import { app, BrowserWindow, session, webContents } from 'electron'
import { uuid } from './spec-helpers'
import { ElectronChromeExtensions } from '../dist'

export const createCrxSession = () => {
const partitionName = `crx-${uuid()}`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'chai'
import { session } from 'electron'
import { ElectronChromeExtensions } from '../dist'
import { ElectronChromeExtensions } from '../'

describe('Extensions', () => {
const testSession = session.fromPartition('test-extensions')
Expand Down
2 changes: 1 addition & 1 deletion packages/electron-chrome-extensions/spec/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ipcMain, BrowserWindow, app, Extension, webContents } from 'electron'
import * as http from 'http'
import * as path from 'node:path'
import { AddressInfo } from 'net'
import { ElectronChromeExtensions } from '../dist'
import { ElectronChromeExtensions } from '../'
import { emittedOnce } from './events-helpers'
import { addCrxPreload, createCrxSession, waitForBackgroundScriptEvaluated } from './crx-helpers'

Expand Down
13 changes: 6 additions & 7 deletions packages/electron-chrome-extensions/src/browser-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import { ipcRenderer, contextBridge, webFrame } from 'electron'
* Injects `<browser-action>` custom element into the current webpage.
*/
export const injectBrowserAction = () => {
// Load node:events directly from Electron rather than bundling
const { EventEmitter } = require('node:events')

const actionMap = new Map<string, any>()
const internalEmitter = new EventEmitter()
const observerCounts = new Map<string, number>()

// Reuse `process` to avoid bundling custom EventEmitter
const internalEmitter = process as NodeJS.EventEmitter

const invoke = <T>(name: string, partition: string, ...args: any[]): Promise<T> => {
return ipcRenderer.invoke('crx-msg-remote', partition, name, ...args)
}
Expand All @@ -24,10 +23,10 @@ export const injectBrowserAction = () => {

const __browserAction__ = {
addEventListener(name: string, listener: (...args: any[]) => void) {
internalEmitter.addListener(name, listener)
internalEmitter.addListener(`-actions-${name}`, listener)
},
removeEventListener(name: string, listener: (...args: any[]) => void) {
internalEmitter.removeListener(name, listener)
internalEmitter.removeListener(`-actions-${name}`, listener)
},

getAction(extensionId: string) {
Expand All @@ -38,7 +37,7 @@ export const injectBrowserAction = () => {
for (const action of state.actions) {
actionMap.set(action.id, action)
}
queueMicrotask(() => internalEmitter.emit('update', state))
queueMicrotask(() => internalEmitter.emit('-actions-update', state))
return state
},

Expand Down
2 changes: 1 addition & 1 deletion packages/electron-chrome-extensions/src/browser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class ElectronChromeExtensions extends EventEmitter {
store,
}

this.modulePath = modulePath || path.join(__dirname, '..')
this.modulePath = modulePath || path.join(__dirname, '../..')

this.api = {
browserAction: new BrowserActionAPI(this.ctx),
Expand Down

0 comments on commit 3a7845e

Please sign in to comment.