dsh-xlock 0.2.8 → 0.2.10

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/README.md CHANGED
@@ -95,9 +95,11 @@ DSH 原生工具 `xlock` 同名(action: `status` / `audit` / `reset` / `resolv
95
95
 
96
96
  ## 安装
97
97
 
98
- profile 依赖 `github:jalousiex/dsh-xlock`,pnpm 从 GitHub tarball 装成真实目录,不依赖同步盘是否就位;`~/.dsh/dsh-plugins/dsh-xlock/` 为开发工作副本。随包 `cordis.patch.yml` 的 insert 自动并入。两台机器都启用。改包内容后需重启 DSH。
98
+ 已发布到 npm:`dsh-xlock`。profile 依赖写纯 semver 范围 `^0.2.8`(**不能写成 `dsh-xlock@^0.2.8` 以外的带 `:` 形态**——DSH 原生更新检测只认 registry 安装且 range 不含冒号),pnpm 从 registry 装成真实目录;`~/.dsh/dsh-plugins/dsh-xlock/` 为开发工作副本。随包 `cordis.patch.yml` 的 insert 自动并入。改包内容后需重启 DSH。
99
99
 
100
- **GitHub 源打包约束**:`package.json` 的 `files` 决定 tarball 内容,`config.json` / `machines.json` / `dsh-xlock.ps1` 必须列出,否则安装副本缺失、宿主静默退回内置默认值(`config.json` 的值恰与默认值相同,所以看不出异常)。
100
+ **为什么不再用 `github:` 源**:DSH 的 `updateRefusal()` 对 Git 安装只接受「钉到完整 commit 的 spec」,设置›插件页不给版本区、也不显示「可更新」;registry 安装才会 `pnpm view` 比对版本。发版流程:bump `package.json` 的 `version` → `pnpm publish --no-git-checks --access public` → 对机 `pnpm install`。
101
+
102
+ **打包约束**:`package.json` 的 `files` 决定 tarball 内容,`config.json` / `dsh-xlock.ps1` 必须列出,否则安装副本缺失、宿主静默退回内置默认值。`machines.json` **故意不进 `files`**(含本机主机名与工作区路径);首次运行由 `core/config.mjs` 的 `learnRegistry()` 在 `~/.dsh/dsh-plugins/dsh-xlock/machines.json` 自建。
101
103
 
102
104
  另一台机器一键安装(同步盘把源码目录带到对机后):
103
105
 
package/client.js CHANGED
@@ -1,307 +1,307 @@
1
- /**
2
- * dsh-xlock —— 浏览器半(会话行标记 + 原生菜单动作)
3
- *
4
- * 数据全部来自宿主半的只读路由:
5
- * GET /dsh-xlock/session-state 每会话的锁 / 归档 / 活跃度
6
- * GET /dsh-xlock/session-ids 全量会话 id(给平台列表做集合变化检测)
7
- * POST /dsh-xlock/session-state/reset {sessionId} 只删锁标记,归档不受影响
8
- *
9
- * 视觉语言(用户 2026-11-19 定稿):
10
- * 圆环 实线 + 呼吸 = 另一台机器**真在写**
11
- * 圆环 虚线 + 灰淡 = 只剩锁标记(残锁),超时也不隐藏——那正是要解锁的情形
12
- * 圆点 实线 = 创建归属来自创建记录;点线 = 由目录桶推断
13
- * 不再画「锁 / 归」文字徽标:锁与圆环虚线态同源、信息更少;归档态平台自己已经用
14
- * 行淡化 + 悬浮卡片表达了,再标一次就是重复。
15
- *
16
- * 加载器契约(DSH 硬规则):必须是
17
- * window.__ModuleLoader__.load({ id, factory: (require) => exports })
18
- * 旧 `{ id, exports: fn }` 形态会崩 web boot。factory 返回的对象必须声明
19
- * `inject`,否则平台不发放 slots 席位(ctx.slots 取不到 → 静默不注册)。
20
- */
21
-
22
- window.__ModuleLoader__.load({
23
- id: "dsh-xlock",
24
- factory: function (require) {
25
- "use strict";
26
- var React = require("react");
27
-
28
- var STATE = "/dsh-xlock/session-state";
29
- var RESET = "/dsh-xlock/session-state/reset";
30
- var IDS = "/dsh-xlock/session-ids";
31
- var REFRESH_MS = 15000;
32
- var CSS_TEXT = [
33
- '.dxl-ring{display:inline-block;width:11px;height:11px;border-radius:50%;box-sizing:border-box;cursor:default;vertical-align:middle}',
34
- '.dxl-ring[data-activity="writing"]{border:2px solid #f0883e;animation:dxl-pulse 1.6s ease-in-out infinite}',
35
- '.dxl-ring[data-activity="silent"]{border:2px dashed #8b949e;opacity:.6}',
36
- '.dxl-origin{display:inline-block;width:8px;height:8px;border-radius:50%;box-sizing:border-box;cursor:default;vertical-align:middle;margin-left:3px}',
37
- '.dxl-origin[data-source="record"]{border:1.5px solid #58a6ff}',
38
- '.dxl-origin[data-source="bucket"]{border:1.5px dotted #58a6ff;opacity:.75}',
39
- '@keyframes dxl-pulse{0%,100%{opacity:1}50%{opacity:.35}}',
40
- '@media (prefers-reduced-motion:reduce){.dxl-ring[data-activity="writing"]{animation:none}}',
41
- ].join("");
42
-
43
- // ── 状态容器 + 订阅(失败保留旧值标 stale,绝不因为一次拉取失败清空标记)──
44
- var state = { byId: {}, loaded: false, stale: false };
45
- var listeners = [];
46
-
47
- function notify() {
48
- for (var i = 0; i < listeners.length; i++) {
49
- try { listeners[i](state); } catch (e) {}
50
- }
51
- }
52
-
53
- function readDoc(doc) {
54
- var byId = {};
55
- var list = doc && Array.isArray(doc.sessions) ? doc.sessions : [];
56
- for (var i = 0; i < list.length; i++) {
57
- var item = list[i];
58
- if (!item || typeof item.sessionId !== "string") continue;
59
- byId[item.sessionId] = item;
60
- }
61
- return { byId: byId, loaded: true, stale: false };
62
- }
63
-
64
- function refresh() {
65
- return fetch(STATE, { headers: { accept: "application/json" } })
66
- .then(function (r) { return r.json(); })
67
- .then(function (doc) { state = readDoc(doc); notify(); return state; })
68
- .catch(function () {
69
- state = { byId: state.byId || {}, loaded: state.loaded, stale: true };
70
- notify();
71
- return state;
72
- });
73
- }
74
-
75
- function useXlock() {
76
- var pair = React.useState(state);
77
- var value = pair[0];
78
- var setValue = pair[1];
79
- React.useEffect(function () {
80
- function listener(next) { setValue(next); }
81
- listeners.push(listener);
82
- listener(state);
83
- return function () {
84
- var at = listeners.indexOf(listener);
85
- if (at >= 0) listeners.splice(at, 1);
86
- };
87
- }, []);
88
- return value;
89
- }
90
-
91
- function hostOf(item) {
92
- return item.host || (item.createdHost || "");
93
- }
94
-
95
- /** 活跃度三态文案(圆环 tooltip)。残锁也如实说明「多久没有新日志」。 */
96
- function ringLabel(item) {
97
- var host = hostOf(item) || "另一台机器";
98
- var act = (item && item.activity) || null;
99
- var writing = act && act.activity === "writing";
100
- var freshMs = act && typeof act.freshMs === "number" ? act.freshMs : null;
101
- if (writing) {
102
- return "另一台机器(" + host + ")正在运行这个会话";
103
- }
104
- if (freshMs !== null) {
105
- var sec = Math.max(0, Math.round(freshMs / 1000));
106
- var ago = sec < 90 ? sec + " 秒" : Math.round(sec / 60) + " 分钟";
107
- return "另一台机器(" + host + ")留下了锁标记,已停止写入 " + ago + ";可在会话菜单「解除跨机锁」";
108
- }
109
- return "另一台机器(" + host + ")留下了锁标记,没有可判读的会话日志;可在会话菜单「解除跨机锁」";
110
- }
111
-
112
- /**
113
- * 会话行左侧的活跃度圆环(`sidebar.session.row.leading`)。
114
- *
115
- * 只画 **held-foreign**:本机持有 / 已释放 / 无标记都是正常状态,画出来只是噪音。
116
- * 平台只在本行 primary status 为 idle 时才渲染这个槽(本机自己在跑的会话由平台
117
- * 自己画状态点),所以两者永不并排。
118
- *
119
- * 不设新鲜度截断:残锁(日志早停)也画,只是换成虚线灰淡 —— 那恰是最需要
120
- * 人看见并解锁的状态。
121
- */
122
- function RemoteRunningMark(props) {
123
- var data = useXlock();
124
- var sessionId = props && props.sessionId;
125
- var item = sessionId && data.byId ? data.byId[sessionId] : null;
126
- if (!item || item.lockState !== "held-foreign") return null;
127
- var writing = Boolean(item.activity && item.activity.activity === "writing");
128
- var label = ringLabel(item);
129
- return React.createElement("span", {
130
- className: "dxl-ring",
131
- role: "img",
132
- "aria-label": label,
133
- title: label,
134
- "data-dsh-xlock": hostOf(item) || "unknown",
135
- "data-activity": writing ? "writing" : "silent",
136
- });
137
- }
138
-
139
- /**
140
- * 会话行左侧的创建归属圆点(同一格,order 排在圆环之后)。
141
- *
142
- * 只画**异机创建**的行:本机建的是默认情况,画满一屏等于噪音;归属未知不画
143
- * (宁可没有,也不拿锁标记猜)。与圆环同格,所以对端正在跑时让路——
144
- * 同一格永远只有一个标记。
145
- */
146
- function SessionOriginMark(props) {
147
- var data = useXlock();
148
- var sessionId = props && props.sessionId;
149
- var item = sessionId && data.byId ? data.byId[sessionId] : null;
150
- if (!item) return null;
151
- if (item.lockState === "held-foreign") return null;
152
- if (item.createdRemote !== true) return null;
153
- var source = item.originSource;
154
- if (source !== "record" && source !== "bucket") return null;
155
- var host = item.createdHost || "另一台机器";
156
- var basis = source === "record" ? "创建记录" : "目录桶推断";
157
- var label = "这条会话是在 " + host + " 上创建的 · 依据" + basis;
158
- return React.createElement("span", {
159
- className: "dxl-origin",
160
- role: "img",
161
- "aria-label": label,
162
- title: label,
163
- "data-dsh-xlock": host,
164
- "data-source": source,
165
- });
166
- }
167
-
168
- /**
169
- * 会话「...」菜单 / 右键菜单里的「解除跨机锁」。
170
- * 走原生 `ctx.contextMenus`:字体、行高、图标位、键盘导航全部由平台渲染,
171
- * 与「重命名 / 固定 / 归档」等内置项完全一致(自绘 slot button 做不到)。
172
- * 只在 held-foreign 时产出动作。
173
- */
174
- function registerMenu(ctx) {
175
- return ctx.contextMenus.register({
176
- id: "xlock.unlock",
177
- target: "session",
178
- order: 120,
179
- observe: function () { return []; },
180
- resolve: function (target) {
181
- var sessionId = target && target.sessionId;
182
- var item = sessionId && state.byId ? state.byId[sessionId] : null;
183
- if (!item || item.lockState !== "held-foreign") return [];
184
- var host = hostOf(item) || "异机";
185
- return [{
186
- id: "xlock.unlock",
187
- label: "解除跨机锁(" + host + ")",
188
- danger: true,
189
- run: function () {
190
- return fetch(RESET, {
191
- method: "POST",
192
- headers: { "content-type": "application/json" },
193
- body: JSON.stringify({ sessionId: sessionId }),
194
- })
195
- .then(function () { return refresh(); })
196
- .catch(function () {});
197
- },
198
- }];
199
- },
200
- });
201
- }
202
-
203
- // ── 会话集合变化检测 ────────────────────────────────────────────────
204
- // 平台只在连接建立时重拉一次会话列表,之后靠事件增量维护。异机删掉一条会话
205
- // 时本机不会产生 session/disposed,那行会一直挂着。宿主半扫盘能看见「少了一条」,
206
- // 所以由它给出全量集合;客户端比对集合变了就让平台重拉权威基线(基线里已消失
207
- // 的身份会被移除)。宁可让平台重算,也不要在客户端自己拼列表。
208
- var sessionSet = { lastIds: "", refreshCount: 0, lastError: "" };
209
- var clientCtx = null;
210
-
211
- function checkSessionSet(ids) {
212
- var key = (Array.isArray(ids) ? ids.slice().sort() : []).join(",");
213
- if (key === sessionSet.lastIds) return false;
214
- sessionSet.lastIds = key;
215
- refreshPlatformSessions();
216
- return true;
217
- }
218
-
219
- function refreshPlatformSessions() {
220
- var sessions = null;
221
- try {
222
- sessions = clientCtx && typeof clientCtx.get === "function" ? clientCtx.get("sessions") : null;
223
- } catch (e) {
224
- sessions = null;
225
- }
226
- if (!sessions || typeof sessions.refresh !== "function") {
227
- sessionSet.lastError = "sessions service unavailable";
228
- return Promise.resolve(false);
229
- }
230
- return Promise.resolve(sessions.refresh()).then(function () {
231
- sessionSet.refreshCount = sessionSet.refreshCount + 1;
232
- sessionSet.lastError = "";
233
- return true;
234
- }).catch(function (error) {
235
- sessionSet.lastError = String((error && error.message) || error);
236
- return false;
237
- });
238
- }
239
-
240
- function pollSessionIds() {
241
- return fetch(IDS, { headers: { accept: "application/json" } })
242
- .then(function (r) { return r.json(); })
243
- .then(function (doc) { checkSessionSet(doc && doc.ids); })
244
- .catch(function () {});
245
- }
246
-
247
- var timer = null;
248
- var idTimer = null;
249
- function install() {
250
- var el = document.createElement("style");
251
- el.setAttribute("data-plugin-css", "dsh-xlock");
252
- el.textContent = CSS_TEXT;
253
- document.head.appendChild(el);
254
- void refresh();
255
- void pollSessionIds();
256
- timer = setInterval(function () { void refresh(); }, REFRESH_MS);
257
- idTimer = setInterval(function () { void pollSessionIds(); }, REFRESH_MS);
258
- return function () {
259
- if (timer) { clearInterval(timer); timer = null; }
260
- if (idTimer) { clearInterval(idTimer); idTimer = null; }
261
- if (el && el.parentNode) el.parentNode.removeChild(el);
262
- };
263
- }
264
-
265
- function apply(ctx) {
266
- clientCtx = ctx;
267
- ctx.effect(function () { return install(); }, "dsh-xlock: polling and styles");
268
- ctx.effect(function () { return registerMenu(ctx); }, "dsh-xlock: session menu action");
269
- ctx.slots.inject("sidebar.session.row.leading", function () {
270
- return ctx.slots.register({
271
- name: "sidebar.session.row.leading",
272
- id: "xlock-remote",
273
- order: 20,
274
- label: function () { return "异机运行中"; },
275
- }, RemoteRunningMark);
276
- });
277
- ctx.slots.inject("sidebar.session.row.leading", function () {
278
- return ctx.slots.register({
279
- name: "sidebar.session.row.leading",
280
- id: "xlock-origin",
281
- order: 30,
282
- label: function () { return "会话创建归属"; },
283
- }, SessionOriginMark);
284
- });
285
- }
286
-
287
- var exports = {
288
- inject: ["slots", "contextMenus"],
289
- STATE: STATE,
290
- RESET: RESET,
291
- IDS: IDS,
292
- apply: apply,
293
- readDoc: readDoc,
294
- refresh: refresh,
295
- pollSessionIds: pollSessionIds,
296
- __internals: {
297
- useXlock: useXlock,
298
- ringLabel: ringLabel,
299
- RemoteRunningMark: RemoteRunningMark,
300
- SessionOriginMark: SessionOriginMark,
301
- registerMenu: registerMenu,
302
- checkSessionSet: checkSessionSet,
303
- },
304
- };
305
- return exports;
306
- },
307
- });
1
+ /**
2
+ * dsh-xlock —— 浏览器半(会话行标记 + 原生菜单动作)
3
+ *
4
+ * 数据全部来自宿主半的只读路由:
5
+ * GET /dsh-xlock/session-state 每会话的锁 / 归档 / 活跃度
6
+ * GET /dsh-xlock/session-ids 全量会话 id(给平台列表做集合变化检测)
7
+ * POST /dsh-xlock/session-state/reset {sessionId} 只删锁标记,归档不受影响
8
+ *
9
+ * 视觉语言(用户 2026-11-19 定稿):
10
+ * 圆环 实线 + 呼吸 = 另一台机器**真在写**
11
+ * 圆环 虚线 + 灰淡 = 只剩锁标记(残锁),超时也不隐藏——那正是要解锁的情形
12
+ * 圆点 实线 = 创建归属来自创建记录;点线 = 由目录桶推断
13
+ * 不再画「锁 / 归」文字徽标:锁与圆环虚线态同源、信息更少;归档态平台自己已经用
14
+ * 行淡化 + 悬浮卡片表达了,再标一次就是重复。
15
+ *
16
+ * 加载器契约(DSH 硬规则):必须是
17
+ * window.__ModuleLoader__.load({ id, factory: (require) => exports })
18
+ * 旧 `{ id, exports: fn }` 形态会崩 web boot。factory 返回的对象必须声明
19
+ * `inject`,否则平台不发放 slots 席位(ctx.slots 取不到 → 静默不注册)。
20
+ */
21
+
22
+ window.__ModuleLoader__.load({
23
+ id: "dsh-xlock",
24
+ factory: function (require) {
25
+ "use strict";
26
+ var React = require("react");
27
+
28
+ var STATE = "/dsh-xlock/session-state";
29
+ var RESET = "/dsh-xlock/session-state/reset";
30
+ var IDS = "/dsh-xlock/session-ids";
31
+ var REFRESH_MS = 15000;
32
+ var CSS_TEXT = [
33
+ '.dxl-ring{display:inline-block;width:11px;height:11px;border-radius:50%;box-sizing:border-box;cursor:default;vertical-align:middle}',
34
+ '.dxl-ring[data-activity="writing"]{border:2px solid #f0883e;animation:dxl-pulse 1.6s ease-in-out infinite}',
35
+ '.dxl-ring[data-activity="silent"]{border:2px dashed #8b949e;opacity:.6}',
36
+ '.dxl-origin{display:inline-block;width:8px;height:8px;border-radius:50%;box-sizing:border-box;cursor:default;vertical-align:middle;margin-left:3px}',
37
+ '.dxl-origin[data-source="record"]{border:1.5px solid #58a6ff}',
38
+ '.dxl-origin[data-source="bucket"]{border:1.5px dotted #58a6ff;opacity:.75}',
39
+ '@keyframes dxl-pulse{0%,100%{opacity:1}50%{opacity:.35}}',
40
+ '@media (prefers-reduced-motion:reduce){.dxl-ring[data-activity="writing"]{animation:none}}',
41
+ ].join("");
42
+
43
+ // ── 状态容器 + 订阅(失败保留旧值标 stale,绝不因为一次拉取失败清空标记)──
44
+ var state = { byId: {}, loaded: false, stale: false };
45
+ var listeners = [];
46
+
47
+ function notify() {
48
+ for (var i = 0; i < listeners.length; i++) {
49
+ try { listeners[i](state); } catch (e) {}
50
+ }
51
+ }
52
+
53
+ function readDoc(doc) {
54
+ var byId = {};
55
+ var list = doc && Array.isArray(doc.sessions) ? doc.sessions : [];
56
+ for (var i = 0; i < list.length; i++) {
57
+ var item = list[i];
58
+ if (!item || typeof item.sessionId !== "string") continue;
59
+ byId[item.sessionId] = item;
60
+ }
61
+ return { byId: byId, loaded: true, stale: false };
62
+ }
63
+
64
+ function refresh() {
65
+ return fetch(STATE, { headers: { accept: "application/json" } })
66
+ .then(function (r) { return r.json(); })
67
+ .then(function (doc) { state = readDoc(doc); notify(); return state; })
68
+ .catch(function () {
69
+ state = { byId: state.byId || {}, loaded: state.loaded, stale: true };
70
+ notify();
71
+ return state;
72
+ });
73
+ }
74
+
75
+ function useXlock() {
76
+ var pair = React.useState(state);
77
+ var value = pair[0];
78
+ var setValue = pair[1];
79
+ React.useEffect(function () {
80
+ function listener(next) { setValue(next); }
81
+ listeners.push(listener);
82
+ listener(state);
83
+ return function () {
84
+ var at = listeners.indexOf(listener);
85
+ if (at >= 0) listeners.splice(at, 1);
86
+ };
87
+ }, []);
88
+ return value;
89
+ }
90
+
91
+ function hostOf(item) {
92
+ return item.host || (item.createdHost || "");
93
+ }
94
+
95
+ /** 活跃度三态文案(圆环 tooltip)。残锁也如实说明「多久没有新日志」。 */
96
+ function ringLabel(item) {
97
+ var host = hostOf(item) || "另一台机器";
98
+ var act = (item && item.activity) || null;
99
+ var writing = act && act.activity === "writing";
100
+ var freshMs = act && typeof act.freshMs === "number" ? act.freshMs : null;
101
+ if (writing) {
102
+ return "另一台机器(" + host + ")正在运行这个会话";
103
+ }
104
+ if (freshMs !== null) {
105
+ var sec = Math.max(0, Math.round(freshMs / 1000));
106
+ var ago = sec < 90 ? sec + " 秒" : Math.round(sec / 60) + " 分钟";
107
+ return "另一台机器(" + host + ")留下了锁标记,已停止写入 " + ago + ";可在会话菜单「解除跨机锁」";
108
+ }
109
+ return "另一台机器(" + host + ")留下了锁标记,没有可判读的会话日志;可在会话菜单「解除跨机锁」";
110
+ }
111
+
112
+ /**
113
+ * 会话行左侧的活跃度圆环(`sidebar.session.row.leading`)。
114
+ *
115
+ * 只画 **held-foreign**:本机持有 / 已释放 / 无标记都是正常状态,画出来只是噪音。
116
+ * 平台只在本行 primary status 为 idle 时才渲染这个槽(本机自己在跑的会话由平台
117
+ * 自己画状态点),所以两者永不并排。
118
+ *
119
+ * 不设新鲜度截断:残锁(日志早停)也画,只是换成虚线灰淡 —— 那恰是最需要
120
+ * 人看见并解锁的状态。
121
+ */
122
+ function RemoteRunningMark(props) {
123
+ var data = useXlock();
124
+ var sessionId = props && props.sessionId;
125
+ var item = sessionId && data.byId ? data.byId[sessionId] : null;
126
+ if (!item || item.lockState !== "held-foreign") return null;
127
+ var writing = Boolean(item.activity && item.activity.activity === "writing");
128
+ var label = ringLabel(item);
129
+ return React.createElement("span", {
130
+ className: "dxl-ring",
131
+ role: "img",
132
+ "aria-label": label,
133
+ title: label,
134
+ "data-dsh-xlock": hostOf(item) || "unknown",
135
+ "data-activity": writing ? "writing" : "silent",
136
+ });
137
+ }
138
+
139
+ /**
140
+ * 会话行左侧的创建归属圆点(同一格,order 排在圆环之后)。
141
+ *
142
+ * 只画**异机创建**的行:本机建的是默认情况,画满一屏等于噪音;归属未知不画
143
+ * (宁可没有,也不拿锁标记猜)。与圆环同格,所以对端正在跑时让路——
144
+ * 同一格永远只有一个标记。
145
+ */
146
+ function SessionOriginMark(props) {
147
+ var data = useXlock();
148
+ var sessionId = props && props.sessionId;
149
+ var item = sessionId && data.byId ? data.byId[sessionId] : null;
150
+ if (!item) return null;
151
+ if (item.lockState === "held-foreign") return null;
152
+ if (item.createdRemote !== true) return null;
153
+ var source = item.originSource;
154
+ if (source !== "record" && source !== "bucket") return null;
155
+ var host = item.createdHost || "另一台机器";
156
+ var basis = source === "record" ? "创建记录" : "目录桶推断";
157
+ var label = "这条会话是在 " + host + " 上创建的 · 依据" + basis;
158
+ return React.createElement("span", {
159
+ className: "dxl-origin",
160
+ role: "img",
161
+ "aria-label": label,
162
+ title: label,
163
+ "data-dsh-xlock": host,
164
+ "data-source": source,
165
+ });
166
+ }
167
+
168
+ /**
169
+ * 会话「...」菜单 / 右键菜单里的「解除跨机锁」。
170
+ * 走原生 `ctx.contextMenus`:字体、行高、图标位、键盘导航全部由平台渲染,
171
+ * 与「重命名 / 固定 / 归档」等内置项完全一致(自绘 slot button 做不到)。
172
+ * 只在 held-foreign 时产出动作。
173
+ */
174
+ function registerMenu(ctx) {
175
+ return ctx.contextMenus.register({
176
+ id: "xlock.unlock",
177
+ target: "session",
178
+ order: 120,
179
+ observe: function () { return []; },
180
+ resolve: function (target) {
181
+ var sessionId = target && target.sessionId;
182
+ var item = sessionId && state.byId ? state.byId[sessionId] : null;
183
+ if (!item || item.lockState !== "held-foreign") return [];
184
+ var host = hostOf(item) || "异机";
185
+ return [{
186
+ id: "xlock.unlock",
187
+ label: "解除跨机锁(" + host + ")",
188
+ danger: true,
189
+ run: function () {
190
+ return fetch(RESET, {
191
+ method: "POST",
192
+ headers: { "content-type": "application/json" },
193
+ body: JSON.stringify({ sessionId: sessionId }),
194
+ })
195
+ .then(function () { return refresh(); })
196
+ .catch(function () {});
197
+ },
198
+ }];
199
+ },
200
+ });
201
+ }
202
+
203
+ // ── 会话集合变化检测 ────────────────────────────────────────────────
204
+ // 平台只在连接建立时重拉一次会话列表,之后靠事件增量维护。异机删掉一条会话
205
+ // 时本机不会产生 session/disposed,那行会一直挂着。宿主半扫盘能看见「少了一条」,
206
+ // 所以由它给出全量集合;客户端比对集合变了就让平台重拉权威基线(基线里已消失
207
+ // 的身份会被移除)。宁可让平台重算,也不要在客户端自己拼列表。
208
+ var sessionSet = { lastIds: "", refreshCount: 0, lastError: "" };
209
+ var clientCtx = null;
210
+
211
+ function checkSessionSet(ids) {
212
+ var key = (Array.isArray(ids) ? ids.slice().sort() : []).join(",");
213
+ if (key === sessionSet.lastIds) return false;
214
+ sessionSet.lastIds = key;
215
+ refreshPlatformSessions();
216
+ return true;
217
+ }
218
+
219
+ function refreshPlatformSessions() {
220
+ var sessions = null;
221
+ try {
222
+ sessions = clientCtx && typeof clientCtx.get === "function" ? clientCtx.get("sessions") : null;
223
+ } catch (e) {
224
+ sessions = null;
225
+ }
226
+ if (!sessions || typeof sessions.refresh !== "function") {
227
+ sessionSet.lastError = "sessions service unavailable";
228
+ return Promise.resolve(false);
229
+ }
230
+ return Promise.resolve(sessions.refresh()).then(function () {
231
+ sessionSet.refreshCount = sessionSet.refreshCount + 1;
232
+ sessionSet.lastError = "";
233
+ return true;
234
+ }).catch(function (error) {
235
+ sessionSet.lastError = String((error && error.message) || error);
236
+ return false;
237
+ });
238
+ }
239
+
240
+ function pollSessionIds() {
241
+ return fetch(IDS, { headers: { accept: "application/json" } })
242
+ .then(function (r) { return r.json(); })
243
+ .then(function (doc) { checkSessionSet(doc && doc.ids); })
244
+ .catch(function () {});
245
+ }
246
+
247
+ var timer = null;
248
+ var idTimer = null;
249
+ function install() {
250
+ var el = document.createElement("style");
251
+ el.setAttribute("data-plugin-css", "dsh-xlock");
252
+ el.textContent = CSS_TEXT;
253
+ document.head.appendChild(el);
254
+ void refresh();
255
+ void pollSessionIds();
256
+ timer = setInterval(function () { void refresh(); }, REFRESH_MS);
257
+ idTimer = setInterval(function () { void pollSessionIds(); }, REFRESH_MS);
258
+ return function () {
259
+ if (timer) { clearInterval(timer); timer = null; }
260
+ if (idTimer) { clearInterval(idTimer); idTimer = null; }
261
+ if (el && el.parentNode) el.parentNode.removeChild(el);
262
+ };
263
+ }
264
+
265
+ function apply(ctx) {
266
+ clientCtx = ctx;
267
+ ctx.effect(function () { return install(); }, "dsh-xlock: polling and styles");
268
+ ctx.effect(function () { return registerMenu(ctx); }, "dsh-xlock: session menu action");
269
+ ctx.slots.inject("sidebar.session.row.leading", function () {
270
+ return ctx.slots.register({
271
+ name: "sidebar.session.row.leading",
272
+ id: "xlock-remote",
273
+ order: 20,
274
+ label: function () { return "异机运行中"; },
275
+ }, RemoteRunningMark);
276
+ });
277
+ ctx.slots.inject("sidebar.session.row.leading", function () {
278
+ return ctx.slots.register({
279
+ name: "sidebar.session.row.leading",
280
+ id: "xlock-origin",
281
+ order: 30,
282
+ label: function () { return "会话创建归属"; },
283
+ }, SessionOriginMark);
284
+ });
285
+ }
286
+
287
+ var exports = {
288
+ inject: ["slots", "contextMenus"],
289
+ STATE: STATE,
290
+ RESET: RESET,
291
+ IDS: IDS,
292
+ apply: apply,
293
+ readDoc: readDoc,
294
+ refresh: refresh,
295
+ pollSessionIds: pollSessionIds,
296
+ __internals: {
297
+ useXlock: useXlock,
298
+ ringLabel: ringLabel,
299
+ RemoteRunningMark: RemoteRunningMark,
300
+ SessionOriginMark: SessionOriginMark,
301
+ registerMenu: registerMenu,
302
+ checkSessionSet: checkSessionSet,
303
+ },
304
+ };
305
+ return exports;
306
+ },
307
+ });
package/cordis.patch.yml CHANGED
@@ -1,10 +1,10 @@
1
- # dsh-xlock — 跨机粘性会话锁 + 冲突副本治理
2
- #
3
- # 把本包插进 profile 的条目列表:cordis 加载 index.js(宿主半),
4
- # apply() 里 ctx.inject(['sessionPersistence']) 后对实例做 open/create
5
- # 包装(跨机锁标记 + 尾部修复 + 冲突副本门禁)。详见包内 README.md。
6
-
7
- - insert:
8
- - id: dsh-xlock
9
- name: 'dsh-xlock'
10
- config: {}
1
+ # dsh-xlock — 跨机粘性会话锁 + 冲突副本治理
2
+ #
3
+ # 把本包插进 profile 的条目列表:cordis 加载 index.js(宿主半),
4
+ # apply() 里 ctx.inject(['sessionPersistence']) 后对实例做 open/create
5
+ # 包装(跨机锁标记 + 尾部修复 + 冲突副本门禁)。详见包内 README.md。
6
+
7
+ - insert:
8
+ - id: dsh-xlock
9
+ name: 'dsh-xlock'
10
+ config: {}