@@ -31,6 +31,7 @@ class CblReactnativeModule(reactContext: ReactApplicationContext) :
3131 private val context: ReactApplicationContext = reactContext
3232 private val replicatorChangeListeners: MutableMap <String , ListenerToken > = mutableMapOf ()
3333 private val replicatorDocumentListeners: MutableMap <String , ListenerToken > = mutableMapOf ()
34+ private val collectionChangeListeners: MutableMap <String , ListenerToken > = mutableMapOf ()
3435
3536 init {
3637 CouchbaseLite .init (context, true )
@@ -560,6 +561,126 @@ class CblReactnativeModule(reactContext: ReactApplicationContext) :
560561 }
561562 }
562563 }
564+ @ReactMethod
565+ fun collection_AddChangeListener (
566+ changeListenerToken : String ,
567+ collectionName : String ,
568+ name : String ,
569+ scopeName : String ,
570+ promise : Promise
571+ ) {
572+ GlobalScope .launch(Dispatchers .IO ) {
573+ try {
574+ if (! DataValidation .validateCollection(collectionName, scopeName, name, promise)) {
575+ return @launch
576+ }
577+ val collection = DatabaseManager .getCollection(collectionName, scopeName, name)
578+ if (collection == null ) {
579+ context.runOnUiQueueThread {
580+ promise.reject(" DATABASE_ERROR" , " Could not find collection" )
581+ }
582+ return @launch
583+ }
584+ val listener = collection.addChangeListener { change ->
585+ val resultMap = Arguments .createMap()
586+ resultMap.putString(" token" , changeListenerToken)
587+
588+ val docIdsArray = Arguments .createArray()
589+ change.documentIDs.forEach { docIdsArray.pushString(it) }
590+ resultMap.putArray(" documentIDs" , docIdsArray)
591+
592+ val collectionMap = DataAdapter .cblCollectionToMap(collection, name)
593+ resultMap.putMap(" collection" , collectionMap)
594+ context.runOnUiQueueThread {
595+ sendEvent(context, " collectionChange" , resultMap)
596+ }
597+ }
598+ collectionChangeListeners[changeListenerToken] = listener
599+ context.runOnUiQueueThread {
600+ promise.resolve(null )
601+ }
602+ } catch (e: Throwable ) {
603+ context.runOnUiQueueThread {
604+ promise.reject(" DATABASE_ERROR" , e.message)
605+ }
606+ }
607+ }
608+ }
609+
610+ @ReactMethod
611+ fun collection_RemoveChangeListener (
612+ changeListenerToken : String ,
613+ promise : Promise
614+ ) {
615+ GlobalScope .launch(Dispatchers .IO ) {
616+ try {
617+ if (collectionChangeListeners.containsKey(changeListenerToken)) {
618+ val listener = collectionChangeListeners[changeListenerToken]
619+ listener?.remove()
620+ collectionChangeListeners.remove(changeListenerToken)
621+ context.runOnUiQueueThread {
622+ promise.resolve(null )
623+ }
624+ } else {
625+ context.runOnUiQueueThread {
626+ promise.reject(" COLLECTION_ERROR" , " No such listener found with token $changeListenerToken " )
627+ }
628+ }
629+ } catch (e: Throwable ) {
630+ context.runOnUiQueueThread {
631+ promise.reject(" COLLECTION_ERROR" , e.message)
632+ }
633+ }
634+ }
635+ }
636+
637+ @ReactMethod
638+ fun collection_AddDocumentChangeListener (
639+ changeListenerToken : String ,
640+ documentId : String ,
641+ collectionName : String ,
642+ name : String ,
643+ scopeName : String ,
644+ promise : Promise
645+ ) {
646+ GlobalScope .launch(Dispatchers .IO ) {
647+ try {
648+ if (! DataValidation .validateCollection(collectionName, scopeName, name, promise) ||
649+ ! DataValidation .validateDocumentId(documentId, promise)
650+ ) {
651+ return @launch
652+ }
653+ val collection = DatabaseManager .getCollection(collectionName, scopeName, name)
654+ if (collection == null ) {
655+ context.runOnUiQueueThread {
656+ promise.reject(" DATABASE_ERROR" , " Could not find collection" )
657+ }
658+ return @launch
659+ }
660+ val listener = collection.addDocumentChangeListener(documentId) { change ->
661+ val resultMap = Arguments .createMap()
662+ resultMap.putString(" token" , changeListenerToken)
663+ resultMap.putString(" documentId" , change.documentID)
664+ val collectionMap = DataAdapter .cblCollectionToMap(collection, name)
665+ resultMap.putMap(" collection" , collectionMap)
666+
667+ resultMap.putString(" database" , name)
668+ context.runOnUiQueueThread {
669+ sendEvent(context, " collectionDocumentChange" , resultMap)
670+ }
671+ }
672+
673+ collectionChangeListeners[changeListenerToken] = listener
674+ context.runOnUiQueueThread {
675+ promise.resolve(null )
676+ }
677+ } catch (e: Throwable ) {
678+ context.runOnUiQueueThread {
679+ promise.reject(" DATABASE_ERROR" , e.message)
680+ }
681+ }
682+ }
683+ }
563684
564685 // Database Functions
565686 @ReactMethod
0 commit comments