-
Notifications
You must be signed in to change notification settings - Fork 944
RxFire Realtime Database #997
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
bbc5559
initial database code
davideast 9e319f6
test setup
davideast 3136d17
database tests
davideast 56972fc
auditTrail and database tests
davideast a940247
[AUTOMATED]: Prettier Code Styling
davideast c9d7d2d
[AUTOMATED]: License Headers
davideast f88d8ba
Josh's comments. Database docs
davideast 9425cc4
[AUTOMATED]: Prettier Code Styling
davideast e0fb675
Firestore docs
davideast 438d0cd
auth docs
davideast cc7caf0
declaration fixes
davideast 40ad987
switch to peerDeps
davideast fa0fd53
[AUTOMATED]: Prettier Code Styling
davideast 9982b4a
test config
davideast 0540151
Merge branch 'master' into rxfire-db
davideast File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{ | ||
"name": "rxfire/auth", | ||
"main": "dist/index.cjs.js", | ||
"module": "dist/index.esm.js" | ||
"module": "dist/index.esm.js", | ||
"typings": "dist/auth/index.d.ts" | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* Copyright 2018 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { database } from 'firebase'; | ||
import { Observable } from 'rxjs'; | ||
import { ListenEvent, QueryChange } from './interfaces'; | ||
/** | ||
* Create an observable from a Database Reference or Database Query. | ||
* @param ref Database Reference | ||
* @param event Listen event type ('value', 'added', 'changed', 'removed', 'moved') | ||
*/ | ||
export declare function fromRef( | ||
ref: database.Query, | ||
event: ListenEvent | ||
): Observable<QueryChange>; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* Copyright 2018 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { database } from 'firebase'; | ||
import { Observable } from 'rxjs'; | ||
import { map, delay, share } from 'rxjs/operators'; | ||
import { ListenEvent, QueryChange } from './interfaces'; | ||
|
||
/** | ||
* Create an observable from a Database Reference or Database Query. | ||
* @param ref Database Reference | ||
* @param event Listen event type ('value', 'added', 'changed', 'removed', 'moved') | ||
*/ | ||
export function fromRef( | ||
ref: database.Query, | ||
event: ListenEvent | ||
): Observable<QueryChange> { | ||
return new Observable<QueryChange>(subscriber => { | ||
const fn = ref.on( | ||
event, | ||
(snapshot, prevKey) => { | ||
subscriber.next({ snapshot, prevKey, event }); | ||
}, | ||
subscriber.error.bind(subscriber) | ||
); | ||
return { | ||
unsubscribe() { | ||
ref.off(event, fn); | ||
} | ||
}; | ||
}).pipe( | ||
// Ensures subscribe on observable is async. This handles | ||
// a quirk in the SDK where on/once callbacks can happen | ||
// synchronously. | ||
delay(0) | ||
); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* Copyright 2018 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
export * from './fromRef'; | ||
export * from './interfaces'; | ||
export * from './list'; | ||
export * from './object'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* Copyright 2018 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { database } from 'firebase'; | ||
export declare enum ListenEvent { | ||
added = 'child_added', | ||
removed = 'child_removed', | ||
changed = 'child_changed', | ||
moved = 'child_moved', | ||
value = 'value' | ||
} | ||
export interface QueryChange { | ||
snapshot: database.DataSnapshot; | ||
prevKey: string | null | undefined; | ||
event: ListenEvent; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Copyright 2018 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { database } from 'firebase'; | ||
|
||
export enum ListenEvent { | ||
added = 'child_added', | ||
removed = 'child_removed', | ||
changed = 'child_changed', | ||
moved = 'child_moved', | ||
value = 'value' | ||
} | ||
|
||
export interface QueryChange { | ||
snapshot: database.DataSnapshot; | ||
prevKey: string | null | undefined; | ||
event: ListenEvent; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* Copyright 2018 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { database } from 'firebase'; | ||
import { Observable } from 'rxjs'; | ||
import { QueryChange, ListenEvent } from '../interfaces'; | ||
export declare function auditTrail( | ||
query: database.Query, | ||
events?: ListenEvent[] | ||
): Observable<QueryChange[]>; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/** | ||
* Copyright 2018 Google Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { database } from 'firebase'; | ||
import { Observable } from 'rxjs'; | ||
import { QueryChange, ListenEvent } from '../interfaces'; | ||
import { fromRef } from '../fromRef'; | ||
import { map, withLatestFrom, scan, skipWhile } from 'rxjs/operators'; | ||
import { stateChanges } from './index'; | ||
|
||
interface LoadedMetadata { | ||
data: QueryChange; | ||
lastKeyToLoad: any; | ||
} | ||
|
||
export function auditTrail( | ||
query: database.Query, | ||
events?: ListenEvent[] | ||
): Observable<QueryChange[]> { | ||
const auditTrail$ = stateChanges(query, events).pipe( | ||
scan<QueryChange>((current, changes) => [...current, changes], []) | ||
); | ||
return waitForLoaded(query, auditTrail$); | ||
} | ||
|
||
function loadedData(query: database.Query): Observable<LoadedMetadata> { | ||
// Create an observable of loaded values to retrieve the | ||
// known dataset. This will allow us to know what key to | ||
// emit the "whole" array at when listening for child events. | ||
return fromRef(query, ListenEvent.value).pipe( | ||
map(data => { | ||
// Store the last key in the data set | ||
let lastKeyToLoad; | ||
// Loop through loaded dataset to find the last key | ||
data.snapshot.forEach(child => { | ||
lastKeyToLoad = child.key; | ||
return false; | ||
}); | ||
// return data set and the current last key loaded | ||
return { data, lastKeyToLoad }; | ||
}) | ||
); | ||
} | ||
|
||
function waitForLoaded( | ||
query: database.Query, | ||
snap$: Observable<QueryChange[]> | ||
) { | ||
const loaded$ = loadedData(query); | ||
return loaded$.pipe( | ||
withLatestFrom(snap$), | ||
// Get the latest values from the "loaded" and "child" datasets | ||
// We can use both datasets to form an array of the latest values. | ||
map(([loaded, changes]) => { | ||
// Store the last key in the data set | ||
let lastKeyToLoad = loaded.lastKeyToLoad; | ||
// Store all child keys loaded at this point | ||
const loadedKeys = changes.map(change => change.snapshot.key); | ||
return { changes, lastKeyToLoad, loadedKeys }; | ||
}), | ||
// This is the magical part, only emit when the last load key | ||
// in the dataset has been loaded by a child event. At this point | ||
// we can assume the dataset is "whole". | ||
skipWhile(meta => meta.loadedKeys.indexOf(meta.lastKeyToLoad) === -1), | ||
// Pluck off the meta data because the user only cares | ||
// to iterate through the snapshots | ||
map(meta => meta.changes) | ||
); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method allows users to dynamically invoke various methods off of a reference. It's kind of scary and I would rather us support only the
on
and have users handle theonce
case themselves.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed