@@ -6,48 +6,20 @@ import { getInfo } from './info';
66import lnd from './connect' ;
77import * as messages from '../bot/messages' ;
88import * as ordersActions from '../bot/ordersActions' ;
9- import { getUserI18nContext , getEmojiRate , decimalRound } from '../util' ;
9+ import {
10+ getUserI18nContext ,
11+ getEmojiRate ,
12+ decimalRound ,
13+ PerOrderIdMutex ,
14+ } from '../util' ;
1015import { logger } from '../logger' ;
1116import { HasTelegram } from '../bot/start' ;
1217import { IOrder } from '../models/order' ;
13- import { Mutex } from 'async-mutex' ;
14-
15- type LockCountedMutex = {
16- lockCount : number ;
17- mutex : Mutex ;
18- } ;
1918
2019// Track pending reconnects to prevent duplicate resubscriptions
2120// when both 'error' and 'end' events fire for the same invoice
2221const pendingReconnects : Set < string > = new Set ( ) ;
2322
24- class PerOrderIdMutex {
25- mutexes : Map < string , LockCountedMutex > = new Map ( ) ;
26-
27- async runExclusive ( orderId : string , callback : ( ) => Promise < any > ) {
28- let mtx : LockCountedMutex ;
29- if ( ! this . mutexes . has ( orderId ) ) {
30- mtx = { lockCount : 1 , mutex : new Mutex ( ) } ;
31- this . mutexes . set ( orderId , mtx ) ;
32- } else {
33- mtx = this . mutexes . get ( orderId ) ! ;
34- mtx . lockCount ++ ;
35- }
36- let ret : any ;
37- try {
38- ret = await mtx . mutex . runExclusive ( callback ) ;
39- } finally {
40- mtx . lockCount -- ;
41- if ( mtx . lockCount == 0 ) {
42- this . mutexes . delete ( orderId ) ;
43- }
44- }
45- return ret ;
46- }
47-
48- static instance = new PerOrderIdMutex ( ) ;
49- }
50-
5123const subscribeInvoice = async (
5224 bot : HasTelegram ,
5325 id : string ,
@@ -307,4 +279,4 @@ const payHoldInvoice = async (bot: HasTelegram, order: IOrder) => {
307279 }
308280} ;
309281
310- export { subscribeInvoice , payHoldInvoice , PerOrderIdMutex } ;
282+ export { subscribeInvoice , payHoldInvoice } ;
0 commit comments