@onekeyfe/hd-transport-electron 1.2.0-alpha.4 → 1.2.0-alpha.41
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.
- package/dist/ble-ops.d.ts +6 -0
- package/dist/ble-ops.d.ts.map +1 -1
- package/dist/{index-02fc07fe.js → index-5f728113.js} +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.js +1 -1
- package/dist/{noble-ble-handler-94c42077.js → noble-ble-handler-d58af248.js} +173 -177
- package/dist/noble-ble-handler.d.ts.map +1 -1
- package/package.json +5 -5
- package/src/__tests__/ble-ops.test.ts +21 -0
- package/src/ble-ops.ts +40 -10
- package/src/noble-ble-handler.ts +157 -210
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onekeyfe/hd-transport-electron",
|
|
3
|
-
"version": "1.2.0-alpha.
|
|
3
|
+
"version": "1.2.0-alpha.41",
|
|
4
4
|
"author": "OneKey",
|
|
5
5
|
"homepage": "https://github.com/OneKeyHQ/hardware-js-sdk#readme",
|
|
6
6
|
"license": "MIT",
|
|
@@ -25,9 +25,9 @@
|
|
|
25
25
|
"electron-log": ">=4.0.0"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@onekeyfe/hd-core": "1.2.0-alpha.
|
|
29
|
-
"@onekeyfe/hd-shared": "1.2.0-alpha.
|
|
30
|
-
"@onekeyfe/hd-transport": "1.2.0-alpha.
|
|
28
|
+
"@onekeyfe/hd-core": "1.2.0-alpha.41",
|
|
29
|
+
"@onekeyfe/hd-shared": "1.2.0-alpha.41",
|
|
30
|
+
"@onekeyfe/hd-transport": "1.2.0-alpha.41",
|
|
31
31
|
"@stoprocent/noble": "2.3.16",
|
|
32
32
|
"p-retry": "^4.6.2"
|
|
33
33
|
},
|
|
@@ -36,5 +36,5 @@
|
|
|
36
36
|
"electron": "^25.0.0",
|
|
37
37
|
"typescript": "^5.3.3"
|
|
38
38
|
},
|
|
39
|
-
"gitHead": "
|
|
39
|
+
"gitHead": "17fd02ad56766b4b576ea48503f92083f2101c29"
|
|
40
40
|
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { runBleCallbackOperation } from '../ble-ops';
|
|
2
|
+
|
|
3
|
+
describe('Electron Noble callback operations', () => {
|
|
4
|
+
test('settles when Noble never invokes the callback', async () => {
|
|
5
|
+
const result = await runBleCallbackOperation(() => undefined, {
|
|
6
|
+
timeoutMs: 5,
|
|
7
|
+
timeoutBehavior: 'resolve',
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
expect(result).toBeUndefined();
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
test('rejects callback errors before the timeout', async () => {
|
|
14
|
+
await expect(
|
|
15
|
+
runBleCallbackOperation(callback => callback(new Error('operation failed')), {
|
|
16
|
+
timeoutMs: 50,
|
|
17
|
+
timeoutBehavior: 'reject',
|
|
18
|
+
})
|
|
19
|
+
).rejects.toThrow('operation failed');
|
|
20
|
+
});
|
|
21
|
+
});
|
package/src/ble-ops.ts
CHANGED
|
@@ -1,6 +1,40 @@
|
|
|
1
1
|
import type { Characteristic } from '@stoprocent/noble';
|
|
2
2
|
import type { Logger } from './types/noble-extended';
|
|
3
3
|
|
|
4
|
+
type BleCallback = (error?: Error) => void;
|
|
5
|
+
|
|
6
|
+
export function runBleCallbackOperation(
|
|
7
|
+
operation: (callback: BleCallback) => void,
|
|
8
|
+
options: { timeoutMs: number; timeoutBehavior: 'resolve' | 'reject' }
|
|
9
|
+
): Promise<void> {
|
|
10
|
+
return new Promise<void>((resolve, reject) => {
|
|
11
|
+
let settled = false;
|
|
12
|
+
const settle = (error?: Error) => {
|
|
13
|
+
if (settled) return;
|
|
14
|
+
settled = true;
|
|
15
|
+
clearTimeout(timeout);
|
|
16
|
+
if (error) {
|
|
17
|
+
reject(error);
|
|
18
|
+
} else {
|
|
19
|
+
resolve();
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
const timeout = setTimeout(() => {
|
|
23
|
+
if (options.timeoutBehavior === 'reject') {
|
|
24
|
+
settle(new Error(`BLE operation timed out after ${options.timeoutMs}ms`));
|
|
25
|
+
} else {
|
|
26
|
+
settle();
|
|
27
|
+
}
|
|
28
|
+
}, options.timeoutMs);
|
|
29
|
+
|
|
30
|
+
try {
|
|
31
|
+
operation(settle);
|
|
32
|
+
} catch (error) {
|
|
33
|
+
settle(error instanceof Error ? error : new Error(String(error)));
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
4
38
|
export interface SoftRefreshParams {
|
|
5
39
|
deviceId: string;
|
|
6
40
|
notifyCharacteristic: Characteristic | null | undefined;
|
|
@@ -30,18 +64,14 @@ export async function softRefreshSubscription(params: SoftRefreshParams): Promis
|
|
|
30
64
|
|
|
31
65
|
subscriptionOperations.set(deviceId, 'subscribing');
|
|
32
66
|
|
|
33
|
-
await
|
|
34
|
-
|
|
67
|
+
await runBleCallbackOperation(callback => notifyCharacteristic.unsubscribe(callback), {
|
|
68
|
+
timeoutMs: 250,
|
|
69
|
+
timeoutBehavior: 'resolve',
|
|
35
70
|
});
|
|
36
71
|
|
|
37
|
-
await
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
reject(error);
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
resolve();
|
|
44
|
-
});
|
|
72
|
+
await runBleCallbackOperation(callback => notifyCharacteristic.subscribe(callback), {
|
|
73
|
+
timeoutMs: 8_000,
|
|
74
|
+
timeoutBehavior: 'reject',
|
|
45
75
|
});
|
|
46
76
|
|
|
47
77
|
notifyCharacteristic.removeAllListeners('data');
|