514p ~ 534p
μμΈκ³Ό ν¨κ³Όλ₯Ό λΆλ¦¬νμ¬ μ€κ³νλ€.
μμΈ(X)μ΄ λ°μνλ©΄ μΈμ λ ν¨κ³Ό(Y)λ₯Ό μΌμΌν¨λ€.
λ°μ΄ν° λ³ν λ¨κ³λ₯Ό νμ΄νλΌμΈμΌλ‘ ꡬμ±νμ¬ μ²λ¦¬νλ€.
κ³μ°μ μ‘°ν©νμ¬ λ³΅μ‘ν λμμ λ§λ€ μ μλ€.
μ½λμ κ²°ν©λμ μμ§λλ₯Ό κ΄λ¦¬νκΈ° μν΄ μ¬μ©νλ€.
λμΌν ν¨κ³Όλ₯Ό μ¬λ¬ κ΅°λ°μμ μ¬μ©νλ κ²½μ°, νλμ κ΅°λ°λ‘ κ²°ν©νμ¬ μ€λ³΅μ μ€μΌ μ μλ€.
μμΈκ³Ό ν¨κ³Όμ μ€μ¬μ λΆλ¦¬νμ¬ μ μ§λ³΄μμ±μ ν₯μμν¨λ€.
λ¨μΌ μ΄λ²€νΈ λμ λ°μ΄ν° μ€νΈλ¦Όμ μ¬μ©νλ κ²½μ°, RX(λ°μν νμ₯ λΌμ΄λΈλ¬λ¦¬)λ₯Ό νμ©ν μ μλ€.
μΈλΆ μ€νΈλ¦Ό μλΉμ€(Kafka, RabbitMQ λ±)λ₯Ό μ¬μ©νμ¬ νμ΄νλΌμΈμ ꡬνν μ μλ€.
function ValueCell(initialValue) {
let currentValue = initialValue;
return {
val: () => {
return currentValue;
},
update: (f) => {
const oldValue = currentValue;
const newValue = f(oldValue);
currentValue = newValue;
},
};
}
cell = ValueCell(1);
console.log(cell.val());
cell.update((x) => x + 1);
console.log(cell.val());
1
2
κ°μμ <Watcher, Observer, listener, eventHandler, callback>
function ValueCell(initialValue) {
let currentValue = initialValue;
let watchers = [];
return {
val: () => {
return currentValue;
},
update: (f) => {
const oldValue = currentValue;
const newValue = f(oldValue);
if (oldValue !== newValue) {
currentValue = newValue;
watchers.forEach((watcher) => watcher(newValue));
}
},
addWatcher: (f) => {
watchers.push(f);
},
};
}
cell = ValueCell(1);
console.log(cell.val());
cell.update((x) => x + 1);
console.log(cell.val());
cell.addWatcher((x) => console.log('watcher1', x));
cell.addWatcher((x) => console.log('watcher2', x));
cell.update((x) => x + 1);
1
2
watcher1 3
watcher2 3
FormulaCellμ νμλ κ°μ κ³μ°νλ€.
λ€λ₯Έμ
μ λ³νκ° κ°μ§λλ©΄ κ°μ λ€μ κ³μ°νλ€.
function ValueCell(initialValue) {
let currentValue = initialValue;
let watchers = [];
return {
val: () => {
return currentValue;
},
update: (f) => {
const oldValue = currentValue;
const newValue = f(oldValue);
if (oldValue !== newValue) {
currentValue = newValue;
watchers.forEach((watcher) => watcher(newValue));
}
},
addWatcher: (f) => {
watchers.push(f);
},
};
}
function FormulaCell(upstreamCell, f) {
//κ³μ°μ ν κ°μ μ΄κΈ°κ°μΌλ‘ μΈν
let myCell = ValueCell(f(upstreamCell.val()));
upstreamCell.addWatcher((newUpstreamValue) => {
myCell.update((currentValue) => {
return f(newUpstreamValue);
});
});
return {
val: myCell.val,
addWatcher: myCell.addWatcher,
};
}
// A1 μ
μ 2λ₯Ό μΈν
νκ³ , A2 μ
μ 3μ μΈν
ν©λλ€.
let A1 = ValueCell(2);
let A2 = FormulaCell(A1, (a) => a + 3);
console.log(A1.val()); // 2
console.log(A2.val()); // 5
// A1 μ
μ κ°μ 5λ‘ λ³κ²½ν©λλ€.
A1.update(() => 5);
console.log(A1.val()); // 5
console.log(A2.val()); // 8
// B1 μ
μ 4λ₯Ό μΈν
νκ³ , B2 μ
μ B1 μ
μ κ°μ 2λ₯Ό κ³±νλ κ³μ°μμ μ€μ ν©λλ€.
let B1 = ValueCell(4);
let B2 = FormulaCell(B1, (b) => b * 2);
console.log(B1.val()); // 4
console.log(B2.val()); // 8
// B1 μ
μ κ°μ 6μΌλ‘ λ³κ²½ν©λλ€.
B1.update(() => 6);
console.log(B1.val()); // 6
console.log(B2.val()); // 12
μ°¨νμ λ μ 리ν΄μΌ νλ€.
μ€μ€... λ²μ¨ λ€ μ½μ΄κ°λ€μ γ γ γ πππ