Last active
May 3, 2022 17:39
-
-
Save Hypercubed/7e25cbcd7c60d58ca2949795d215284a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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