@doubao-apps/taro-runtime 0.0.29-rc.0 → 0.0.30

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.
@@ -3,6 +3,7 @@ export * from './device/index.js';
3
3
  export * from './device/index.js';
4
4
  export * from './media/index.js';
5
5
  export { request } from './request/index.js';
6
+ export { connectSocket, onSocketOpen, onSocketMessage, onSocketError, onSocketClose } from './request/websocket.js';
6
7
  export { exitMiniProgram, navigateBack, navigateTo, getCurrentPages, reLaunch, redirectTo } from './router/index.js';
7
8
  export { setStorage, setStorageSync, getStorage, getStorageSync, removeStorage, removeStorageSync, clearStorage, clearStorageSync, getStorageInfo, getStorageInfoSync } from './storage/index.js';
8
9
  export * from './system/index.js';
package/dist/api/index.js CHANGED
@@ -3,6 +3,7 @@ export * from './device/index.js';
3
3
  export * from './device/index.js';
4
4
  export * from './media/index.js';
5
5
  export { request } from './request/index.js';
6
+ export { connectSocket, onSocketOpen, onSocketMessage, onSocketError, onSocketClose } from './request/websocket.js';
6
7
  export { exitMiniProgram, navigateBack, navigateTo, getCurrentPages, reLaunch, redirectTo } from './router/index.js';
7
8
  export { setStorage, setStorageSync, getStorage, getStorageSync, removeStorage, removeStorageSync, clearStorage, clearStorageSync, getStorageInfo, getStorageInfoSync } from './storage/index.js';
8
9
  export * from './system/index.js';
@@ -0,0 +1,14 @@
1
+ import type Taro from '@tarojs/taro';
2
+ import type { connectSocket as taroConnectSocket } from '@tarojs/taro';
3
+ type TaroConnectSocketOption = Parameters<typeof taroConnectSocket>[0];
4
+ type SocketTask = Taro.SocketTask;
5
+ type SocketOpenCallback = Parameters<typeof Taro.onSocketOpen>[0];
6
+ type SocketErrorCallback = Parameters<typeof Taro.onSocketError>[0];
7
+ type SocketCloseCallback = Parameters<typeof Taro.onSocketClose>[0];
8
+ export declare function connectSocket(option: TaroConnectSocketOption): SocketTask;
9
+ export declare function onSocketOpen(callback: SocketOpenCallback): void;
10
+ export declare function onSocketMessage<T = any>(callback: Taro.onSocketMessage.Callback<T>): void;
11
+ export declare function onSocketError(callback: SocketErrorCallback): void;
12
+ export declare function onSocketClose(callback: SocketCloseCallback): void;
13
+ export {};
14
+ //# sourceMappingURL=websocket.d.ts.map
@@ -0,0 +1,135 @@
1
+ import * as appFrameworkApi from '@byted-doubao-apps/framework/api';
2
+ const globalOpenCallbacks = new Set();
3
+ const globalMessageCallbacks = new Set();
4
+ const globalErrorCallbacks = new Set();
5
+ const globalCloseCallbacks = new Set();
6
+ function normalizeHeader(header) {
7
+ if (!header || typeof header !== 'object') {
8
+ return undefined;
9
+ }
10
+ return Object.entries(header).reduce((result, [key, value]) => {
11
+ if (key) {
12
+ result[key] = typeof value === 'string' ? value : String(value);
13
+ }
14
+ return result;
15
+ }, {});
16
+ }
17
+ class RuntimeSocketTask {
18
+ constructor(appTask) {
19
+ this.appTask = appTask;
20
+ this.CONNECTING = this.appTask.CONNECTING;
21
+ this.OPEN = this.appTask.OPEN;
22
+ this.CLOSING = this.appTask.CLOSING;
23
+ this.CLOSED = this.appTask.CLOSED;
24
+ this.ws = undefined;
25
+ this.errMsg = 'connectSocket:ok';
26
+ this.socketTaskId = 0;
27
+ this.appTask.onOpen((event) => {
28
+ globalOpenCallbacks.forEach((callback) => callback({ header: event.header }));
29
+ });
30
+ this.appTask.onMessage((event) => {
31
+ globalMessageCallbacks.forEach((callback) => callback({ data: event.data }));
32
+ });
33
+ this.appTask.onError((event) => {
34
+ globalErrorCallbacks.forEach((callback) => callback({ errMsg: event.errMsg }));
35
+ });
36
+ this.appTask.onClose((event) => {
37
+ globalCloseCallbacks.forEach((callback) => callback({
38
+ code: typeof event.code === 'number' ? event.code : 1000,
39
+ reason: event.reason ?? event.errMsg ?? ''
40
+ }));
41
+ });
42
+ }
43
+ get readyState() {
44
+ return this.appTask.readyState ?? this.CLOSED;
45
+ }
46
+ send(option) {
47
+ if (this.appTask.readyState !== this.appTask.OPEN) {
48
+ const result = { errMsg: 'SocketTask.send:fail socketTask is not ready' };
49
+ option.fail?.(result);
50
+ option.complete?.(result);
51
+ return;
52
+ }
53
+ this.appTask.send({ data: option.data });
54
+ const result = { errMsg: 'SocketTask.send:ok' };
55
+ option.success?.(result);
56
+ option.complete?.(result);
57
+ }
58
+ close(option = {}) {
59
+ if (this.appTask.readyState === this.appTask.CLOSED) {
60
+ const result = { errMsg: 'SocketTask.close:fail socketTask is already closed' };
61
+ option.fail?.(result);
62
+ option.complete?.(result);
63
+ return;
64
+ }
65
+ this.appTask.close({ code: option.code, reason: option.reason });
66
+ const result = { errMsg: 'SocketTask.close:ok' };
67
+ option.success?.(result);
68
+ option.complete?.(result);
69
+ }
70
+ onOpen(callback) {
71
+ this.appTask.onOpen((event) => callback({ header: event.header }));
72
+ }
73
+ onClose(callback) {
74
+ this.appTask.onClose((event) => callback({
75
+ code: typeof event.code === 'number' ? event.code : 1000,
76
+ reason: event.reason ?? event.errMsg ?? ''
77
+ }));
78
+ }
79
+ onError(callback) {
80
+ this.appTask.onError((event) => callback({ errMsg: event.errMsg }));
81
+ }
82
+ onMessage(callback) {
83
+ this.appTask.onMessage((event) => callback({ data: event.data }));
84
+ }
85
+ }
86
+ export function connectSocket(option) {
87
+ const { url = '', header, protocols, success, fail, complete } = option ?? {};
88
+ const appTask = appFrameworkApi.connectSocket({
89
+ url,
90
+ header: normalizeHeader(header),
91
+ protocols: Array.isArray(protocols) ? protocols.filter(Boolean) : undefined
92
+ });
93
+ const task = new RuntimeSocketTask(appTask);
94
+ let settled = false;
95
+ appTask.onOpen(() => {
96
+ if (settled) {
97
+ return;
98
+ }
99
+ settled = true;
100
+ const result = { errMsg: 'connectSocket:ok' };
101
+ success?.(result);
102
+ complete?.(result);
103
+ });
104
+ appTask.onError((event) => {
105
+ if (settled) {
106
+ return;
107
+ }
108
+ settled = true;
109
+ const result = { errMsg: `connectSocket:fail ${event.errMsg}` };
110
+ fail?.(result);
111
+ complete?.(result);
112
+ });
113
+ return task;
114
+ }
115
+ export function onSocketOpen(callback) {
116
+ if (typeof callback === 'function') {
117
+ globalOpenCallbacks.add(callback);
118
+ }
119
+ }
120
+ export function onSocketMessage(callback) {
121
+ if (typeof callback === 'function') {
122
+ globalMessageCallbacks.add(callback);
123
+ }
124
+ }
125
+ export function onSocketError(callback) {
126
+ if (typeof callback === 'function') {
127
+ globalErrorCallbacks.add(callback);
128
+ }
129
+ }
130
+ export function onSocketClose(callback) {
131
+ if (typeof callback === 'function') {
132
+ globalCloseCallbacks.add(callback);
133
+ }
134
+ }
135
+ //# sourceMappingURL=websocket.js.map
@@ -1,2 +1,2 @@
1
- export type { RequestOption, RequestSuccessCallbackResult, RequestFailCallbackResult, RequestCompleteCallbackResult, RequestTask, RequestHeader, OnChunkReceivedCallback, OnHeadersReceivedCallback } from './request.js';
1
+ export type { RequestOption, RequestSuccessCallbackResult, RequestFailCallbackResult, RequestCompleteCallbackResult, RequestTask, RequestHeader, OnChunkReceivedCallback, OnHeadersReceivedCallback, ConnectSocketOption, SocketTask, SocketOpenCallback, SocketMessageCallback, SocketErrorCallback, SocketCloseCallback } from './request.js';
2
2
  //# sourceMappingURL=index.d.ts.map
@@ -10,5 +10,11 @@ export type RequestCompleteCallbackResult<T extends string | TaroGeneral.IAnyObj
10
10
  export type RequestTask<T = unknown> = Taro.RequestTask<T>;
11
11
  export type OnHeadersReceivedCallback = Parameters<Taro.RequestTask<any>['onHeadersReceived']>[0];
12
12
  export type OnChunkReceivedCallback = Parameters<Taro.RequestTask<any>['onChunkReceived']>[0];
13
+ export type ConnectSocketOption = Taro.connectSocket.Option;
14
+ export type SocketTask = Taro.SocketTask;
15
+ export type SocketOpenCallback = Taro.onSocketOpen.Callback;
16
+ export type SocketMessageCallback<T = any> = Taro.onSocketMessage.Callback<T>;
17
+ export type SocketErrorCallback = Taro.onSocketError.Callback;
18
+ export type SocketCloseCallback = Taro.onSocketClose.Callback;
13
19
  export {};
14
20
  //# sourceMappingURL=request.d.ts.map
@@ -1,4 +1,4 @@
1
- export type { RequestOption, RequestSuccessCallbackResult, RequestFailCallbackResult, RequestCompleteCallbackResult, RequestTask, RequestHeader, OnChunkReceivedCallback, OnHeadersReceivedCallback } from './api/request.js';
1
+ export type { RequestOption, RequestSuccessCallbackResult, RequestFailCallbackResult, RequestCompleteCallbackResult, RequestTask, RequestHeader, OnChunkReceivedCallback, OnHeadersReceivedCallback, ConnectSocketOption, SocketTask, SocketOpenCallback, SocketMessageCallback, SocketErrorCallback, SocketCloseCallback } from './api/request.js';
2
2
  export type { AppLifecycleRegistry, AppLifecycleType, LifecycleRegistry, PageLifecycleType, WidgetLifecycleType } from './internal/lifecycle-types.js';
3
3
  export type { Callback, LoadCallback, LoadOptions, PageNotFoundResult } from './hooks/hook-types.js';
4
4
  //# sourceMappingURL=index.d.ts.map
package/package.json CHANGED
@@ -37,7 +37,7 @@
37
37
  "access": "public",
38
38
  "registry": "https://registry.npmjs.org/"
39
39
  },
40
- "version": "0.0.29-rc.0",
40
+ "version": "0.0.30",
41
41
  "scripts": {
42
42
  "dev": "npm run build -- -w",
43
43
  "build": "rimraf dist bundle && tsc -p tsconfig.build.json",