Observer for listening some registered events that trigger React/Vue methods
- 開一個 observerInterface.js 並且 new 它
import Observer from 'observer';
const observerInterface = new Observer();
export default observerInterface;
- 將你要註冊的程式碼餵給 observer
observerInterface.subscribe('TOAST_PUSH', (args) => this.push(args));
...
push(options) {
const message = {
...options,
id: Toasts.idFactory(),
};
this.setState((state) => [...state.queue, message]);
}
- 執行剛剛註冊的程式碼,透過 emit 來執行
observerInterface.emit('TOAST_PUSH', args);