dsh-workbench-danmaku 0.4.2 → 0.4.9

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/db/db.js CHANGED
@@ -1,33 +1,42 @@
1
1
  "use strict";
2
2
  // 弹幕库的表结构与基础工具(node:sqlite DatabaseSync)。
3
3
 
4
- export const DANMAKU_DDL = [
5
- `CREATE TABLE IF NOT EXISTS danmaku (
6
- id TEXT PRIMARY KEY,
7
- body TEXT NOT NULL,
8
- tone TEXT NOT NULL DEFAULT 'normal',
9
- enabled INTEGER NOT NULL DEFAULT 1,
10
- card INTEGER NOT NULL DEFAULT 1,
11
- font_size INTEGER,
12
- speed INTEGER,
13
- opacity REAL,
14
- font_weight INTEGER,
15
- font_family TEXT,
16
- band TEXT,
17
- prefix TEXT,
18
- chance INTEGER NOT NULL DEFAULT 1,
19
- sort_order INTEGER NOT NULL DEFAULT 0,
20
- created_at INTEGER NOT NULL,
21
- updated_at INTEGER NOT NULL,
22
- deleted_at INTEGER,
23
- rev INTEGER NOT NULL DEFAULT 1
24
- )`,
25
- ];
26
-
27
- export const DANMAKU_INDEX_DDL = [
28
- "CREATE INDEX IF NOT EXISTS idx_danmaku_live ON danmaku(deleted_at, sort_order, id)",
29
- "CREATE UNIQUE INDEX IF NOT EXISTS idx_danmaku_body ON danmaku(body) WHERE deleted_at IS NULL",
30
- ];
4
+ import { createTableSql, defineTable, ensureTable } from "../../lib/backend/schema.js";
5
+
6
+ const DANMAKU_TABLE = defineTable({
7
+ name: "danmaku",
8
+ columns: [
9
+ { name: "id", type: "TEXT", primaryKey: true },
10
+ { name: "body", type: "TEXT", notNull: true },
11
+ { name: "tone", type: "TEXT", notNull: true, default: "normal" },
12
+ { name: "enabled", type: "INTEGER", notNull: true, default: 1 },
13
+ { name: "card", type: "INTEGER", notNull: true, default: 1 },
14
+ { name: "font_size", type: "INTEGER" },
15
+ { name: "speed", type: "INTEGER" },
16
+ { name: "opacity", type: "REAL" },
17
+ { name: "font_weight", type: "INTEGER" },
18
+ { name: "font_family", type: "TEXT" },
19
+ { name: "band", type: "TEXT" },
20
+ { name: "prefix", type: "TEXT" },
21
+ { name: "chance", type: "INTEGER", notNull: true, default: 1 },
22
+ { name: "sort_order", type: "INTEGER", notNull: true, default: 0 },
23
+ { name: "created_at", type: "INTEGER", notNull: true },
24
+ { name: "updated_at", type: "INTEGER", notNull: true },
25
+ { name: "deleted_at", type: "INTEGER" },
26
+ { name: "rev", type: "INTEGER", notNull: true, default: 1 },
27
+ ],
28
+ indexes: [
29
+ "CREATE INDEX IF NOT EXISTS idx_danmaku_live ON danmaku(deleted_at, sort_order, id)",
30
+ "CREATE UNIQUE INDEX IF NOT EXISTS idx_danmaku_body ON danmaku(body) WHERE deleted_at IS NULL",
31
+ ],
32
+ });
33
+
34
+ /** 声明式表结构:建表、补列、索引都由此展开,供结构审计读取。 */
35
+ export const SCHEMA_TABLES = [DANMAKU_TABLE];
36
+
37
+ /** 兼容既有导出:由同一份声明生成(备份/同步脚本会直接 exec)。 */
38
+ export const DANMAKU_DDL = [createTableSql(DANMAKU_TABLE)];
39
+ export const DANMAKU_INDEX_DDL = DANMAKU_TABLE.indexes;
31
40
 
32
41
  let clock = null;
33
42
  let last = 0;
@@ -55,47 +64,34 @@ export function tableExists(db, name) {
55
64
  }
56
65
  }
57
66
 
58
- export function hasColumn(db, table, column) {
59
- try {
60
- return db.prepare(`PRAGMA table_info(${table})`).all().some((row) => row.name === column);
61
- } catch (error) {
62
- return false;
63
- }
67
+ export function ensureSchema(db) {
68
+ for (const table of SCHEMA_TABLES) ensureTable(db, table);
64
69
  }
65
70
 
66
- export function ensureSchema(db, log) {
67
- for (const ddl of DANMAKU_DDL) db.exec(ddl);
68
- // 每条弹幕可以有自己的外观与轮播参数;老库逐列补齐。
69
- const extraColumns = [
70
- "card INTEGER NOT NULL DEFAULT 1",
71
- "font_size INTEGER",
72
- "speed INTEGER",
73
- "opacity REAL",
74
- "font_weight INTEGER",
75
- "font_family TEXT",
76
- "band TEXT",
77
- "prefix TEXT",
78
- "chance INTEGER NOT NULL DEFAULT 1",
79
- ];
80
- for (const column of extraColumns) {
81
- const name = column.split(" ")[0];
82
- if (hasColumn(db, "danmaku", name)) continue;
83
- try {
84
- db.exec(`ALTER TABLE danmaku ADD COLUMN ${column}`);
85
- } catch (error) {
86
- if (log) log(`danmaku ${name} column skipped`, String((error && error.message) || error));
87
- }
88
- }
89
- for (const ddl of DANMAKU_INDEX_DDL) {
71
+ let savepointSeq = 0;
72
+
73
+ /**
74
+ * 事务:顶层用 BEGIN IMMEDIATE;宿主迁移守卫已经在事务里时退到 SAVEPOINT。
75
+ * 迁移期 setup 被宿主单事务包住,内层再 BEGIN 会直接报
76
+ * 「cannot start a transaction within a transaction」,而运行期 action 仍需要真事务。
77
+ */
78
+ export function tx(db, fn) {
79
+ if (db.isTransaction) {
80
+ const name = "wb_danmaku_tx_" + ++savepointSeq;
81
+ db.exec("SAVEPOINT " + name);
90
82
  try {
91
- db.exec(ddl);
83
+ const value = fn();
84
+ db.exec("RELEASE " + name);
85
+ return value;
92
86
  } catch (error) {
93
- if (log) log("danmaku index skipped", String((error && error.message) || error));
87
+ try {
88
+ db.exec("ROLLBACK TO " + name + "; RELEASE " + name);
89
+ } catch (rollbackError) {
90
+ /* 事务可能已结束 */
91
+ }
92
+ throw error;
94
93
  }
95
94
  }
96
- }
97
-
98
- export function tx(db, fn) {
99
95
  db.exec("BEGIN IMMEDIATE");
100
96
  try {
101
97
  const value = fn();
package/lib/index.js CHANGED
@@ -1,18 +1,11 @@
1
1
  /**
2
2
  * 弹幕业务插件(DSH 原生插件)。
3
- *
4
- * 定位:把用户自己维护的一份弹幕清单,持续轮播在整个 DSH 界面(body 固定层),
5
- * 并可选叠加到整个桌面(独立 Electron helper 透明置顶窗)。
6
- *
7
- * 节奏只由宿主调度器决定(lib/player.js),两个显示端都只是渲染器:
8
- * 界面层走 SSE domain-event,桌面层走 helper 的 stdin。这样两端看到同一条弹幕、
9
- * 同一时刻出现,不会各飘各的。
10
- *
11
- * 配置由宿主统一配置中心维护(表 host_config_scheme):本插件只声明 configDecl(),
12
- * 通过 setup 的 api.config 读生效值并订阅变更,不再自建配置表、也不参与同步与备份。
13
- *
14
- * 与其他插件弱耦合:不 import 任何业务插件;agent 通过 danmaku.* action 操作弹幕库。
15
- * 0.4.0:删除 danmaku.config*(旧配置兼容 action) 等兼容 action 与自带的配置视图,配置只走宿主 wb.configCenter*。
3
+ * 把用户维护的弹幕清单持续轮播在 DSH 界面,并可叠加到整个桌面。
4
+ * 节奏只由宿主调度器决定(lib/player.js):界面走 SSE domain-event,桌面走 helper stdin。
5
+ * 配置由宿主统一配置中心维护,本插件只声明 configDecl() 并通过 api.config 读生效值。
6
+ * 与其他插件弱耦合:不 import 任何业务插件,agent 通过 danmaku.* action 操作弹幕库。
7
+ * 当前版本:0.4.7(以 package.json 为准)。
8
+ * 版本历史见 CHANGELOG.md。
16
9
  */
17
10
 
18
11
  import { bindClock, ensureSchema } from "./db/db.js";
@@ -26,7 +19,6 @@ import { createPlayer } from "./player.js";
26
19
  import { createSupervisor } from "./desktop/supervisor.js";
27
20
  import { registerManageActions } from "./actions/manage.js";
28
21
  import { registerOverlayActions } from "./actions/config.js";
29
- import { registerSchemaAction, trackActions } from "./actions/schema.js";
30
22
 
31
23
  export const inject = ["workbench"];
32
24
 
@@ -78,10 +70,8 @@ export function setup(api) {
78
70
  }
79
71
 
80
72
  const deps = { db: api.db, player, desktop, log, changed };
81
- const actionDefs = trackActions(api);
82
73
  registerManageActions(api, deps);
83
74
  registerOverlayActions(api, deps);
84
- registerSchemaAction(api, actionDefs, DOMAIN);
85
75
 
86
76
  // 按当前配置启动调度器;桌面层只在 surface 需要时起步,失败也不影响界面层。
87
77
  try {
@@ -132,7 +122,7 @@ export function apply(ctx) {
132
122
  // 弹幕库跟着云同步与备份走(表里已有 updated_at/deleted_at,可直接走行级 LWW)。
133
123
  tables: [{ name: TABLES[0], persistence: "persistent", backupKey: TABLES[0] }],
134
124
  // 只同步/备份弹幕库本体;播放节奏与桌面层开关属于本机配置,留在配置中心。
135
- sync: { tables: [{ name: TABLES[0], mode: "row" }] },
125
+ sync: { tables: [{ name: TABLES[0] }] },
136
126
  exportData: () => exportDanmakuData(host.db),
137
127
  importData: (payload, _ctx, options) => importDanmakuData(host.db, payload, options && options.policy),
138
128
  config: configDecl(),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-workbench-danmaku",
3
- "version": "0.4.2",
3
+ "version": "0.4.9",
4
4
  "description": "个人工作台弹幕插件:把自建弹幕轮播在整个 DSH 界面,并可叠加到整个桌面",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
@@ -10,14 +10,15 @@
10
10
  "cordis.patch.yml",
11
11
  "package.json",
12
12
  "README.md",
13
- "LICENSE"
13
+ "LICENSE",
14
+ "CHANGELOG.md"
14
15
  ],
15
16
  "wb": {
16
17
  "addon": {
17
18
  "id": "wb-addon-danmaku",
18
19
  "title": "弹幕",
19
20
  "description": "在整个 DSH 界面(含工作台业务页与宿主弹窗之外的所有区域)与桌面透明层轮播自建弹幕;含弹幕库视图,配置项由宿主统一配置中心渲染",
20
- "version": "0.4.2",
21
+ "version": "0.4.9",
21
22
  "minShell": "0.8.0",
22
23
  "tables": [
23
24
  "danmaku"
@@ -45,7 +46,7 @@
45
46
  "./package.json": "./package.json"
46
47
  },
47
48
  "dependencies": {
48
- "dsh-workbench-host": "^0.30.0"
49
+ "dsh-workbench-host": "^0.35.4"
49
50
  },
50
51
  "dsh": {
51
52
  "bundle": {
@@ -1,34 +0,0 @@
1
- "use strict";
2
- // 「Action」页的数据源:只返回运行时注册的 schema,不执行 action、不写库。
3
-
4
- export function registerSchemaAction(api, defs, domain) {
5
- const prefix = domain.replace(/^wb-addon-/, "");
6
- api.action(prefix + ".actionSchema", {
7
- description: "只读:返回本插件运行时注册的 action schema(供「Action」页渲染)",
8
- input: { type: "object", properties: {}, additionalProperties: false },
9
- execute: () => ({
10
- shellVersion: api.shellVersion || "",
11
- domains: [{ id: domain, version: api.addon && api.addon.version ? api.addon.version : "" }],
12
- actions: Array.from(defs, ([name, def]) => ({
13
- name,
14
- domain,
15
- description: typeof def.description === "string" ? def.description : "",
16
- input: def.input && typeof def.input === "object" ? def.input : { type: "object", properties: {} },
17
- examples: Array.isArray(def.examples) ? def.examples : [],
18
- })),
19
- }),
20
- render: (r) => `弹幕插件 ${r.actions.length} 个 action`,
21
- });
22
- }
23
-
24
- export function trackActions(api) {
25
- const defs = new Map();
26
- const raw = typeof api.action === "function" ? api.action.bind(api) : null;
27
- if (raw) {
28
- api.action = function (name, def) {
29
- defs.set(name, def || {});
30
- return raw(name, def);
31
- };
32
- }
33
- return defs;
34
- }