1
- /* global chrome */
1
+ /* global browser, Proxy */
2
2
3
3
import Ember from 'ember' ;
4
- import spanan from 'npm:spanan' ;
5
4
6
- const Spanan = spanan . default ;
5
+ function createActionWrapperForModule ( module ) {
6
+ const actionHandler = ( action , ...args ) => browser . runtime . sendMessage ( {
7
+ module,
8
+ action,
9
+ args,
10
+ } ) ;
7
11
8
- function createSpananForModule ( moduleName ) {
9
- const spanan = new Spanan ( ( { uuid, action, args } ) => {
10
- const message = {
11
- module : moduleName ,
12
- action,
13
- requestId : uuid ,
14
- args
15
- } ;
16
- const onResponse = ( response ) => {
17
- if ( ! response ) {
18
- return ;
12
+ // Return a proxy so that people can call `module.actionName(...args)` instead
13
+ // of `module.action('actionName', ...args)`. This is not strictly needed but
14
+ // modules are expecting this API.
15
+ return new Proxy ( { } , {
16
+ get : ( obj , prop ) => {
17
+ if ( prop === 'action' ) {
18
+ return actionHandler ;
19
19
}
20
- spanan . handleMessage ( {
21
- uuid,
22
- response : response . response
23
- } ) ;
24
- } ;
25
-
26
- const promise = chrome . runtime . sendMessage ( message , onResponse ) ;
27
20
28
- if ( promise && promise . then ) {
29
- promise . then ( onResponse ) ;
30
- }
21
+ return actionHandler . bind ( obj , prop ) ;
22
+ } ,
31
23
} ) ;
32
-
33
- return spanan ;
34
24
}
35
25
36
26
export default Ember . Service . extend ( {
@@ -39,8 +29,7 @@ export default Ember.Service.extend({
39
29
init ( ) {
40
30
this . _super ( ...arguments ) ;
41
31
42
- const history = createSpananForModule ( 'history' ) ;
43
- const historyProxy = history . createProxy ( ) ;
32
+ const historyProxy = createActionWrapperForModule ( 'history' ) ;
44
33
this . deleteVisit = historyProxy . deleteVisit ;
45
34
this . deleteVisits = historyProxy . deleteVisits ;
46
35
this . showHistoryDeletionPopup = historyProxy . showHistoryDeletionPopup ;
@@ -52,26 +41,17 @@ export default Ember.Service.extend({
52
41
this . openUrl = historyProxy . openUrl ;
53
42
this . selectTabAtIndex = historyProxy . selectTabAtIndex ;
54
43
55
- const core = createSpananForModule ( 'core' ) ;
56
- const coreProxy = core . createProxy ( ) ;
44
+ const coreProxy = createActionWrapperForModule ( 'core' ) ;
57
45
this . getCliqzStatus = coreProxy . status ;
58
46
this . queryCliqz = coreProxy . queryCliqz ;
59
47
this . redoQuery = coreProxy . redoQuery ;
60
48
this . sendTelemetry = coreProxy . sendTelemetry ;
61
49
this . openFeedbackPage = coreProxy . openFeedbackPage ;
62
50
63
- chrome . runtime . onMessage . addListener ( ( message ) => {
64
- if ( message . action === "updateHistoryUrls" && message . message ) {
65
- this . get ( 'historySync' ) . updateHistoryUrls ( message . message . urls ) ;
66
- }
67
-
68
- if ( message . response ) {
69
- const spananMessage = {
70
- uuid : message . requestId ,
71
- response : message . response
72
- } ;
73
- history . handleMessage ( spananMessage ) ;
74
- core . handleMesssage ( spananMessage ) ;
51
+ browser . runtime . onMessage . addListener ( ( message ) => {
52
+ if ( message . action === "updateHistoryUrls" ) {
53
+ const { urls } = message . args [ 0 ] ;
54
+ this . get ( 'historySync' ) . updateHistoryUrls ( urls ) ;
75
55
}
76
56
} ) ;
77
57
} ,
0 commit comments