UNPKG

2.52 kBJavaScriptView Raw
1/**
2 * Copyright (c) 2013-present, Facebook, Inc.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 *
8 */
9
10'use strict';
11
12var ReactTypeOfWork = require('./ReactTypeOfWork');
13var ClassComponent = ReactTypeOfWork.ClassComponent,
14 HostContainer = ReactTypeOfWork.HostContainer,
15 HostComponent = ReactTypeOfWork.HostComponent;
16
17var _require = require('./ReactFiberUpdateQueue'),
18 callCallbacks = _require.callCallbacks;
19
20module.exports = function (config) {
21 var updateContainer = config.updateContainer;
22 var commitUpdate = config.commitUpdate;
23
24 function commitWork(current, finishedWork) {
25 switch (finishedWork.tag) {
26 case ClassComponent:
27 {
28 // Clear updates from current fiber. This must go before the callbacks
29 // are reset, in case an update is triggered from inside a callback. Is
30 // this safe? Relies on the assumption that work is only committed if
31 // the update queue is empty.
32 if (finishedWork.alternate) {
33 finishedWork.alternate.updateQueue = null;
34 }
35 if (finishedWork.callbackList) {
36 var callbackList = finishedWork.callbackList;
37
38 finishedWork.callbackList = null;
39 callCallbacks(callbackList, finishedWork.stateNode);
40 }
41 // TODO: Fire componentDidMount/componentDidUpdate, update refs
42 return;
43 }
44 case HostContainer:
45 {
46 // TODO: Attach children to root container.
47 var children = finishedWork.output;
48 var root = finishedWork.stateNode;
49 var containerInfo = root.containerInfo;
50 updateContainer(containerInfo, children);
51 return;
52 }
53 case HostComponent:
54 {
55 if (finishedWork.stateNode == null || !current) {
56 throw new Error('This should only be done during updates.');
57 }
58 // Commit the work prepared earlier.
59 var child = finishedWork.child;
60 var _children = child && !child.sibling ? child.output : child;
61 var newProps = finishedWork.memoizedProps;
62 var oldProps = current.memoizedProps;
63 var instance = finishedWork.stateNode;
64 commitUpdate(instance, oldProps, newProps, _children);
65 return;
66 }
67 default:
68 throw new Error('This unit of work tag should not have side-effects.');
69 }
70 }
71
72 return {
73 commitWork: commitWork
74 };
75};
\No newline at end of file