Skip to content

enable eslint for rxfire #1873

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
merged 11 commits into from
Jul 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/rxfire/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../config/.eslintrc.json",
"parserOptions": {
"project": "tsconfig.json"
},
"rules": {
"import/no-extraneous-dependencies" : "off"
}
}
20 changes: 3 additions & 17 deletions packages/rxfire/.gitignore
Original file line number Diff line number Diff line change
@@ -1,21 +1,7 @@
node_modules
dist

/rxfire*.js
/rxfire*.map
/rxfire*.gz
/rxfire*.tgz

# generated declaration files
# declaration files currently break testing builds
auth/index.d.ts
firestore/collection/index.d.ts
firestore/document/index.d.ts
firestore/fromRef.d.ts
firestore/index.d.ts
database/fromRef.d.ts
database/interfaces.d.ts
database/utils.d.ts
database/list/audit-trail.d.ts
functions/index.d.ts
index.d.ts
storage/index.d.ts
test/index.d.ts
test/firestore.test.d.ts
2 changes: 2 additions & 0 deletions packages/rxfire/auth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* limitations under the License.
*/

// auth is used as a namespace to access types
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { auth, User } from 'firebase';
import { Observable, from, of } from 'rxjs';
import { switchMap } from 'rxjs/operators';
Expand Down
2 changes: 1 addition & 1 deletion packages/rxfire/database/fromRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import { database } from 'firebase';
import { Observable } from 'rxjs';
import { map, delay, share } from 'rxjs/operators';
import { delay } from 'rxjs/operators';
import { ListenEvent, QueryChange } from './interfaces';

/**
Expand Down
16 changes: 11 additions & 5 deletions packages/rxfire/database/list/audit-trail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,18 @@ import { stateChanges } from './index';

interface LoadedMetadata {
data: QueryChange;
lastKeyToLoad: any;
lastKeyToLoad: unknown;
}

export function auditTrail(
query: database.Query,
events?: ListenEvent[]
): Observable<QueryChange[]> {
const auditTrail$ = stateChanges(query, events).pipe(
scan<QueryChange>((current, changes) => [...current, changes], [])
scan<QueryChange, QueryChange[]>(
(current, changes) => [...current, changes],
[]
)
);
return waitForLoaded(query, auditTrail$);
}
Expand All @@ -59,23 +62,26 @@ function loadedData(query: database.Query): Observable<LoadedMetadata> {
function waitForLoaded(
query: database.Query,
snap$: Observable<QueryChange[]>
) {
): 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;
const 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),
skipWhile(
meta =>
meta.loadedKeys.indexOf(meta.lastKeyToLoad as string | null) === -1
),
// Pluck off the meta data because the user only cares
// to iterate through the snapshots
map(meta => meta.changes)
Expand Down
13 changes: 8 additions & 5 deletions packages/rxfire/database/list/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import { fromRef } from '../fromRef';
import { switchMap, scan, distinctUntilChanged, map } from 'rxjs/operators';
import { changeToData } from '../object';

export function stateChanges(query: database.Query, events?: ListenEvent[]) {
export function stateChanges(
query: database.Query,
events?: ListenEvent[]
): Observable<QueryChange> {
events = validateEventsArray(events);
const childEvent$ = events.map(event => fromRef(query, event));
return merge(...childEvent$);
Expand Down Expand Up @@ -69,7 +72,7 @@ export function listVal<T>(
);
}

function positionFor(changes: QueryChange[], key: string | null) {
function positionFor(changes: QueryChange[], key: string | null): number {
const len = changes.length;
for (let i = 0; i < len; i++) {
if (changes[i].snapshot.key === key) {
Expand All @@ -79,7 +82,7 @@ function positionFor(changes: QueryChange[], key: string | null) {
return -1;
}

function positionAfter(changes: QueryChange[], prevKey?: string) {
function positionAfter(changes: QueryChange[], prevKey?: string): number {
if (prevKey == null) {
return 0;
} else {
Expand All @@ -92,7 +95,7 @@ function positionAfter(changes: QueryChange[], prevKey?: string) {
}
}

function buildView(current: QueryChange[], change: QueryChange) {
function buildView(current: QueryChange[], change: QueryChange): QueryChange[] {
const { snapshot, prevKey, event } = change;
const { key } = snapshot;
const currentKeyPosition = positionFor(current, key);
Expand All @@ -117,7 +120,7 @@ function buildView(current: QueryChange[], change: QueryChange) {
if (currentKeyPosition > -1) {
// check that the previouskey is what we expect, else reorder
const previous = current[currentKeyPosition - 1];
if (((previous && previous.snapshot.key) || null) != prevKey) {
if (((previous && previous.snapshot.key) || null) !== prevKey) {
current = current.filter(x => x.snapshot.key !== snapshot.key);
current.splice(afterPreviousKeyPosition, 0, change);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/rxfire/database/object/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function objectVal<T>(
);
}

export function changeToData(change: QueryChange, keyField?: string) {
export function changeToData(change: QueryChange, keyField?: string): {} {
return {
...change.snapshot.val(),
...(keyField ? { [keyField]: change.snapshot.key } : null)
Expand Down
23 changes: 14 additions & 9 deletions packages/rxfire/firestore/collection/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import { firestore } from 'firebase/app';
import { fromCollectionRef } from '../fromRef';
import { Observable } from 'rxjs';
import { Observable, MonoTypeOperatorFunction } from 'rxjs';
import { map, filter, scan } from 'rxjs/operators';
import { snapToData } from '../document';

Expand All @@ -32,7 +32,9 @@ const ALL_EVENTS: firestore.DocumentChangeType[] = [
* are specified by the event filter. If the document change type is not
* in specified events array, it will not be emitted.
*/
const filterEvents = (events?: firestore.DocumentChangeType[]) =>
const filterEvents = (
events?: firestore.DocumentChangeType[]
): MonoTypeOperatorFunction<firestore.DocumentChange[]> =>
filter((changes: firestore.DocumentChange[]) => {
let hasChange = false;
for (let i = 0; i < changes.length; i++) {
Expand Down Expand Up @@ -67,7 +69,7 @@ function processIndividualChange(
case 'added':
if (
combined[change.newIndex] &&
combined[change.newIndex].doc.id == change.doc.id
combined[change.newIndex].doc.id === change.doc.id
) {
// Skip duplicate emissions. This is rare.
// TODO: Investigate possible bug in SDK.
Expand All @@ -78,7 +80,7 @@ function processIndividualChange(
case 'modified':
if (
combined[change.oldIndex] == null ||
combined[change.oldIndex].doc.id == change.doc.id
combined[change.oldIndex].doc.id === change.doc.id
) {
// When an item changes position we first remove it
// and then add it's new position
Expand All @@ -93,11 +95,12 @@ function processIndividualChange(
case 'removed':
if (
combined[change.oldIndex] &&
combined[change.oldIndex].doc.id == change.doc.id
combined[change.oldIndex].doc.id === change.doc.id
) {
combined.splice(change.oldIndex, 1);
}
break;
default: // ignore
}
return combined;
}
Expand All @@ -113,7 +116,7 @@ function processDocumentChanges(
current: firestore.DocumentChange[],
changes: firestore.DocumentChange[],
events: firestore.DocumentChangeType[] = ALL_EVENTS
) {
): firestore.DocumentChange[] {
changes.forEach(change => {
// skip unwanted change types
if (events.indexOf(change.type) > -1) {
Expand All @@ -131,7 +134,7 @@ function processDocumentChanges(
export function collectionChanges(
query: firestore.Query,
events: firestore.DocumentChangeType[] = ALL_EVENTS
) {
): Observable<firestore.DocumentChange[]> {
return fromCollectionRef(query).pipe(
map(snapshot => snapshot.docChanges()),
filterEvents(events),
Expand All @@ -143,7 +146,9 @@ export function collectionChanges(
* Return a stream of document snapshots on a query. These results are in sort order.
* @param query
*/
export function collection(query: firestore.Query) {
export function collection(
query: firestore.Query
): Observable<firestore.QueryDocumentSnapshot[]> {
return fromCollectionRef(query).pipe(map(changes => changes.docs));
}

Expand All @@ -154,7 +159,7 @@ export function collection(query: firestore.Query) {
export function sortedChanges(
query: firestore.Query,
events?: firestore.DocumentChangeType[]
) {
): Observable<firestore.DocumentChange[]> {
return collectionChanges(query, events).pipe(
scan(
(
Expand Down
6 changes: 4 additions & 2 deletions packages/rxfire/firestore/document/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ import { fromDocRef } from '../fromRef';
import { map } from 'rxjs/operators';
import { Observable } from 'rxjs';

export function doc(ref: firestore.DocumentReference) {
export function doc(
ref: firestore.DocumentReference
): Observable<firestore.DocumentSnapshot> {
return fromDocRef(ref);
}

Expand All @@ -38,7 +40,7 @@ export function docData<T>(
export function snapToData(
snapshot: firestore.DocumentSnapshot,
idField?: string
) {
): {} {
return {
...snapshot.data(),
...(idField ? { [idField]: snapshot.id } : null)
Expand Down
9 changes: 6 additions & 3 deletions packages/rxfire/firestore/fromRef.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,28 @@
import { firestore } from 'firebase/app';
import { Observable } from 'rxjs';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function _fromRef(ref: any): Observable<any> {
return new Observable(subscriber => {
const unsubscribe = ref.onSnapshot(subscriber);
return { unsubscribe };
});
}

export function fromRef(ref: firestore.DocumentReference | firestore.Query) {
export function fromRef(
ref: firestore.DocumentReference | firestore.Query
): Observable<{}> {
return _fromRef(ref);
}

export function fromDocRef(
ref: firestore.DocumentReference
): Observable<firestore.DocumentSnapshot> {
return fromRef(ref);
return fromRef(ref) as Observable<firestore.DocumentSnapshot>;
}

export function fromCollectionRef(
ref: firestore.Query
): Observable<firestore.QuerySnapshot> {
return fromRef(ref);
return fromRef(ref) as Observable<firestore.QuerySnapshot>;
}
8 changes: 5 additions & 3 deletions packages/rxfire/functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@
* limitations under the License.
*/

// function is used as a namespace to access types
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { functions } from 'firebase/app';
import { Observable, from } from 'rxjs';
import { from, Observable } from 'rxjs';
import { map } from 'rxjs/operators';

export function httpsCallable<T = any, R = any>(
export function httpsCallable<T, R>(
functions: functions.Functions,
name: string
) {
): (data: T) => Observable<R> {
const callable = functions.httpsCallable(name);
return (data: T) => {
return from(callable(data)).pipe(map(r => r.data as R));
Expand Down
11 changes: 9 additions & 2 deletions packages/rxfire/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
"url": "https://github.com/firebase/firebase-js-sdk.git"
},
"scripts": {
"lint": "eslint -c .eslintrc.json '**/*.ts' --ignore-path './.gitignore'",
"lint:fix": "eslint --fix -c .eslintrc.json '**/*.ts' --ignore-path './.gitignore'",
"build": "rollup -c",
"dev": "rollup -c -w",
"prepare": "yarn build",
"test": "yarn test:browser",
"test": "run-p lint test:browser",
"test:browser": "karma start --single-run",
"test:browser:debug": "karma start --browsers=Chrome --auto-watch"
},
Expand Down Expand Up @@ -67,7 +69,12 @@
"ts-node": "8.1.0",
"tslint": "5.16.0",
"webpack": "4.30.0",
"yargs": "13.2.2"
"yargs": "13.2.2",
"eslint": "5.16.0",
"@typescript-eslint/parser": "1.10.2",
"@typescript-eslint/eslint-plugin": "1.10.2",
"@typescript-eslint/eslint-plugin-tslint": "1.10.2",
"eslint-plugin-import": "2.17.3"
},
"typings": "dist/index.d.ts",
"files": [
Expand Down
Loading