MCPcopy Create free account
hub / github.com/scriptscat/scriptcat / setValues

Method setValues

src/app/service/service_worker/value.ts:82–169  ·  view source on GitHub ↗
(params: TSetValuesParams)

Source from the content-addressed store, hash-verified

80
81 // 批量设置
82 async setValues(params: TSetValuesParams) {
83 const { uuid, keyValuePairs, isReplace } = params;
84 const id = params.id || "";
85 const ts = params.ts || 0;
86 const valueSender = params.valueSender || {
87 runFlag: "user",
88 tabId: -2,
89 };
90 // 查询出脚本
91 const script = await this.scriptDAO.get(uuid);
92 if (!script) {
93 throw new Error("script not found");
94 }
95 // 查询老的值
96 const storageName = getStorageName(script);
97 let oldValueRecord: ValueStore = {};
98 const cacheKey = `${CACHE_KEY_SET_VALUE}${storageName}`;
99 const entries = [] as ValueUpdateDataREntry[];
100 const _flag = await stackAsyncTask<boolean>(cacheKey, async () => {
101 let valueModel: Value | undefined = await this.valueDAO.get(storageName);
102 if (!valueModel) {
103 const now = Date.now();
104 const dataModel: ValueStore = {};
105 for (const [key, rTyped1] of keyValuePairs) {
106 const value = decodeRValue(rTyped1);
107 if (value !== undefined) {
108 dataModel[key] = value;
109 entries.push([key, rTyped1, R_UNDEFINED]);
110 }
111 }
112 // 即使是空 dataModel 也进行更新
113 // 由于没entries, valueUpdated 是 false, 但 valueDAO 会有一个空的 valueModel 记录 updatetime
114 valueModel = {
115 uuid: uuid,
116 storageName: storageName,
117 data: dataModel,
118 createtime: ts ? Math.min(ts, now) : now,
119 updatetime: ts ? Math.min(ts, now) : now,
120 };
121 } else {
122 let changed = false;
123 let dataModel = (oldValueRecord = valueModel.data);
124 dataModel = { ...dataModel }; // 每次储存使用新参考
125 const containedKeys = new Set<string>();
126 for (const [key, rTyped1] of keyValuePairs) {
127 containedKeys.add(key);
128 const value = decodeRValue(rTyped1);
129 const oldValue = dataModel[key];
130 if (oldValue === value) continue;
131 changed = true;
132 if (value === undefined) {
133 delete dataModel[key];
134 } else {
135 dataModel[key] = value;
136 }
137 const rTyped2 = encodeRValue(oldValue);
138 entries.push([key, rTyped1, rTyped2]);
139 }

Callers 4

setScriptValuesMethod · 0.95
value.test.tsFile · 0.80
GM_setValueMethod · 0.80
GM_setValuesMethod · 0.80

Calls 10

pushValueUpdateMethod · 0.95
getStorageNameFunction · 0.90
decodeRValueFunction · 0.90
encodeRValueFunction · 0.90
pushMethod · 0.80
addMethod · 0.80
keysMethod · 0.80
getMethod · 0.65
hasMethod · 0.65
saveMethod · 0.65

Tested by

no test coverage detected