-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathwindows.example.js
49 lines (42 loc) · 1.43 KB
/
windows.example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { WindowsMessagingConfig } from '../windows.js'
import { MessagingContext } from '../../index.js'
/**
* These 3 required methods that get assigned by the Native side.
*/
// @ts-expect-error - webview is not in @types/chrome
const windowsInteropPostMessage = window.chrome.webview.postMessage
// @ts-expect-error - webview is not in @types/chrome
const windowsInteropAddEventListener = window.chrome.webview.addEventListener
// @ts-expect-error - webview is not in @types/chrome
const windowsInteropRemoveEventListener = window.chrome.webview.removeEventListener
/**
* With those methods available in the same lexical scope, we can then create
* our WindowsMessagingConfig
*/
const config = new WindowsMessagingConfig({
methods: {
postMessage: windowsInteropPostMessage,
addEventListener: windowsInteropAddEventListener,
removeEventListener: windowsInteropRemoveEventListener
}
})
const messagingContext = new MessagingContext({
context: 'contentScopeScripts',
featureName: 'hello-world',
env: 'development'
})
/**
* And then send notifications!
*/
const messaging = config.intoMessaging(messagingContext)
messaging.notify('helloWorld')
/**
* Or request some data
*/
messaging.request('getData', { foo: 'bar' }).then(console.log).catch(console.error)
/**
* Or subscribe for push messages
*/
const unsubscribe = messaging.subscribe('getData', (data) => console.log(data))
// later
unsubscribe()