11import 'dart:async' ;
22
3+ import 'package:rxdart/rxdart.dart' ;
34import 'package:supabase/supabase.dart' ;
45
56enum _FilterType { eq, neq, lt, lte, gt, gte }
@@ -41,8 +42,6 @@ class SupabaseStreamBuilder extends Stream<SupabaseStreamEvent> {
4142
4243 RealtimeChannel ? _channel;
4344
44- PostgrestBuilder ? _postgrestBuilder;
45-
4645 final String _schema;
4746
4847 final String _table;
@@ -51,7 +50,7 @@ class SupabaseStreamBuilder extends Stream<SupabaseStreamEvent> {
5150 final List <String > _uniqueColumns;
5251
5352 /// StreamController for `stream()` method.
54- StreamController <SupabaseStreamEvent >? _streamController;
53+ BehaviorSubject <SupabaseStreamEvent >? _streamController;
5554
5655 /// Contains the combined data of postgrest and realtime to emit as stream.
5756 SupabaseStreamEvent _streamData = [];
@@ -84,7 +83,7 @@ class SupabaseStreamBuilder extends Stream<SupabaseStreamEvent> {
8483 /// Only one filter can be applied to `.stream()` .
8584 ///
8685 /// ```dart
87- /// supabase.from('users').stream(['id']).eq('name', 'Supabase');
86+ /// supabase.from('users').stream(primaryKey: ['id']).eq('name', 'Supabase');
8887 /// ```
8988 SupabaseStreamBuilder eq (String column, dynamic value) {
9089 assert (
@@ -104,7 +103,7 @@ class SupabaseStreamBuilder extends Stream<SupabaseStreamEvent> {
104103 /// Only one filter can be applied to `.stream()` .
105104 ///
106105 /// ```dart
107- /// supabase.from('users').stream(['id']).neq('name', 'Supabase');
106+ /// supabase.from('users').stream(primaryKey: ['id']).neq('name', 'Supabase');
108107 /// ```
109108 SupabaseStreamBuilder neq (String column, dynamic value) {
110109 assert (
@@ -124,7 +123,7 @@ class SupabaseStreamBuilder extends Stream<SupabaseStreamEvent> {
124123 /// Only one filter can be applied to `.stream()` .
125124 ///
126125 /// ```dart
127- /// supabase.from('users').stream(['id']).lt('likes', 100);
126+ /// supabase.from('users').stream(primaryKey: ['id']).lt('likes', 100);
128127 /// ```
129128 SupabaseStreamBuilder lt (String column, dynamic value) {
130129 assert (
@@ -144,7 +143,7 @@ class SupabaseStreamBuilder extends Stream<SupabaseStreamEvent> {
144143 /// Only one filter can be applied to `.stream()` .
145144 ///
146145 /// ```dart
147- /// supabase.from('users').stream(['id']).lte('likes', 100);
146+ /// supabase.from('users').stream(primaryKey: ['id']).lte('likes', 100);
148147 /// ```
149148 SupabaseStreamBuilder lte (String column, dynamic value) {
150149 assert (
@@ -164,7 +163,7 @@ class SupabaseStreamBuilder extends Stream<SupabaseStreamEvent> {
164163 /// Only one filter can be applied to `.stream()` .
165164 ///
166165 /// ```dart
167- /// supabase.from('users').stream(['id']).gt('likes', '100');
166+ /// supabase.from('users').stream(primaryKey: ['id']).gt('likes', '100');
168167 /// ```
169168 SupabaseStreamBuilder gt (String column, dynamic value) {
170169 assert (
@@ -184,7 +183,7 @@ class SupabaseStreamBuilder extends Stream<SupabaseStreamEvent> {
184183 /// Only one filter can be applied to `.stream()` .
185184 ///
186185 /// ```dart
187- /// supabase.from('users').stream(['id']).gte('likes', 100);
186+ /// supabase.from('users').stream(primaryKey: ['id']).gte('likes', 100);
188187 /// ```
189188 SupabaseStreamBuilder gte (String column, dynamic value) {
190189 assert (
@@ -204,7 +203,7 @@ class SupabaseStreamBuilder extends Stream<SupabaseStreamEvent> {
204203 /// When `ascending` value is true, the result will be in ascending order.
205204 ///
206205 /// ```dart
207- /// supabase.from('users').stream(['id']).order('username', ascending: false);
206+ /// supabase.from('users').stream(primaryKey: ['id']).order('username', ascending: false);
208207 /// ```
209208 SupabaseStreamBuilder order (String column, {bool ascending = false }) {
210209 _orderBy = _Order (column: column, ascending: ascending);
@@ -214,7 +213,7 @@ class SupabaseStreamBuilder extends Stream<SupabaseStreamEvent> {
214213 /// Limits the result with the specified `count` .
215214 ///
216215 /// ```dart
217- /// supabase.from('users').stream(['id']).limit(10);
216+ /// supabase.from('users').stream(primaryKey: ['id']).limit(10);
218217 /// ```
219218 SupabaseStreamBuilder limit (int count) {
220219 _limit = count;
@@ -223,13 +222,7 @@ class SupabaseStreamBuilder extends Stream<SupabaseStreamEvent> {
223222
224223 @Deprecated ('Directly listen without execute instead. Deprecated in 1.0.0' )
225224 Stream <SupabaseStreamEvent > execute () {
226- _streamController = StreamController .broadcast (
227- onCancel: () {
228- _channel? .unsubscribe ();
229- _streamController? .close ();
230- },
231- );
232- _getStreamData ();
225+ _setupStream ();
233226 return _streamController! .stream;
234227 }
235228
@@ -240,37 +233,27 @@ class SupabaseStreamBuilder extends Stream<SupabaseStreamEvent> {
240233 void Function ()? onDone,
241234 bool ? cancelOnError,
242235 }) {
243- if (_postgrestBuilder == null ) {
244- PostgrestFilterBuilder query = _queryBuilder.select ();
245- if (_streamFilter != null ) {
246- query = query.eq (_streamFilter! .column, _streamFilter! .value);
247- }
248- PostgrestTransformBuilder ? transformQuery;
249- if (_orderBy != null ) {
250- transformQuery =
251- query.order (_orderBy! .column, ascending: _orderBy! .ascending);
252- }
253- if (_limit != null ) {
254- transformQuery = (transformQuery ?? query).limit (_limit! );
255- }
256- _postgrestBuilder = transformQuery ?? query;
257- }
236+ _setupStream ();
237+ return _streamController! .stream.listen (
238+ onData,
239+ onError: onError,
240+ onDone: onDone,
241+ cancelOnError: cancelOnError,
242+ );
243+ }
258244
259- _streamController = StreamController .broadcast (
245+ /// Sets up the stream controller and calls the method to get data as necessary
246+ void _setupStream () {
247+ _streamController ?? = BehaviorSubject (
260248 onListen: () {
261249 _getStreamData ();
262250 },
263251 onCancel: () {
264252 _channel? .unsubscribe ();
265253 _streamController? .close ();
254+ _streamController = null ;
266255 },
267256 );
268- return _streamController! .stream.listen (
269- onData,
270- onError: onError,
271- onDone: onDone,
272- cancelOnError: cancelOnError,
273- );
274257 }
275258
276259 Future <void > _getStreamData () async {
@@ -300,9 +283,7 @@ class SupabaseStreamBuilder extends Stream<SupabaseStreamEvent> {
300283 event: 'UPDATE' ,
301284 schema: _schema,
302285 table: _table,
303- filter: _streamFilter != null
304- ? '${_streamFilter !.column }=eq.${_streamFilter !.value }'
305- : null ,
286+ filter: realtimeFilter,
306287 ), (payload, [ref]) {
307288 final updatedIndex = _streamData.indexWhere (
308289 (element) => _isTargetRecord (record: element, payload: payload),
@@ -320,9 +301,7 @@ class SupabaseStreamBuilder extends Stream<SupabaseStreamEvent> {
320301 event: 'DELETE' ,
321302 schema: _schema,
322303 table: _table,
323- filter: _streamFilter != null
324- ? '${_streamFilter !.column }=eq.${_streamFilter !.value }'
325- : null ,
304+ filter: realtimeFilter,
326305 ), (payload, [ref]) {
327306 final deletedIndex = _streamData.indexWhere (
328307 (element) => _isTargetRecord (record: element, payload: payload),
@@ -368,7 +347,7 @@ class SupabaseStreamBuilder extends Stream<SupabaseStreamEvent> {
368347 }
369348
370349 try {
371- final data = await _postgrestBuilder ;
350+ final data = await (transformQuery ?? query) ;
372351 final rows = SupabaseStreamEvent .from (data as List );
373352 _streamData.addAll (rows);
374353 _addStream ();
0 commit comments