@mldong/jeeflow 1.8.52 → 1.8.53

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/dist/engine.d.ts CHANGED
@@ -101,12 +101,19 @@ export declare class EngineImpl implements Engine {
101
101
  setExtensions(ext: EngineExtensions): void;
102
102
  setRegistry(reg: HandlerRegistry): void;
103
103
  private interceptorCache;
104
- /** 定义级拦截器解析(issue 34,对齐 Java 模型级 postInterceptors):
105
- * 流程定义顶层 postInterceptors 声明 → 按名从 interceptorRegistry 取(未声明该流程不触发);
106
- * 未声明 → 回落引擎级列表(向后兼容)。结果按 defineId 缓存。
104
+ /** 流程根 pre/post 声明缓存(每 defineId 只回查一行定义):节点档拆开后若逐节点读定义行,
105
+ * 就把 issue 34 的"读一次"放大成逐任务读——spec.test 的 defineReads 次数不变量钉的就是它。 */
106
+ private rootDeclCache;
107
+ /** 定义级/节点级拦截器解析(issue 34 + audit8 R91,对齐 java `NodeModel.java:49-59`):
108
+ * 档位=`pre`/`post` 两档各自独立取串——节点 properties 声明优先,串空才回落流程根同名键,
109
+ * **两档不叠加**(spec02 §3「节点级拦截器的优先序、回落与键形」);两档都没声明才回落引擎级列表
110
+ * (向后兼容)。解析键=注册名(键形按丙=各家注册表沿用 java 全类名串,本栈不另发明短名)。
111
+ * 结果缓存键含 defineId+节点+档位(issues/234 §4.2:按 defineId 单键会把两个节点的声明串档)。
107
112
  * issues/60:解析与校验分离——定义读取/JSON 解析失败回落引擎级(现状语义),
108
- * 声明中存在未注册名时抛错(不静默跳过),且错误不写缓存保证持续报错。 */
113
+ * 声明中存在未注册名时抛错(不静默跳过,带节点 id 与档位),且错误不写缓存保证持续报错。 */
109
114
  private resolveInterceptors;
115
+ /** 流程根 pre/post 声明:每一定义只回查一行(读取/JSON 解析失败按 issues/60 现状语义视作"根级未声明",同样缓存)。 */
116
+ private resolveRootDecls;
110
117
  private firePre;
111
118
  /** 表达式求值(v1.5.0,门面 highLight 决策分支过滤用) */
112
119
  evalExpr(expr: string, vars: Record<string, any>): Promise<any>;
@@ -151,6 +158,12 @@ export declare class EngineImpl implements Engine {
151
158
  /** 事务包裹:有事务载体(JDBC `withTx`)则整段并入外层/自开一段;无(内存仓)裸跑。
152
159
  * 引擎入口包裹与门面动作级包裹都走这一枚 —— 嵌套调用由 withTx 的 join 支并入最外层。 */
153
160
  inTx<T>(fn: () => Promise<T>): Promise<T>;
161
+ /** 提交后腿通道(audit8 R77 读法一,issues/234 §4.3/spec12 §12.3.1;java 形状=监听器侧自取
162
+ * afterCommit,php 侧 R25 已收口同形):监听器/集成层把"必须看到已提交世界"的那一腿(反查、
163
+ * 外发通知、二次写)交到这里声明 ⇒ COMMIT 之后执行、回滚整批丢弃。
164
+ * 没有活动事务(或仓储没这条通道)⇒ 立即执行,行为与上一版逐字一致。
165
+ * ⚠ 引擎自身的 fire 不经过这里:派发点与「监听器随事件拿到动作事务连接」由 145-3 钉住。 */
166
+ runAfterCommit(fn: () => any | Promise<any>): Promise<void>;
154
167
  startProcessInstanceById(defineId: string, operator: string, args?: Record<string, any>): Promise<ProcessInstance>;
155
168
  private startProcessInstanceByIdInner;
156
169
  executeProcessTask(taskId: string, operator: string, args?: Record<string, any>): Promise<ProcessTask[]>;
package/dist/engine.js CHANGED
@@ -193,52 +193,74 @@ export class EngineImpl {
193
193
  setExtensions(ext) {
194
194
  this.ext = ext;
195
195
  this.interceptorCache = new Map();
196
+ this.rootDeclCache = new Map();
196
197
  }
197
198
  setRegistry(reg) { this.registry = reg; }
198
199
  interceptorCache = new Map();
199
- /** 定义级拦截器解析(issue 34,对齐 Java 模型级 postInterceptors):
200
- * 流程定义顶层 postInterceptors 声明 → 按名从 interceptorRegistry 取(未声明该流程不触发);
201
- * 未声明 → 回落引擎级列表(向后兼容)。结果按 defineId 缓存。
200
+ /** 流程根 pre/post 声明缓存(每 defineId 只回查一行定义):节点档拆开后若逐节点读定义行,
201
+ * 就把 issue 34 的"读一次"放大成逐任务读——spec.test 的 defineReads 次数不变量钉的就是它。 */
202
+ rootDeclCache = new Map();
203
+ /** 定义级/节点级拦截器解析(issue 34 + audit8 R91,对齐 java `NodeModel.java:49-59`):
204
+ * 档位=`pre`/`post` 两档各自独立取串——节点 properties 声明优先,串空才回落流程根同名键,
205
+ * **两档不叠加**(spec02 §3「节点级拦截器的优先序、回落与键形」);两档都没声明才回落引擎级列表
206
+ * (向后兼容)。解析键=注册名(键形按丙=各家注册表沿用 java 全类名串,本栈不另发明短名)。
207
+ * 结果缓存键含 defineId+节点+档位(issues/234 §4.2:按 defineId 单键会把两个节点的声明串档)。
202
208
  * issues/60:解析与校验分离——定义读取/JSON 解析失败回落引擎级(现状语义),
203
- * 声明中存在未注册名时抛错(不静默跳过),且错误不写缓存保证持续报错。 */
204
- async resolveInterceptors(inst) {
209
+ * 声明中存在未注册名时抛错(不静默跳过,带节点 id 与档位),且错误不写缓存保证持续报错。 */
210
+ async resolveInterceptors(node, inst, phase) {
205
211
  if (!this.ext)
206
212
  return [];
207
213
  const defineId = inst.defineId;
214
+ const key = `${defineId}|${node.id}|${phase}`;
208
215
  if (defineId == null)
209
216
  return this.ext.interceptors ?? [];
210
- const cached = this.interceptorCache.get(defineId);
217
+ const cached = this.interceptorCache.get(key);
211
218
  if (cached)
212
219
  return cached;
213
- let list = this.ext.interceptors ?? [];
214
- let declared = '';
220
+ const prop = `${phase}Interceptors`;
221
+ let declared = String(node.properties?.[prop] ?? '').trim();
222
+ if (!declared) {
223
+ const list = this.ext.interceptors ?? [];
224
+ declared = (await this.resolveRootDecls(defineId))[phase];
225
+ if (!declared) {
226
+ this.interceptorCache.set(key, list);
227
+ return list;
228
+ }
229
+ }
230
+ const list = [];
231
+ for (const name of declared.split(',').map(n => n.trim())) {
232
+ if (!name)
233
+ continue;
234
+ const ic = this.ext.interceptorRegistry?.[name];
235
+ if (!ic)
236
+ throw new Error(`${prop} 声明的拦截器未注册: ${name}(nodeId=${node.id})`);
237
+ list.push(ic);
238
+ }
239
+ this.interceptorCache.set(key, list);
240
+ return list;
241
+ }
242
+ /** 流程根 pre/post 声明:每一定义只回查一行(读取/JSON 解析失败按 issues/60 现状语义视作"根级未声明",同样缓存)。 */
243
+ async resolveRootDecls(defineId) {
244
+ const cached = this.rootDeclCache.get(defineId);
245
+ if (cached)
246
+ return cached;
247
+ let decl = { pre: '', post: '' };
215
248
  try {
216
249
  const def = await this.repo.findDefineById(defineId);
217
250
  if (def) {
218
251
  const content = typeof def.content === 'string' ? def.content : new TextDecoder().decode(def.content);
219
252
  const meta = JSON.parse(content);
220
- declared = String(meta.postInterceptors ?? '').trim();
253
+ decl = { pre: String(meta.preInterceptors ?? '').trim(), post: String(meta.postInterceptors ?? '').trim() };
221
254
  }
222
255
  }
223
256
  catch { /* 定义读取/JSON 解析失败回落引擎级 */ }
224
- if (declared) {
225
- list = [];
226
- for (const name of declared.split(',').map(n => n.trim())) {
227
- if (!name)
228
- continue;
229
- const ic = this.ext.interceptorRegistry?.[name];
230
- if (!ic)
231
- throw new Error(`postInterceptors 声明的拦截器未注册: ${name}`);
232
- list.push(ic);
233
- }
234
- }
235
- this.interceptorCache.set(defineId, list);
236
- return list;
257
+ this.rootDeclCache.set(defineId, decl);
258
+ return decl;
237
259
  }
238
260
  async firePre(node, inst) {
239
261
  if (!this.ext?.interceptors && !this.ext?.interceptorRegistry)
240
262
  return true;
241
- const list = await this.resolveInterceptors(inst);
263
+ const list = await this.resolveInterceptors(node, inst, 'pre');
242
264
  for (const ic of [...list].sort((a, b) => a.order - b.order))
243
265
  if (!(await ic.preHandle(node, inst)))
244
266
  return false;
@@ -253,7 +275,7 @@ export class EngineImpl {
253
275
  async firePost(node, inst) {
254
276
  if (!this.ext?.interceptors && !this.ext?.interceptorRegistry)
255
277
  return;
256
- for (const ic of await this.resolveInterceptors(inst))
278
+ for (const ic of await this.resolveInterceptors(node, inst, 'post'))
257
279
  await ic.postHandle(node, inst);
258
280
  }
259
281
  async fireEvent(evt) {
@@ -337,6 +359,16 @@ export class EngineImpl {
337
359
  async inTx(fn) {
338
360
  return this.repo.withTx ? this.repo.withTx(fn) : fn();
339
361
  }
362
+ /** 提交后腿通道(audit8 R77 读法一,issues/234 §4.3/spec12 §12.3.1;java 形状=监听器侧自取
363
+ * afterCommit,php 侧 R25 已收口同形):监听器/集成层把"必须看到已提交世界"的那一腿(反查、
364
+ * 外发通知、二次写)交到这里声明 ⇒ COMMIT 之后执行、回滚整批丢弃。
365
+ * 没有活动事务(或仓储没这条通道)⇒ 立即执行,行为与上一版逐字一致。
366
+ * ⚠ 引擎自身的 fire 不经过这里:派发点与「监听器随事件拿到动作事务连接」由 145-3 钉住。 */
367
+ async runAfterCommit(fn) {
368
+ if (this.repo.afterCommit?.call(this.repo, fn))
369
+ return;
370
+ await fn();
371
+ }
340
372
  // ─── Start ─────────────────────────────────────────────────────────────────
341
373
  async startProcessInstanceById(defineId, operator, args = {}) {
342
374
  // audit8 145-2①:入口级包裹——直连引擎的调用方也拿到"一次动作一笔原子"(spec/12 §12.2)。
@@ -2,6 +2,11 @@ import { AsyncLocalStorage } from 'node:async_hooks';
2
2
  import { ProcessInstance, ProcessTask, type ProcessDefine, type CcInstanceRow, type DefineRow, type InstanceRow, type TaskRow } from '../model.js';
3
3
  import type { IDGenerator, ProcessRepository, QueryCondition, InstanceStatsRow, TaskStatsRow } from '../spi.js';
4
4
  export declare const txStore: AsyncLocalStorage<SqlConnection>;
5
+ export interface AfterCommitScope {
6
+ cbs: Array<() => any | Promise<any>>;
7
+ done: boolean;
8
+ }
9
+ export declare const afterCommitStore: AsyncLocalStorage<AfterCommitScope>;
5
10
  /** 当前事务连接(无事务 ⇒ null)。集成方自研写腿(如 DynamicTableWriter 的 MySQL 实现)
6
11
  * 用它并入引擎事务——spec/12 §12.2 覆盖面:一次动作内所有写腿取同一条连接。 */
7
12
  export declare function currentTxConnection(): SqlConnection | null;
@@ -48,6 +53,10 @@ export declare class JdbcRepository implements ProcessRepository {
48
53
  private readonly adapter;
49
54
  private readonly idGen;
50
55
  constructor(adapter: SqlAdapter, idGen?: IDGenerator);
56
+ /** 登记「本次事务提交之后」执行的回调(audit8 R77 读法一;java 形状=监听器侧自取 afterCommit,
57
+ * php 侧 R25 已收口同形)。返回 false=当前无活动事务 ⇒ 调用方须自行立即执行
58
+ * (门面级没外包事务模板的栈,「落库后」与「提交后」等价)。 */
59
+ afterCommit(fn: () => any | Promise<any>): boolean;
51
60
  private sql;
52
61
  withTx<T>(fn: () => Promise<T>): Promise<T>;
53
62
  /** 返回当前连接:有事务绑定用事务连接,否则从适配器获取 */
@@ -4,7 +4,7 @@
4
4
  // - 本文件是**唯一维护点**:15 个仓储方法的 SQL 逻辑、行映射、ID 生成
5
5
  // - SQL 占位符统一使用 `?`,由各数据库适配器(SqlAdapter)转换为自家风格
6
6
  // (MySQL `?` 原生 / PostgreSQL `$n`)
7
- // - 事务(spec §7.4):withTx 用 AsyncLocalStorage 绑定当前异步上下文的事务连接
7
+ // - 事务(规范 05「事务约定」节):withTx 用 AsyncLocalStorage 绑定当前异步上下文的事务连接
8
8
  //
9
9
  // 新增数据库 = 写一个适配器(约 80 行):实现 SqlAdapter + 连接包装
10
10
  // (execute/fetchOne/fetchAll/begin/commit/rollback)。参考 mysql.ts / postgres.ts。
@@ -40,6 +40,7 @@ const DEFINE_WHITELIST = new Set([
40
40
  // audit8 145-2:**导出**——ext.ts 必须共用这一枚(此前它自带第二枚且从不 run,扩展写腿
41
41
  // 永远逃逸出引擎事务)。集成方写腿也可经 currentTxConnection/transactionAwarePool 并入。
42
42
  export const txStore = new AsyncLocalStorage();
43
+ export const afterCommitStore = new AsyncLocalStorage();
43
44
  /** 当前事务连接(无事务 ⇒ null)。集成方自研写腿(如 DynamicTableWriter 的 MySQL 实现)
44
45
  * 用它并入引擎事务——spec/12 §12.2 覆盖面:一次动作内所有写腿取同一条连接。 */
45
46
  export function currentTxConnection() {
@@ -108,25 +109,48 @@ export class JdbcRepository {
108
109
  this.adapter = adapter;
109
110
  this.idGen = idGen;
110
111
  }
112
+ /** 登记「本次事务提交之后」执行的回调(audit8 R77 读法一;java 形状=监听器侧自取 afterCommit,
113
+ * php 侧 R25 已收口同形)。返回 false=当前无活动事务 ⇒ 调用方须自行立即执行
114
+ * (门面级没外包事务模板的栈,「落库后」与「提交后」等价)。 */
115
+ afterCommit(fn) {
116
+ const sc = afterCommitStore.getStore();
117
+ if (!sc || sc.done)
118
+ return false;
119
+ sc.cbs.push(fn);
120
+ return true;
121
+ }
111
122
  sql(s) {
112
123
  return convertPlaceholder(s, this.adapter.placeholder);
113
124
  }
114
- // ── 事务(spec §7.4:AsyncLocalStorage 绑定连接)─────────────────────────
125
+ // ── 事务(规范 05「事务约定」节:AsyncLocalStorage 绑定连接)─────────────────────────
115
126
  async withTx(fn) {
116
127
  // audit8 145-2③(spec/12 §12.2 归属面):已有活动事务 ⇒ **并入**(不再开第二段事务、
117
128
  // 不提前提交;提交/回滚归最外层)——引擎入口包裹与门面动作级包裹嵌套时落这一支。
118
129
  if (txStore.getStore())
119
130
  return fn();
120
131
  const conn = await this.adapter.acquire();
132
+ const scope = { cbs: [], done: false };
121
133
  try {
122
134
  await conn.begin();
123
135
  try {
124
- const result = await txStore.run(conn, fn);
136
+ const result = await txStore.run(conn, () => afterCommitStore.run(scope, fn));
125
137
  await conn.commit();
138
+ scope.done = true;
139
+ // R77 提交后腿:逐条吞掉自家异常,不打断主流程(登记方自己承担失败语义)
140
+ for (const cb of scope.cbs) {
141
+ try {
142
+ await cb();
143
+ }
144
+ catch (err) {
145
+ console.warn('[jeeflow] afterCommit 回调失败: ' + String(err));
146
+ }
147
+ }
126
148
  return result;
127
149
  }
128
150
  catch (err) {
129
151
  await conn.rollback();
152
+ scope.done = true;
153
+ scope.cbs = []; // R77:回滚 ⇒ 提交后腿整批丢弃(监听器不得看到未提交的世界)
130
154
  throw err;
131
155
  }
132
156
  }
@@ -283,7 +307,7 @@ export class JdbcRepository {
283
307
  'operator=?, expire_time=?, variable=?, update_time=?, update_user=? WHERE id=?'), [inst.state, inst.parentNodeName ?? '', inst.businessNo ?? '', inst.operator,
284
308
  inst.expireTime ?? null, JSON.stringify(inst.variables ?? {}),
285
309
  inst.updateTime, inst.updateUser, inst.id]);
286
- // v1.0.1:级联持久化聚合根内任务状态变更(同连接,spec §7.4)
310
+ // v1.0.1:级联持久化聚合根内任务状态变更(同连接,规范 05「事务约定」节)
287
311
  for (const task of inst.tasks) {
288
312
  if (task.id)
289
313
  await this.updateTaskWithConn(conn, task);
package/dist/spi.d.ts CHANGED
@@ -148,6 +148,10 @@ export interface ProcessRepository {
148
148
  /** 事务载体(audit8 145-2,spec/12 §12.2)——JDBC 仓储天然实现(AsyncLocalStorage);
149
149
  * 内存仓没有此方法 ⇒ 引擎 inTx 显式降级裸跑(§12.1 原则 4)。 */
150
150
  withTx?<T>(fn: () => Promise<T>): Promise<T>;
151
+ /** 提交后腿通道(audit8 R77 读法一,spec/12 §12.3.1):有活动事务时登记回调并返回 true
152
+ * (Commit 后执行、回滚丢弃);无活动事务返回 false ⇒ 调用方须自行立即执行。
153
+ * 内存仓没有此方法 ⇒ 一律立即执行(与"落库后"等价)。 */
154
+ afterCommit?(fn: () => any | Promise<any>): boolean;
151
155
  findDefineById(id: string): Promise<ProcessDefine | null>;
152
156
  findDefineByName(name: string): Promise<ProcessDefine | null>;
153
157
  saveDefine(define: ProcessDefine): Promise<void>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mldong/jeeflow",
3
- "version": "1.8.52",
3
+ "version": "1.8.53",
4
4
  "description": "jeeflow workflow engine — Node.js / TypeScript implementation",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -20,7 +20,7 @@
20
20
  "build": "tsc",
21
21
  "typecheck": "tsc -p tsconfig.check.json",
22
22
  "test": "node --import tsx __tests__/spec.test.ts",
23
- "test-mysql": "node --import tsx --test __tests__/jdbc.test.ts __tests__/tx-tier-145.test.ts",
23
+ "test-mysql": "node --import tsx --test __tests__/jdbc.test.ts __tests__/tx-tier-145.test.ts __tests__/after_commit_r77.test.ts",
24
24
  "demo": "node --import tsx demo/main.ts",
25
25
  "prepublishOnly": "npm run build"
26
26
  },