Skip to content

Commit

Permalink
fix(io): simplify IoService to have just one method for updates
Browse files Browse the repository at this point in the history
This will also decrease API surface area and reduce BC chances
  • Loading branch information
gund committed Aug 27, 2022
1 parent 6e0a6ff commit 21b0e20
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 53 deletions.
Expand Up @@ -164,7 +164,6 @@ export class DynamicDirectivesDirective implements OnDestroy, DoCheck {
private updateDirective(dirDef: DynamicDirectiveDef<unknown>) {
const io = this.dirIo.get(dirDef.type);
io.update(dirDef.inputs, dirDef.outputs);
io.maybeUpdate();
}

private initDirective(
Expand Down
@@ -1,4 +1,4 @@
import { Directive, DoCheck, Input, OnChanges } from '@angular/core';
import { Directive, DoCheck, Input } from '@angular/core';

import { InputsType, IoService, IoServiceProvider, OutputsType } from '../io';

Expand All @@ -9,7 +9,7 @@ import { InputsType, IoService, IoServiceProvider, OutputsType } from '../io';
'[ndcDynamicInputs],[ndcDynamicOutputs],[ngComponentOutletNdcDynamicInputs],[ngComponentOutletNdcDynamicOutputs]',
providers: [IoServiceProvider],
})
export class DynamicIoDirective implements OnChanges, DoCheck {
export class DynamicIoDirective implements DoCheck {
@Input()
ndcDynamicInputs: InputsType;
@Input()
Expand All @@ -29,11 +29,7 @@ export class DynamicIoDirective implements OnChanges, DoCheck {

constructor(private ioService: IoService) {}

ngOnChanges() {
this.ioService.update(this.inputs, this.outputs);
}

ngDoCheck() {
this.ioService.maybeUpdate();
this.ioService.update(this.inputs, this.outputs);
}
}
53 changes: 8 additions & 45 deletions projects/ng-dynamic-component/src/lib/io/io.service.ts
Expand Up @@ -132,67 +132,30 @@ export class IoService implements OnDestroy {
this.disconnectOutputs();
}

/**
* Call when you know that inputs/outputs were changed
* or when setting them for the first time
*/
update(inputs: InputsType, outputs: OutputsType) {
if (!this.compRef) {
this.disconnectOutputs();
return;
}

const changes = this.updateIO(inputs, outputs);

const compChanged = this.componentInstChanged;

if (compChanged || changes.inputsChanged) {
const inputsChanges = this.getInputsChanges();
if (inputsChanges) {
this.updateChangedInputs(inputsChanges);
}
this.updateInputs(compChanged || !this.lastChangedInputs.size);
}

if (compChanged || changes.outputsChanged) {
this.bindOutputs();
}
}
const inputsChanges = this.getInputsChanges();
const outputsChanged = this.outputsChanged(this.outputs);

/**
* Call when you do not know if inputs/outputs changed
*
* Usually must be called from the `DoCheck` lifecycle hook
*/
maybeUpdate() {
if (!this.compRef) {
this.disconnectOutputs();
return;
if (inputsChanges) {
this.updateChangedInputs(inputsChanges);
}

if (this.componentInstChanged) {
this.updateInputs(true);
this.bindOutputs();
return;
if (compChanged || inputsChanges) {
this.updateInputs(compChanged || !this.lastChangedInputs.size);
}

if (this.outputsChanged(this.outputs)) {
if (compChanged || outputsChanged || changes.outputsChanged) {
this.bindOutputs();
}

if (!this.inputs) {
return;
}

const inputsChanges = this.getInputsChanges();

if (inputsChanges) {
const isNotFirstChange = !!this.lastChangedInputs.size;
this.updateChangedInputs(inputsChanges);

if (isNotFirstChange) {
this.updateInputs();
}
}
}

private updateIO(inputs: InputsType, outputs: OutputsType) {
Expand Down

0 comments on commit 21b0e20

Please sign in to comment.