-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathtest.example.js
53 lines (47 loc) · 1.15 KB
/
test.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
50
51
52
53
import { MessagingContext, TestTransportConfig } from '../../index.js'
/**
* Creates an ad-hoc messaging transport on the fly - useful for testing
* You need to implement
* - notify
* - request
* - subscribe
*/
const config = new TestTransportConfig({
notify (msg) {
console.log(msg)
},
request: (msg) => {
if (msg.method === 'getUserValues') {
return Promise.resolve({
foo: 'bar'
})
}
return Promise.resolve(null)
},
subscribe (msg) {
console.log(msg)
return () => {
console.log('teardown')
}
}
})
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()