| 38 | * @public |
| 39 | */ |
| 40 | export class ProcessMiddleware extends BasePresentationMiddleware implements PresentationMiddleware { |
| 41 | private constructor (client: Client, next?: PresentationMiddleware) { |
| 42 | super(client, next) |
| 43 | } |
| 44 | |
| 45 | async notifyTx (...tx: Tx[]): Promise<void> { |
| 46 | await this.provideNotifyTx(...tx) |
| 47 | } |
| 48 | |
| 49 | async close (): Promise<void> { |
| 50 | await this.provideClose() |
| 51 | } |
| 52 | |
| 53 | static create (client: Client, next?: PresentationMiddleware): ProcessMiddleware { |
| 54 | return new ProcessMiddleware(client, next) |
| 55 | } |
| 56 | |
| 57 | private readonly txFactory = new TxOperations(this.client, getCurrentAccount().primarySocialId).txFactory |
| 58 | |
| 59 | async tx (tx: Tx): Promise<TxResult> { |
| 60 | const preTx: Array<TxCUD<Doc>> = [] |
| 61 | const postTx: Array<TxCUD<Doc>> = [] |
| 62 | await this.handleTx(preTx, postTx, tx) |
| 63 | |
| 64 | if (preTx.length > 0 || postTx.length > 0) { |
| 65 | if (TxProcessor.isExtendsCUD(tx._class)) { |
| 66 | const applyIf = this.txFactory.createTxApplyIf( |
| 67 | core.space.Tx, |
| 68 | generateId(), |
| 69 | [], |
| 70 | [], |
| 71 | [...preTx, tx as TxCUD<Doc>, ...postTx], |
| 72 | 'process', |
| 73 | true |
| 74 | ) |
| 75 | return await this.provideTx(applyIf) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | return await this.provideTx(tx) |
| 80 | } |
| 81 | |
| 82 | private async handleTx (preTx: Array<TxCUD<Doc>>, postTx: Array<TxCUD<Doc>>, ...txes: Tx[]): Promise<void> { |
| 83 | for (const etx of txes) { |
| 84 | if (etx._class === core.class.TxApplyIf) { |
| 85 | const applyIf = etx as TxApplyIf |
| 86 | await this.handleTx(preTx, postTx, ...applyIf.txes) |
| 87 | } |
| 88 | |
| 89 | await this.handleCardCreate(postTx, etx) |
| 90 | await this.handleCardUpdate(preTx, etx) |
| 91 | await this.handleTagAdd(postTx, etx) |
| 92 | await this.handleToDoDone(preTx, etx) |
| 93 | await this.handleApproveRequest(preTx, etx) |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | private async handleCardUpdate (preTx: Array<TxCUD<Doc>>, etx: Tx): Promise<void> { |
nothing calls this directly
no test coverage detected