浏览代码

fix name space

/main
siyao 3 年前
当前提交
c9e46c47
共有 2 个文件被更改,包括 329 次插入328 次删除
  1. 181
      com.unity.uiwidgets/Runtime/async/async_cast.cs
  2. 476
      com.unity.uiwidgets/Runtime/widgets/async.cs

181
com.unity.uiwidgets/Runtime/async/async_cast.cs


using System;
using System.Data.Common;
using System.Runtime.Versioning;
namespace Unity.UIWidgets.async {
public class CastStream<S, T> : Stream<T> {
readonly Stream<S> _source;
public class CastStream<S, T> : Stream<T> {
readonly Stream<S> _source;
public CastStream(Stream<S> _source) {
this._source = _source;
}
public CastStream(Stream<S> _source) {
this._source = _source;
}
bool isBroadcast {
get { return _source.isBroadcast; }
}
bool isBroadcast {
get { return _source.isBroadcast; }
}
public override StreamSubscription<T> listen(Action<T> onData, Action<object, string> onError = null,
Action onDone = null, bool cancelOnError = false) {
var result = new CastStreamSubscription<S, T>(
_source.listen(null, onDone: onDone, cancelOnError: cancelOnError));
public override StreamSubscription<T> listen(Action<T> onData, Action<object, string> onError = null,
Action onDone = null, bool cancelOnError = false) {
var result = new CastStreamSubscription<S, T>(
_source.listen(null, onDone: onDone, cancelOnError: cancelOnError));
result.onData(onData);
result.onError(onError);
return result;
}
result.onData(onData);
result.onError(onError);
return result;
Stream<R> cast<R>() where R : class => new CastStream<S, R>(_source);
Stream<R> cast<R>() where R : class => new CastStream<S, R>(_source);
}
class CastStreamSubscription<S, T> : StreamSubscription<T> {
readonly StreamSubscription<S> _source;
class CastStreamSubscription<S, T> : StreamSubscription<T> {
readonly StreamSubscription<S> _source;
/// Zone where listen was called.
readonly Zone _zone = Zone.current;
/// Zone where listen was called.
readonly Zone _zone = Zone.current;
/// User's data handler. May be null.
ZoneUnaryCallback _handleData;
/// User's data handler. May be null.
ZoneUnaryCallback _handleData;
/// Copy of _source's handleError so we can report errors in onData.
/// May be null.
ZoneBinaryCallback _handleError;
/// Copy of _source's handleError so we can report errors in onData.
/// May be null.
ZoneBinaryCallback _handleError;
public CastStreamSubscription(StreamSubscription<S> _source) {
this._source = _source;
_source.onData(_onData);
}
public CastStreamSubscription(StreamSubscription<S> _source) {
this._source = _source;
_source.onData(_onData);
}
public override Future cancel() => _source.cancel();
public override Future cancel() => _source.cancel();
public override void onData(Action<T> handleData) {
_handleData = handleData == null
? null
: _zone.registerUnaryCallback(data => {
handleData((T) data);
return null;
});
}
public override void onError(Action<object, string> handleError) {
_source.onError(handleError);
if (handleError == null) {
_handleError = null;
}
else {
_handleError = _zone
.registerBinaryCallback((a, b) => {
handleError(a, (string) b);
public override void onData(Action<T> handleData) {
_handleData = handleData == null
? null
: _zone.registerUnaryCallback(data => {
handleData((T) data);
}
public override void onDone(Action handleDone) {
_source.onDone(handleDone);
}
public override void onError(Action<object, string> handleError) {
_source.onError(handleError);
if (handleError == null) {
_handleError = null;
}
else {
_handleError = _zone
.registerBinaryCallback((a, b) => {
handleError(a, (string) b);
return null;
});
}
}
void _onData(S data) {
if (_handleData == null) return;
T targetData;
try {
// siyao: this might go wrong
targetData = (T) (object) data;
public override void onDone(Action handleDone) {
_source.onDone(handleDone);
catch (Exception error) {
if (_handleError == null) {
_zone.handleUncaughtError(error);
void _onData(S data) {
if (_handleData == null) return;
T targetData;
try {
// siyao: this might go wrong
targetData = (T) (object) data;
else {
_zone.runBinaryGuarded(_handleError, error, error.StackTrace);
catch (Exception error) {
if (_handleError == null) {
_zone.handleUncaughtError(error);
}
else {
_zone.runBinaryGuarded(_handleError, error, error.StackTrace);
}
return;
return;
_zone.runUnaryGuarded(_handleData, targetData);
_zone.runUnaryGuarded(_handleData, targetData);
}
public override void pause(Future resumeSignal = null) {
_source.pause(resumeSignal);
}
public override void pause(Future resumeSignal = null) {
_source.pause(resumeSignal);
}
public override void resume() {
_source.resume();
}
bool isPaused {
get { return _source.isPaused; }
}
public override void resume() {
_source.resume();
public override Future<E> asFuture<E>(E futureValue) => _source.asFuture<E>(futureValue);
bool isPaused {
get { return _source.isPaused; }
}
public override Future<E> asFuture<E>(E futureValue) => _source.asFuture<E>(futureValue);
}
class CastStreamTransformer<SS, ST, TS, TT>
: StreamTransformerBase<TS, TT> where TT : class where ST : class {
public readonly StreamTransformer<SS, ST> _source;
public CastStreamTransformer(StreamTransformer<SS, ST> _source) {
this._source = _source;
}
class CastStreamTransformer<SS, ST, TS, TT>
: StreamTransformerBase<TS, TT> where TT : class where ST : class {
public readonly StreamTransformer<SS, ST> _source;
public override StreamTransformer<RS, RT> cast<RS, RT>() =>
new CastStreamTransformer<SS, ST, RS, RT>(_source);
public CastStreamTransformer(StreamTransformer<SS, ST> _source) {
this._source = _source;
public override Stream<TT> bind(Stream<TS> stream) =>
_source.bind(stream.cast<SS>()).cast<TT>();
public override StreamTransformer<RS, RT> cast<RS, RT>() =>
new CastStreamTransformer<SS, ST, RS, RT>(_source);
public override Stream<TT> bind(Stream<TS> stream) =>
_source.bind(stream.cast<SS>()).cast<TT>();
}

476
com.unity.uiwidgets/Runtime/widgets/async.cs


using System.Collections.Generic;
using Unity.UIWidgets.async;
using Unity.UIWidgets.foundation;
using Unity.UIWidgets.widgets;
namespace Unity.UIWidgets.widgets {
public abstract class StreamBuilderBase<T, S> : StatefulWidget {
public StreamBuilderBase(Key key = null, Stream<T> stream = null) : base(key: key) {
this.stream = stream;
}
public abstract class StreamBuilderBase<T, S> : StatefulWidget {
public StreamBuilderBase(Key key = null, Stream<T> stream = null) : base(key: key) {
this.stream = stream;
}
public readonly Stream<T> stream;
public readonly Stream<T> stream;
public abstract S initial();
public abstract S initial();
public virtual S afterConnected(S current) => current;
public virtual S afterConnected(S current) => current;
public abstract S afterData(S current, T data);
public abstract S afterData(S current, T data);
public virtual S afterError(S current, object error) => current;
public virtual S afterError(S current, object error) => current;
public virtual S afterDone(S current) => current;
public virtual S afterDone(S current) => current;
public virtual S afterDisconnected(S current) => current;
public virtual S afterDisconnected(S current) => current;
public abstract Widget build(BuildContext context, S currentSummary);
public abstract Widget build(BuildContext context, S currentSummary);
public override State createState() => new _StreamBuilderBaseState<T, S>();
}
public override State createState() => new _StreamBuilderBaseState<T, S>();
}
class _StreamBuilderBaseState<T, S> : State<StreamBuilderBase<T, S>> {
StreamSubscription<T> _subscription;
S _summary;
class _StreamBuilderBaseState<T, S> : State<StreamBuilderBase<T, S>> {
StreamSubscription<T> _subscription;
S _summary;
public override void initState() {
base.initState();
_summary = widget.initial();
_subscribe();
}
public override void initState() {
base.initState();
_summary = widget.initial();
_subscribe();
}
public override void didUpdateWidget(StatefulWidget statefulWidget) {
StreamBuilderBase<T, S> oldWidget = statefulWidget as StreamBuilderBase<T, S>;
if (oldWidget == null) {
return;
}
public override void didUpdateWidget(StatefulWidget statefulWidget) {
StreamBuilderBase<T, S> oldWidget = statefulWidget as StreamBuilderBase<T, S>;
if (oldWidget == null) {
return;
}
base.didUpdateWidget(statefulWidget);
if (oldWidget != null) {
if (oldWidget.stream != widget.stream) {
if (_subscription != null) {
_unsubscribe();
_summary = widget.afterDisconnected(_summary);
}
base.didUpdateWidget(statefulWidget);
if (oldWidget != null) {
if (oldWidget.stream != widget.stream) {
if (_subscription != null) {
_unsubscribe();
_summary = widget.afterDisconnected(_summary);
_subscribe();
_subscribe();
}
public override Widget build(BuildContext context) => widget.build(context, _summary);
public override Widget build(BuildContext context) => widget.build(context, _summary);
public override void dispose() {
_unsubscribe();
base.dispose();
}
public override void dispose() {
_unsubscribe();
base.dispose();
}
void _subscribe() {
if (widget.stream != null) {
_subscription = widget.stream.listen(
(T data) => { setState(() => { _summary = widget.afterData(_summary, data); }); },
onError: (object error, string stackTrace) => { setState(() => { _summary = widget.afterError(_summary, error); }); },
onDone: () => { setState(() => { _summary = widget.afterDone(_summary); }); });
_summary = widget.afterConnected(_summary);
void _subscribe() {
if (widget.stream != null) {
_subscription = widget.stream.listen(
(T data) => { setState(() => { _summary = widget.afterData(_summary, data); }); },
onError: (object error, string stackTrace) => {
setState(() => { _summary = widget.afterError(_summary, error); });
},
onDone: () => { setState(() => { _summary = widget.afterDone(_summary); }); });
_summary = widget.afterConnected(_summary);
}
}
void _unsubscribe() {
if (_subscription != null) {
_subscription.cancel();
_subscription = null;
void _unsubscribe() {
if (_subscription != null) {
_subscription.cancel();
_subscription = null;
}
}
public enum ConnectionState {
none,
public enum ConnectionState {
none,
waiting,
waiting,
active,
active,
done,
}
done,
}
public class AsyncSnapshot<T> : IEquatable<AsyncSnapshot<T>> {
AsyncSnapshot(ConnectionState connectionState, object data, object error) {
D.assert(connectionState != null);
D.assert(!(data != null && error != null));
this.connectionState = connectionState;
this.data = (T) data;
this.error = error;
}
public class AsyncSnapshot<T> : IEquatable<AsyncSnapshot<T>> {
AsyncSnapshot(ConnectionState connectionState, object data, object error) {
D.assert(connectionState != null);
D.assert(!(data != null && error != null));
this.connectionState = connectionState;
this.data = (T) data;
this.error = error;
}
public static AsyncSnapshot<object> nothing() {
return new AsyncSnapshot<object>(ConnectionState.none, null, null);
}
public static AsyncSnapshot<object> nothing() {
return new AsyncSnapshot<object>(ConnectionState.none, null, null);
}
public static AsyncSnapshot<T> withData(ConnectionState state, T data) {
return new AsyncSnapshot<T>(state, data, null);
}
public static AsyncSnapshot<T> withData(ConnectionState state, T data) {
return new AsyncSnapshot<T>(state, data, null);
}
public static AsyncSnapshot<T> withError(ConnectionState state, object error) {
return new AsyncSnapshot<T>(state, null, error);
}
public static AsyncSnapshot<T> withError(ConnectionState state, object error) {
return new AsyncSnapshot<T>(state, null, error);
}
public readonly ConnectionState connectionState;
public readonly ConnectionState connectionState;
public readonly T data;
public readonly T data;
public T requireData {
get {
if (hasData)
return data;
if (hasError)
//TODO: not sure if cast works
throw (Exception) error;
throw new Exception("Snapshot has neither data nor error");
public T requireData {
get {
if (hasData)
return data;
if (hasError)
//TODO: not sure if cast works
throw (Exception) error;
throw new Exception("Snapshot has neither data nor error");
}
}
public readonly object error;
public readonly object error;
public AsyncSnapshot<T> inState(ConnectionState state) {
return new AsyncSnapshot<T>(state, data, error);
}
public AsyncSnapshot<T> inState(ConnectionState state) {
return new AsyncSnapshot<T>(state, data, error);
}
public bool hasData {
get => data != null;
}
public bool hasError {
get => error != null;
}
public bool hasData {
get => data != null;
}
public override string ToString() =>
$"{foundation_.objectRuntimeType(this, "AsyncSnapshot")}({connectionState}, {data}, {error})";
public bool hasError {
get => error != null;
}
public static bool operator ==(AsyncSnapshot<T> left, AsyncSnapshot<T> right) {
return Equals(left, right);
}
public override string ToString() =>
$"{foundation_.objectRuntimeType(this, "AsyncSnapshot")}({connectionState}, {data}, {error})";
public static bool operator !=(AsyncSnapshot<T> left, AsyncSnapshot<T> right) {
return !Equals(left, right);
}
public bool Equals(AsyncSnapshot<T> other) {
if (ReferenceEquals(null, other)) {
return false;
public static bool operator ==(AsyncSnapshot<T> left, AsyncSnapshot<T> right) {
return Equals(left, right);
if (ReferenceEquals(this, other)) {
return true;
public static bool operator !=(AsyncSnapshot<T> left, AsyncSnapshot<T> right) {
return !Equals(left, right);
return connectionState == other.connectionState && EqualityComparer<T>.Default.Equals(data, other.data) &&
Equals(error, other.error);
}
public bool Equals(AsyncSnapshot<T> other) {
if (ReferenceEquals(null, other)) {
return false;
}
public override bool Equals(object obj) {
if (ReferenceEquals(null, obj)) {
return false;
}
if (ReferenceEquals(this, other)) {
return true;
}
if (ReferenceEquals(this, obj)) {
return true;
return connectionState == other.connectionState && EqualityComparer<T>.Default.Equals(data, other.data) &&
Equals(error, other.error);
if (obj.GetType() != GetType()) {
return false;
}
public override bool Equals(object obj) {
if (ReferenceEquals(null, obj)) {
return false;
}
return Equals((AsyncSnapshot<T>) obj);
}
if (ReferenceEquals(this, obj)) {
return true;
}
public override int GetHashCode() {
unchecked {
var hashCode = (int) connectionState;
hashCode = (hashCode * 397) ^ EqualityComparer<T>.Default.GetHashCode(data);
hashCode = (hashCode * 397) ^ (error != null ? error.GetHashCode() : 0);
return hashCode;
if (obj.GetType() != GetType()) {
return false;
}
return Equals((AsyncSnapshot<T>) obj);
}
public override int GetHashCode() {
unchecked {
var hashCode = (int) connectionState;
hashCode = (hashCode * 397) ^ EqualityComparer<T>.Default.GetHashCode(data);
hashCode = (hashCode * 397) ^ (error != null ? error.GetHashCode() : 0);
return hashCode;
}
}
public static partial class _async {
public delegate Widget AsyncWidgetBuilder<T>(BuildContext context, AsyncSnapshot<T> snapshot);
}
public static partial class _async {
public delegate Widget AsyncWidgetBuilder<T>(BuildContext context, AsyncSnapshot<T> snapshot);
}
public class StreamBuilder<T> : StreamBuilderBase<T, AsyncSnapshot<T>> {
public StreamBuilder(
_async.AsyncWidgetBuilder<T> builder,
Key key = null,
T initialData = default,
Stream<T> stream = null
) : base(key: key, stream: stream) {
D.assert(builder != null);
this.builder = builder;
this.initialData = initialData;
}
public class StreamBuilder<T> : StreamBuilderBase<T, AsyncSnapshot<T>> {
public StreamBuilder(
_async.AsyncWidgetBuilder<T> builder,
Key key = null,
T initialData = default,
Stream<T> stream = null
) : base(key: key, stream: stream) {
D.assert(builder != null);
this.builder = builder;
this.initialData = initialData;
}
public readonly _async.AsyncWidgetBuilder<T> builder;
public readonly _async.AsyncWidgetBuilder<T> builder;
public readonly T initialData;
public readonly T initialData;
public override
AsyncSnapshot<T> initial() => global::AsyncSnapshot<T>.withData(ConnectionState.none, initialData);
public override
AsyncSnapshot<T> initial() => AsyncSnapshot<T>.withData(ConnectionState.none, initialData);
public override
AsyncSnapshot<T> afterConnected(AsyncSnapshot<T> current) => current.inState(ConnectionState.waiting);
public override
AsyncSnapshot<T> afterConnected(AsyncSnapshot<T> current) => current.inState(ConnectionState.waiting);
public override
AsyncSnapshot<T> afterData(AsyncSnapshot<T> current, T data) {
return global::AsyncSnapshot<T>.withData(ConnectionState.active, data);
}
public override
AsyncSnapshot<T> afterData(AsyncSnapshot<T> current, T data) {
return AsyncSnapshot<T>.withData(ConnectionState.active, data);
}
public override
AsyncSnapshot<T> afterError(AsyncSnapshot<T> current, object error) {
return AsyncSnapshot<T>.withError(ConnectionState.active, error);
}
public override
AsyncSnapshot<T> afterError(AsyncSnapshot<T> current, object error) {
return AsyncSnapshot<T>.withError(ConnectionState.active, error);
}
public override
AsyncSnapshot<T> afterDone(AsyncSnapshot<T> current) => current.inState(ConnectionState.done);
public override
AsyncSnapshot<T> afterDone(AsyncSnapshot<T> current) => current.inState(ConnectionState.done);
public override
AsyncSnapshot<T> afterDisconnected(AsyncSnapshot<T> current) => current.inState(ConnectionState.none);
public override
AsyncSnapshot<T> afterDisconnected(AsyncSnapshot<T> current) => current.inState(ConnectionState.none);
public override
Widget build(BuildContext context, AsyncSnapshot<T> currentSummary) => builder(context, currentSummary);
}
// TODO(ianh): remove unreachable code above once https://github.com/dart-lang/linter/issues/1141 is fixed
class FutureBuilder<T> : StatefulWidget {
public FutureBuilder(
_async.AsyncWidgetBuilder<T> builder,
Key key = null,
Future<T> future = null,
T initialData = default
) :
base(key: key) {
D.assert(builder != null);
this.builder = builder;
this.future = future;
this.initialData = initialData;
public override
Widget build(BuildContext context, AsyncSnapshot<T> currentSummary) => builder(context, currentSummary);
public readonly Future<T> future;
public readonly _async.AsyncWidgetBuilder<T> builder;
// TODO(ianh): remove unreachable code above once https://github.com/dart-lang/linter/issues/1141 is fixed
class FutureBuilder<T> : StatefulWidget {
public FutureBuilder(
_async.AsyncWidgetBuilder<T> builder,
Key key = null,
Future<T> future = null,
T initialData = default
) :
base(key: key) {
D.assert(builder != null);
this.builder = builder;
this.future = future;
this.initialData = initialData;
}
public readonly T initialData;
public readonly Future<T> future;
public override
State createState() => new _FutureBuilderState<T>();
}
public readonly _async.AsyncWidgetBuilder<T> builder;
class _FutureBuilderState<T> : State<FutureBuilder<T>> {
object _activeCallbackIdentity;
AsyncSnapshot<T> _snapshot;
public readonly T initialData;
public override
void initState() {
base.initState();
_snapshot = AsyncSnapshot<T>.withData(ConnectionState.none, widget.initialData);
_subscribe();
public override
State createState() => new _FutureBuilderState<T>();
public override
void didUpdateWidget(StatefulWidget statefulWidget) {
var oldWidget = statefulWidget as FutureBuilder<T>;
if (oldWidget == null) {
return;
class _FutureBuilderState<T> : State<FutureBuilder<T>> {
object _activeCallbackIdentity;
AsyncSnapshot<T> _snapshot;
public override
void initState() {
base.initState();
_snapshot = AsyncSnapshot<T>.withData(ConnectionState.none, widget.initialData);
_subscribe();
base.didUpdateWidget(oldWidget);
if (oldWidget.future != widget.future) {
if (_activeCallbackIdentity != null) {
_unsubscribe();
_snapshot = _snapshot.inState(ConnectionState.none);
public override
void didUpdateWidget(StatefulWidget statefulWidget) {
var oldWidget = statefulWidget as FutureBuilder<T>;
if (oldWidget == null) {
return;
_subscribe();
base.didUpdateWidget(oldWidget);
if (oldWidget.future != widget.future) {
if (_activeCallbackIdentity != null) {
_unsubscribe();
_snapshot = _snapshot.inState(ConnectionState.none);
}
_subscribe();
}
}
public override
Widget build(BuildContext context) => widget.builder(context, _snapshot);
public override
Widget build(BuildContext context) => widget.builder(context, _snapshot);
public override
void dispose() {
_unsubscribe();
base.dispose();
}
public override
void dispose() {
_unsubscribe();
base.dispose();
}
void _subscribe() {
if (widget.future != null) {
object callbackIdentity = new object();
_activeCallbackIdentity = callbackIdentity;
widget.future.then((object dataIn) => {
var data = (T) dataIn;
if (_activeCallbackIdentity == callbackIdentity) {
setState(() => { _snapshot = AsyncSnapshot<T>.withData(ConnectionState.done, data); });
}
}, onError: (Exception error) => {
if (_activeCallbackIdentity == callbackIdentity) {
setState(() => { _snapshot = AsyncSnapshot<T>.withError(ConnectionState.done, error); });
}
void _subscribe() {
if (widget.future != null) {
object callbackIdentity = new object();
_activeCallbackIdentity = callbackIdentity;
widget.future.then((object dataIn) => {
var data = (T) dataIn;
if (_activeCallbackIdentity == callbackIdentity) {
setState(() => { _snapshot = AsyncSnapshot<T>.withData(ConnectionState.done, data); });
}
}, onError: (Exception error) => {
if (_activeCallbackIdentity == callbackIdentity) {
setState(() => { _snapshot = AsyncSnapshot<T>.withError(ConnectionState.done, error); });
}
return FutureOr.nil;
});
_snapshot = _snapshot.inState(ConnectionState.waiting);
return FutureOr.nil;
});
_snapshot = _snapshot.inState(ConnectionState.waiting);
}
}
void _unsubscribe() {
_activeCallbackIdentity = null;
void _unsubscribe() {
_activeCallbackIdentity = null;
}
}
}
正在加载...
取消
保存