using Bts.DataStream; using mbts.config; using System; namespace BCG { /// /// Marker abstract class /// public abstract class ViewStream : DataStream { private IBlueRowCollection myLastValueForTestMode; protected bool myUseUncopiedNewRowsInFireEvent = Configuration.get().UseUncopiedNewRowsInFire; string streamName; public ViewStream (ViewStream sourceStream) : base(sourceStream) {} public ViewStream(string streamName) { this.streamName = streamName; } public ViewStream() {} public Guid StreamId { get; } = Guid.NewGuid(); public string StreamName => streamName ?? (myRealSource as ViewStream)?.StreamName ?? GetType().Name; public IBlueRowCollection rows { get { if (myRealSource != null) { // BlueEventLog.logAssert (myRealSource is ViewStream); return (myRealSource as ViewStream).rows; } else { // used to force a computation of a stream which // happens to have no subscribers onChange += new DataStream.ChangeDelegate(myDummy); try { if (myLastValue != null) return ((DataPacket) myLastValue).newRows; else return null; } finally { onChange -= new DataStream.ChangeDelegate(myDummy); } } } } protected override object myGetValueForInitialSubscription() { if (myLastValue == null) return null; else { return new DataPacket( (myLastValue as DataPacket).newRows, (myLastValue as DataPacket).newRows, new BlueTable((myLastValue as DataPacket).newRows.schema)); } } protected override void myFireEvent(object o) { var dp = o as IDataPacket; // BlueTrace.WriteViewStreamFireEvent(this.ID, // dp.newRows == null ? 0 : dp.newRows.Count, // dp.addedRows == null ? 0 : dp.addedRows.Count, // dp.deletedRows == null ? 0 : dp.deletedRows.Count); if (myTestMode) { // BlueEventLog.logAssert ((myFirstSubImplemented && myLastSubImplemented) | myRealSource != null, // @"either myOnFirstSub/myOnLastSub should be implemented or mySetRealSource // called with a non-null arg in " + this.GetType().FullName); if (myLastValueForTestMode != null) BlueDataUtility.checkConsistency(myLastValueForTestMode, dp.newRows, dp.deletedRows, dp.addedRows); myLastValueForTestMode = new ImmutableBlueTable(dp.newRows); } // the view already garantees that added & deleted are immutable. // this garantess that new is also immutable base.myFireEvent(myUseUncopiedNewRowsInFireEvent ? dp : dp.ToImmutable()); } private void myDummy (object o) {} } }