Skip to content

Commit

Permalink
Simplify return types; rename a var
Browse files Browse the repository at this point in the history
  • Loading branch information
ssciolla committed Jan 20, 2021
1 parent c10b9f1 commit ba5614e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
25 changes: 13 additions & 12 deletions src/assets/src/changes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const transformProperty = (value: string, propertyMap: HumanReadableMap) => {
// Core functions

function detectChanges<T extends ComparableEntity> (
versOne: T, versTwo: T, propsToWatch: (keyof T)[], transforms: ValueTransform[]): string[] | undefined
versOne: T, versTwo: T, propsToWatch: (keyof T)[], transforms: ValueTransform[]): string[]
{
let changedPropMessages = [];
for (const property of propsToWatch) {
Expand All @@ -67,11 +67,12 @@ function detectChanges<T extends ComparableEntity> (
changedPropMessages.push(`The ${propName} changed from "${valueOne}" to "${valueTwo}".`);
}
}
if (changedPropMessages.length > 0) return changedPropMessages;
return changedPropMessages;
}


// Any new types added to ComparableEntity need to be supported in this function.

function describeEntity (entity: ComparableEntity): string[] {
let entityType;
let permIdent;
Expand All @@ -89,18 +90,18 @@ function describeEntity (entity: ComparableEntity): string[] {

// https://lodash.com/docs/4.17.15#xorWith

export function compareEntities<T extends ComparableEntity> (oldOnes: T[], newOnes: T[]): string[] | undefined
export function compareEntities<T extends ComparableEntity> (oldOnes: T[], newOnes: T[]): string[]
{
const symDiff = xorWith(oldOnes, newOnes, isEqual);
if (symDiff.length === 0) return;
if (symDiff.length === 0) return [];

const oldIDs = oldOnes.map((value) => value.id);
const newIDs = newOnes.map((value) => value.id);

let changeMessages: string[] = [];
let changedIDsProcessed: number[] = [];
let processedChangedObjectIDs: number[] = [];
for (const entity of symDiff) {
if (changedIDsProcessed.includes(entity.id)) continue;
if (processedChangedObjectIDs.includes(entity.id)) continue;
const [entityType, permIdent] = describeEntity(entity);
if (oldIDs.includes(entity.id) && !newIDs.includes(entity.id)) {
changeMessages.push(`The ${entityType} with ${permIdent} was deleted.`);
Expand All @@ -111,21 +112,21 @@ export function compareEntities<T extends ComparableEntity> (oldOnes: T[], newOn
const [firstEntity, secondEntity] = symDiff.filter(value => value.id === entity.id);
let changesDetected: string[] = [];
if (isMeeting(firstEntity) && isMeeting(secondEntity)) {
const detectResult = detectChanges<Meeting>(firstEntity, secondEntity, meetingPropsToWatch, standardTransforms);
if (detectResult) changesDetected.push(...detectResult);
const changes = detectChanges<Meeting>(firstEntity, secondEntity, meetingPropsToWatch, standardTransforms);
if (changes.length > 0) changesDetected.push(...changes);
// Custom check for Meeting.status, since only some status changes are relevant here.
if (firstEntity.status !== secondEntity.status && secondEntity.status === MeetingStatus.STARTED) {
changesDetected.push('The meeting is now in progress.');
}
} else if (isQueueBase(firstEntity) && isQueueBase(secondEntity)) {
const detectResult = detectChanges<QueueBase>(firstEntity, secondEntity, queueBasePropsToWatch, standardTransforms);
if (detectResult) changesDetected.push(...detectResult);
const changes = detectChanges<QueueBase>(firstEntity, secondEntity, queueBasePropsToWatch, standardTransforms);
if (changes.length > 0) changesDetected.push(...changes);
}
if (changesDetected.length > 0) {
changeMessages.push(`The ${entityType} with ${permIdent} was changed. ` + changesDetected.join(' '));
}
changedIDsProcessed.push(entity.id)
processedChangedObjectIDs.push(entity.id)
}
}
if (changeMessages.length > 0) return changeMessages;
return changeMessages;
}
2 changes: 1 addition & 1 deletion src/assets/src/hooks/useEntityChanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function useEntityChanges<T extends ComparableEntity>():

const compareAndSetChangeEvents = (oldEntities: readonly T[], newEntities: readonly T[]): void => {
const changeMessages = compareEntities<T>(oldEntities.slice(), newEntities.slice());
if (changeMessages !== undefined) {
if (changeMessages.length > 0) {
let eventID = nextID;
const newChangeEvents = changeMessages.map(
(m) => {
Expand Down

0 comments on commit ba5614e

Please sign in to comment.