dsh-mobile-plugin 0.5.58 → 0.5.59

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.
Files changed (2) hide show
  1. package/lib/client.js +146 -38
  2. package/package.json +1 -1
package/lib/client.js CHANGED
@@ -4,8 +4,9 @@
4
4
  *
5
5
  * 五件事:桌面侧边栏的展开态与「读过了」报给宿主;手机端的已读回执应用回桌面绿点;
6
6
  * 设置里那一项「移动端」(一张薄卡,一颗「打开配置页面」);在侧边栏最底下那一行挂一颗
7
- * 手机图标(点一下等于那张卡上的按钮);聊天嵌入页里按注入的目标打开会话。方向是单向的
8
- * (桌面 → 宿主 → 手机),手机上的折叠只存在 App 本地,不写回。
7
+ * 手机图标(点一下等于那张卡上的按钮);聊天嵌入页里按目标打开会话、守住它不被网页端启动时
8
+ * 那次「恢复上次会话」顶掉,换目标则在页面里切过去、不重新加载。方向是单向的(桌面 → 宿主 →
9
+ * 手机),手机上的折叠只存在 App 本地,不写回。
9
10
  *
10
11
  * 本文件不经任何构建(package.json 直接收录 lib 下的 .js),所以不能写 JSX,统一用
11
12
  * `React.createElement` 的别名 `h`;客户端插件按约定写成惰性工厂,定时器与页签在物化时才起。
@@ -2116,26 +2117,80 @@ window.__ModuleLoader__.load({
2116
2117
  const CHAT_EMBED_KIND = 'dsh-chat-embed';
2117
2118
  /** 等清单出现目标的最长时间:九秒够页面把工作区与会话清单拉起来。 */
2118
2119
  const CHAT_EMBED_WAIT_MS = 9000;
2120
+ /**
2121
+ * 打开之后再盯住主视图的时长与节拍。
2122
+ *
2123
+ * 网页端启动时会把「上次那条会话」摆回主视图(选择记在本地,页面每加载一次就恢复一次),
2124
+ * 页面装载的那几趟(第一次打开、换电脑、点重试)都会跑到它。打开只是把主视图指过去,
2125
+ * 不等于停在那儿:窗口内每一拍复核一次,被抢回去就再指一次。窗口走完停在哪就是哪,按
2126
+ * 那一刻回报。
2127
+ */
2128
+ const CHAT_EMBED_HOLD_TICKS = 10;
2129
+ const CHAT_EMBED_HOLD_STEP_MS = 150;
2119
2130
  function openEmbeddedSession(ctx) {
2120
2131
  const target = globalThis.__DSH_CHAT_EMBED_TARGET__;
2132
+ if (!ctx || typeof ctx.inject !== 'function') return;
2121
2133
  const sessionId = target && typeof target.sessionId === 'string' ? target.sessionId : '';
2122
- if (!sessionId || !ctx || typeof ctx.inject !== 'function') return;
2123
2134
  // 本插件是 immediately 那一档,apply 时 ctx.get 还看不见宿主稍后才提供的 sessions
2124
2135
  // (绿点两条同步当初就是被这点坑过,才统一改走 ctx.inject)。liveshare 的 viewer
2125
2136
  // 插件同样把 sessions 与 uiWorkspace 列进 inject 才读清单、打开会话。这里等两者
2126
2137
  // 就位,拿到的是带服务的子 ctx;没就位就一直不回调,安静降级,不回报也不抛。
2127
2138
  ctx.inject(['sessions', 'uiWorkspace'], (scope) => {
2128
- armEmbeddedSession(scope, sessionId);
2139
+ scope.effect(() => {
2140
+ // 入口一直挂着,装载时目标为空也照挂:App 切会话走这个入口进来,会话还没建出来的
2141
+ // 新会话页切到网页模式时,也靠它稍后补一个目标。
2142
+ installEmbedOpener(scope);
2143
+ if (sessionId) openEmbeddedTarget(scope, sessionId);
2144
+ return () => {
2145
+ disposeEmbedTask();
2146
+ const win = typeof window !== 'undefined' ? window : null;
2147
+ if (win) {
2148
+ try { delete win.__DSH_CHAT_EMBED_OPEN__; } catch { /* 删不掉不影响会话本身 */ }
2149
+ }
2150
+ };
2151
+ }, 'mobile-plugin: 打开聊天嵌入页目标会话');
2129
2152
  });
2130
2153
  }
2131
2154
 
2132
- /** 清单与导航就位后:等目标出现,退订再打开一次,并按原 kind 回报 App。 */
2133
- function armEmbeddedSession(scope, sessionId) {
2155
+ /** 正在跑的那一次定位。换目标时把上一次的等待与盯住收掉,免得两趟抢同一条会话。 */
2156
+ let embedTask = null;
2157
+
2158
+ function disposeEmbedTask() {
2159
+ const task = embedTask;
2160
+ embedTask = null;
2161
+ if (task) task.dispose();
2162
+ }
2163
+
2164
+ /**
2165
+ * 页面里那个「换到这条会话」的入口。
2166
+ *
2167
+ * App 切会话不再重建 WebView(页面因此不重新加载,也就不会触发网页端启动时那次会话恢复),
2168
+ * 改由它在页面里调这个入口把新目标送进来。同名入口只留最新一个。
2169
+ */
2170
+ function installEmbedOpener(scope) {
2171
+ const win = typeof window !== 'undefined' ? window : null;
2172
+ if (!win) return;
2173
+ try {
2174
+ win.__DSH_CHAT_EMBED_OPEN__ = (next) => {
2175
+ if (typeof next !== 'string' || next === '') return;
2176
+ openEmbeddedTarget(scope, next);
2177
+ };
2178
+ } catch { /* 挂不上入口:这一趟就只剩装载时那一次定位 */ }
2179
+ }
2180
+
2181
+ /** 清单与导航就位后:等目标出现,退订再打开一次,盯住它,并按原 kind 回报 App。 */
2182
+ function openEmbeddedTarget(scope, sessionId) {
2134
2183
  const sessions = scope.sessions;
2135
2184
  const uiWorkspace = scope.uiWorkspace;
2136
2185
  if (!sessions || !sessions.list || typeof sessions.list.subscribe !== 'function'
2137
2186
  || typeof sessions.list.getSnapshot !== 'function'
2138
2187
  || !uiWorkspace || typeof uiWorkspace.openSession !== 'function') return;
2188
+ disposeEmbedTask();
2189
+ embedTask = armEmbeddedTarget(sessions, uiWorkspace, sessionId);
2190
+ }
2191
+
2192
+ /** 一次定位:返回一个随时能收掉的任务(换目标、页面卸载都走它)。 */
2193
+ function armEmbeddedTarget(sessions, uiWorkspace, sessionId) {
2139
2194
  const win = typeof window !== 'undefined' ? window : null;
2140
2195
 
2141
2196
  const post = (located) => {
@@ -2155,45 +2210,98 @@ window.__ModuleLoader__.load({
2155
2210
  }
2156
2211
  };
2157
2212
 
2158
- scope.effect(() => {
2159
- let done = false;
2160
- let opened = false;
2161
- let unsubscribe = null;
2162
- let timer = null;
2163
- const finish = (located) => {
2164
- if (done) return;
2165
- done = true;
2166
- if (timer !== null && win && typeof win.clearTimeout === 'function') win.clearTimeout(timer);
2167
- if (typeof unsubscribe === 'function') unsubscribe();
2168
- if (located && !opened) {
2169
- opened = true;
2170
- try {
2171
- uiWorkspace.openSession(sessionId);
2172
- } catch {
2173
- located = false;
2174
- }
2175
- }
2176
- post(located);
2177
- };
2178
- const onListed = () => {
2179
- if (done || !listed()) return;
2180
- // 退订要赶在打开之前:打开会再次通知清单,留着监听器会重进自己直到栈溢出。
2181
- finish(true);
2182
- };
2183
- if (listed()) {
2184
- finish(true);
2185
- return () => {};
2213
+ /** 目标此刻是不是网页的主视图。网页端把「当前会话」记在清单条目的 `retainedBy.mainView` 上。 */
2214
+ const isMain = () => {
2215
+ try {
2216
+ const entry = sessions.list.getSnapshot().byId[sessionId];
2217
+ return !!entry && !!(entry.retainedBy && entry.retainedBy.mainView > 0);
2218
+ } catch {
2219
+ return false;
2220
+ }
2221
+ };
2222
+
2223
+ let done = false;
2224
+ let opened = false;
2225
+ let unsubscribe = null;
2226
+ let timer = null;
2227
+ let holdTimer = null;
2228
+ let ticks = 0;
2229
+ const clearTimers = () => {
2230
+ if (timer !== null && win && typeof win.clearTimeout === 'function') win.clearTimeout(timer);
2231
+ if (holdTimer !== null && win && typeof win.clearTimeout === 'function') win.clearTimeout(holdTimer);
2232
+ timer = null;
2233
+ holdTimer = null;
2234
+ };
2235
+ const settle = (located) => {
2236
+ if (done) return;
2237
+ done = true;
2238
+ clearTimers();
2239
+ if (typeof unsubscribe === 'function') unsubscribe();
2240
+ post(located);
2241
+ };
2242
+ /** 把主视图指到目标;宿主没给这个方法就由调用方收成一次「没定位到」。 */
2243
+ const point = () => {
2244
+ try {
2245
+ uiWorkspace.openSession(sessionId);
2246
+ return true;
2247
+ } catch {
2248
+ return false;
2249
+ }
2250
+ };
2251
+ /**
2252
+ * 盯住主视图直到窗口走完。
2253
+ *
2254
+ * 每一拍先看目标还在不在主视图上,网页端那次恢复把它顶掉时就再指一次。读不到那一格
2255
+ * (宿主没这个字段)同样按「不在」处理,于是每拍补指一次 —— 那不过是一次重复打开,
2256
+ * 同一条会话重复打开是幂等的。
2257
+ */
2258
+ const hold = () => {
2259
+ if (!isMain() && !point()) {
2260
+ settle(false);
2261
+ return;
2262
+ }
2263
+ if (ticks <= 0) {
2264
+ settle(true);
2265
+ return;
2266
+ }
2267
+ ticks -= 1;
2268
+ if (win && typeof win.setTimeout === 'function') {
2269
+ holdTimer = win.setTimeout(hold, CHAT_EMBED_HOLD_STEP_MS);
2270
+ } else {
2271
+ settle(true);
2272
+ }
2273
+ };
2274
+ const finish = (located) => {
2275
+ if (done || opened) return;
2276
+ opened = true;
2277
+ // 退订要赶在打开之前:打开会再次通知清单,留着监听器会重进自己直到栈溢出。
2278
+ if (typeof unsubscribe === 'function') unsubscribe();
2279
+ if (!located || !point()) {
2280
+ settle(false);
2281
+ return;
2186
2282
  }
2283
+ ticks = CHAT_EMBED_HOLD_TICKS;
2284
+ hold();
2285
+ };
2286
+ const onListed = () => {
2287
+ if (done || opened || !listed()) return;
2288
+ finish(true);
2289
+ };
2290
+ if (listed()) {
2291
+ finish(true);
2292
+ } else {
2187
2293
  unsubscribe = sessions.list.subscribe(onListed);
2188
2294
  if (win && typeof win.setTimeout === 'function') {
2189
2295
  timer = win.setTimeout(() => finish(false), CHAT_EMBED_WAIT_MS);
2190
2296
  }
2191
- return () => {
2297
+ }
2298
+ return {
2299
+ dispose() {
2192
2300
  done = true;
2193
- if (timer !== null && win && typeof win.clearTimeout === 'function') win.clearTimeout(timer);
2301
+ clearTimers();
2194
2302
  if (typeof unsubscribe === 'function') unsubscribe();
2195
- };
2196
- }, 'mobile-plugin: 打开聊天嵌入页目标会话');
2303
+ },
2304
+ };
2197
2305
  }
2198
2306
 
2199
2307
  return {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-mobile-plugin",
3
- "version": "0.5.58",
3
+ "version": "0.5.59",
4
4
  "description": "Mobile API (/api/mobile/v1) and relay client for the DSH mobile app — desktop profile build (dsh 0.2.x, peers resolved by the host)",
5
5
  "license": "MIT",
6
6
  "icon": "./icon.png",