Skip to content

Commit b3286e1

Browse files
committed
Added react internals
1 parent 37e4519 commit b3286e1

File tree

4 files changed

+273
-1
lines changed

4 files changed

+273
-1
lines changed

src/lib/react/React.hx

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package react;
22

3+
import js.Symbol;
34
import react.ReactComponent.ReactElement;
45
import react.ReactComponent.ReactFragment;
56
import react.ReactComponent.ReactSingleFragment;
@@ -55,6 +56,14 @@ extern class React
5556
public static var Children:ReactChildren;
5657

5758
public static var version:String;
59+
60+
public static var Fragment:Symbol;
61+
public static var StrictMode:Symbol;
62+
public static var unstable_AsyncMode:Symbol;
63+
public static var unstable_Profiler:Symbol;
64+
65+
@:native('__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED')
66+
public static var _internals:ReactSharedInternals;
5867
}
5968

6069
/**

src/lib/react/ReactComponent.hx

+13-1
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,23 @@ extern class ReactComponentOf<TProps, TState>
103103
#end
104104
}
105105

106+
typedef ReactSource = {
107+
fileName:String,
108+
lineNumber:Int
109+
}
110+
106111
typedef ReactElement = {
107112
type:Dynamic,
108113
props:Dynamic,
109114
?key:Dynamic,
110-
?ref:Dynamic
115+
?ref:Dynamic,
116+
?_owner:Dynamic,
117+
118+
#if debug
119+
?_store:{validated:Bool},
120+
?_shadowChildren:Dynamic,
121+
?_source:ReactSource,
122+
#end
111123
}
112124

113125
typedef ReactSingleFragment = EitherType<

src/lib/react/ReactContext.hx

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package react;
2+
3+
extern interface ReactContext<T>
4+
{
5+
var Consumer:ReactContext<T>;
6+
var Provider:ReactProviderType<T>;
7+
8+
var unstable_read:Void->T;
9+
var _calculateChangedBits:Null<T->T->Int>;
10+
11+
var _currentValue:T;
12+
var _currentValue2:T;
13+
14+
#if debug
15+
@:optional var _currentRenderer:Null<Dynamic>;
16+
@:optional var _currentRenderer2:Null<Dynamic>;
17+
#end
18+
}
19+
20+
extern interface ReactProviderType<T>
21+
{
22+
var _context:ReactContext<T>;
23+
}

src/lib/react/ReactSharedInternals.hx

+228
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
package react;
2+
3+
import react.ReactComponent.ReactSource;
4+
5+
extern interface ReactSharedInternals
6+
{
7+
var ReactCurrentOwner:{
8+
current: Null<ReactFiber>,
9+
currentDispatcher: Null<ReactDispatcher>
10+
};
11+
12+
#if debug
13+
var ReactDebugCurrentFrame:{
14+
getCurrentStack:Null<Void->String>,
15+
getStackAddendum:Void->String
16+
};
17+
#end
18+
}
19+
20+
/**
21+
https://github.com/facebook/react/blob/master/packages/shared/ReactWorkTags.js
22+
**/
23+
@:enum abstract WorkTag(Int) from Int to Int
24+
{
25+
var FunctionalComponent = 0;
26+
var FunctionalComponentLazy = 1;
27+
var ClassComponent = 2;
28+
var ClassComponentLazy = 3;
29+
var IndeterminateComponent = 4; // Before we know whether it is functional or class
30+
var HostRoot = 5; // Root of a host tree. Could be nested inside another node.
31+
var HostPortal = 6; // A subtree. Could be an entry point to a different renderer.
32+
var HostComponent = 7;
33+
var HostText = 8;
34+
var Fragment = 9;
35+
var Mode = 10;
36+
var ContextConsumer = 11;
37+
var ContextProvider = 12;
38+
var ForwardRef = 13;
39+
var ForwardRefLazy = 14;
40+
var Profiler = 15;
41+
var PlaceholderComponent = 16;
42+
}
43+
44+
extern interface Instance {
45+
// Tag identifying the type of fiber.
46+
var tag:WorkTag;
47+
48+
// Unique identifier of this child.
49+
var key:Null<String>;
50+
51+
// The function/class/module associated with this fiber.
52+
var type:Any;
53+
54+
// The local state associated with this fiber.
55+
var stateNode:Any;
56+
}
57+
58+
extern interface ReactFiber extends Instance {
59+
60+
// The Fiber to return to after finishing processing this one.
61+
// This is effectively the parent, but there can be multiple parents (two)
62+
// so this is only the parent of the thing we're currently processing.
63+
// It is conceptually the same as the return address of a stack frame.
64+
@:native("return")
65+
var _return:Null<ReactFiber>;
66+
67+
// Singly Linked List Tree Structure.
68+
var child:Null<ReactFiber>;
69+
var sibling:Null<ReactFiber>;
70+
var index:Int;
71+
72+
// The ref last used to attach this node.
73+
// I'll avoid adding an owner field for prod and model that as functions.
74+
// ref: null | (((handle: mixed) => void) & {_stringRef: ?string}) | RefObject,
75+
var ref:Null<Dynamic>;
76+
77+
// Input is the data coming into process this fiber. Arguments. Props.
78+
var pendingProps:Any; // This type will be more specific once we overload the tag.
79+
var memoizedProps:Any; // The props used to create the output.
80+
81+
// A queue of state updates and callbacks.
82+
var updateQueue:Null<UpdateQueue<Any>>;
83+
84+
// The state used to create the output
85+
var memoizedState:Any;
86+
87+
// A linked-list of contexts that this fiber depends on
88+
var firstContextDependency:Null<ContextDependency<Dynamic>>;
89+
90+
// Bitfield that describes properties about the fiber and its subtree. E.g.
91+
// the AsyncMode flag indicates whether the subtree should be async-by-
92+
// default. When a fiber is created, it inherits the mode of its
93+
// parent. Additional flags can be set at creation time, but after that the
94+
// value should remain unchanged throughout the fiber's lifetime, particularly
95+
// before its child fibers are created.
96+
var mode:TypeOfMode;
97+
98+
// Effect
99+
var effectTag:SideEffectTag;
100+
101+
// Singly linked list fast path to the next fiber with side-effects.
102+
var nextEffect:Null<ReactFiber>;
103+
104+
// The first and last fiber with side-effect within this subtree. This allows
105+
// us to reuse a slice of the linked list when we reuse the work done within
106+
// this fiber.
107+
var firstEffect:Null<ReactFiber>;
108+
var lastEffect:Null<ReactFiber>;
109+
110+
// Represents a time in the future by which this work should be completed.
111+
// Does not include work found in its subtree.
112+
var expirationTime:Float;
113+
114+
// This is used to quickly determine if a subtree has no pending changes.
115+
var childExpirationTime:Float;
116+
117+
// This is a pooled version of a Fiber. Every fiber that gets updated will
118+
// eventually have a pair. There are cases when we can clean up pairs to save
119+
// memory if we need to.
120+
var alternate:Null<ReactFiber>;
121+
122+
// Time spent rendering this Fiber and its descendants for the current update.
123+
// This tells us how well the tree makes use of sCU for memoization.
124+
// It is reset to 0 each time we render and only updated when we don't bailout.
125+
// This field is only set when the enableProfilerTimer flag is enabled.
126+
@:optional var actualDuration:Float;
127+
128+
// If the Fiber is currently active in the "render" phase,
129+
// This marks the time at which the work began.
130+
// This field is only set when the enableProfilerTimer flag is enabled.
131+
@:optional var actualStartTime:Float;
132+
133+
// Duration of the most recent render time for this Fiber.
134+
// This value is not updated when we bailout for memoization purposes.
135+
// This field is only set when the enableProfilerTimer flag is enabled.
136+
@:optional var selfBaseDuration:Float;
137+
138+
// Sum of base times for all descedents of this Fiber.
139+
// This value bubbles up during the "complete" phase.
140+
// This field is only set when the enableProfilerTimer flag is enabled.
141+
@:optional var treeBaseDuration:Float;
142+
143+
#if debug
144+
@:optional var debugID:Float;
145+
@:optional var debugSource:Null<ReactSource>;
146+
@:optional var debugOwner:Null<ReactFiber>;
147+
@:optional var debugIsCurrentlyTiming:Bool;
148+
#end
149+
}
150+
151+
extern interface Update<State>
152+
{
153+
var expirationTime:Float;
154+
155+
var tag:UpdateTag;
156+
var payload:Any;
157+
var callback:Null<Void->Dynamic>;
158+
159+
var next:Null<Update<State>>;
160+
var nextEffect:Null<Update<State>>;
161+
}
162+
163+
extern interface UpdateQueue<State>
164+
{
165+
var baseState:State;
166+
167+
var firstUpdate:Null<Update<State>>;
168+
var lastUpdate:Null<Update<State>>;
169+
170+
var firstCapturedUpdate:Null<Update<State>>;
171+
var lastCapturedUpdate:Null<Update<State>>;
172+
173+
var firstEffect:Null<Update<State>>;
174+
var lastEffect:Null<Update<State>>;
175+
176+
var firstCapturedEffect:Null<Update<State>>;
177+
var lastCapturedEffect:Null<Update<State>>;
178+
}
179+
180+
@:enum abstract UpdateTag(Int) from Int to Int
181+
{
182+
var UpdateState = 0;
183+
var ReplaceState = 1;
184+
var ForceUpdate = 2;
185+
var CaptureUpdate = 3;
186+
}
187+
188+
@:enum abstract TypeOfMode(Int) from Int to Int
189+
{
190+
var NoContext = 0;
191+
var AsyncMode = 1;
192+
var StrictMode = 2;
193+
var ProfileMode = 3;
194+
}
195+
196+
@:enum abstract SideEffectTag(Int) from Int to Int
197+
{
198+
var NoEffect = 0;
199+
var PerformedWork = 1;
200+
201+
var Placement = 2;
202+
var Update = 4;
203+
var PlacementAndUpdate = Placement & Update;
204+
var Deletion = 8;
205+
var ContentReset = 16;
206+
var Callback = 32;
207+
var DidCapture = 64;
208+
var Ref = 128;
209+
var Snapshot = 256;
210+
var LifecycleEffectMask = Update & Callback & Ref & Snapshot;
211+
212+
// Union of all host effects
213+
var HostEffectMask = 511;
214+
215+
var Incomplete = 512;
216+
var ShouldCapture = 1024;
217+
}
218+
219+
extern interface ContextDependency<T>
220+
{
221+
var context:ReactContext<T>;
222+
var observedBits:Int;
223+
var next:Null<ContextDependency<Any>>;
224+
}
225+
226+
typedef ReactDispatcher = {
227+
var readContext:haxe.Constraints.Function;
228+
}

0 commit comments

Comments
 (0)