Skip to content

Commit

Permalink
apply updating to multiple users
Browse files Browse the repository at this point in the history
Signed-off-by: Artem Buslaev <[email protected]>
  • Loading branch information
artembuslaev committed May 3, 2023
1 parent eed4858 commit b34c0ca
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class PolicyComponentsUtils {
/**
* Block update function
*/
public static BlockUpdateFn: (uuid: string, state: any, user: IPolicyUser, tag?: string) => Promise<void>;
public static BlockUpdateFn: (uuid: string, user: IPolicyUser) => Promise<void>;
/**
* Block error function
*/
Expand Down
5 changes: 2 additions & 3 deletions guardian-service/src/policy-engine/policy-engine.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,13 @@ export class PolicyEngineService {
* @param uuid {string} - id of block
* @param user {IPolicyUser} - short user object
*/
private async stateChangeCb(blocks: string[], state: any, user: IPolicyUser) {
private async stateChangeCb(blocks: string[], user: IPolicyUser) {
if (!user || !user.did) {
return;
}

await this.channel.publish('update-block', {
blocks,
state,
user
});
}
Expand Down Expand Up @@ -199,7 +198,7 @@ export class PolicyEngineService {

switch (type) {
case 'update':
PolicyComponentsUtils.BlockUpdateFn(args[0], args[1], args[2], args[3]);
PolicyComponentsUtils.BlockUpdateFn(args[0], args[1]);
break;

case 'error':
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, {}, user, ref.tag);
PolicyComponentsUtils.BlockUpdateFn(ref.parent, user);
}

/**
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, {}, user, ref.tag);
PolicyComponentsUtils.BlockUpdateFn(ref.parent, user);
PolicyComponentsUtils.ExternalEventFn(new ExternalEvent(ExternalEventType.Set, ref, user, data));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export class MultiSignBlock {
await this.updateThreshold(users, sourceDoc, documentId, user);

ref.triggerEvents(PolicyOutputEventType.RefreshEvent, user, null);
PolicyComponentsUtils.BlockUpdateFn(ref.parent, {}, user, ref.tag);
PolicyComponentsUtils.BlockUpdateFn(ref.parent, user);
PolicyComponentsUtils.ExternalEventFn(new ExternalEvent(ExternalEventType.Set, ref, user, {
documents: ExternalDocuments(document)
}));
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, {}, user, ref.tag);
PolicyComponentsUtils.BlockUpdateFn(ref.parent, user);

PolicyComponentsUtils.ExternalEventFn(new ExternalEvent(ExternalEventType.Set, ref, user, data));
}
Expand Down
2 changes: 1 addition & 1 deletion policy-service/src/policy-engine/blocks/policy-roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ export class PolicyRolesBlock {

await PolicyComponentsUtils.UpdateUserInfoFn(user, ref.policyInstance);

PolicyComponentsUtils.BlockUpdateFn(ref.parent, {}, user, ref.tag);
PolicyComponentsUtils.BlockUpdateFn(ref.parent, user);
PolicyComponentsUtils.ExternalEventFn(new ExternalEvent(ExternalEventType.Set, ref, user, {
group: group.uuid,
role: group.role
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 as any, state, item, tag);
PolicyComponentsUtils.BlockUpdateFn(this as any, item);
}
}

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, {}, args[0], this.tag);
PolicyComponentsUtils.BlockUpdateFn(parentBlock, args[0]);
return result;
}

Expand Down
40 changes: 17 additions & 23 deletions policy-service/src/policy-engine/policy-components-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class PolicyComponentsUtils {
/**
* Block update timeout
*/
private static _blockUpdateTimeout: any;
private static _blockUpdateTimeoutMap: Map<string, any> = new Map<string, any>();

/**
* Update block map
Expand All @@ -53,38 +53,32 @@ export class PolicyComponentsUtils {
/**
* Block update function
*/
public static BlockUpdateFn = (
block: IPolicyBlock,
state: any,
user: IPolicyUser,
tag?: string
) => {
let blocksToUpdate = PolicyComponentsUtils._updateBlockMap.get(
user?.did
);
public static BlockUpdateFn = (block: IPolicyBlock, user: IPolicyUser) => {
const did = user?.did;
if (!did || !block?.uuid) {
return;
}

let blocksToUpdate = PolicyComponentsUtils._updateBlockMap.get(did);
if (!blocksToUpdate) {
blocksToUpdate = new Set<string>();
PolicyComponentsUtils._updateBlockMap.set(
user?.did,
blocksToUpdate
);
PolicyComponentsUtils._updateBlockMap.set(did, blocksToUpdate);
}
blocksToUpdate.add(block?.uuid);
if (!PolicyComponentsUtils._blockUpdateTimeout) {
PolicyComponentsUtils._blockUpdateTimeout = setTimeout(() => {

if (!PolicyComponentsUtils._blockUpdateTimeoutMap.has(did)) {
PolicyComponentsUtils._blockUpdateTimeoutMap.set(did, setTimeout(() => {
blockUpdate(
'update',
PolicyComponentsUtils.getCommonBlocksToUpdate(
PolicyComponentsUtils.getParentBlocksToUpdate(
block?.policyInstance?.config,
blocksToUpdate
),
state,
user,
tag
user
);
PolicyComponentsUtils._blockUpdateTimeout = null;
PolicyComponentsUtils._blockUpdateTimeoutMap.delete(did);
blocksToUpdate.clear();
}, 2000);
}, 2000));
}
};
/**
Expand All @@ -94,7 +88,7 @@ export class PolicyComponentsUtils {
* @param result Result
* @returns
*/
private static getCommonBlocksToUpdate(
private static getParentBlocksToUpdate(
root: any,
blocksToUpdate: Set<string>
): string[] {
Expand Down

0 comments on commit b34c0ca

Please sign in to comment.