Skip to content

Commit

Permalink
feat: 🎸 Memoize exported method by firebase config
Browse files Browse the repository at this point in the history
  • Loading branch information
rakannimer committed Oct 10, 2018
1 parent 82acded commit 83064f7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 22 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"license": "MIT",
"devDependencies": {
"@types/jest": "^23.3.3",
"@types/lodash.memoize": "^4.1.4",
"comment-json": "^1.1.3",
"firebase": "^5.5.3",
"git-cz": "^1.7.1",
Expand Down Expand Up @@ -56,6 +57,7 @@
"description": "Tame your firebase database with MobX",
"dependencies": {
"get-firebase-ref": "^1.1.1",
"initialize-firebase-app": "^1.0.0"
"initialize-firebase-app": "^1.0.0",
"lodash.memoize": "^4.1.2"
}
}
49 changes: 28 additions & 21 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
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 All @@ -14,27 +16,32 @@ export type GetMobxFireArgs = {
firebase: any;
};

export function getMobxFire({ config, firebase }: GetMobxFireArgs) {
initializeFirebaseApp({ ...config, firebase });
let refs = [] as any[];
const destroy = () => {
for (let ref of refs) {
ref.off();
}
};
const _getFirebaseRef = (query: FirebaseQuery) => {
const ref = getFirebaseRef({ firebase, ...query });
refs.push(ref);
return ref;
};
export const getMobxFire = memoize(
({ config, firebase }: GetMobxFireArgs) => {
initializeFirebaseApp({ ...config, firebase });
let refs = [] as any[];
const destroy = () => {
for (let ref of refs) {
ref.off();
}
};
const _getFirebaseRef = (query: FirebaseQuery) => {
const ref = getFirebaseRef({ firebase, ...query });
refs.push(ref);
return ref;
};

return {
toArray,
toBox,
toMap,
getFirebaseRef: _getFirebaseRef,
destroy
};
}
return {
toArray,
toBox,
toMap,
getFirebaseRef: _getFirebaseRef,
destroy
};
},
({ config, firebase }) => {
return config;
}
);

export default getMobxFire;
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,18 @@
resolved "https://registry.npmjs.org/@types/jest/-/jest-23.3.3.tgz#246ebcc52771d2327bb8e37aa971b412d9dc4237"
integrity sha512-G6EBrbjWDfmIpYu8UcRBOhwtDiYaLj5N5jUR5rx0YvbKxRBhXPZVLUmtfShewSUNKiQwpHavpML69a2WMbIlEQ==

"@types/lodash.memoize@^4.1.4":
version "4.1.4"
resolved "https://registry.npmjs.org/@types/lodash.memoize/-/lodash.memoize-4.1.4.tgz#d810e7cf438d322074b86bf3cfc14f8b5186619e"
integrity sha512-3u6RDBxMGQ+XM8lOD+Of5D1c4UZNwo5GmrYAIar6bruSpNb3feECCmG2Dr23/uZTCcEoGrMe/r84nelum3NpUQ==
dependencies:
"@types/lodash" "*"

"@types/lodash@*":
version "4.14.117"
resolved "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.117.tgz#695a7f514182771a1e0f4345d189052ee33c8778"
integrity sha512-xyf2m6tRbz8qQKcxYZa7PA4SllYcay+eh25DN3jmNYY6gSTL7Htc/bttVdkqj2wfJGbeWlQiX8pIyJpKU+tubw==

"@types/node@*":
version "10.1.2"
resolved "https://registry.npmjs.org/@types/node/-/node-10.1.2.tgz#1b928a0baa408fc8ae3ac012cc81375addc147c6"
Expand Down

0 comments on commit 83064f7

Please sign in to comment.