@esengine/transaction 2.0.7 → 2.1.1

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 (41) hide show
  1. package/dist/core/TransactionContext.d.ts +79 -0
  2. package/dist/core/TransactionContext.d.ts.map +1 -0
  3. package/dist/core/TransactionManager.d.ts +104 -0
  4. package/dist/core/TransactionManager.d.ts.map +1 -0
  5. package/dist/core/index.d.ts +8 -0
  6. package/dist/core/index.d.ts.map +1 -0
  7. package/dist/core/types.d.ts +393 -0
  8. package/dist/core/types.d.ts.map +1 -0
  9. package/dist/distributed/SagaOrchestrator.d.ts +173 -0
  10. package/dist/distributed/SagaOrchestrator.d.ts.map +1 -0
  11. package/dist/distributed/index.d.ts +6 -0
  12. package/dist/distributed/index.d.ts.map +1 -0
  13. package/dist/index.d.ts +56 -0
  14. package/dist/index.d.ts.map +1 -0
  15. package/dist/index.js +1621 -0
  16. package/dist/index.js.map +1 -0
  17. package/dist/integration/RoomTransactionMixin.d.ts +108 -0
  18. package/dist/integration/RoomTransactionMixin.d.ts.map +1 -0
  19. package/dist/integration/index.d.ts +6 -0
  20. package/dist/integration/index.d.ts.map +1 -0
  21. package/dist/operations/BaseOperation.d.ts +43 -0
  22. package/dist/operations/BaseOperation.d.ts.map +1 -0
  23. package/dist/operations/CurrencyOperation.d.ts +122 -0
  24. package/dist/operations/CurrencyOperation.d.ts.map +1 -0
  25. package/dist/operations/InventoryOperation.d.ts +152 -0
  26. package/dist/operations/InventoryOperation.d.ts.map +1 -0
  27. package/dist/operations/TradeOperation.d.ts +155 -0
  28. package/dist/operations/TradeOperation.d.ts.map +1 -0
  29. package/dist/operations/index.d.ts +9 -0
  30. package/dist/operations/index.d.ts.map +1 -0
  31. package/dist/storage/MemoryStorage.d.ts +63 -0
  32. package/dist/storage/MemoryStorage.d.ts.map +1 -0
  33. package/dist/storage/MongoStorage.d.ts +118 -0
  34. package/dist/storage/MongoStorage.d.ts.map +1 -0
  35. package/dist/storage/RedisStorage.d.ts +125 -0
  36. package/dist/storage/RedisStorage.d.ts.map +1 -0
  37. package/dist/storage/index.d.ts +8 -0
  38. package/dist/storage/index.d.ts.map +1 -0
  39. package/dist/tokens.d.ts +17 -0
  40. package/dist/tokens.d.ts.map +1 -0
  41. package/package.json +2 -2
@@ -0,0 +1,173 @@
1
+ /**
2
+ * @zh Saga 编排器
3
+ * @en Saga Orchestrator
4
+ *
5
+ * @zh 实现分布式事务的 Saga 模式编排
6
+ * @en Implements Saga pattern orchestration for distributed transactions
7
+ */
8
+ import type { ITransactionStorage, OperationResult } from '../core/types.js';
9
+ /**
10
+ * @zh Saga 步骤状态
11
+ * @en Saga step state
12
+ */
13
+ export type SagaStepState = 'pending' | 'executing' | 'completed' | 'compensating' | 'compensated' | 'failed';
14
+ /**
15
+ * @zh Saga 步骤
16
+ * @en Saga step
17
+ */
18
+ export interface SagaStep<T = unknown> {
19
+ /**
20
+ * @zh 步骤名称
21
+ * @en Step name
22
+ */
23
+ name: string;
24
+ /**
25
+ * @zh 目标服务器 ID(分布式用)
26
+ * @en Target server ID (for distributed)
27
+ */
28
+ serverId?: string;
29
+ /**
30
+ * @zh 执行函数
31
+ * @en Execute function
32
+ */
33
+ execute: (data: T) => Promise<OperationResult>;
34
+ /**
35
+ * @zh 补偿函数
36
+ * @en Compensate function
37
+ */
38
+ compensate: (data: T) => Promise<void>;
39
+ /**
40
+ * @zh 步骤数据
41
+ * @en Step data
42
+ */
43
+ data: T;
44
+ }
45
+ /**
46
+ * @zh Saga 步骤日志
47
+ * @en Saga step log
48
+ */
49
+ export interface SagaStepLog {
50
+ name: string;
51
+ serverId?: string;
52
+ state: SagaStepState;
53
+ startedAt?: number;
54
+ completedAt?: number;
55
+ error?: string;
56
+ }
57
+ /**
58
+ * @zh Saga 日志
59
+ * @en Saga log
60
+ */
61
+ export interface SagaLog {
62
+ id: string;
63
+ state: 'pending' | 'running' | 'completed' | 'compensating' | 'compensated' | 'failed';
64
+ steps: SagaStepLog[];
65
+ createdAt: number;
66
+ updatedAt: number;
67
+ metadata?: Record<string, unknown>;
68
+ }
69
+ /**
70
+ * @zh Saga 结果
71
+ * @en Saga result
72
+ */
73
+ export interface SagaResult {
74
+ success: boolean;
75
+ sagaId: string;
76
+ completedSteps: string[];
77
+ failedStep?: string;
78
+ error?: string;
79
+ duration: number;
80
+ }
81
+ /**
82
+ * @zh Saga 编排器配置
83
+ * @en Saga orchestrator configuration
84
+ */
85
+ export interface SagaOrchestratorConfig {
86
+ /**
87
+ * @zh 存储实例
88
+ * @en Storage instance
89
+ */
90
+ storage?: ITransactionStorage;
91
+ /**
92
+ * @zh 默认超时时间(毫秒)
93
+ * @en Default timeout in milliseconds
94
+ */
95
+ timeout?: number;
96
+ /**
97
+ * @zh 服务器 ID
98
+ * @en Server ID
99
+ */
100
+ serverId?: string;
101
+ }
102
+ /**
103
+ * @zh Saga 编排器
104
+ * @en Saga Orchestrator
105
+ *
106
+ * @zh 管理分布式事务的 Saga 模式执行流程
107
+ * @en Manages Saga pattern execution flow for distributed transactions
108
+ *
109
+ * @example
110
+ * ```typescript
111
+ * const orchestrator = new SagaOrchestrator({
112
+ * storage: redisStorage,
113
+ * serverId: 'server1',
114
+ * })
115
+ *
116
+ * const result = await orchestrator.execute([
117
+ * {
118
+ * name: 'deduct_currency',
119
+ * serverId: 'server1',
120
+ * execute: async (data) => {
121
+ * // 扣除货币
122
+ * return { success: true }
123
+ * },
124
+ * compensate: async (data) => {
125
+ * // 恢复货币
126
+ * },
127
+ * data: { playerId: '1', amount: 100 },
128
+ * },
129
+ * {
130
+ * name: 'add_item',
131
+ * serverId: 'server2',
132
+ * execute: async (data) => {
133
+ * // 添加物品
134
+ * return { success: true }
135
+ * },
136
+ * compensate: async (data) => {
137
+ * // 移除物品
138
+ * },
139
+ * data: { playerId: '1', itemId: 'sword' },
140
+ * },
141
+ * ])
142
+ * ```
143
+ */
144
+ export declare class SagaOrchestrator {
145
+ private _storage;
146
+ private _timeout;
147
+ private _serverId;
148
+ constructor(config?: SagaOrchestratorConfig);
149
+ /**
150
+ * @zh 执行 Saga
151
+ * @en Execute Saga
152
+ */
153
+ execute<T>(steps: SagaStep<T>[]): Promise<SagaResult>;
154
+ /**
155
+ * @zh 恢复未完成的 Saga
156
+ * @en Recover pending Sagas
157
+ */
158
+ recover(): Promise<number>;
159
+ /**
160
+ * @zh 获取 Saga 日志
161
+ * @en Get Saga log
162
+ */
163
+ getSagaLog(sagaId: string): Promise<SagaLog | null>;
164
+ private _saveSagaLog;
165
+ private _getPendingSagas;
166
+ private _recoverSaga;
167
+ }
168
+ /**
169
+ * @zh 创建 Saga 编排器
170
+ * @en Create Saga orchestrator
171
+ */
172
+ export declare function createSagaOrchestrator(config?: SagaOrchestratorConfig): SagaOrchestrator;
173
+ //# sourceMappingURL=SagaOrchestrator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SagaOrchestrator.d.ts","sourceRoot":"","sources":["../../src/distributed/SagaOrchestrator.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,KAAK,EACR,mBAAmB,EAGnB,eAAe,EAClB,MAAM,kBAAkB,CAAC;AAE1B;;;GAGG;AACH,MAAM,MAAM,aAAa,GAAG,SAAS,GAAG,WAAW,GAAG,WAAW,GAAG,cAAc,GAAG,aAAa,GAAG,QAAQ,CAAA;AAE7G;;;GAGG;AACH,MAAM,WAAW,QAAQ,CAAC,CAAC,GAAG,OAAO;IACjC;;;OAGG;IACH,IAAI,EAAE,MAAM,CAAA;IAEZ;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAA;IAEjB;;;OAGG;IACH,OAAO,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,OAAO,CAAC,eAAe,CAAC,CAAA;IAE9C;;;OAGG;IACH,UAAU,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;IAEtC;;;OAGG;IACH,IAAI,EAAE,CAAC,CAAA;CACV;A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ACG,OAAO,CAAC,CAAC,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,EAAE,GAAG,OAAO,CAAC,UAAU,CAAC;IAqG3D;;;OAGG;IACG,OAAO,IAAI,OAAO,CAAC,MAAM,CAAC;IAkBhC;;;OAGG;IACG,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,GAAG,IAAI,CAAC;YAK3C,YAAY;YAMZ,gBAAgB;YAIhB,YAAY;CAc7B;AAED;;;GAGG;AACH,wBAAgB,sBAAsB,CAAC,MAAM,GAAE,sBAA2B,GAAG,gBAAgB,CAE5F"}
@@ -0,0 +1,6 @@
1
+ /**
2
+ * @zh 分布式模块导出
3
+ * @en Distributed module exports
4
+ */
5
+ export { SagaOrchestrator, createSagaOrchestrator, type SagaOrchestratorConfig, type SagaStep, type SagaStepState, type SagaStepLog, type SagaLog, type SagaResult } from './SagaOrchestrator.js';
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/distributed/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EACH,gBAAgB,EAChB,sBAAsB,EACtB,KAAK,sBAAsB,EAC3B,KAAK,QAAQ,EACb,KAAK,aAAa,EAClB,KAAK,WAAW,EAChB,KAAK,OAAO,EACZ,KAAK,UAAU,EAClB,MAAM,uBAAuB,CAAC"}
@@ -0,0 +1,56 @@
1
+ /**
2
+ * @zh @esengine/transaction 事务系统
3
+ * @en @esengine/transaction Transaction System
4
+ *
5
+ * @zh 提供游戏事务处理能力,支持商店购买、玩家交易、分布式事务
6
+ * @en Provides game transaction capabilities, supporting shop purchases, player trading, and distributed transactions
7
+ *
8
+ * @example
9
+ * ```typescript
10
+ * import {
11
+ * TransactionManager,
12
+ * MemoryStorage,
13
+ * CurrencyOperation,
14
+ * InventoryOperation,
15
+ * } from '@esengine/transaction'
16
+ *
17
+ * // 创建事务管理器
18
+ * const manager = new TransactionManager({
19
+ * storage: new MemoryStorage(),
20
+ * })
21
+ *
22
+ * // 执行事务
23
+ * const result = await manager.run((tx) => {
24
+ * tx.addOperation(new CurrencyOperation({
25
+ * type: 'deduct',
26
+ * playerId: 'player1',
27
+ * currency: 'gold',
28
+ * amount: 100,
29
+ * }))
30
+ * tx.addOperation(new InventoryOperation({
31
+ * type: 'add',
32
+ * playerId: 'player1',
33
+ * itemId: 'sword',
34
+ * quantity: 1,
35
+ * }))
36
+ * })
37
+ *
38
+ * if (result.success) {
39
+ * console.log('Transaction completed!')
40
+ * }
41
+ * ```
42
+ */
43
+ export type { TransactionState, OperationResult, TransactionResult, OperationLog, TransactionLog, TransactionOptions, TransactionManagerConfig, ITransactionStorage, ITransactionOperation, ITransactionContext } from './core/types.js';
44
+ export { TransactionContext, createTransactionContext } from './core/TransactionContext.js';
45
+ export { TransactionManager, createTransactionManager } from './core/TransactionManager.js';
46
+ export { MemoryStorage, createMemoryStorage, type MemoryStorageConfig } from './storage/MemoryStorage.js';
47
+ export { RedisStorage, createRedisStorage, type RedisStorageConfig, type RedisClient } from './storage/RedisStorage.js';
48
+ export { MongoStorage, createMongoStorage, type MongoStorageConfig } from './storage/MongoStorage.js';
49
+ export { BaseOperation } from './operations/BaseOperation.js';
50
+ export { CurrencyOperation, createCurrencyOperation, type CurrencyOperationType, type CurrencyOperationData, type CurrencyOperationResult, type ICurrencyProvider } from './operations/CurrencyOperation.js';
51
+ export { InventoryOperation, createInventoryOperation, type InventoryOperationType, type InventoryOperationData, type InventoryOperationResult, type IInventoryProvider, type ItemData } from './operations/InventoryOperation.js';
52
+ export { TradeOperation, createTradeOperation, type TradeOperationData, type TradeOperationResult, type TradeItem, type TradeCurrency, type TradeParty, type ITradeProvider } from './operations/TradeOperation.js';
53
+ export { SagaOrchestrator, createSagaOrchestrator, type SagaOrchestratorConfig, type SagaStep, type SagaStepState, type SagaStepLog, type SagaLog, type SagaResult } from './distributed/SagaOrchestrator.js';
54
+ export { withTransactions, TransactionRoom, type TransactionRoomConfig, type ITransactionRoom } from './integration/RoomTransactionMixin.js';
55
+ export { TransactionManagerToken, TransactionStorageToken } from './tokens.js';
56
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AAMH,YAAY,EACR,gBAAgB,EAChB,eAAe,EACf,iBAAiB,EACjB,YAAY,EACZ,cAAc,EACd,kBAAkB,EAClB,wBAAwB,EACxB,mBAAmB,EACnB,qBAAqB,EACrB,mBAAmB,EACtB,MAAM,iBAAiB,CAAC;AAEzB,OAAO,EACH,kBAAkB,EAClB,wBAAwB,EAC3B,MAAM,8BAA8B,CAAC;AAEtC,OAAO,EACH,kBAAkB,EAClB,wBAAwB,EAC3B,MAAM,8BAA8B,CAAC;AAMtC,OAAO,EACH,aAAa,EACb,mBAAmB,EACnB,KAAK,mBAAmB,EAC3B,MAAM,4BAA4B,CAAC;AAEpC,OAAO,EACH,YAAY,EACZ,kBAAkB,EAClB,KAAK,kBAAkB,EACvB,KAAK,WAAW,EACnB,MAAM,2BAA2B,CAAC;AAEnC,OAAO,EACH,YAAY,EACZ,kBAAkB,EAClB,KAAK,kBAAkB,EAC1B,MAAM,2BAA2B,CAAC;AAMnC,OAAO,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAC;AAE9D,OAAO,EACH,iBAAiB,EACjB,uBAAuB,EACvB,KAAK,qBAAqB,EAC1B,KAAK,qBAAqB,EAC1B,KAAK,uBAAuB,EAC5B,KAAK,iBAAiB,EACzB,MAAM,mCAAmC,CAAC;AAE3C,OAAO,EACH,kBAAkB,EAClB,wBAAwB,EACxB,KAAK,sBAAsB,EAC3B,KAAK,sBAAsB,EAC3B,KAAK,wBAAwB,EAC7B,KAAK,kBAAkB,EACvB,KAAK,QAAQ,EAChB,MAAM,oCAAoC,CA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