Tomasz Ducin
independent consultant, architect, developer, trainer, speaker #architecture #performance #javascript #typescript #react #angular
Independent Consultant & Software Architect
Trainer, Speaker, JS/TS Expert
ArchitekturaNaFroncie.pl (ANF)
Warsaw, PL
tomasz (at) ducin.dev
@tomasz_ducin
let rate = 3.94;
let amount = 1000;
let exchange = amount / rate; // 253.80
rate = 3.97;
exchange // OUT OF SYNC!
a condition which is true BEFORE and AFTER a change
let rate$ = new BehaviorSubject(3.94);
let amount$ = new BehaviorSubject(1000);
let exchange$ = combineLatest(
amount$, rate$,
(amount, rate) => amount / rate
);
rate$.next(3.97) // exchange -> 251.89
let rate = signal(3.94);
let amount = signal(1000);
let exchange = computed(
() => amount() / rate()); // 253.80
rate.set(3.97) // exchange() == 251.89
@Component({...})
class MyComponent {
private rate = signal(...)
private amount = signal(...)
public exchange = computed(...)
}
@Injectable({...})
class MyService {
private rate = signal(...)
private amount = signal(...)
public exchange = computed(...)
}
@Component({...})
class MyComponent {
constructor(
private myService: MyService
){}
}
class MySignalState {
private rate = signal(...)
private amount = signal(...)
public exchange = computed(...)
}
@Component({...})
class MyComponent {
state = new MySignalState()
}
@Injectable({...})
class MyService {
state = new MySignalState()
}
type LoadingState =
| { type: "LOADING" }
| { type: "DATA_LOADED", items: Entity[] }
| { type: "ERROR", error: Error }
class MySignalState {
#loadingSignal = signal<LoadingState>({ type: "LOADING" })
public state = this.#loadingSignal.asReadonly()
}
<div>
@if (state.type == "DATA_LOADED") {
<app-entity-list [items]="state.items" />
} @else if (state.type == "ERROR") {
<app-error-messsage [message]="state.error.message" />
} @else ...
</div>
sig = signal(t) // WritableSignal<T>
sig.set(t)
sig.update(oldT => newT)
sig.asReadonly() // Signal<T>
c = computed(() => sig())
e = effect(() => display(sig()))
// NO SUBSCRIBE, NO UNSUBSCRIBE
let rate = 3.94;
let amount = 1000;
let exchange = amount / rate; // 253.80
rate = 3.97;
exchange // OUT OF SYNC!
let rate$ = new BehaviorSubject(3.94);
let amount$ = new BehaviorSubject(1000);
let exchange$ = combineLatest(
amount$, rate$,
(amount, rate) => amount / rate
);
rate$.next(3.97) // exchange -> 251.89
let rate = signal(3.94);
let amount = signal(1000);
let exchange = computed(
() => amount() / rate()); // 253.80
rate.set(3.97) // exchange() == 251.89
By Tomasz Ducin
independent consultant, architect, developer, trainer, speaker #architecture #performance #javascript #typescript #react #angular