@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 CHANGED
@@ -76,4 +76,3 @@ export declare enum RemotePermissionType {
76
76
  ADMINS_ONLY = "adminsOnly",
77
77
  NO_ONE = "noOne"
78
78
  }
79
- export declare const cameraPositionMap: Record<number, number>;
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", {
@@ -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 [productAbilitiesRes, tttList] = await Promise.allSettled([
64
- getProductAbilities(rt.config.devInfo.devId, [
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
- getDeviceAdvancedAbility(rt.config.devInfo.devId).then((res) => {
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
- ? getDataWithPreFetchForRuntime(runtime, "getRotate", () => getRotateInfo(devId))
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 { cameraPositionMap, RemotePermissionType } from "../../constant";
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
+ }
@@ -12,6 +12,7 @@ export interface LockSDKOption {
12
12
  supportMultipleFace?: boolean;
13
13
  supportOfflineDps?: boolean;
14
14
  preFetch?: boolean;
15
+ localCache?: boolean;
15
16
  notSupportWake?: boolean;
16
17
  logLevel?: SDKLogLevel;
17
18
  logger?: SDKLogSink;
@@ -21,6 +21,7 @@ export type LockConfigState = {
21
21
  supportMultipleFace: boolean;
22
22
  supportOfflineDps: boolean;
23
23
  preFetch: boolean;
24
+ localCache: boolean;
24
25
  supportTalk: boolean;
25
26
  isTwoWayTalk: boolean;
26
27
  cameraConfig: CameraConfig[];
@@ -31,6 +31,7 @@ export function createLockConfigState() {
31
31
  supportMultipleFace: false,
32
32
  supportOfflineDps: false,
33
33
  preFetch: false,
34
+ localCache: true,
34
35
  supportTalk: false,
35
36
  isTwoWayTalk: false,
36
37
  cameraConfig: [],
@@ -12,3 +12,4 @@ export declare enum LoopTypes {
12
12
  WEEK = 2,
13
13
  MONTH = 3
14
14
  }
15
+ export declare const cameraPositionMap: Record<number, number>;
@@ -14,3 +14,9 @@ export var LoopTypes;
14
14
  LoopTypes[LoopTypes["WEEK"] = 2] = "WEEK";
15
15
  LoopTypes[LoopTypes["MONTH"] = 3] = "MONTH";
16
16
  })(LoopTypes || (LoopTypes = {}));
17
+ export const cameraPositionMap = {
18
+ 0: 4,
19
+ 1: 3,
20
+ 2: 5,
21
+ 3: 6,
22
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ray-js/lock-sdk",
3
- "version": "1.2.0-beta.18",
3
+ "version": "1.2.0-beta.19",
4
4
  "files": [
5
5
  "lib",
6
6
  "LICENSE.md"