import * as uuid from 'uuid' ; import { Module } from '@ringcentral-integration/commons/lib/di' ; import { action , computed , RcModuleV2 , state , } from '@ringcentral-integration/core' ; import { BlockItem , DepsModules , IBlock } from './Block.interface' ; type ModalMappingType = Record < string , BlockItem > ; @ Module ( { name : 'Block' , deps : [ { dep : 'BlockOptions' , optional : true } ] , } ) export class Block extends RcModuleV2 < DepsModules > implements IBlock { @ state blockIds : string [ ] = [ ] ; @ state blockMapping : ModalMappingType = { } ; @ computed ( ( that ) => [ that . blockIds , that . blockMapping ] ) get blocks ( ) { return this . blockIds . map ( ( id ) => this . blockMapping [ id ] ) ; } @ action private _setListItem ( id , data ) { this . blockIds . push ( id ) ; this . blockMapping [ id ] = data ; } @ action private _removeListItem ( id ) { this . blockIds = this . blockIds . filter ( ( blockId ) => blockId !== id ) ; delete this . blockMapping [ id ] ; } @ action private _clearAllItem ( ) { this . blockIds . length = 0 ; this . blockMapping = { } ; } param props block ( props = { } ) { const id = this . _getId ( ) ; this . _setListItem ( id , props ) ; return id ; } param id unblock ( id ) { this . _removeListItem ( id ) ; } param cb async next ( cb ) { const id = this . block ( ) ; try { await cb ( ) ; } finally { this . unblock ( id ) ; } } /**
     * clear all block item, and unblock view
     */ unblockAll ( ) { this . _clearAllItem ( ) ; } private _getId ( ) { return uuid . v4 ( ) ; } }