The server request data in the model

****** If the model, namespace, connect, dispatch, action, reducer which is the cornerstone of dva, dva essence that would reflect that effect. *******

About Effect and Generator function

So what effect is it in the end? effect is a noun dva context. And reducers Similarly, we can define a dva model in the effectsmembers.

Export default {
   namespace: 'some_namespace', State: {}, effects: { // definition of effects members 'someEffect': function * () {}, 'someOtherEffect': function * () {}, // ...} , the reducers: { // ...},} 

partial perspective effect is a one of the
generator function generator function . Macro perspective, effect is a layer of middleware.

After last chapter is action dispatch can directly reach reducer. To ensure the purity of the reducer, but at the same time be able to handle the side effects, it is necessary to break the "direct" this feature. effect acts as such an intermediate layer, when the action is dispatch, will first arrive effect treatment side effects, then the effect will eventually prompt new action sent out this new action may be another effect recapture continue processing, it may be reducer capture and ends, no matter what the final end processing logic will be reducer.

In the previous section, we know that the structure is action.type namespace 名称+ /+ reducer 名称, in fact action.type can be namespace 名称+ /+ effect 名称. For the view layer is concerned, in fact, can not detect the difference between the effect and reducer. View layer by just want to describe what action, as after this action is not directly perceived by reducer treatment or reducer effect then, the view layer, should not be concerned. So we do separate data processing logic and view logic.

Generator function

Let us explain why the generator function can be used to handle asynchronous logic. In fact generator function processing patent asynchronous logic is not dva in many js frameworks are used, and most notably co. Use handle asynchronous generator function is not characteristic of the indiscriminate use of language, but that generator function naturally would have the characteristics of asynchronous processing. In a typical effect dva wording is:

the getData: function * ({ payload}, { Call, PUT}) { const Data = the yield Call ( SomeService. getEndpointData, payload, 'maybeSomeOtherParams'); the yield PUT ({ type: 'getData_success', payload: Data});}

Let me talk Conclusion: when the generator function is executed, the execution of the process appears to be synchronized! There are two parameters into objects, the first object is to match the objects of this action effect, it can be agreed to take the payload field, the second object is the effect primitives,
wherein call, putthe most commonly used. into the reference generator function are the two objects at run time by the dva injected into the generator function. callIs actually a function, and yieldkey processing with the use of asynchronous logic , Call first parameter is a function, the function returns required Promise,
After the reference parameter is the time of the function call. yield call after call blocking, the Promise is resolved, and the results of the asynchronous call, the data is stored, and then the program can continue. See the following line and execute the put.
putIt is also a function, put and yieldused in conjunction with a distribution to action, and dispatch functions exactly the same !
Just use it in effect function.

Note: The yield putdistribution of action if it is to trigger other effect of the same model / reducer execution, do not need to specify the namespace name.

 The essence is asynchronous events to promote the implementation of the program point jump back and forth. We use the callback is essentially a means of describing jump. generator function is no essential change asynchronous except for changing the manner described, such as the program looks like synchronization.

There is a generator function in the implementation of the two parties . One side is the generator function itself, the other is the generator function handle holders, which are generally held by the frame. Let us call this handle is genStub. When the framework calls genStub.next (), generator function is executed to the next yieldand then pause, and to calculate the yield value of the expression later returned to the frame, while the implementation of the right to the framework program. Framework do get the value of the deal, such as is the asynchronous processing, the process ends to get the result, calling genStub.next again (), the return value to the generator function simultaneously drive it to resume execution. When resumes, you may think the results are returned will be replaced as a wholeyield <expression> , then the program continues to the next yield.

 

yield the word used here in particular image: yield itself has a "concession" means, but also the "output" means.

 

"Generator function yield to external value" and "return to the external value of the generator function is" not the same thing! ! !

 

generator function defines the process and want to do reporting in each node yield. But the real implementation of asynchronous logic by the generator function handle on behalf of holders of execution. Corresponds to a dva is the same. Do take the call example, call is actually a particularly simple function . The return value of a call just plain javascript object:

 

{
  CALL: {
    fn: SomeService.getEndpointData,
    args: [payload, 'maybeSomeOtherParams'] } }

 

We call to describe a dva want to do: Please help me perform this function, informed me parsed Promise continues, and the Promise of the analytical value returned to me.

 











Guess you like

Origin www.cnblogs.com/YbchengXumin/p/11240464.html