Skip to content

Commit

Permalink
fix: uint8array and date should not be given a changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
Maximvdw committed Jul 1, 2024
1 parent 47ee3a1 commit 2b3c37c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/data/decorators/ChangeLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ export interface SerializableChangelog {
[CHANGELOG_METADATA_KEY]?: ChangeLog;
}

/**
* Change log for tracking changes on an object
*/
export class ChangeLog {
/**
* Changes
Expand Down Expand Up @@ -136,6 +139,8 @@ export function getChangeLog<T extends Object>(target: T & SerializableChangelog
return target[CHANGELOG_METADATA_KEY];
}

const IGNORED_TYPES = [Uint8Array, Date];

/**
* Create a change log for an object
* @param target Target object
Expand All @@ -162,7 +167,10 @@ export function createChangeLog<T extends Object>(target: T): T & SerializableCh
}
});
} else if (target[member.key] instanceof Object) {
target[member.key] = createChangeLog(target[member.key]);
// Only wrap objects that are not ignored
if (!IGNORED_TYPES.includes(target[member.key].constructor)) {
target[member.key] = createChangeLog(target[member.key]);
}
}
}
});
Expand Down
12 changes: 12 additions & 0 deletions test/specs/data/data.object.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,18 @@ describe('DataObject', () => {
});
});

it('should not break dates', () => {
@SerializableObject()
class MyObject {
@SerializableMember()
date: Date;
}
const object = new MyObject();
object.date = new Date();
const objectWithChangelog = createChangeLog(object);
console.log(object)
});

it('should detect changes with setters', () => {
const object = new DataObject('test', 'Maxim');
object.setPosition(new GeographicalPosition(123, 45));
Expand Down

0 comments on commit 2b3c37c

Please sign in to comment.