Skip to content

Commit

Permalink
Merge pull request #212 from cocopon/fix-disposing-plugin-2
Browse files Browse the repository at this point in the history
Fix disposing plugin (take 2)
  • Loading branch information
cocopon authored Mar 22, 2021
2 parents f0f1ad8 + 8b40a9b commit 860fec6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
3 changes: 3 additions & 0 deletions lib/plugin/blade/common/controller/input-binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,8 @@ export class InputBindingController<In> implements BladeController {
if (this.controller.onDispose) {
this.controller.onDispose();
}
if (this.controller.view.onDispose) {
this.controller.view.onDispose();
}
}
}
3 changes: 3 additions & 0 deletions lib/plugin/blade/common/controller/monitor-binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,8 @@ export class MonitorBindingController<T> implements BladeController {
if (this.controller.onDispose) {
this.controller.onDispose();
}
if (this.controller.view.onDispose) {
this.controller.view.onDispose();
}
}
}
18 changes: 11 additions & 7 deletions lib/plugin/input-binding-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@ import {createController, InputBindingPlugin} from './input-binding';

class TestView implements View {
public readonly element: HTMLElement;
public disposed = false;

constructor(doc: Document) {
this.element = doc.createElement('div');
}

onDispose() {
this.disposed = true;
}
}

class TestController implements ValueController<string> {
public readonly view: View;
public readonly view: TestView;
public disposed = false;

constructor(doc: Document, public readonly value: Value<string>) {
Expand Down Expand Up @@ -51,12 +56,11 @@ describe(createController.name, () => {
params: {},
target: new BindingTarget({foo: 'bar'}, 'foo'),
});
assert.isFalse(
bc?.controller instanceof TestController && bc.controller.disposed,
);
const c = bc?.controller as TestController;
assert.isFalse(c.disposed);
assert.isFalse(c.view.disposed);
bc?.blade.dispose();
assert.isTrue(
bc?.controller instanceof TestController && bc.controller.disposed,
);
assert.isTrue(c.disposed);
assert.isTrue(c.view.disposed);
});
});
18 changes: 11 additions & 7 deletions lib/plugin/monitor-binding-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ import {createController, MonitorBindingPlugin} from './monitor-binding';

class TestView implements View {
public readonly element: HTMLElement;
public disposed = false;

constructor(doc: Document) {
this.element = doc.createElement('div');
}

onDispose() {
this.disposed = true;
}
}

class TestController implements ValueController<Buffer<string>> {
public readonly view: View;
public readonly view: TestView;
public disposed = false;

constructor(doc: Document, public readonly value: BufferedValue<string>) {
Expand Down Expand Up @@ -48,12 +53,11 @@ describe(createController.name, () => {
params: {},
target: new BindingTarget({foo: 'bar'}, 'foo'),
});
assert.isFalse(
bc?.controller instanceof TestController && bc.controller.disposed,
);
const c = bc?.controller as TestController;
assert.isFalse(c.disposed);
assert.isFalse(c.view.disposed);
bc?.blade.dispose();
assert.isTrue(
bc?.controller instanceof TestController && bc.controller.disposed,
);
assert.isTrue(c.disposed);
assert.isTrue(c.view.disposed);
});
});

0 comments on commit 860fec6

Please sign in to comment.