官术网_书友最值得收藏!

  • MobX Quick Start Guide
  • Pavan Podila Michel Weststrate
  • 411字
  • 2021-08-05 10:34:25

when()

As the name suggests, when() only executes the effect-function when a condition is met and automatically disposes the side-effect after that. Thus, when() is a one-time side-effect compared to autorun() and reaction(), which are long-running. The predicate function normally relies on some observables to do the conditional checks. If the observables change, the predicate function will be re-evaluated.

when() takes two arguments, which are as follows:

when(predicate-function, effect-function): disposer-function

predicate-function: () => boolean, effect-function: ()=>{}

The predicate function is expected to return a Boolean value. When it becomes true, the effect function is executed, and the when() is automatically disposed. Note that when() also gives you back a disposer function that you can call to prematurely cancel the side-effect.

In this following code block, we are monitoring the availability of an item and notifying the user when it is back in stock. This is the case of a one-time effect that you don't really have to continuously monitor. It's only when the item count in the inventory goes above zero, that you execute the side-effect of notifying the user. Take a look at this:

import { observable, action, when } from 'mobx';

class Inventory {
@observable items = [];

cancelTracker = null;

trackAvailability(name) {

// 1. Establish the tracker with when
this.cancelTracker = when(
() => {
const item = this.items.find(x => x.name === name);
return item ? item.quantity > 0 : false;
},
() => {
console.log(`${name} is now available`);
},
);
}

@action
addItem(name, quantity) {
const item = this.items.find(x => x.name === name);
if (item) {
item.quantity += quantity;
} else {
this.items.push({ name, quantity });
}
}
}

const inventory = new Inventory();

inventory.addItem('Shoes', 0);
inventory.trackAvailability('Shoes');

// 2. Add two pairs
inventory.addItem('Shoes', 2);

// 3. Add one more pair
inventory.addItem('Shoes', 1);

// Prints:
// Shoes is now available

when() here takes two arguments. The predicate function returns true when the item.quantity is greater than zero. The effect function simply notifies (via console.log) that the item is available in the store. When the predicate becomes true, when() executes the side-effect and automatically disposes itself. Thus, when we add two pairs of shoes into the inventory, when() executes and logs the availability.

Notice that when we add one more pair of shoes into the inventory, no logs are printed. This is because at this time when() has been disposed and is no longer monitoring the availability of Shoes. This is the one-time effect of when().

主站蜘蛛池模板: 灯塔市| 西乌珠穆沁旗| 漯河市| 定远县| 类乌齐县| 乐业县| 凯里市| 青铜峡市| 罗源县| 施甸县| 尉氏县| 木里| 永寿县| 镶黄旗| 大化| 疏勒县| 清新县| 双桥区| 武强县| 涟源市| 若尔盖县| 得荣县| 张家川| 亚东县| 唐海县| 手游| 治多县| 桦南县| 合肥市| 讷河市| 淳安县| 宝兴县| 梁山县| 石台县| 海宁市| 西华县| 吉安市| 闽侯县| 潜山县| 昌吉市| 荣昌县|