Skip to content

Commit

Permalink
fix: deprecate toArray and leave it to userland
Browse files Browse the repository at this point in the history
  • Loading branch information
rakannimer committed Nov 12, 2018
1 parent 7d6cc3f commit 08acb4a
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 174 deletions.
24 changes: 4 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import firebase from "firebase/app";
import "firebase/database";

// Don't worry about calling it more than once.
const { toBox, toArray, toMap, getFirebaseRef, destroy } = getMobxFire({
const { toBox, toMap, getFirebaseRef, destroy } = getMobxFire({
config,
firebase
});
Expand Down Expand Up @@ -97,7 +97,7 @@ type Output<T> = {
};
```

### `toArray`, `toBox` and `toMap`
### `toBox` and `toMap`

#### Input

Expand Down Expand Up @@ -127,16 +127,6 @@ type ToMapOptions<K, V> = {
};
```

##### toArray

```typescript
type ToArrayOptions<K, V> = {
map?: (k: K, v: V) => any;
filter?: (k: K, v: V) => boolean;
initial?: Array<V>;
};
```

#### Output

An object with the following shape :
Expand All @@ -154,15 +144,9 @@ const { value, unsub } = toBox(ref, { initial: "something" });
##### toMap

```typescript
const { value: map } = toMap(ref);
const { value: map, keys } = toMap(ref);
// map: ObservableMap<string, any>
```

##### toArray

```typescript
const { value: array } = toArray(ref);
// IObservableArray<string, any>
// keys: IObservableArray<string>
```

[circleci-href]: https://circleci.com/gh/rakannimer/mobx-firebase-database
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
],
"description": "Tame your firebase database with MobX",
"dependencies": {
"firebase-array": "^1.0.0",
"firebase-array": "^1.1.1",
"get-firebase-ref": "^1.1.1",
"initialize-firebase-app": "^1.0.0",
"lodash.memoize": "^4.1.2"
Expand Down
57 changes: 0 additions & 57 deletions src/__tests__/to-array.test.ts

This file was deleted.

8 changes: 4 additions & 4 deletions src/__tests__/to-map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Object {
const ref = getFirebaseRef({ firebase, path: testPath });
const { value: map, keys } = toMap(ref);
await ref.set(listAsObject);
expect(keys.get()).toMatchInlineSnapshot(`
expect(keys).toMatchInlineSnapshot(`
Array [
"id_0",
"id_1",
Expand All @@ -79,14 +79,14 @@ Array [
const { value: map, keys } = toMap(readRef);

await writeRef.set(listAsObject);
expect(keys.get()).toMatchInlineSnapshot(`
expect(keys).toMatchInlineSnapshot(`
Array [
"id_0",
"id_1",
]
`);
await writeRef.update({ id_: { A: "B" } });
expect(keys.get()).toMatchInlineSnapshot(`
expect(keys).toMatchInlineSnapshot(`
Array [
"id_",
"id_0",
Expand Down Expand Up @@ -121,7 +121,7 @@ Object {

test("works with custom mapValue", async () => {
const ref = getFirebaseRef({ firebase, path: testPath, limitToFirst: 2 });
const { value: map, unsub } = toMap<string, { data: any }>(ref, {
const { value: map, unsub } = toMap(ref, {
mapValue: v => {
if (!v) return v;
return v.data;
Expand Down
2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import initializeFirebaseApp from "initialize-firebase-app";
import { getFirebaseRef, FirebaseQuery } from "get-firebase-ref";
import memoize from "lodash.memoize";

import { toArray } from "./to-array";
import { toBox } from "./to-box";
import { toMap } from "./to-map";

Expand Down Expand Up @@ -32,7 +31,6 @@ export const getMobxFire = memoize(
};

return {
toArray,
toBox,
toMap,
getFirebaseRef: _getFirebaseRef,
Expand Down
49 changes: 0 additions & 49 deletions src/to-array.ts

This file was deleted.

16 changes: 1 addition & 15 deletions src/to-box.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,17 @@ function defaultFilter<T>(a: T) {
return true;
}

function defaultShouldUnsub<T>(a: T) {
return false;
}

export type ToBoxArgs<T> = {
map?: (m: T) => any;
filter?: (m: T) => boolean;
shouldUnsubWhen?: (m: T) => boolean;
initial?: T | null;
};

export function toBox<T>(
ref: any,
{
map = defaultMap,
filter = defaultFilter,
shouldUnsubWhen = defaultShouldUnsub,
initial = null
} = {
{ map = defaultMap, filter = defaultFilter, initial = null } = {
map: defaultMap,
filter: defaultFilter,
shouldUnsubWhen: defaultShouldUnsub,
initial: null
} as ToBoxArgs<T>
) {
Expand All @@ -38,9 +27,6 @@ export function toBox<T>(
if (filter(valueOrNull)) {
box.set(map(valueOrNull));
}
if (shouldUnsubWhen(valueOrNull)) {
unsub && unsub();
}
});
const update = (value: any) => {
return ref.update(value);
Expand Down
37 changes: 15 additions & 22 deletions src/to-map.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@
import { observable, ObservableMap } from "mobx";
import FirebaseArray from "firebase-array";
function defaultMap<T>(a: T) {
function defaultMap(a: any) {
return a;
}
function defaultFilter<V>(p: V, c: V) {
function defaultFilter(p: any, c: any) {
return true;
}

export type ToMapArgs<K, V> = {
mapKey?: (m: K) => any;
mapValue?: (m: V) => any;
filter?: (prevValue: V, currentValue: V) => boolean;
initial?: ObservableMap<K, V>;
export type ToMapArgs = {
mapKey?: (m: string | number) => any;
mapValue?: (m: any) => any;
filter?: (prevValue: any, currentValue: any) => boolean;
};

export function toMap<K extends string, V>(
export function toMap(
ref: any,
{
mapKey = defaultMap,
mapValue = defaultMap,
filter = defaultFilter,
// For better types. Object cant take enum for keys but maps can.
initial = observable.map({})
} = {
map: defaultMap,
filter: defaultFilter,
initial: observable.map({})
} as ToMapArgs<K, V>
{ mapKey = defaultMap, mapValue = defaultMap, filter = defaultFilter } = {
mapKey: defaultMap,
mapValue: defaultMap,
filter: defaultFilter
}
) {
const map = initial;
const orderedKeys = new FirebaseArray();
const map = observable.map({});
const orderedKeys = new FirebaseArray(observable.array([]));
const unsubChildAdded = ref.on("child_added", (v: any, previousKey: any) => {
const valueOrNull = !v ? null : v.val();
const keyOrNull = !v ? null : v.key;
Expand Down Expand Up @@ -97,5 +90,5 @@ export function toMap<K extends string, V>(
unsubChildMoved && unsubChildMoved();
};

return { value: map, unsub, keys: orderedKeys };
return { value: map, unsub, keys: orderedKeys.get() };
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3677,10 +3677,10 @@ find-versions@^2.0.0:
array-uniq "^1.0.0"
semver-regex "^1.0.0"

firebase-array@^1.0.0:
version "1.0.0"
resolved "https://registry.npmjs.org/firebase-array/-/firebase-array-1.0.0.tgz#b10f89b0622dd6bf1857a8031a3a90aac84eaf7b"
integrity sha512-fhkHX6qYqffG7P+bJhIg/Gel1W7zdrfbNvET+24cs13S0jRt4Lfv626M7If6GC5fnwznjUCwFOWxL9dHgQpvrg==
firebase-array@^1.1.1:
version "1.1.1"
resolved "https://registry.npmjs.org/firebase-array/-/firebase-array-1.1.1.tgz#dda4ced3307d31ae53760520fbcb4daa72b5f3d7"
integrity sha512-EGj5oyDpxjFGBL4oIQadH0LcVa0HjqufvZcKNC8EvaIemw3xLw4HBbLYxHpPUspH6OdbjNHNCCj9qqNHNWLNMw==

firebase@^5.5.3:
version "5.5.3"
Expand Down

0 comments on commit 08acb4a

Please sign in to comment.