-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from galenwarren/remove-firebase
refactor: refactor to simplify
- Loading branch information
Showing
12 changed files
with
549 additions
and
449 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { Observable } from 'rxjs'; | ||
import objectPath from 'object-path'; | ||
import { SET_MUTATION, DELETE_MUTATION } from './store'; | ||
import { DEFAULT_DISPOSE_DELAY } from './constants'; | ||
|
||
export class VuexRxSyncConfig { | ||
constructor(store, options = {}) { | ||
this.store = store; | ||
Object.assign(this, options); | ||
} | ||
|
||
get disposeDelay() { | ||
return DEFAULT_DISPOSE_DELAY; | ||
} | ||
|
||
getStoreValue(path) { | ||
return objectPath.get(this.store.state, path); | ||
} | ||
|
||
setStoreValue(path, value) { | ||
this.store.commit(SET_MUTATION, { path, value }); | ||
} | ||
|
||
deleteStoreValue(path) { | ||
this.store.commit(DELETE_MUTATION, { path }); | ||
} | ||
|
||
createStoreObservable(path) { | ||
// ensure reactive | ||
this.setStoreValue(path, this.getStoreValue(path)); | ||
|
||
// the observable for the value in the store we have just ensured exists | ||
return Observable.create(subscriber => { | ||
return this.store.watch( | ||
() => this.getStoreValue(path), | ||
value => subscriber.next(value) | ||
); | ||
}); | ||
} | ||
|
||
shouldDeleteStoreValue(path, value) { | ||
return value === undefined; | ||
} | ||
|
||
updateStore(path, value) { | ||
if (this.shouldDeleteStoreValue(path, value)) { | ||
this.deleteStoreValue(path); | ||
} else { | ||
this.setStoreValue(path, value); | ||
} | ||
} | ||
|
||
resetStore(path) { | ||
this.deleteStoreValue(path); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const DEFAULT_DISPOSE_DELAY = 3000; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { VueRxSync } from './plugin'; | ||
export { rxSyncMutations } from './store'; | ||
export { addAttribute } from './operators'; | ||
export { VuexRxSync } from './plugin'; | ||
export { VuexRxSyncConfig } from './config'; | ||
export { vuexRxSyncMutations } from './store'; | ||
export { addAttribute, resolve } from './operators'; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,45 @@ | ||
import { combineLatest, of } from 'rxjs'; | ||
import { pluck, switchMap } from 'rxjs/operators'; | ||
import { pluck } from 'rxjs/operators'; | ||
import { syncObservableRefCounted } from './sync'; | ||
import { findObservable } from './util'; | ||
import { resolve, addAttribute } from './operators'; | ||
|
||
/** | ||
* Watches a value through the provided vm, returning the current value | ||
* and just returning the new values | ||
*/ | ||
export const watchFactory = vm => target => { | ||
return vm | ||
.$watchAsObservable(target, { immediate: true }) | ||
.pipe(pluck('newValue')); | ||
}; | ||
|
||
export const resolve = (value$, getKeys, getObservable) => { | ||
return value$.pipe( | ||
switchMap(value => { | ||
const keys = getKeys(value); | ||
if (keys && keys.length) { | ||
return combineLatest(keys.map(getObservable)); | ||
} else { | ||
return of([]); | ||
} | ||
}) | ||
); | ||
}; | ||
export function watchFactory(vm) { | ||
return target => | ||
vm.$watchAsObservable(target, { immediate: true }).pipe(pluck('newValue')); | ||
} | ||
|
||
export const rxSync = options => { | ||
const sync = syncObservableRefCounted(options); | ||
return function() { | ||
return { sync, resolve, watch: watchFactory(this) }; | ||
}; | ||
}; | ||
|
||
// kgw consider contributing this to vue-rx? | ||
export const getObservable = function(name) { | ||
if (this.$observables) { | ||
const observable = this.$observables[name]; | ||
if (observable) { | ||
return observable; | ||
} | ||
} | ||
if (this.$parent) { | ||
return this.$parent.$getObservable(name); | ||
} | ||
return null; | ||
}; | ||
|
||
export const VueRxSync = { | ||
export const VuexRxSync = { | ||
install(Vue, options) { | ||
// kgw remove if contribute to vue-rx | ||
Vue.prototype.$getObservable = getObservable; | ||
|
||
// create the sync function to expose here | ||
const sync = syncObservableRefCounted(options); | ||
|
||
Vue.prototype.$rxSync = function() { | ||
return { | ||
sync, | ||
resolve, | ||
watch: watchFactory(this), | ||
}; | ||
}; | ||
// the base context information, everything but 'watch' | ||
const context = { sync, resolve, addAttribute }; | ||
|
||
// assign the $findObservable helper | ||
Vue.prototype.$findObservable = findObservable; | ||
|
||
// assign the $rxSync property | ||
Object.defineProperty(Vue.prototype, '$rxSync', { | ||
get: function() { | ||
return Object.assign({}, context, { | ||
watch: watchFactory(this), | ||
}); | ||
}, | ||
}); | ||
|
||
// the vm we use for off-component watching | ||
let vmWatch = null; | ||
|
||
Vue.$rxSync = { | ||
sync, | ||
resolve, | ||
// assing a global $rxSync with a watch that uses the | ||
// Vue instance above | ||
Vue.$rxSync = Object.assign({}, context, { | ||
get watch() { | ||
if (!vmWatch) { | ||
vmWatch = new Vue(); | ||
} | ||
return watchFactory(vmWatch); | ||
}, | ||
}; | ||
}); | ||
}, | ||
}; |
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
Oops, something went wrong.