[FIX] context: always remove subscription when destroyed

Before this rev., components destroyed before being mounted didn't
stop listening to the context changes.

Closes #476
This commit is contained in:
Aaron Bohy
2019-11-20 11:09:19 +01:00
committed by Géry Debongnie
parent 2922cee6ea
commit 28ee790b3e
2 changed files with 37 additions and 3 deletions
+5 -3
View File
@@ -2,7 +2,7 @@ import { Component } from "./component/component";
import { scheduler } from "./component/scheduler";
import { EventBus } from "./core/event_bus";
import { Observer } from "./core/observer";
import { onWillUnmount } from "./hooks";
/**
* The `Context` object provides a way to share data between an arbitrary number
* of component. Usually, data is passed from a parent to its children component,
@@ -138,9 +138,11 @@ export function useContextWithCB(ctx: Context, component: Component<any, any>, m
await method();
}
});
onWillUnmount(() => {
const __destroy = component.__destroy;
component.__destroy = (parent) => {
ctx.off("update", component);
delete mapping[id];
});
__destroy.call(component, parent);
}
return ctx.state;
}