-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split ContentApi into a ContentBus and AppBus
- Loading branch information
Showing
6 changed files
with
168 additions
and
110 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Listener, Message, Namespace } from '@/bus/Message'; | ||
|
||
enum Actions { | ||
ElementClicked = 1, | ||
} | ||
|
||
export const AppBus = { | ||
namespace: `${ Namespace }_APP`, | ||
actions: Actions, | ||
listen, | ||
stopListening, | ||
elementClicked, | ||
}; | ||
|
||
let listener: Listener; | ||
|
||
function listen( list: Listener ) { | ||
stopListening(); | ||
listener = ( message: Message ) => { | ||
if ( message.namespace !== AppBus.namespace ) { | ||
return; | ||
} | ||
list( message ); | ||
}; | ||
browser.runtime.onMessage.addListener( listener ); | ||
} | ||
|
||
function stopListening() { | ||
if ( listener ) { | ||
browser.runtime.onMessage.removeListener( listener ); | ||
} | ||
} | ||
|
||
async function elementClicked( content: string ): Promise< void > { | ||
return sendMessageToApp( { | ||
action: Actions.ElementClicked, | ||
payload: { content }, | ||
} ); | ||
} | ||
|
||
async function sendMessageToApp( | ||
message: Omit< Message, 'namespace' > | ||
): Promise< void > { | ||
const messageWithNamespace: Message = { | ||
namespace: AppBus.namespace, | ||
action: message.action, | ||
payload: message.payload, | ||
}; | ||
await browser.runtime.sendMessage( messageWithNamespace ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { Listener, Message, Namespace } from '@/bus/Message'; | ||
|
||
enum Actions { | ||
EnableHighlighting = 1, | ||
DisableHighlighting, | ||
} | ||
|
||
export const ContentBus = { | ||
namespace: `${ Namespace }_CONTENT`, | ||
actions: Actions, | ||
listen, | ||
stopListening, | ||
enableHighlighting, | ||
disableHighlighting, | ||
}; | ||
|
||
let listener: Listener; | ||
|
||
function listen( list: Listener ) { | ||
stopListening(); | ||
listener = ( message: Message ) => { | ||
if ( message.namespace !== ContentBus.namespace ) { | ||
return; | ||
} | ||
list( message ); | ||
}; | ||
browser.runtime.onMessage.addListener( listener ); | ||
} | ||
|
||
function stopListening() { | ||
if ( listener ) { | ||
browser.runtime.onMessage.removeListener( listener ); | ||
} | ||
} | ||
|
||
async function enableHighlighting(): Promise< void > { | ||
return sendMessageToContent( { | ||
action: Actions.EnableHighlighting, | ||
payload: {}, | ||
} ); | ||
} | ||
|
||
async function disableHighlighting(): Promise< void > { | ||
return sendMessageToContent( { | ||
action: Actions.DisableHighlighting, | ||
payload: {}, | ||
} ); | ||
} | ||
|
||
async function sendMessageToContent( | ||
message: Omit< Message, 'namespace' > | ||
): Promise< void > { | ||
const currentTabId = await getCurrentTabId(); | ||
if ( ! currentTabId ) { | ||
throw Error( 'current tab not found' ); | ||
} | ||
const messageWithNamespace: Message = { | ||
namespace: ContentBus.namespace, | ||
action: message.action, | ||
payload: message.payload, | ||
}; | ||
await browser.tabs.sendMessage( currentTabId, messageWithNamespace ); | ||
} | ||
|
||
async function getCurrentTabId(): Promise< number | undefined > { | ||
const tabs = await browser.tabs.query( { | ||
currentWindow: true, | ||
active: true, | ||
} ); | ||
if ( tabs.length !== 1 ) { | ||
return; | ||
} | ||
return tabs[ 0 ]?.id; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export const Namespace = 'TRY_WORDPRESS'; | ||
|
||
export interface Message { | ||
namespace: string; | ||
action: number; | ||
payload: object; | ||
} | ||
|
||
export type Listener = ( message: Message ) => void; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters