Normally, I use onWillDestroy to cleanup the data before leaving the app. I added some notification to the POS but the callbackFn keeps getting called after I left the POS so I want to unsubscribe before I leave.
// original class in Point_of_Sale
export class PosStore extends Reactive {
async setup( .... )
}
// my modification
import { onWillDestroy } from "@odoo/owl";
patch(PosStore.prototype, {
async setup() {
await super.setup(...arguments);
this.bus.addChannel("promoModule");
let callbackFn = (data) => {....};
this.bus.subscribe("REDALERT", callbackFn);
// doesn't work
onWillDestroy(async () => {
this.bus.unsubscribe("REDALERT", callbackFn);
this.bus.deleteChannel("promoModule");
});
}
}
But this time it throws this owl error even I already use it in the setup function. It works fine if I remove the onWillDestroy.
OwlError: No active component (a hook function should only be called in 'setup')
Error: No active component (a hook function should only be called in 'setup')
at getCurrent (point_of_sale.assets_prod.min.js:1047:46)
at onWillDestroy (point_of_sale.assets_prod.min.js:1108:39)
at Proxy.setup (point_of_sale.assets_prod.min.js:15949:365)
at async Proxy.setup (point_of_sale.assets_prod.min.js:15997:415)
at async Promise.all (:8069/pos/index 0)
at async start (point_of_sale.assets_prod.min.js:5219:1)
at async _startServices (point_of_sale.assets_prod.min.js:5220:72)
at async startServices (point_of_sale.assets_prod.min.js:5213:157)
at async mountComponent (point_of_sale.assets_prod.min.js:5226:170)
at async startPosApp (point_of_sale.assets_prod.min.js:15955:1188)
I'm not sure what cause the error. Is there something else I should try?