Skip to content

Instantly share code, notes, and snippets.

@Hypercubed
Last active May 3, 2022 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hypercubed/7e25cbcd7c60d58ca2949795d215284a to your computer and use it in GitHub Desktop.
Save Hypercubed/7e25cbcd7c60d58ca2949795d215284a to your computer and use it in GitHub Desktop.
// 1. Reacting to a state change
save() {
this.store.setSaveButtonState(ButtonState.InProgress);
this.store.saveRecord();
this.store.saveButtonState
.pipe(
filter(state => state !== ButtonState.InProgress),
take(1)
)
.subscribe(state => {
// Do something when done
});
}
// 2. Wraped in a Promise
savePromise(): Promise<void> {
this.store.setSaveButtonState(ButtonState.InProgress);
this.store.saveRecord();
return new Promise((resolve, reject) => {
this.store.saveButtonState
.pipe(
filter(state => state !== ButtonState.InProgress),
take(1)
)
.subscribe(state => {
if (state === ButtonState.Fail) {
return reject();
}
return resolve();
});
});
}
// 3. Callback into the effect
save() {
this.store.saveRecord(() => {
// Do something when done
});
}
// 4. Event emitter
save() {
this.store.saveRecord();
this.store.saveDone.subscribe(() => {
// Do something when done
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment