Skip to content

Commit

Permalink
AG-11148 Change memento to save full version
Browse files Browse the repository at this point in the history
  • Loading branch information
lsjroberts committed May 10, 2024
1 parent 40a6758 commit 7b1817f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ export interface AnnotationsRestoreEvent {

export class AnnotationsMemento implements Memento {
type = 'annotations';
version = '10.0';

constructor(public readonly annotations?: any) {}
constructor(
public readonly version: string,
public readonly annotations?: any
) {}
}

export class AnnotationManager
Expand All @@ -30,10 +32,8 @@ export class AnnotationManager
super();
}

public createMemento(_version: string) {
const memento = new AnnotationsMemento(this.annotations);
memento.version = '10.0';
return memento;
public createMemento(version: string) {
return new AnnotationsMemento(version, this.annotations);
}

public guardMemento(blob: unknown): blob is AnnotationsMemento {
Expand Down
25 changes: 13 additions & 12 deletions packages/ag-charts-community/src/chart/memento.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ describe('Memento Caretaker', () => {

class TestMemento implements Memento {
type = 'test';
version = '10.0';

constructor(public readonly data?: any) {}
constructor(
public readonly version: string,
public readonly data?: any
) {}
}

class TestOriginator implements MementoOriginator {
mementoOriginatorName = 'TestOriginator';
data?: any;
restored?: any;
data?: object;
restored?: object;

createMemento() {
return new TestMemento(this.data);
createMemento(version: string) {
return new TestMemento(version, this.data);
}

guardMemento(blob: unknown): boolean {
Expand All @@ -46,7 +47,7 @@ describe('Memento Caretaker', () => {
const blob = caretaker.save(originator);
caretaker.restore(originator, blob);

expect(blob).toBe('eyJkYXRhIjp7ImhlbGxvIjoid29ybGQifSwidHlwZSI6InRlc3QiLCJ2ZXJzaW9uIjoiMTAuMCJ9');
expect(blob).toBe('eyJ2ZXJzaW9uIjoiMTAuMC4wIiwiZGF0YSI6eyJoZWxsbyI6IndvcmxkIn0sInR5cGUiOiJ0ZXN0In0=');
expect(originator.restored).toEqual({ hello: 'world' });
});

Expand All @@ -56,7 +57,7 @@ describe('Memento Caretaker', () => {
const blob = caretaker.save(originator);
caretaker.restore(originator, blob);

expect(blob).toBe('eyJkYXRhIjp7ImhlbGxvIjoi8J+MjSJ9LCJ0eXBlIjoidGVzdCIsInZlcnNpb24iOiIxMC4wIn0=');
expect(blob).toBe('eyJ2ZXJzaW9uIjoiMTAuMC4wIiwiZGF0YSI6eyJoZWxsbyI6IvCfjI0ifSwidHlwZSI6InRlc3QifQ==');
expect(originator.restored).toEqual({ hello: '🌍' });
});

Expand All @@ -68,8 +69,8 @@ describe('Memento Caretaker', () => {
class OtherTestOriginator extends TestOriginator {
override mementoOriginatorName = 'OtherTestOriginator';

override createMemento() {
return new OtherTestMemento(this.data);
override createMemento(version: string) {
return new OtherTestMemento(version, this.data);
}

override guardMemento(blob: unknown): boolean {
Expand All @@ -86,7 +87,7 @@ describe('Memento Caretaker', () => {
expectWarning('AG Charts - TestOriginator - Could not restore data, memento was invalid, ignoring.', {
data: otherOriginator.data,
type: 'other-test',
version: '10.0',
version: '10.0.0',
});
expect(originator.restored).toBeUndefined();
});
Expand Down
6 changes: 2 additions & 4 deletions packages/ag-charts-community/src/chart/memento.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ export class MementoCaretaker {
private readonly version: string;

constructor(version: string) {
// Only consider the major and minor when versioning mementos, to handle migrating breaking changes and
// deprecations. Changes in patch versions can be safely ignored.
const [major, minor] = version.split('.');
this.version = `${major}.${minor}`;
// Strip out version suffixes, e.g. `-beta`
this.version = version.split('-')[0];
}

save(originator: MementoOriginator) {
Expand Down

0 comments on commit 7b1817f

Please sign in to comment.