@@ -17,14 +17,14 @@ class SceneNode extends NativeFieldWrapperClass1 {
17
17
/// The asset must be a file produced as the output of the `scenec` importer.
18
18
/// The constructed object should then be reused via the [shader]
19
19
/// method to create [Shader] objects that can be used by [Shader.paint] .
20
- static Future < SceneNode > fromAsset (String assetKey) {
20
+ static SceneNodeValue fromAsset (String assetKey) {
21
21
// The flutter tool converts all asset keys with spaces into URI
22
22
// encoded paths (replacing ' ' with '%20', for example). We perform
23
23
// the same encoding here so that users can load assets with the same
24
24
// key they have written in the pubspec.
25
25
final String encodedKey = Uri (path: Uri .encodeFull (assetKey)).path;
26
26
{
27
- final Future < SceneNode > ? futureSceneNode = _ipsceneRegistry[encodedKey]? .target;
27
+ final SceneNodeValue ? futureSceneNode = _ipsceneRegistry[encodedKey]? .target;
28
28
if (futureSceneNode != null ) {
29
29
return futureSceneNode;
30
30
}
@@ -45,14 +45,15 @@ class SceneNode extends NativeFieldWrapperClass1 {
45
45
return null ;
46
46
}).then ((_) => sceneNode);
47
47
48
- _ipsceneRegistry[encodedKey] = WeakReference <Future <SceneNode >>(futureSceneNode);
49
- return futureSceneNode;
48
+ final SceneNodeValue result = SceneNodeValue .fromFuture (futureSceneNode);
49
+ _ipsceneRegistry[encodedKey] = WeakReference <SceneNodeValue >(result);
50
+ return result;
50
51
}
51
52
52
- static SceneNode fromTransform (Float64List matrix4) {
53
+ static SceneNodeValue fromTransform (Float64List matrix4) {
53
54
final SceneNode sceneNode = SceneNode ._create ();
54
55
sceneNode._initFromTransform (matrix4);
55
- return sceneNode;
56
+ return SceneNodeValue . fromValue ( sceneNode) ;
56
57
}
57
58
58
59
void addChild (SceneNode sceneNode) {
@@ -75,11 +76,11 @@ class SceneNode extends NativeFieldWrapperClass1 {
75
76
// SceneNode.fromAsset. It holds weak references to the SceneNodes so that the
76
77
// case where an in-use ipscene is requested again can be fast, but scenes
77
78
// that are no longer referenced are not retained because of the cache.
78
- static final Map <String , WeakReference <Future < SceneNode > >> _ipsceneRegistry =
79
- < String , WeakReference <Future < SceneNode > >> {};
79
+ static final Map <String , WeakReference <SceneNodeValue >> _ipsceneRegistry =
80
+ < String , WeakReference <SceneNodeValue >> {};
80
81
81
82
static Future <void > _reinitializeScene (String assetKey) async {
82
- final WeakReference <Future < SceneNode > >? sceneRef = _ipsceneRegistry == null
83
+ final WeakReference <SceneNodeValue >? sceneRef = _ipsceneRegistry == null
83
84
? null
84
85
: _ipsceneRegistry[assetKey];
85
86
@@ -89,7 +90,7 @@ class SceneNode extends NativeFieldWrapperClass1 {
89
90
return ;
90
91
}
91
92
92
- final Future <SceneNode >? sceneNodeFuture = sceneRef.target;
93
+ final Future <SceneNode >? sceneNodeFuture = sceneRef.target? .future ;
93
94
if (sceneNodeFuture == null ) {
94
95
return ;
95
96
}
@@ -129,6 +130,52 @@ class SceneNode extends NativeFieldWrapperClass1 {
129
130
SceneShader sceneShader () => SceneShader ._(this , debugName: _debugName);
130
131
}
131
132
133
+ class SceneNodeValue {
134
+ SceneNodeValue ._(this ._future, this ._value) {
135
+ _future? .then ((SceneNode result) => _value = result);
136
+ }
137
+
138
+ static SceneNodeValue fromFuture (Future <SceneNode > future) {
139
+ return SceneNodeValue ._(future, null );
140
+ }
141
+
142
+ static SceneNodeValue fromValue (SceneNode value) {
143
+ return SceneNodeValue ._(null , value);
144
+ }
145
+
146
+ final Future <SceneNode >? _future;
147
+ SceneNode ? _value;
148
+
149
+ bool get isComplete {
150
+ return _value != null ;
151
+ }
152
+
153
+ Future <SceneNode >? get future {
154
+ return _future;
155
+ }
156
+
157
+ SceneNode ? get value {
158
+ return _value;
159
+ }
160
+
161
+ /// Calls `callback` when the `SceneNode` has finished initializing. If the
162
+ /// initialization is already finished, `callback` is called synchronously.
163
+ SceneNodeValue whenComplete (void Function (SceneNode ) callback) {
164
+ if (_value == null && _future == null ) {
165
+ return this ;
166
+ }
167
+
168
+ if (_value != null ) {
169
+ callback (_value! );
170
+ return this ;
171
+ }
172
+
173
+ // _future != null
174
+ _future! .then ((SceneNode node) => callback (node));
175
+ return this ;
176
+ }
177
+ }
178
+
132
179
/// A [Shader] generated from a [SceneNode] .
133
180
///
134
181
/// Instances of this class can be obtained from the
0 commit comments