@yagolive/event-kit 1.0.0

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.
@@ -0,0 +1,479 @@
1
+ /**
2
+ * YAGO Event Kit - Action Creator (Core Only)
3
+ * @version 1.0.0
4
+ *
5
+ * Generated by build script - do not edit manually.
6
+ * Supports: Browser script tag, CommonJS, AMD
7
+ */
8
+
9
+ (function (root, factory) {
10
+ if (typeof module === 'object' && module.exports) {
11
+ module.exports = factory();
12
+ } else if (typeof define === 'function' && define.amd) {
13
+ define([], factory);
14
+ } else {
15
+ root.__action_creator__ = factory();
16
+ }
17
+ })(typeof self !== 'undefined' ? self : typeof global !== 'undefined' ? global : this, function () {
18
+ 'use strict';
19
+
20
+ // src/core/types/Jump.js
21
+ var Jump = class {
22
+ constructor(path) {
23
+ this.text = null;
24
+ if (path == null) {
25
+ throw new Error("path cannot be null");
26
+ }
27
+ this.path = path;
28
+ }
29
+ getText() {
30
+ return this.text;
31
+ }
32
+ setText(text) {
33
+ this.text = text;
34
+ return this;
35
+ }
36
+ getPath() {
37
+ return this.path;
38
+ }
39
+ setPath(path) {
40
+ if (path == null) throw new Error("path cannot be null");
41
+ this.path = path;
42
+ return this;
43
+ }
44
+ toMap() {
45
+ const map = {};
46
+ if (this.text != null) map.text = this.text;
47
+ if (this.path != null) map.path = this.path;
48
+ return map;
49
+ }
50
+ };
51
+ var Jump_default = Jump;
52
+
53
+ // src/core/types/Navigate.js
54
+ var Navigate = class {
55
+ constructor(screen, params) {
56
+ this.text = null;
57
+ if (screen == null) {
58
+ throw new Error("screen cannot be null");
59
+ }
60
+ this.screen = screen;
61
+ this.params = params != null ? params : {};
62
+ }
63
+ getText() {
64
+ return this.text;
65
+ }
66
+ setText(text) {
67
+ this.text = text;
68
+ return this;
69
+ }
70
+ getScreen() {
71
+ return this.screen;
72
+ }
73
+ setScreen(screen) {
74
+ if (screen == null) throw new Error("screen cannot be null");
75
+ this.screen = screen;
76
+ return this;
77
+ }
78
+ getParams() {
79
+ return this.params;
80
+ }
81
+ setParams(params) {
82
+ this.params = params != null ? params : {};
83
+ return this;
84
+ }
85
+ toMap() {
86
+ const map = {};
87
+ if (this.text != null) map.text = this.text;
88
+ if (this.screen != null) map.screen = this.screen;
89
+ if (this.params != null && Object.keys(this.params).length > 0) map.params = this.params;
90
+ return map;
91
+ }
92
+ };
93
+ var Navigate_default = Navigate;
94
+
95
+ // src/core/types/Replace.js
96
+ var Replace = class extends Navigate_default {
97
+ constructor(screen, params) {
98
+ super(screen, params);
99
+ }
100
+ };
101
+ var Replace_default = Replace;
102
+
103
+ // src/core/types/EnterRoom.js
104
+ var OpenGiftPopupCallback = class {
105
+ constructor() {
106
+ this.id = null;
107
+ this.type = null;
108
+ this.count = null;
109
+ }
110
+ getId() {
111
+ return this.id;
112
+ }
113
+ setId(id) {
114
+ this.id = id;
115
+ return this;
116
+ }
117
+ getType() {
118
+ return this.type;
119
+ }
120
+ setType(type) {
121
+ this.type = type;
122
+ return this;
123
+ }
124
+ getCount() {
125
+ return this.count;
126
+ }
127
+ setCount(count) {
128
+ this.count = count;
129
+ return this;
130
+ }
131
+ toMap() {
132
+ const map = {};
133
+ if (this.id != null) map.id = this.id;
134
+ if (this.type != null) map.type = this.type;
135
+ if (this.count != null) map.count = this.count;
136
+ return map;
137
+ }
138
+ };
139
+ var OpenGameCallback = class {
140
+ constructor() {
141
+ this.id = null;
142
+ }
143
+ getId() {
144
+ return this.id;
145
+ }
146
+ setId(id) {
147
+ if (id == null) throw new Error("id cannot be null");
148
+ this.id = id;
149
+ return this;
150
+ }
151
+ toMap() {
152
+ const map = {};
153
+ if (this.id != null) map.id = this.id;
154
+ return map;
155
+ }
156
+ };
157
+ var OpenActivityCallback = class {
158
+ constructor() {
159
+ this.id = null;
160
+ }
161
+ getId() {
162
+ return this.id;
163
+ }
164
+ setId(id) {
165
+ if (id == null) throw new Error("id cannot be null");
166
+ this.id = id;
167
+ return this;
168
+ }
169
+ toMap() {
170
+ const map = {};
171
+ if (this.id != null) map.id = this.id;
172
+ return map;
173
+ }
174
+ };
175
+ var SwitchRoomTypeCallback = class {
176
+ constructor() {
177
+ this.type = null;
178
+ }
179
+ getType() {
180
+ return this.type;
181
+ }
182
+ setType(type) {
183
+ if (type == null) throw new Error("type cannot be null");
184
+ this.type = type;
185
+ return this;
186
+ }
187
+ toMap() {
188
+ const map = {};
189
+ if (this.type != null) map.type = this.type;
190
+ return map;
191
+ }
192
+ };
193
+ var CallbackType = {
194
+ OPEN_GIFT_POPUP: "open_gift_popup",
195
+ OPEN_GAME: "open_game",
196
+ OPEN_ACTIVITY: "open_activity",
197
+ SWITCH_ROOM_TYPE: "switch_room_type"
198
+ };
199
+ var EnterRoom = class {
200
+ constructor() {
201
+ this.text = null;
202
+ this.id = null;
203
+ this.match = null;
204
+ this.callbacks = null;
205
+ this.useReplace = null;
206
+ }
207
+ getText() {
208
+ return this.text;
209
+ }
210
+ setText(text) {
211
+ this.text = text;
212
+ return this;
213
+ }
214
+ getId() {
215
+ return this.id;
216
+ }
217
+ setId(id) {
218
+ this.id = id;
219
+ return this;
220
+ }
221
+ getMatch() {
222
+ return this.match;
223
+ }
224
+ setMatch(match) {
225
+ this.match = match;
226
+ return this;
227
+ }
228
+ getUseReplace() {
229
+ return this.useReplace;
230
+ }
231
+ setUseReplace(useReplace) {
232
+ this.useReplace = useReplace;
233
+ return this;
234
+ }
235
+ addOpenGiftPopupCallback(cb) {
236
+ if (!(cb instanceof OpenGiftPopupCallback)) throw new Error("callback must be an instance of OpenGiftPopupCallback");
237
+ if (this.callbacks == null) this.callbacks = {};
238
+ this.callbacks[CallbackType.OPEN_GIFT_POPUP] = cb;
239
+ return this;
240
+ }
241
+ addOpenGameCallback(cb) {
242
+ if (!(cb instanceof OpenGameCallback)) throw new Error("callback must be an instance of OpenGameCallback");
243
+ if (this.callbacks == null) this.callbacks = {};
244
+ this.callbacks[CallbackType.OPEN_GAME] = cb;
245
+ return this;
246
+ }
247
+ addOpenActivityCallback(cb) {
248
+ if (!(cb instanceof OpenActivityCallback)) throw new Error("callback must be an instance of OpenActivityCallback");
249
+ if (this.callbacks == null) this.callbacks = {};
250
+ this.callbacks[CallbackType.OPEN_ACTIVITY] = cb;
251
+ return this;
252
+ }
253
+ addSwitchRoomTypeCallback(cb) {
254
+ if (!(cb instanceof SwitchRoomTypeCallback)) throw new Error("callback must be an instance of SwitchRoomTypeCallback");
255
+ if (this.callbacks == null) this.callbacks = {};
256
+ this.callbacks[CallbackType.SWITCH_ROOM_TYPE] = cb;
257
+ return this;
258
+ }
259
+ toMap() {
260
+ const map = {};
261
+ if (this.text != null) map.text = this.text;
262
+ if (this.id != null) map.id = this.id;
263
+ if (this.match != null) map.match = this.match;
264
+ if (this.callbacks != null && Object.keys(this.callbacks).length > 0) {
265
+ const callbacksMap = {};
266
+ for (const [key, value] of Object.entries(this.callbacks)) {
267
+ callbacksMap[key] = value.toMap();
268
+ }
269
+ map.callbacks = callbacksMap;
270
+ }
271
+ if (this.useReplace != null) map.useReplace = this.useReplace;
272
+ return map;
273
+ }
274
+ };
275
+ EnterRoom.CallbackType = CallbackType;
276
+ EnterRoom.OpenGiftPopupCallback = OpenGiftPopupCallback;
277
+ EnterRoom.OpenGameCallback = OpenGameCallback;
278
+ EnterRoom.OpenActivityCallback = OpenActivityCallback;
279
+ EnterRoom.SwitchRoomTypeCallback = SwitchRoomTypeCallback;
280
+ var EnterRoom_default = EnterRoom;
281
+
282
+ // src/core/types/LiveDetect.js
283
+ var LiveDetect = class {
284
+ constructor() {
285
+ this.text = null;
286
+ }
287
+ getText() {
288
+ return this.text;
289
+ }
290
+ setText(text) {
291
+ this.text = text;
292
+ return this;
293
+ }
294
+ toMap() {
295
+ const map = {};
296
+ if (this.text != null) map.text = this.text;
297
+ return map;
298
+ }
299
+ };
300
+ var LiveDetect_default = LiveDetect;
301
+
302
+ // src/core/types/Onelink.js
303
+ var Onelink = class {
304
+ constructor(url) {
305
+ this.text = null;
306
+ this.url = url != null ? url : null;
307
+ this.share = null;
308
+ }
309
+ getText() {
310
+ return this.text;
311
+ }
312
+ setText(text) {
313
+ this.text = text;
314
+ return this;
315
+ }
316
+ getUrl() {
317
+ return this.url;
318
+ }
319
+ setUrl(url) {
320
+ if (url == null) throw new Error("url cannot be null");
321
+ this.url = url;
322
+ return this;
323
+ }
324
+ getShare() {
325
+ return this.share;
326
+ }
327
+ setShare(share) {
328
+ this.share = share;
329
+ return this;
330
+ }
331
+ toMap() {
332
+ const map = {};
333
+ if (this.text != null) map.text = this.text;
334
+ if (this.url != null) map.url = this.url;
335
+ if (this.share != null) map.share = this.share;
336
+ return map;
337
+ }
338
+ };
339
+ var Onelink_default = Onelink;
340
+
341
+ // src/core/types/Action.js
342
+ var Action = class {
343
+ constructor(type, data) {
344
+ this.type = type;
345
+ this.data = data;
346
+ this.messageId = null;
347
+ }
348
+ getType() {
349
+ return this.type;
350
+ }
351
+ setType(type) {
352
+ this.type = type;
353
+ return this;
354
+ }
355
+ getData() {
356
+ return this.data;
357
+ }
358
+ setData(data) {
359
+ this.data = data;
360
+ return this;
361
+ }
362
+ setBaseData(text) {
363
+ if (this.data != null && typeof this.data.setText === "function") {
364
+ this.data.setText(text);
365
+ }
366
+ return this;
367
+ }
368
+ toJson() {
369
+ return JSON.stringify({
370
+ type: this.type,
371
+ data: this.data != null ? this.data.toMap() : {}
372
+ });
373
+ }
374
+ };
375
+ var Action_default = Action;
376
+
377
+ // src/core/ActionCreator.js
378
+ var ActionType = {
379
+ JUMP: "jump",
380
+ NAVIGATE: "navigate",
381
+ REPLACE: "replace",
382
+ ENTER_ROOM: "enter_room",
383
+ LIVE_DETECT: "live_detect",
384
+ ONELINK: "onelink"
385
+ };
386
+ var ActionData = {
387
+ Jump: Jump_default,
388
+ Navigate: Navigate_default,
389
+ Replace: Replace_default,
390
+ EnterRoom: EnterRoom_default,
391
+ LiveDetect: LiveDetect_default,
392
+ Onelink: Onelink_default
393
+ };
394
+ var ActionCreator = {
395
+ ActionData,
396
+ ActionType,
397
+ // Jump
398
+ buildJump(path) {
399
+ return new Jump_default(path);
400
+ },
401
+ createJumpAction(path) {
402
+ return new Action_default(ActionType.JUMP, new Jump_default(path));
403
+ },
404
+ createJumpActionWithData(jump) {
405
+ return new Action_default(ActionType.JUMP, jump);
406
+ },
407
+ // Navigate
408
+ buildNavigate(screen, params) {
409
+ return new Navigate_default(screen, params);
410
+ },
411
+ createNavigateAction(screen) {
412
+ return new Action_default(ActionType.NAVIGATE, new Navigate_default(screen));
413
+ },
414
+ createNavigateActionWithParams(screen, params) {
415
+ return new Action_default(ActionType.NAVIGATE, new Navigate_default(screen, params));
416
+ },
417
+ createNavigateActionWithData(navigate) {
418
+ return new Action_default(ActionType.NAVIGATE, navigate);
419
+ },
420
+ // Replace
421
+ buildReplace(screen, params) {
422
+ return new Replace_default(screen, params);
423
+ },
424
+ createReplaceAction(screen) {
425
+ return new Action_default(ActionType.REPLACE, new Replace_default(screen));
426
+ },
427
+ createReplaceActionWithParams(screen, params) {
428
+ return new Action_default(ActionType.REPLACE, new Replace_default(screen, params));
429
+ },
430
+ createReplaceActionWithData(replace) {
431
+ return new Action_default(ActionType.REPLACE, replace);
432
+ },
433
+ // EnterRoom
434
+ buildEnterRoom() {
435
+ return new EnterRoom_default();
436
+ },
437
+ createEnterRoomAction(id) {
438
+ var enterRoom = new EnterRoom_default();
439
+ if (id != null) enterRoom.setId(id);
440
+ return new Action_default(ActionType.ENTER_ROOM, enterRoom);
441
+ },
442
+ createEnterRoomActionWithData(enterRoom) {
443
+ return new Action_default(ActionType.ENTER_ROOM, enterRoom);
444
+ },
445
+ // LiveDetect
446
+ buildLiveDetect() {
447
+ return new LiveDetect_default();
448
+ },
449
+ createLiveDetectAction() {
450
+ return new Action_default(ActionType.LIVE_DETECT, new LiveDetect_default());
451
+ },
452
+ createLiveDetectActionWithData(liveDetect) {
453
+ return new Action_default(ActionType.LIVE_DETECT, liveDetect);
454
+ },
455
+ // Onelink
456
+ buildOnelink(url) {
457
+ return new Onelink_default(url);
458
+ },
459
+ createOnelinkAction(url) {
460
+ return new Action_default(ActionType.ONELINK, new Onelink_default(url));
461
+ },
462
+ createOnelinkActionWithData(onelink) {
463
+ return new Action_default(ActionType.ONELINK, onelink);
464
+ }
465
+ };
466
+
467
+ return {
468
+ ActionCreator: ActionCreator,
469
+ ActionData: ActionData,
470
+ ActionType: ActionType,
471
+ Jump: Jump,
472
+ Navigate: Navigate,
473
+ Replace: Replace,
474
+ EnterRoom: EnterRoom,
475
+ LiveDetect: LiveDetect,
476
+ Onelink: Onelink,
477
+ Action: Action
478
+ };
479
+ });
package/package.json ADDED
@@ -0,0 +1,84 @@
1
+ {
2
+ "name": "@yagolive/event-kit",
3
+ "version": "1.0.0",
4
+ "description": "YAGO Event Kit - 跨环境 Action 创建与通信工具库,支持浏览器、React、Vue",
5
+ "main": "src/index.js",
6
+ "browser": "build/js/action_bridge.umd.js",
7
+ "types": "src/index.d.ts",
8
+ "files": [
9
+ "src/",
10
+ "build/",
11
+ "CHANGELOG.md"
12
+ ],
13
+ "exports": {
14
+ ".": {
15
+ "types": "./src/index.d.ts",
16
+ "browser": "./build/js/action_bridge.umd.js",
17
+ "import": "./src/index.js",
18
+ "require": "./src/index.js",
19
+ "default": "./src/index.js"
20
+ },
21
+ "./core": {
22
+ "types": "./src/core/index.d.ts",
23
+ "browser": "./build/js/action_creator.umd.js",
24
+ "import": "./src/core/index.js",
25
+ "require": "./src/core/index.js",
26
+ "default": "./src/core/index.js"
27
+ },
28
+ "./react": {
29
+ "types": "./src/integrations/react/index.d.ts",
30
+ "import": "./src/integrations/react/index.js",
31
+ "require": "./src/integrations/react/index.js",
32
+ "default": "./src/integrations/react/index.js"
33
+ },
34
+ "./vue": {
35
+ "types": "./src/integrations/vue/index.d.ts",
36
+ "import": "./src/integrations/vue/index.js",
37
+ "require": "./src/integrations/vue/index.js",
38
+ "default": "./src/integrations/vue/index.js"
39
+ },
40
+ "./bridge": {
41
+ "types": "./src/bridge/index.d.ts",
42
+ "import": "./src/bridge/index.js",
43
+ "require": "./src/bridge/index.js",
44
+ "default": "./src/bridge/index.js"
45
+ },
46
+ "./adapters": {
47
+ "types": "./src/adapters/index.d.ts",
48
+ "import": "./src/adapters/index.js",
49
+ "require": "./src/adapters/index.js",
50
+ "default": "./src/adapters/index.js"
51
+ }
52
+ },
53
+ "keywords": [
54
+ "yago",
55
+ "event-kit",
56
+ "action-creator",
57
+ "action-bridge",
58
+ "react-native-webview",
59
+ "react",
60
+ "vue"
61
+ ],
62
+ "peerDependencies": {
63
+ "react": ">=16.8.0",
64
+ "vue": ">=3.0.0"
65
+ },
66
+ "peerDependenciesMeta": {
67
+ "react": {
68
+ "optional": true
69
+ },
70
+ "vue": {
71
+ "optional": true
72
+ }
73
+ },
74
+ "scripts": {
75
+ "build": "node scripts/build.js",
76
+ "build:core": "node scripts/build.js --core",
77
+ "build:bridge": "node scripts/build.js --bridge",
78
+ "build:watch": "node scripts/build.js --watch"
79
+ },
80
+ "devDependencies": {
81
+ "esbuild": "^0.25.0"
82
+ },
83
+ "license": "UNLICENSED"
84
+ }
@@ -0,0 +1,44 @@
1
+ export default class MockAdapter {
2
+ constructor(options) {
3
+ options = options || {};
4
+ this._mockResults = {};
5
+ this._calls = [];
6
+ this._delay = options.delay || 0;
7
+ }
8
+
9
+ mockResult(actionType, result) { this._mockResults[actionType] = result; return this; }
10
+ mockError(actionType, error) { this._mockResults[actionType] = { __error: error }; return this; }
11
+ getCalls() { return this._calls.slice(); }
12
+ clearCalls() { this._calls = []; return this; }
13
+ clearMocks() { this._mockResults = {}; return this; }
14
+
15
+ init() {}
16
+ destroy() { this._calls = []; this._mockResults = {}; }
17
+
18
+ sendAction(action) {
19
+ var self = this;
20
+ this._calls.push({
21
+ type: action.type,
22
+ data: action.data != null ? action.data.toMap() : {},
23
+ messageId: action.messageId,
24
+ timestamp: Date.now(),
25
+ });
26
+ return new Promise(function (resolve, reject) {
27
+ var mockResult = self._mockResults[action.type];
28
+ setTimeout(function () {
29
+ if (mockResult && mockResult.__error) reject(mockResult.__error);
30
+ else if (mockResult !== undefined) resolve(mockResult);
31
+ else resolve({ success: true });
32
+ }, self._delay);
33
+ });
34
+ }
35
+
36
+ sendActionNoReply(action) {
37
+ this._calls.push({
38
+ type: action.type,
39
+ data: action.data != null ? action.data.toMap() : {},
40
+ messageId: action.messageId,
41
+ timestamp: Date.now(),
42
+ });
43
+ }
44
+ }
@@ -0,0 +1,76 @@
1
+ export default class RNWebViewAdapter {
2
+ constructor(options) {
3
+ options = options || {};
4
+ this._callbacks = {};
5
+ this._messageListener = null;
6
+ this._timeout = options.timeout || 30000;
7
+ }
8
+
9
+ init() {
10
+ if (typeof window === 'undefined') {
11
+ throw new Error('RNWebViewAdapter requires a browser environment');
12
+ }
13
+ this._messageListener = this._handleMessage.bind(this);
14
+
15
+ if (window.ReactNativeOS === 'android') {
16
+ // https://github.com/react-native-webview/react-native-webview/issues/3776
17
+ document.addEventListener('message', this._messageListener);
18
+ } else {
19
+ window.addEventListener('message', this._messageListener);
20
+ }
21
+ }
22
+
23
+ destroy() {
24
+ if (this._messageListener) {
25
+ window.removeEventListener('message', this._messageListener);
26
+ document.removeEventListener('message', this._messageListener);
27
+ this._messageListener = null;
28
+ }
29
+ this._callbacks = {};
30
+ }
31
+
32
+ sendAction(action) {
33
+ var self = this;
34
+ var messageId = 'msg_' + Date.now() + '_' + Math.random().toString(36).substr(2, 9);
35
+ action.messageId = messageId;
36
+ return new Promise(function (resolve, reject) {
37
+ var timer = setTimeout(function () {
38
+ delete self._callbacks[messageId];
39
+ reject(new Error('[ActionBridge] Timeout: no response for action ' + action.type));
40
+ }, self._timeout);
41
+ self._callbacks[messageId] = function (result, error) {
42
+ clearTimeout(timer);
43
+ delete self._callbacks[messageId];
44
+ if (error) reject(error);
45
+ else resolve(result);
46
+ };
47
+ self._postMessage(action);
48
+ });
49
+ }
50
+
51
+ sendActionNoReply(action) {
52
+ this._postMessage(action);
53
+ }
54
+
55
+ _postMessage(action) {
56
+ if (typeof window !== 'undefined' && window.ReactNativeWebView) {
57
+ window.ReactNativeWebView.postMessage(
58
+ JSON.stringify({
59
+ type: action.type,
60
+ data: action.data != null ? action.data.toMap() : {},
61
+ messageId: action.messageId,
62
+ }),
63
+ );
64
+ }
65
+ }
66
+
67
+ _handleMessage(event) {
68
+ try {
69
+ var data = typeof event.data === 'string' ? JSON.parse(event.data) : event.data;
70
+ var callback = this._callbacks[data.messageId];
71
+ if (callback) callback(data.result, data.error);
72
+ } catch (e) {
73
+ console.error('[RNWebViewAdapter] Parse error:', e);
74
+ }
75
+ }
76
+ }