Skip to content

Commit fec4b6e

Browse files
committed
Tidy up path usage throughout firestack in preparation for looking at storage
1 parent 3281c90 commit fec4b6e

File tree

8 files changed

+26
-99
lines changed

8 files changed

+26
-99
lines changed

lib/firestack.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -176,16 +176,6 @@ export default class Firestack extends Singleton {
176176
return promisify('serverValue', FirestackModule)();
177177
}
178178

179-
// TODO what are these for?
180-
get app(): Object {
181-
return this.appInstance;
182-
}
183-
184-
// TODO what are these for?
185-
getInstance(): Object {
186-
return this.appInstance;
187-
}
188-
189179
get apps(): Array<string> {
190180
return Object.keys(instances);
191181
}

lib/modules/base.js

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,6 @@ export class Base extends EventEmitter {
3535
return logs[this.namespace];
3636
}
3737

38-
// TODO unused - do we need this anymore?
39-
_addConstantExports(constants) {
40-
Object.keys(constants).forEach((name) => {
41-
FirestackModule[name] = constants[name];
42-
});
43-
}
44-
45-
// TODO unused - do we need this anymore?
46-
_addToFirestackInstance(...methods: Array<string>) {
47-
methods.forEach((name) => {
48-
this.firestack[name] = this[name].bind(this);
49-
});
50-
}
51-
5238
/**
5339
* app instance
5440
**/
@@ -91,26 +77,12 @@ export class Base extends EventEmitter {
9177
}
9278

9379
export class ReferenceBase extends Base {
94-
constructor(firestack: Object, path: Array<string> | string) {
80+
constructor(firestack: Object, path: string) {
9581
super(firestack);
96-
97-
this.path = Array.isArray(path) ? path : (typeof path === 'string' ? [path] : []);
98-
99-
// sanitize path, just in case
100-
this.path = this.path.filter(str => str !== '');
82+
this.path = path || '/';
10183
}
10284

10385
get key(): string {
104-
const path = this.path;
105-
return path.length === 0 ? '/' : path[path.length - 1];
106-
}
107-
108-
pathToString(): string {
109-
const path = this.path;
110-
let pathStr = (path.length > 0 ? path.join('/') : '/');
111-
if (pathStr[0] != '/') {
112-
pathStr = `/${pathStr}`;
113-
}
114-
return pathStr;
86+
return this.path === '/' ? null : this.path.substring(this.path.lastIndexOf('/') + 1);
11587
}
11688
}

lib/modules/database/disconnect.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default class Disconnect {
1717
}
1818

1919
setValue(val: string | Object) {
20-
const path = this.ref.dbPath();
20+
const path = this.ref._dbPath();
2121
if (typeof val === 'string') {
2222
return promisify('onDisconnectSetString', FirestackDatabase)(path, val);
2323
} else if (typeof val === 'object') {
@@ -26,10 +26,10 @@ export default class Disconnect {
2626
}
2727

2828
remove() {
29-
return promisify('onDisconnectRemove', FirestackDatabase)(this.ref.dbPath());
29+
return promisify('onDisconnectRemove', FirestackDatabase)(this.ref._dbPath());
3030
}
3131

3232
cancel() {
33-
return promisify('onDisconnectCancel', FirestackDatabase)(this.ref.dbPath());
33+
return promisify('onDisconnectCancel', FirestackDatabase)(this.ref._dbPath());
3434
}
3535
}

lib/modules/database/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export default class Database extends Base {
5858
* @param path
5959
* @returns {Reference}
6060
*/
61-
ref(...path: Array<string>) {
61+
ref(path: string) {
6262
return new Reference(this, path);
6363
}
6464

lib/modules/database/query.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
import { ReferenceBase } from './../base';
77
import Reference from './reference.js';
88

9-
// TODO why randomly 1000000? comments?
10-
let uid = 1000000;
11-
129
/**
1310
* @class Query
1411
*/
@@ -19,10 +16,9 @@ export default class Query extends ReferenceBase {
1916

2017
ref: Reference;
2118

22-
constructor(ref: Reference, path: Array<string>, existingModifiers?: Array<string>) {
19+
constructor(ref: Reference, path: string, existingModifiers?: Array<string>) {
2320
super(ref.db, path);
2421
this.log.debug('creating Query ', path, existingModifiers);
25-
this.uid = uid++; // uuid.v4();
2622
this.ref = ref;
2723
this.modifiers = existingModifiers ? [...existingModifiers] : [];
2824
}

lib/modules/database/reference.js

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default class Reference extends ReferenceBase {
2121
db: FirestackDatabase;
2222
query: Query;
2323

24-
constructor(db: FirestackDatabase, path: Array<string>, existingModifiers?: Array<string>) {
24+
constructor(db: FirestackDatabase, path: string, existingModifiers?: Array<string>) {
2525
super(db.firestack, path);
2626
this.db = db;
2727
this.namespace = 'firestack:db:ref';
@@ -77,9 +77,8 @@ export default class Reference extends ReferenceBase {
7777
*/
7878
push(value: any, onComplete: Function) {
7979
if (value === null || value === undefined) {
80-
// todo add server timestamp to push id call.
81-
const _paths = this.path.concat([generatePushID(this.db.serverTimeOffset)]);
82-
return new Reference(this.db, _paths);
80+
const _path = this.path + '/' + generatePushID(this.db.serverTimeOffset);
81+
return new Reference(this.db, _path);
8382
}
8483

8584
const path = this._dbPath();
@@ -253,19 +252,12 @@ export default class Reference extends ReferenceBase {
253252
return newRef;
254253
}
255254

256-
// TODO why is this presence here on DB ref? its unrelated?
257-
presence(path: string) {
258-
const presence = this.firestack.presence;
259-
const ref = path ? this.child(path) : this;
260-
return presence.ref(ref, this._dbPath());
261-
}
262-
263255
onDisconnect() {
264256
return new Disconnect(this);
265257
}
266258

267259
child(path: string) {
268-
return new Reference(this.db, this.path.concat(path.split('/')));
260+
return new Reference(this.db, this.path + '/' + path);
269261
}
270262

271263
toString(): string {
@@ -276,22 +268,13 @@ export default class Reference extends ReferenceBase {
276268
* GETTERS
277269
*/
278270

279-
/**
280-
* Returns the current key of this ref - i.e. /foo/bar returns 'bar'
281-
* @returns {*}
282-
*/
283-
get key(): string|null {
284-
if (!this.path.length) return null;
285-
return this.path.slice(this.path.length - 1, this.path.length)[0];
286-
}
287-
288271
/**
289272
* Returns the parent ref of the current ref i.e. a ref of /foo/bar would return a new ref to '/foo'
290273
* @returns {*}
291274
*/
292275
get parent(): Reference|null {
293-
if (!this.path.length || this.path.length === 1) return null;
294-
return new Reference(this.db, this.path.slice(0, -1));
276+
if (this.path === '/') return null;
277+
return new Reference(this.db, this.path.substring(0, this.path.lastIndexOf('/')));
295278
}
296279

297280

@@ -300,22 +283,15 @@ export default class Reference extends ReferenceBase {
300283
* @returns {Reference}
301284
*/
302285
get root(): Reference {
303-
return new Reference(this.db, []);
286+
return new Reference(this.db, '/');
304287
}
305288

306289
/**
307290
* INTERNALS
308291
*/
309292

310-
_dbPath(paths?: Array<string>): string {
311-
const path = paths || this.path;
312-
const pathStr = (path.length > 0 ? path.join('/') : '/');
313-
314-
if (pathStr[0] !== '/') {
315-
return `/${pathStr}`;
316-
}
317-
318-
return pathStr;
293+
_dbPath(): string {
294+
return this.path;
319295
}
320296

321297
/**

lib/modules/storage/index.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,8 @@ export default class Storage extends Base {
1818
this.refs = {};
1919
}
2020

21-
ref(...path: Array<string>): StorageRef {
22-
const key = this._pathKey(...path);
23-
if (!this.refs[key]) {
24-
this.refs[key] = new StorageRef(this, path);
25-
}
26-
return this.refs[key];
21+
ref(path: string): StorageRef {
22+
return new StorageRef(this, path);
2723
}
2824

2925
/**

lib/modules/storage/reference.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,16 @@ import Storage from './';
88
const FirestackStorage = NativeModules.FirestackStorage;
99

1010
export default class StorageRef extends ReferenceBase {
11-
constructor(storage: Storage, path: Array<string>) {
11+
constructor(storage: Storage, path: string) {
1212
super(storage.firestack, path);
13-
1413
this.storage = storage;
1514
}
1615

1716
downloadUrl(): Promise<Object> {
18-
const path = this.pathToString();
19-
this.log.debug('downloadUrl(', path, ')');
20-
return promisify('downloadUrl', FirestackStorage)(path)
17+
this.log.debug('downloadUrl(', this.path, ')');
18+
return promisify('downloadUrl', FirestackStorage)(this.path)
2119
.catch((err) => {
22-
this.log.error('Error downloading URL for ', path, '. Error: ', err);
20+
this.log.error('Error downloading URL for ', this.path, '. Error: ', err);
2321
throw err;
2422
});
2523
}
@@ -31,22 +29,21 @@ export default class StorageRef extends ReferenceBase {
3129
* @return {Promise}
3230
*/
3331
download(downloadPath: string, listener: Function = noop): Promise<Object> {
34-
const path = this.pathToString();
35-
this.log.debug('download(', path, ') -> ', downloadPath);
32+
this.log.debug('download(', this.path, ') -> ', downloadPath);
3633
const listeners = [
3734
this.storage._addListener('download_progress', listener),
3835
this.storage._addListener('download_paused', listener),
3936
this.storage._addListener('download_resumed', listener),
4037
];
4138

42-
return promisify('downloadFile', FirestackStorage)(path, downloadPath)
39+
return promisify('downloadFile', FirestackStorage)(this.path, downloadPath)
4340
.then((res) => {
4441
this.log.debug('res --->', res);
4542
listeners.forEach(l => l.remove());
4643
return res;
4744
})
4845
.catch((err) => {
49-
this.log.error('Error downloading ', path, ' to ', downloadPath, '. Error: ', err);
46+
this.log.error('Error downloading ', this.path, ' to ', downloadPath, '. Error: ', err);
5047
throw err;
5148
});
5249
}

0 commit comments

Comments
 (0)