From 6a6a8856066235a7eaf08acc9a08554f04609815 Mon Sep 17 00:00:00 2001 From: rakannimer Date: Mon, 12 Nov 2018 16:43:20 +0200 Subject: [PATCH] fix: update firebase array --- package.json | 2 +- src/to-map.ts | 28 +++++++++++++++++++++++----- yarn.lock | 8 ++++---- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 7ee0355..cfdeb94 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/to-map.ts b/src/to-map.ts index 1cdad46..5763926 100644 --- a/src/to-map.ts +++ b/src/to-map.ts @@ -1,4 +1,4 @@ -import { observable, ObservableMap } from "mobx"; +import { observable } from "mobx"; import FirebaseArray from "firebase-array"; function defaultMap(a: any) { return a; @@ -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; @@ -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 = () => { @@ -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 + }; } diff --git a/yarn.lock b/yarn.lock index 6e94dee..f59ee87 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"