Created
July 7, 2023 09:34
-
-
Save jamesikanos/a894b8419412b580f7484302f03a96f2 to your computer and use it in GitHub Desktop.
The fromLiveQueryArray function is a utility function for Reactive Programming in JavaScript. It takes a Live Query, which is a special kind of query that dynamically updates as its results change, and converts it into an Observable.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* MIT License | |
* | |
* Copyright (c) 2023 James Woodall ([email protected]) | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in all | |
* copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
* SOFTWARE. | |
*/ | |
import { LiveQuery } from "remult"; | |
import { Observable, map, shareReplay } from "rxjs"; | |
/** | |
* Converts a Remult Live Query into an Observable, emitting an array of values whenever the query changes | |
* @param liveQuery The query to subscribe to | |
* @returns An observable emitting an array of values whenever the query changes | |
*/ | |
export function fromLiveQueryArray<T>(liveQuery: LiveQuery<T>): Observable<T[]> { | |
return new Observable<T[]>(subscribe => { | |
let items: T[] = []; | |
const liveQuerySubscription = liveQuery.subscribe(next => { | |
items = next.applyChanges(items); | |
subscribe.next(items); | |
}); | |
return () => { | |
// Unsubscribe from the query | |
liveQuerySubscription(); | |
}; | |
}).pipe( | |
// Share the query, but unsubscribe when the last subscriber has left | |
shareReplay({ bufferSize: 1, refCount: true }) | |
); | |
} | |
/** | |
* Converts a Remult Live Query into an Observable, emitting the first value whenever the query changes. This is useful if you're only keeping track of a single value | |
* @param liveQuery The query to subscribe to | |
* @returns | |
*/ | |
export function fromLiveQueryFirst<T>(liveQuery: LiveQuery<T>): Observable<T> { | |
return fromLiveQueryArray(liveQuery).pipe(map(j => j[0])); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment