Skip to content

Commit

Permalink
upgrade ws updating messages
Browse files Browse the repository at this point in the history
Signed-off-by: artembuslaev <[email protected]>
  • Loading branch information
artembuslaev committed Apr 26, 2023
1 parent fc27a9b commit 4602dc8
Show file tree
Hide file tree
Showing 24 changed files with 132 additions and 43 deletions.
2 changes: 1 addition & 1 deletion api-gateway/src/api/service/websockets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export class WebSocketsService {
if (this.checkUserByDid(client, msg)) {
this.send(client, {
type: 'update-event',
data: msg.uuid
data: msg.blocks
});
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ export class ActionBlockComponent implements OnInit {
}
}

onUpdate(id: string): void {
if (this.id == id) {
onUpdate(blocks: string[]): void {
if (Array.isArray(blocks) && blocks.includes(this.id)) {
this.loadData();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ export class ButtonBlockComponent implements OnInit, AfterContentChecked {
}
}

onUpdate(id: string): void {
if (this.id == id) {
onUpdate(blocks: string[]): void {
if (Array.isArray(blocks) && blocks.includes(this.id)) {
this.loadData();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ export class ContainerBlockComponent implements OnInit, OnDestroy {
}
}

onUpdate(id: string): void {
if (this.id == id) {
onUpdate(blocks: string[]): void {
if (Array.isArray(blocks) && blocks.includes(this.id)) {
this.loadData();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ export class CreateTokenBlockComponent implements OnInit {
}
}

onUpdate(id: string): void {
if (this.id == id) {
onUpdate(blocks: string[]): void {
if (Array.isArray(blocks) && blocks.includes(this.id)) {
this.loadData();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ export class DocumentsSourceBlockComponent implements OnInit {
}
}

onUpdate(id: string): void {
if (this.id == id) {
onUpdate(blocks: string[]): void {
if (Array.isArray(blocks) && blocks.includes(this.id)) {
this.loadData();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ export class FiltersAddonBlockComponent implements OnInit {
}
}

onUpdate(id: string): void {
if (this.id == id) {
onUpdate(blocks: string[]): void {
if (Array.isArray(blocks) && blocks.includes(this.id)) {
this.loadData();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ export class GroupManagerBlockComponent implements OnInit {
}
}

onUpdate(id: string): void {
if (this.id == id) {
onUpdate(blocks: string[]): void {
if (Array.isArray(blocks) && blocks.includes(this.id)) {
this.loadData();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ export class InformationBlockComponent implements OnInit {
}
}

onUpdate(id: string): void {
if (this.id == id) {
onUpdate(blocks: string[]): void {
if (Array.isArray(blocks) && blocks.includes(this.id)) {
this.loadData();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ export class MultiSignBlockComponent implements OnInit {
}
}

onUpdate(id: string): void {
if (this.id == id) {
onUpdate(blocks: string[]): void {
if (Array.isArray(blocks) && blocks.includes(this.id)) {
this.loadData();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ export class ReportBlockComponent implements OnInit {
}
}

onUpdate(id: string): void {
if (this.id == id) {
onUpdate(blocks: string[]): void {
if (Array.isArray(blocks) && blocks.includes(this.id)) {
this.loadData();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ export class RequestDocumentBlockComponent implements OnInit {
}
}

onUpdate(id: string): void {
if (this.id == id) {
onUpdate(blocks: string[]): void {
if (Array.isArray(blocks) && blocks.includes(this.id)) {
this.loadData();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ export class RolesBlockComponent implements OnInit {
}
}

onUpdate(id: string): void {
if (this.id == id) {
onUpdate(blocks: string[]): void {
if (Array.isArray(blocks) && blocks.includes(this.id)) {
this.loadData();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ export class StepBlockComponent implements OnInit {
}
}

onUpdate(id: string): void {
if (this.id == id) {
onUpdate(blocks: string[]): void {
if (Array.isArray(blocks) && blocks.includes(this.id)) {
this.loadData();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ export class TagsManagerBlockComponent implements OnInit {
}
}

public onUpdate(id: string): void {
if (this.id == id) {
public onUpdate(blocks: string[]): void {
if (Array.isArray(blocks) && blocks.includes(this.id)) {
this.loadData();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ export class TokenConfirmationBlockComponent implements OnInit {
}
}

onUpdate(id: string): void {
if (this.id == id) {
onUpdate(blocks: string[]): void {
if (Array.isArray(blocks) && blocks.includes(this.id)) {
this.loadData();
}
}
Expand Down
4 changes: 2 additions & 2 deletions guardian-service/src/policy-engine/policy-engine.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ export class PolicyEngineService {
* @param uuid {string} - id of block
* @param user {IPolicyUser} - short user object
*/
private async stateChangeCb(uuid: string, state: any, user: IPolicyUser) {
private async stateChangeCb(blocks: string[], state: any, user: IPolicyUser) {
if (!user || !user.did) {
return;
}

await this.channel.publish('update-block', {
uuid,
blocks,
state,
user
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class DocumentsSourceAddon {
this.state = oldState;

const ref = PolicyComponentsUtils.GetBlockRef(this);
PolicyComponentsUtils.BlockUpdateFn(ref.parent.uuid, {}, user, ref.tag);
PolicyComponentsUtils.BlockUpdateFn(ref.parent, {}, user, ref.tag);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class InterfaceDocumentsSource {
this.state = oldState;

const ref = PolicyComponentsUtils.GetBlockRef(this);
PolicyComponentsUtils.BlockUpdateFn(ref.parent.uuid, {}, user, ref.tag);
PolicyComponentsUtils.BlockUpdateFn(ref.parent, {}, user, ref.tag);
PolicyComponentsUtils.ExternalEventFn(new ExternalEvent(ExternalEventType.Set, ref, user, data));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class PaginationAddon {
this.state = oldState;

const ref = PolicyComponentsUtils.GetBlockRef(this);
PolicyComponentsUtils.BlockUpdateFn(ref.parent.uuid, {}, user, ref.tag);
PolicyComponentsUtils.BlockUpdateFn(ref.parent, {}, user, ref.tag);

PolicyComponentsUtils.ExternalEventFn(new ExternalEvent(ExternalEventType.Set, ref, user, data));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ export function BasicBlock<T>(options: Partial<PolicyBlockDecoratorOptions>) {
users[user.did] = user;
}
for (const item of Object.values(users)) {
PolicyComponentsUtils.BlockUpdateFn(this.uuid, state, item, tag);
PolicyComponentsUtils.BlockUpdateFn(this as any, state, item, tag);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export function DataSourceAddon(options: Partial<PolicyBlockDecoratorOptions>) {
}
}

PolicyComponentsUtils.BlockUpdateFn(parentBlock.uuid, {}, args[0], this.tag);
PolicyComponentsUtils.BlockUpdateFn(parentBlock, {}, args[0], this.tag);
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,19 @@ export function EventBlock(options: Partial<PolicyBlockDecoratorOptions>) {
throw new BlockActionError('Block is unavailable', this.blockType, this.uuid);
}
this._accessMap.set(user?.did, true);
PolicyComponentsUtils.BlockUpdateFn(super.parent.uuid, {}, user, super.tag);
PolicyComponentsUtils.BlockUpdateFn(super.parent, {}, user, super.tag);
let result = {};
if (typeof super.getData === 'function') {
try {
result = await super.setData(...args);
} catch (err) {
this._accessMap.delete(user?.did);
PolicyComponentsUtils.BlockUpdateFn(super.parent.uuid, {}, user, super.tag);
PolicyComponentsUtils.BlockUpdateFn(super.parent, {}, user, super.tag);
throw err;
}
}
this._accessMap.delete(user?.did);
PolicyComponentsUtils.BlockUpdateFn(super.parent.uuid, {}, user, super.tag);
PolicyComponentsUtils.BlockUpdateFn(super.parent, {}, user, super.tag);
return result;
}

Expand Down
93 changes: 91 additions & 2 deletions policy-service/src/policy-engine/policy-components-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,107 @@ export type PolicyActionMap = Map<string, Map<PolicyInputEventType, EventCallbac
* @param type
* @param args
*/
export function blockUpdate(type: string, args: any[]) {
export function blockUpdate(type: string, ...args) {
new BlockTreeGenerator().sendMessage(PolicyEvents.BLOCK_UPDATE_BROADCAST, { type, args });
}

/**
* Policy component utils
*/
export class PolicyComponentsUtils {
/**
* Block update timeout
*/
private static _blockUpdateTimeout: any;

/**
* Update block map
*/
private static _updateBlockMap: Map<string, Set<string>> = new Map<string, Set<string>>();

/**
* Block update function
*/
public static BlockUpdateFn: (uuid: string, state: any, user: IPolicyUser, tag?: string) => Promise<void> = async (...args) => { blockUpdate('update', args); };
public static BlockUpdateFn = (
block: IPolicyBlock,
state: any,
user: IPolicyUser,
tag?: string
) => {
let blocks = PolicyComponentsUtils._updateBlockMap.get(user?.did);
if (!blocks) {
blocks = new Set<string>();
PolicyComponentsUtils._updateBlockMap.set(user?.did, blocks);
}

if (!PolicyComponentsUtils._blockUpdateTimeout) {
PolicyComponentsUtils._blockUpdateTimeout = setTimeout(() => {
blockUpdate('update', Array.from(blocks), state, user, tag);
PolicyComponentsUtils._blockUpdateTimeout = null;
blocks.clear();
}, 3000);
}

if (!PolicyComponentsUtils.isExistToUpdate(block, blocks)) {
PolicyComponentsUtils.addBlockToUpdateSet(block, blocks);
}
};
/**
* Is block exist to update
* @param block Block to update
* @param blocksToUpdate Blocks to update
* @returns Is exist
*/
private static isExistToUpdate(
block: IPolicyBlock,
blocksToUpdate: Set<string>
): boolean {
return (
block?.uuid &&
(blocksToUpdate.has(block.uuid) ||
PolicyComponentsUtils.isExistToUpdate(
block.parent,
blocksToUpdate
))
);
}
/**
* Add block to update set
* @param block Block to update
* @param blocksToUpdate Blocks to update
* @returns Is exist
*/
private static addBlockToUpdateSet(
block: IPolicyBlock,
blocksToUpdate: Set<string>
): Set<string> {
if (!block?.uuid) {
return blocksToUpdate;
}
PolicyComponentsUtils.removeChildrenBlocksFromUpdate(
block,
blocksToUpdate
);
return blocksToUpdate.add(block?.uuid);
}
/**
* Remove children blocks from update set
* @param children Children blocks
* @param blocksToUpdate Blocks to update
*/
private static removeChildrenBlocksFromUpdate(
block: IPolicyBlock,
blocksToUpdate: Set<string>
) {
block?.children?.forEach((child) => {
blocksToUpdate.delete(child?.uuid);
blocksToUpdate = PolicyComponentsUtils.removeChildrenBlocksFromUpdate(
child,
blocksToUpdate
);
});
return blocksToUpdate;
}
/**
* Block error function
*/
Expand Down

0 comments on commit 4602dc8

Please sign in to comment.