-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add unit tests for BaseStore #4404
Conversation
Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
Might be the issue related to sindresorhus/conf#114 ? Currently protected async saveToFile(model: T) {
logger.info(`[STORE]: SAVING ${this.path}`);
// todo: update when fixed https://github.com/sindresorhus/conf/issues/114
if (this.storeConfig) {
for (const [key, value] of Object.entries(model)) {
this.storeConfig.set(key, value);
}
}
} I don't know how internally |
src/common/base-store.ts
Outdated
@@ -168,7 +168,7 @@ export abstract class BaseStore<T> extends Singleton { | |||
|
|||
protected async onModelChange(model: T) { | |||
if (ipcMain) { | |||
this.saveToFile(model); // save config file | |||
await this.saveToFile(model); // save config file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like this isn't the solution here. For me, it looks that the real issue is that onModelChange
is called multiple times. Not sure though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If writing to the storage file is sync
why it could be a an issue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If writing to the storage file is
sync
why it could be a an issue?
onModelChange
can be triggered multiple times, if we don't await
then the order of execution of sync
writes might happen out of order.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. What if remove async
from onModelChange
? And probably from saveToFile
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. What if remove
async
fromonModelChange
? And probably fromsaveToFile
.
It works as well but it modifies api behaviour (this class is exposed via extension api).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Nokel81 yes, probably you're right.
Initially I thought there could be some timing issues, but maybe not..
This code produces expected result:
class Foo {
constructor(){
this.bar();
}
bar(): void {
console.log("in Foo.bar");
}
}
class Bat extends Foo {
async bar(): Promise<void> {
await new Promise(resolve => setTimeout(resolve, 500));
super.bar();
console.log("in Bat.bar");
}
}
new Bat();
OUTPUT:
[LOG]: "in Foo.bar"
[LOG]: "in Bat.bar"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is what I believe yes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jakolehm yes, it could be best solution I guess.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have been testing different things and I'm quite certain now that this PR mainly just adds tests for BaseStore, nothing actually changed in the execution order. I still removed async
because it's quite misleading in this context.
Signed-off-by: Jari Kolehmainen <jari.kolehmainen@gmail.com>
async
fromonModelChange
&saveToFile