Created
July 5, 2016 15:49
-
-
Save tonypee/c2dcf22a1f3075e388fc0a7454a88ef6 to your computer and use it in GitHub Desktop.
Standalone Aurelia Reactive Collection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable */ | |
/* | |
I wanted to remove all dependancies, and update to firebase v3 | |
Forked from: https://github.com/PulsarBlow/aurelia-firebase/blob/master/dist/es6/configuration.js | |
*/ | |
export class ReactiveCollection { | |
_query = null; | |
_valueMap = new Map(); | |
items = []; | |
constructor(ref) { | |
this._ref = ref | |
this._listenToQuery(ref); | |
} | |
add(item) { | |
return new Promise((resolve, reject) => { | |
let query = this._ref.push(); | |
query.set(item, (error) => { | |
if (error) { | |
reject(error); | |
return; | |
} | |
resolve(item); | |
}); | |
}); | |
} | |
remove(item) { | |
if (item === null || item.__firebaseKey__ === null) { | |
return Promise.reject({message: 'Unknown item'}); | |
} | |
return this.removeByKey(item.__firebaseKey__); | |
} | |
getByKey(key) { | |
return this._valueMap.get(key); | |
} | |
removeByKey(key) { | |
return new Promise((resolve, reject) => { | |
this._ref.child(key).remove((error) =>{ | |
if (error) { | |
reject(error); | |
return; | |
} | |
resolve(key); | |
}); | |
}); | |
} | |
clear() { | |
//this._stopListeningToQuery(this._query); | |
return new Promise((resolve, reject) => { | |
let query = this._ref; | |
query.remove((error) => { | |
if (error) { | |
reject(error); | |
return; | |
} | |
resolve(); | |
}); | |
}); | |
} | |
_listenToQuery(query) { | |
query.on('child_added', (snapshot, previousKey) => { | |
this._onItemAdded(snapshot, previousKey); | |
}); | |
query.on('child_removed', (snapshot) => { | |
this._onItemRemoved(snapshot); | |
}); | |
query.on('child_changed', (snapshot, previousKey) => { | |
this._onItemChanded(snapshot, previousKey); | |
}); | |
query.on('child_moved', (snapshot, previousKey) => { | |
this._onItemMoved(snapshot, previousKey); | |
}); | |
} | |
_stopListeningToQuery(query) { | |
query.off(); | |
} | |
_onItemAdded(snapshot, previousKey) { | |
let value = this._valueFromSnapshot(snapshot); | |
let index = previousKey !== null ? | |
this.items.indexOf(this._valueMap.get(previousKey)) + 1 : 0; | |
this._valueMap.set(value.__firebaseKey__, value); | |
this.items.splice(index, 0, value); | |
} | |
_onItemRemoved(oldSnapshot) { | |
let key = oldSnapshot.key(); | |
let value = this._valueMap.get(key); | |
if (!value) { | |
return; | |
} | |
let index = this.items.indexOf(value); | |
this._valueMap.delete(key); | |
if (index !== -1) { | |
this.items.splice(index, 1); | |
} | |
} | |
_onItemChanged(snapshot, previousKey) { | |
let value = this._valueFromSnapshot(snapshot); | |
let oldValue = this._valueMap.get(value.__firebaseKey__); | |
if (!oldValue) { | |
return; | |
} | |
this._valueMap.delete(oldValue.__firebaseKey__); | |
this._valueMap.set(value.__firebaseKey__, value); | |
this.items.splice(this.items.indexOf(oldValue), 1, value); | |
} | |
_onItemMoved(snapshot, previousKey) { | |
let key = snapshot.key(); | |
let value = this._valueMap.get(key); | |
if (!value) { | |
return; | |
} | |
let previousValue = this._valueMap.get(previousKey); | |
let newIndex = previousValue !== null ? this.items.indexOf(previousValue) + 1 : 0; | |
this.items.splice(this.items.indexOf(value), 1); | |
this.items.splice(newIndex, 0, value); | |
} | |
_valueFromSnapshot(snapshot) { | |
let value = snapshot.val(); | |
if (!(value instanceof Object)) { | |
value = { | |
value: value, | |
__firebasePrimitive__: true | |
}; | |
} | |
value.__firebaseKey__ = snapshot.key; | |
return value; | |
} | |
static _getChildLocation(root, path) { | |
if (!path) { | |
return root; | |
} | |
if (!root.endsWith('/')) { | |
root = root + '/'; | |
} | |
return root + (Array.isArray(path) ? path.join('/') : path); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment