Skip to content
This repository has been archived by the owner on Sep 21, 2021. It is now read-only.

Commit

Permalink
Compatibility with new messaging format (#29)
Browse files Browse the repository at this point in the history
* Use new module wrapper
* Bump version
  • Loading branch information
Vladimir Zhuravlev committed Aug 14, 2019
1 parent 5c6c80c commit 8953734
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 52 deletions.
64 changes: 22 additions & 42 deletions app/services/cliqz.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,26 @@
/* global chrome */
/* global browser, Proxy */

import Ember from 'ember';
import spanan from 'npm:spanan';

const Spanan = spanan.default;
function createActionWrapperForModule(module) {
const actionHandler = (action, ...args) => browser.runtime.sendMessage({
module,
action,
args,
});

function createSpananForModule(moduleName) {
const spanan = new Spanan(({ uuid, action, args }) => {
const message = {
module: moduleName,
action,
requestId: uuid,
args
};
const onResponse = (response) => {
if (!response) {
return;
// Return a proxy so that people can call `module.actionName(...args)` instead
// of `module.action('actionName', ...args)`. This is not strictly needed but
// modules are expecting this API.
return new Proxy({}, {
get: (obj, prop) => {
if (prop === 'action') {
return actionHandler;
}
spanan.handleMessage({
uuid,
response: response.response
});
};

const promise = chrome.runtime.sendMessage(message, onResponse);

if (promise && promise.then) {
promise.then(onResponse);
}
return actionHandler.bind(obj, prop);
},
});

return spanan;
}

export default Ember.Service.extend({
Expand All @@ -39,8 +29,7 @@ export default Ember.Service.extend({
init() {
this._super(...arguments);

const history = createSpananForModule('history');
const historyProxy = history.createProxy();
const historyProxy = createActionWrapperForModule('history');
this.deleteVisit = historyProxy.deleteVisit;
this.deleteVisits = historyProxy.deleteVisits;
this.showHistoryDeletionPopup = historyProxy.showHistoryDeletionPopup;
Expand All @@ -52,26 +41,17 @@ export default Ember.Service.extend({
this.openUrl = historyProxy.openUrl;
this.selectTabAtIndex = historyProxy.selectTabAtIndex;

const core = createSpananForModule('core');
const coreProxy = core.createProxy();
const coreProxy = createActionWrapperForModule('core');
this.getCliqzStatus = coreProxy.status;
this.queryCliqz = coreProxy.queryCliqz;
this.redoQuery = coreProxy.redoQuery;
this.sendTelemetry = coreProxy.sendTelemetry;
this.openFeedbackPage = coreProxy.openFeedbackPage;

chrome.runtime.onMessage.addListener((message) => {
if(message.action === "updateHistoryUrls" && message.message) {
this.get('historySync').updateHistoryUrls(message.message.urls);
}

if (message.response) {
const spananMessage = {
uuid: message.requestId,
response: message.response
};
history.handleMessage(spananMessage);
core.handleMesssage(spananMessage);
browser.runtime.onMessage.addListener((message) => {
if(message.action === "updateHistoryUrls") {
const { urls } = message.args[0];
this.get('historySync').updateHistoryUrls(urls);
}
});
},
Expand Down
8 changes: 1 addition & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cliqz-history",
"version": "1.1.3",
"version": "1.2.0",
"description": "",
"license": "MPL-2.0",
"author": "",
Expand Down Expand Up @@ -42,8 +42,7 @@
"ember-source": "~2.14.0",
"ember-welcome-page": "^3.2.0",
"liquid-fire": "^0.29.5",
"loader.js": "^4.2.3",
"spanan": "^2.0.0"
"loader.js": "^4.2.3"
},
"engines": {
"node": "^4.5 || 6.* || >= 7.*"
Expand Down

0 comments on commit 8953734

Please sign in to comment.