Skip to content

Commit

Permalink
fix: update firebase array
Browse files Browse the repository at this point in the history
  • Loading branch information
rakannimer committed Nov 12, 2018
1 parent 4b37cec commit 6a6a885
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
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.1.1",
"firebase-array": "^1.1.2",
"get-firebase-ref": "^1.1.1",
"initialize-firebase-app": "^1.0.0",
"lodash.memoize": "^4.1.2"
Expand Down
28 changes: 23 additions & 5 deletions src/to-map.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { observable, ObservableMap } from "mobx";
import { observable } from "mobx";
import FirebaseArray from "firebase-array";
function defaultMap(a: any) {
return a;
Expand All @@ -22,14 +22,21 @@ export function toMap(
}
) {
const map = observable.map({});
const orderedKeys = new FirebaseArray(observable.array([]));
const orderedKeys = new FirebaseArray(
observable.array([]),
observable.array([])
);
const unsubChildAdded = ref.on("child_added", (v: any, previousKey: any) => {
const valueOrNull = !v ? null : v.val();
const keyOrNull = !v ? null : v.key;
const previousKeyOrNull = !previousKey ? null : previousKey;
const currentMapKey = mapKey(keyOrNull);
const currentMapValue = mapValue(valueOrNull);
orderedKeys.childAdded(currentMapKey, mapKey(previousKeyOrNull));
orderedKeys.childAdded(
currentMapKey,
currentMapValue,
mapKey(previousKeyOrNull)
);
if (!map.has(currentMapKey)) {
map.set(currentMapKey, observable.box(currentMapValue));
return;
Expand Down Expand Up @@ -77,10 +84,16 @@ export function toMap(
});

const unsubChildMoved = ref.on("child_moved", (v: any, previousKey: any) => {
const valueOrNull = !v ? null : v.val();
const keyOrNull = !v ? null : v.key;
const currentMapKey = mapKey(keyOrNull);
const currentMapValue = mapValue(valueOrNull);
const previousKeyOrNull = !previousKey ? null : previousKey;
orderedKeys.childMoved(currentMapKey, mapKey(previousKeyOrNull));
orderedKeys.childMoved(
currentMapKey,
currentMapValue,
mapKey(previousKeyOrNull)
);
});

const unsub = () => {
Expand All @@ -90,5 +103,10 @@ export function toMap(
unsubChildMoved && unsubChildMoved();
};

return { value: map, unsub, keys: orderedKeys.get() };
return {
value: map,
unsub,
keys: orderedKeys.get().keys,
values: orderedKeys.get().values
};
}
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.1.1:
version "1.1.1"
resolved "https://registry.npmjs.org/firebase-array/-/firebase-array-1.1.1.tgz#dda4ced3307d31ae53760520fbcb4daa72b5f3d7"
integrity sha512-EGj5oyDpxjFGBL4oIQadH0LcVa0HjqufvZcKNC8EvaIemw3xLw4HBbLYxHpPUspH6OdbjNHNCCj9qqNHNWLNMw==
firebase-array@^1.1.2:
version "1.1.2"
resolved "https://registry.npmjs.org/firebase-array/-/firebase-array-1.1.2.tgz#7d479358b7991ba0b17fc18468a8c968eebb2432"
integrity sha512-lpzcwLz8Tfbv4w5oU+oOtKzmFIBDcO6g5gwqdjCp9rdx05Fm0ZqvPO7KPZ5Dve+OaZL9hIBnV4WZv+MB/Xx37Q==

firebase@^5.5.3:
version "5.5.3"
Expand Down

0 comments on commit 6a6a885

Please sign in to comment.