Skip to content
This repository was archived by the owner on Feb 12, 2023. It is now read-only.

Commit a68b151

Browse files
author
Ke, Mingze
committed
Split into _api.js and chrome.{API_NAME}.js
1 parent 5f27006 commit a68b151

File tree

3 files changed

+87
-68
lines changed

3 files changed

+87
-68
lines changed

‎examples/chrome.serial.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<html>
22

33
<head>
4+
<script src="../lib/chrome._api.js"></script>
45
<script src="../lib/chrome.serial.js"></script>
56
<script src="../bower_components/json-human/src/json.human.js"></script>
67
<script>

‎lib/chrome._api.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
var chrome = chrome || {};
2+
3+
chrome._api = (function (window) {
4+
5+
'use strict';
6+
7+
var slice = Array.prototype.slice,
8+
wnd = window.top,
9+
undef = undefined,
10+
callbackHash = {},
11+
listenerHash = {},
12+
apiCalls = 0;
13+
14+
function proxyRequest(api) {
15+
return function () {
16+
var params = slice.call(arguments),
17+
id = ++apiCalls + '';
18+
19+
if (typeof params[params.length - 1] === 'function') {
20+
callbackHash[id] = params.pop();
21+
}
22+
invoke(id, api, params);
23+
};
24+
}
25+
26+
function proxyAddListener(api) {
27+
return function (listener) {
28+
var id = ++apiCalls + '';
29+
30+
if (typeof listener === 'function') {
31+
listenerHash[id] = listener;
32+
invoke(id, api, []);
33+
}
34+
};
35+
}
36+
37+
function proxyRemoveListener(api) {
38+
return function (listener) {
39+
Object.keys(listenerHash).some(function (id) {
40+
if (listenerHash[id] === listener) {
41+
delete listenerHash[id];
42+
invoke(id, api, []);
43+
return true;
44+
}
45+
});
46+
};
47+
}
48+
49+
function invoke(id, method, params) {
50+
wnd.postMessage({
51+
jsonrpc: '2.0',
52+
id: id,
53+
method: method,
54+
params: params
55+
}, '*');
56+
}
57+
58+
wnd.addEventListener('message', function (event) {
59+
var msg = event.data;
60+
61+
if (msg.jsonrpc && !msg.method) {
62+
if (msg.error) {
63+
throw msg.error;
64+
} else if (msg.result) {
65+
if (callbackHash[msg.id]) {
66+
callbackHash[msg.id].apply(undef, msg.result);
67+
delete callbackHash[msg.id];
68+
} else if (listenerHash[msg.id]) {
69+
listenerHash[msg.id].apply(undef, msg.result);
70+
}
71+
}
72+
}
73+
}, false);
74+
75+
return {
76+
proxyRequest: proxyRequest,
77+
proxyAddListener: proxyAddListener,
78+
proxyRemoveListener: proxyRemoveListener
79+
};
80+
81+
}(window));

‎lib/chrome.serial.js

Lines changed: 5 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,12 @@
1-
chrome = chrome || {};
2-
3-
chrome.serial = chrome.serial || (function (window) {
1+
chrome.serial = chrome.serial || (function (_api) {
42

53
'use strict';
64

75
var slice = Array.prototype.slice,
8-
wnd = window.top,
96
undef = undefined,
10-
callbackHash = {},
11-
listenerHash = {},
12-
apiCalls = 0;
13-
14-
function proxyRequest(api) {
15-
return function () {
16-
var params = slice.call(arguments),
17-
id = ++apiCalls + '';
18-
19-
if (typeof params[params.length - 1] === 'function') {
20-
callbackHash[id] = params.pop();
21-
}
22-
invoke(id, api, params);
23-
};
24-
}
25-
26-
function proxyAddListener(api) {
27-
return function (listener) {
28-
var id = ++apiCalls + '';
29-
30-
if (typeof listener === 'function') {
31-
listenerHash[id] = listener;
32-
invoke(id, api, []);
33-
}
34-
};
35-
}
36-
37-
function proxyRemoveListener(api) {
38-
return function (listener) {
39-
Object.keys(listenerHash).some(function (id) {
40-
if (listenerHash[id] === listener) {
41-
delete listenerHash[id];
42-
invoke(id, api, []);
43-
}
44-
});
45-
};
46-
}
47-
48-
function invoke(id, method, params) {
49-
wnd.postMessage({
50-
jsonrpc: '2.0',
51-
id: id,
52-
method: method,
53-
params: params
54-
}, '*');
55-
}
56-
57-
wnd.addEventListener('message', function (event) {
58-
var msg = event.data;
59-
60-
if (msg.jsonrpc && !msg.method) {
61-
if (msg.error) {
62-
throw msg.error;
63-
} else if (msg.result) {
64-
if (callbackHash[msg.id]) {
65-
callbackHash[msg.id].apply(undef, msg.result);
66-
delete callbackHash[msg.id];
67-
} else if (listenerHash[msg.id]) {
68-
listenerHash[msg.id].apply(undef, msg.result);
69-
}
70-
}
71-
}
72-
}, false);
7+
proxyRequest = _api.proxyRequest,
8+
proxyAddListener = _api.proxyAddListener,
9+
proxyRemoveListener = _api.proxyRemoveListener;
7310

7411
return {
7512
getDevices: proxyRequest('chrome.serial.getDevices'),
@@ -96,4 +33,4 @@ chrome.serial = chrome.serial || (function (window) {
9633
}
9734
};
9835

99-
}(window));
36+
}(chrome._api));

0 commit comments

Comments
 (0)