Skip to content

Commit 8a2df9f

Browse files
committed
Update to send previous value as well
1 parent 0a2812a commit 8a2df9f

2 files changed

Lines changed: 10 additions & 8 deletions

File tree

src/ig-template/features/settings/Setting.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export abstract class Setting {
1515

1616
requirement: Requirement;
1717

18-
protected _onChange = new SimpleEventDispatcher<SettingsValue>();
18+
protected _onChange = new SimpleEventDispatcher<SettingsValue[]>();
1919

2020
protected constructor(id: SettingId, displayName: string, options: SettingOption[], defaultValue: SettingsValue, requirement: Requirement = new NoRequirement()) {
2121
this.id = id;
@@ -31,7 +31,7 @@ export abstract class Setting {
3131
/**
3232
* Emitted whenever the setting is changed.
3333
*/
34-
public get onChange(): ISimpleEvent<SettingsValue> {
34+
public get onChange(): ISimpleEvent<SettingsValue[]> {
3535
return this._onChange.asEvent();
3636
}
3737

@@ -40,8 +40,9 @@ export abstract class Setting {
4040
return;
4141
}
4242
if (this.validValue(value)) {
43+
const prevValue = this.value;
4344
this.value = value;
44-
this._onChange.dispatch(value);
45+
this._onChange.dispatch([prevValue, value]);
4546
} else {
4647
console.warn(`${value} is not a valid value for setting ${this.id}. It could be that the option is not yet unlocked.`);
4748
}

tests/unit/ig-template/features/settings/Settings.spec.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,18 @@ describe('Settings', () => {
6363
});
6464

6565
test('test change event', () => {
66-
expect.assertions(3);
66+
expect.assertions(4);
6767

6868
const setting = settings.getSetting(id);
69-
setting?.onChange.subscribe(value => {
70-
expect(value).toBe(2);
69+
setting?.onChange.subscribe(([prevValue, value]) => {
70+
expect(prevValue).toBe(2);
71+
expect(value).toBe(3);
7172
});
7273

73-
settings.setSetting(id, 2);
74+
settings.setSetting(id, 3);
7475

7576
expect(setting).toBeDefined();
76-
expect(setting?.value).toBe(2);
77+
expect(setting?.value).toBe(3);
7778
});
7879

7980
test('save empty', () => {

0 commit comments

Comments
 (0)