siyao
3 年前
当前提交
c9e46c47
共有 2 个文件被更改,包括 329 次插入 和 328 次删除
-
181com.unity.uiwidgets/Runtime/async/async_cast.cs
-
476com.unity.uiwidgets/Runtime/widgets/async.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>(); |
|||
} |
撰写
预览
正在加载...
取消
保存
Reference in new issue