@ray-js/lock-sdk 1.2.0-beta.18 → 1.2.0-beta.19
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/lib/constant.d.ts +0 -1
- package/lib/constant.js +0 -6
- package/lib/entries/main/bootstrap/bootstrap.js +1 -0
- package/lib/feature/log/log.js +1 -1
- package/lib/feature/misc/capability.js +9 -5
- package/lib/feature/state/init-state.js +7 -3
- package/lib/feature/state/remote-permissions.js +2 -1
- package/lib/feature/storage/swr-cache.d.ts +7 -0
- package/lib/feature/storage/swr-cache.js +54 -0
- package/lib/interface.d.ts +1 -0
- package/lib/runtime/lock-config.d.ts +1 -0
- package/lib/runtime/lock-config.js +1 -0
- package/lib/utils/constant.d.ts +1 -0
- package/lib/utils/constant.js +6 -0
- package/package.json +1 -1
package/lib/constant.d.ts
CHANGED
package/lib/constant.js
CHANGED
|
@@ -87,9 +87,3 @@ export var RemotePermissionType;
|
|
|
87
87
|
RemotePermissionType["ADMINS_ONLY"] = "adminsOnly";
|
|
88
88
|
RemotePermissionType["NO_ONE"] = "noOne";
|
|
89
89
|
})(RemotePermissionType || (RemotePermissionType = {}));
|
|
90
|
-
export const cameraPositionMap = {
|
|
91
|
-
0: 4,
|
|
92
|
-
1: 3,
|
|
93
|
-
2: 5,
|
|
94
|
-
3: 6,
|
|
95
|
-
};
|
|
@@ -47,6 +47,7 @@ export async function applyLockOptionsToRuntime(runtime, options) {
|
|
|
47
47
|
config.idsByCode = idsByCode;
|
|
48
48
|
config.supportBigData = !!dpSchema[dpCodes.synchMethodW];
|
|
49
49
|
config.preFetch = options?.preFetch ?? false;
|
|
50
|
+
config.localCache = options?.localCache ?? true;
|
|
50
51
|
config.notSupportWake = options?.notSupportWake ?? false;
|
|
51
52
|
config.communication = getCapabilities(devInfo.capability).map((item) => item.id);
|
|
52
53
|
sdkLog("info", "bootstrap", "applyOptions", {
|
package/lib/feature/log/log.js
CHANGED
|
@@ -6,7 +6,7 @@ import { LOGCHANGE } from "../../utils/constant";
|
|
|
6
6
|
import emitter from "../../utils/event";
|
|
7
7
|
import { handleLocalOperation } from "../../utils/log";
|
|
8
8
|
import { parseWeek } from "../../utils";
|
|
9
|
-
import { cameraPositionMap } from "../../constant";
|
|
9
|
+
import { cameraPositionMap } from "../../utils/constant";
|
|
10
10
|
export function getLogsRefreshEvent(runtime) {
|
|
11
11
|
return `${LOGCHANGE}_${runtime.devId}`;
|
|
12
12
|
}
|
|
@@ -2,6 +2,7 @@ import { getDeviceAdvancedAbility, getProductAbilities } from "../../api";
|
|
|
2
2
|
import { ProductCommunicationType } from "../../constant";
|
|
3
3
|
import { checkShowMatterMutilpleShare } from "../../platform/device";
|
|
4
4
|
import { ipc } from "../../platform";
|
|
5
|
+
import { getWithLocalCache } from "../storage/swr-cache";
|
|
5
6
|
export const hasCapabilityForRuntime = (runtime, capability) => runtime.config.communication.includes(capability);
|
|
6
7
|
export const isBlePureDeviceForRuntime = (rt) => hasCapabilityForRuntime(rt, ProductCommunicationType.BLUETOOTH) &&
|
|
7
8
|
rt.config.communication.length === 1;
|
|
@@ -60,15 +61,17 @@ export const getDeviceAdvancedAbilitiesForRuntime = async (rt) => {
|
|
|
60
61
|
if (rt.cache.deviceAdvanceAbilityCache) {
|
|
61
62
|
return rt.cache.deviceAdvanceAbilityCache;
|
|
62
63
|
}
|
|
63
|
-
const
|
|
64
|
-
|
|
64
|
+
const devId = rt.config.devInfo.devId;
|
|
65
|
+
const [productAbilitiesRes, deviceAdvancedAbilityRes, tttList] = await Promise.allSettled([
|
|
66
|
+
getWithLocalCache(rt, "productAbilities", () => getProductAbilities(devId, [
|
|
65
67
|
"tyabi4ucx5",
|
|
66
68
|
"tyabis9tpe",
|
|
67
69
|
"tyabiwxrn9",
|
|
68
70
|
"tyabipd4hh",
|
|
69
71
|
"tyabin4u5v",
|
|
70
72
|
"visual_door_lock_cloud_store",
|
|
71
|
-
]),
|
|
73
|
+
])),
|
|
74
|
+
getWithLocalCache(rt, "deviceAdvancedAbility", () => getDeviceAdvancedAbility(devId)),
|
|
72
75
|
ipc.getTTTAbilityKeyList("isSupportedVRMode"),
|
|
73
76
|
]);
|
|
74
77
|
const productAbilities = productAbilitiesRes.status === "fulfilled" ? productAbilitiesRes.value : [];
|
|
@@ -180,12 +183,13 @@ export const getDeviceAdvancedAbilitiesForRuntime = async (rt) => {
|
|
|
180
183
|
console.warn(`setting [${item.abilityCode}] config error`, error);
|
|
181
184
|
}
|
|
182
185
|
});
|
|
183
|
-
|
|
186
|
+
if (deviceAdvancedAbilityRes.status === "fulfilled") {
|
|
187
|
+
const res = deviceAdvancedAbilityRes.value;
|
|
184
188
|
deviceAdvanceAbilityCache.cloudStorage.support =
|
|
185
189
|
res.types?.includes(0) ?? false;
|
|
186
190
|
deviceAdvanceAbilityCache.wechatVoip.support =
|
|
187
191
|
res.types?.includes(1) ?? false;
|
|
188
|
-
}
|
|
192
|
+
}
|
|
189
193
|
rt.cache.deviceAdvanceAbilityCache = deviceAdvanceAbilityCache;
|
|
190
194
|
return deviceAdvanceAbilityCache;
|
|
191
195
|
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import dpCodes from "../../config/dp-code";
|
|
2
2
|
import { getDeviceSkill, getIPCConfig, getRotateInfo } from "../../api/video";
|
|
3
3
|
import { getDataWithPreFetchForRuntime } from "../utils";
|
|
4
|
+
import { getWithLocalCache } from "../storage/swr-cache";
|
|
4
5
|
import { getDeviceAdvancedAbilitiesForRuntime, } from "../misc/capability";
|
|
5
6
|
import { getCurrentUserForRuntime } from "../user/current-user";
|
|
6
7
|
import { checkOfflineDpUpdateForRuntime } from "../offline-dps";
|
|
@@ -45,19 +46,22 @@ export const initState = async (runtime) => {
|
|
|
45
46
|
fetchDeviceProperties(runtime),
|
|
46
47
|
getDeviceAdvancedAbilitiesForRuntime(runtime),
|
|
47
48
|
isSupportVideo
|
|
48
|
-
?
|
|
49
|
+
?
|
|
50
|
+
getDataWithPreFetchForRuntime(runtime, "getRotate", () => getWithLocalCache(runtime, "rotateInfo", () => getRotateInfo(devId), {
|
|
51
|
+
version: 1,
|
|
52
|
+
}))
|
|
49
53
|
: Promise.resolve({
|
|
50
54
|
angle: 0,
|
|
51
55
|
actualAngle: 0,
|
|
52
56
|
}),
|
|
53
57
|
isSupportVideo
|
|
54
|
-
? getDeviceSkill(devId)
|
|
58
|
+
? getWithLocalCache(runtime, "deviceSkill", () => getDeviceSkill(devId))
|
|
55
59
|
: Promise.resolve({
|
|
56
60
|
devId,
|
|
57
61
|
json: { videos: [{ width: 0, height: 0 }] },
|
|
58
62
|
}),
|
|
59
63
|
isSupportVideo
|
|
60
|
-
? getIPCConfig(devId)
|
|
64
|
+
? getWithLocalCache(runtime, "ipcConfig", () => getIPCConfig(devId))
|
|
61
65
|
: Promise.resolve({
|
|
62
66
|
audioAttributes: {
|
|
63
67
|
hardwareCapability: [],
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { getDeviceProperties } from "../../api/setting";
|
|
2
2
|
import { getDataWithPreFetchForRuntime } from "../utils";
|
|
3
|
-
import {
|
|
3
|
+
import { RemotePermissionType } from "../../constant";
|
|
4
|
+
import { cameraPositionMap } from "../../utils/constant";
|
|
4
5
|
export const permissions = [
|
|
5
6
|
RemotePermissionType.ADMINS_ONLY,
|
|
6
7
|
RemotePermissionType.EVERY_ONE,
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { LockDeviceRuntime } from "../../runtime/LockDeviceRuntime";
|
|
2
|
+
export interface SwrOptions<T> {
|
|
3
|
+
version?: number;
|
|
4
|
+
maxAge?: number;
|
|
5
|
+
applyToSession?: (rt: LockDeviceRuntime, fresh: T) => void;
|
|
6
|
+
}
|
|
7
|
+
export declare function getWithLocalCache<T>(rt: LockDeviceRuntime, key: string, fetcher: () => Promise<T>, options?: SwrOptions<T>): Promise<T>;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { getRuntimeByDevId } from "../../runtime/registry";
|
|
2
|
+
import { sdkLog } from "../../utils/sdk-log";
|
|
3
|
+
import { getStorageForRuntime, setStorageForRuntime } from "./index";
|
|
4
|
+
const CACHE_KEY_PREFIX = "swr_";
|
|
5
|
+
const buildKey = (key) => `${CACHE_KEY_PREFIX}${key}`;
|
|
6
|
+
function readValidCache(rt, storageKey, version, maxAge) {
|
|
7
|
+
const envelope = getStorageForRuntime(rt, storageKey);
|
|
8
|
+
if (!envelope ||
|
|
9
|
+
typeof envelope !== "object" ||
|
|
10
|
+
envelope.v !== version ||
|
|
11
|
+
!("data" in envelope)) {
|
|
12
|
+
return { hit: false };
|
|
13
|
+
}
|
|
14
|
+
if (typeof maxAge === "number" && maxAge >= 0 && Date.now() - envelope.t > maxAge) {
|
|
15
|
+
return { hit: false };
|
|
16
|
+
}
|
|
17
|
+
return { hit: true, data: envelope.data };
|
|
18
|
+
}
|
|
19
|
+
export async function getWithLocalCache(rt, key, fetcher, options) {
|
|
20
|
+
if (!rt.config.localCache) {
|
|
21
|
+
return fetcher();
|
|
22
|
+
}
|
|
23
|
+
const version = options?.version ?? 1;
|
|
24
|
+
const storageKey = buildKey(key);
|
|
25
|
+
const devId = rt.devId;
|
|
26
|
+
const writeCache = (data) => {
|
|
27
|
+
if (getRuntimeByDevId(devId) !== rt) {
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
const envelope = { v: version, t: Date.now(), data };
|
|
31
|
+
setStorageForRuntime(rt, storageKey, envelope);
|
|
32
|
+
};
|
|
33
|
+
const cached = readValidCache(rt, storageKey, version, options?.maxAge);
|
|
34
|
+
if (cached.hit) {
|
|
35
|
+
void (async () => {
|
|
36
|
+
try {
|
|
37
|
+
const fresh = await fetcher();
|
|
38
|
+
writeCache(fresh);
|
|
39
|
+
options?.applyToSession?.(rt, fresh);
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
sdkLog("warn", "swrCache", "revalidateFailed", {
|
|
43
|
+
devId,
|
|
44
|
+
data: { key },
|
|
45
|
+
error,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
})();
|
|
49
|
+
return cached.data;
|
|
50
|
+
}
|
|
51
|
+
const fresh = await fetcher();
|
|
52
|
+
writeCache(fresh);
|
|
53
|
+
return fresh;
|
|
54
|
+
}
|
package/lib/interface.d.ts
CHANGED
package/lib/utils/constant.d.ts
CHANGED
package/lib/utils/constant.js
CHANGED