@hylid/call 3.2.0-alpha.0 → 3.2.0-alpha.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,229 +0,0 @@
1
- var __assign = this && this.__assign || function () {
2
- __assign = Object.assign || function (t) {
3
- for (var s, i = 1, n = arguments.length; i < n; i++) {
4
- s = arguments[i];
5
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
6
- }
7
- return t;
8
- };
9
- return __assign.apply(this, arguments);
10
- };
11
- var __awaiter = this && this.__awaiter || function (thisArg, _arguments, P, generator) {
12
- function adopt(value) {
13
- return value instanceof P ? value : new P(function (resolve) {
14
- resolve(value);
15
- });
16
- }
17
- return new (P || (P = Promise))(function (resolve, reject) {
18
- function fulfilled(value) {
19
- try {
20
- step(generator.next(value));
21
- } catch (e) {
22
- reject(e);
23
- }
24
- }
25
- function rejected(value) {
26
- try {
27
- step(generator["throw"](value));
28
- } catch (e) {
29
- reject(e);
30
- }
31
- }
32
- function step(result) {
33
- result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
34
- }
35
- step((generator = generator.apply(thisArg, _arguments || [])).next());
36
- });
37
- };
38
- var __generator = this && this.__generator || function (thisArg, body) {
39
- var _ = {
40
- label: 0,
41
- sent: function sent() {
42
- if (t[0] & 1) throw t[1];
43
- return t[1];
44
- },
45
- trys: [],
46
- ops: []
47
- },
48
- f,
49
- y,
50
- t,
51
- g;
52
- return g = {
53
- next: verb(0),
54
- "throw": verb(1),
55
- "return": verb(2)
56
- }, typeof Symbol === "function" && (g[Symbol.iterator] = function () {
57
- return this;
58
- }), g;
59
- function verb(n) {
60
- return function (v) {
61
- return step([n, v]);
62
- };
63
- }
64
- function step(op) {
65
- if (f) throw new TypeError("Generator is already executing.");
66
- while (_) try {
67
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
68
- if (y = 0, t) op = [op[0] & 2, t.value];
69
- switch (op[0]) {
70
- case 0:
71
- case 1:
72
- t = op;
73
- break;
74
- case 4:
75
- _.label++;
76
- return {
77
- value: op[1],
78
- done: false
79
- };
80
- case 5:
81
- _.label++;
82
- y = op[1];
83
- op = [0];
84
- continue;
85
- case 7:
86
- op = _.ops.pop();
87
- _.trys.pop();
88
- continue;
89
- default:
90
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
91
- _ = 0;
92
- continue;
93
- }
94
- if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
95
- _.label = op[1];
96
- break;
97
- }
98
- if (op[0] === 6 && _.label < t[1]) {
99
- _.label = t[1];
100
- t = op;
101
- break;
102
- }
103
- if (t && _.label < t[2]) {
104
- _.label = t[2];
105
- _.ops.push(op);
106
- break;
107
- }
108
- if (t[2]) _.ops.pop();
109
- _.trys.pop();
110
- continue;
111
- }
112
- op = body.call(thisArg, _);
113
- } catch (e) {
114
- op = [6, e];
115
- y = 0;
116
- } finally {
117
- f = t = 0;
118
- }
119
- if (op[0] & 5) throw op[1];
120
- return {
121
- value: op[0] ? op[1] : void 0,
122
- done: true
123
- };
124
- }
125
- };
126
- var WebViewBridge = /** @class */function () {
127
- function WebViewBridge(ctx, config) {
128
- if (config === void 0) {
129
- config = {};
130
- }
131
- var _this = this;
132
- this.config = {};
133
- this.listen = function (data) {
134
- return __awaiter(_this, void 0, void 0, function () {
135
- var command, config, api, type, notAllowedMessage;
136
- var _this = this;
137
- return __generator(this, function (_a) {
138
- command = data.detail;
139
- if (command.source === 'hylid') {
140
- config = command.config, api = command.api;
141
- type = (config || {}).type;
142
- // 权限管控不允许调用 JSAPI
143
- if (!this.isAllowed(api)) {
144
- notAllowedMessage = __assign(__assign({}, command), {
145
- result: {
146
- _type_: 'fail',
147
- _data_: {
148
- errorCode: -2,
149
- errorMessage: 'api is not allowed'
150
- }
151
- }
152
- });
153
- this.webview.postMessage(notAllowedMessage);
154
- return [2 /*return*/];
155
- }
156
- if (type === 'callback') {
157
- // @ts-ignore
158
- my[api](function (result) {
159
- _this.webview.postMessage(__assign(__assign({}, command), {
160
- result: result
161
- }));
162
- });
163
- } else {
164
- this.handleApi(command).then(function (result) {
165
- _this.webview.postMessage(__assign(__assign({}, command), {
166
- result: result
167
- }));
168
- });
169
- }
170
- // 表示被 hylid-bridge 消费了
171
- return [2 /*return*/, true];
172
- }
173
- return [2 /*return*/];
174
- });
175
- });
176
- };
177
- this.handleApi = function (data) {
178
- var api = data.api,
179
- _options = data.options,
180
- config = data.config;
181
- var _a = (config || {}).type,
182
- type = _a === void 0 ? 'async' : _a;
183
- return new Promise(function (resolve) {
184
- var _a;
185
- var options = __assign(__assign({}, _options), {
186
- success: function success(res) {
187
- resolve({
188
- _type_: 'success',
189
- _data_: res
190
- });
191
- },
192
- fail: function fail(res) {
193
- resolve({
194
- _type_: 'fail',
195
- _data_: res
196
- });
197
- }
198
- });
199
- // @ts-ignore
200
- var mpApi = ((_a = _this.config.customApi) === null || _a === void 0 ? void 0 : _a[api]) || my[api];
201
- if (type === 'sync') {
202
- var data_1 = mpApi(_options);
203
- resolve({
204
- _type_: 'success',
205
- _data_: data_1
206
- });
207
- }
208
- if (type === 'async') {
209
- if (!mpApi) {
210
- // @ts-ignore
211
- my.call(api, options);
212
- } else {
213
- mpApi(options);
214
- }
215
- }
216
- });
217
- };
218
- this.webview = ctx;
219
- this.config = config;
220
- }
221
- WebViewBridge.prototype.isAllowed = function (api) {
222
- // 黑白名单校验
223
- if (this.config.whitelist && !this.config.whitelist.includes(api)) return false;
224
- if (this.config.blacklist && this.config.blacklist.includes(api)) return false;
225
- return true;
226
- };
227
- return WebViewBridge;
228
- }();
229
- export { WebViewBridge };