Created
December 13, 2015 19:23
-
-
Save jadehopepunk/57d693a4ca39ab6aca30 to your computer and use it in GitHub Desktop.
Model enrichment on the client side for GetStream.io
This file contains hidden or 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
import ModelStore from './model_store' | |
class ModelEnricher { | |
constructor() { | |
this.modelStore = new ModelStore(); | |
} | |
enrichAggregateResult(result, onSuccess) { | |
const modelKeys = this.modelKeys(result.results); | |
this.modelStore.loadModels(modelKeys, (results) => { | |
console.log('results', results); | |
}); | |
} | |
modelKeys(data) { | |
const result = this._traverseModelKeys(data); | |
return _.uniq(_.compact(result)); | |
} | |
// PRIVATE | |
_traverseModelKeys(data) { | |
if (_.isString(data)) { | |
return this._buildEnrichable(data); | |
} else { | |
return _.flatten(_.map(data, (value) => { | |
return this._traverseModelKeys(value); | |
}), false); | |
} | |
} | |
_buildEnrichable(string) { | |
return /^[a-z]+\:[0-9]+$/i.test(string) ? string : null; | |
} | |
} | |
export default ModelEnricher; |
This file contains hidden or 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
class ModelFetcher { | |
fetchModels(modelKeys, onSuccess) { | |
$.ajax({ | |
type: "GET", | |
url: '/enrichment.json', | |
data: {keys: modelKeys}, | |
success: (data) => { | |
console.log('success', data) | |
}, | |
error: (xhr, status, error) => { | |
console.log(`error fetching models (${status}): ${error}`) | |
} | |
}); | |
} | |
} | |
export default ModelFetcher; |
This file contains hidden or 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
import ModelFetcher from './model_fetcher' | |
class ModelStore { | |
constructor() { | |
this.store = {}; | |
this.fetcher = new ModelFetcher(); | |
} | |
loadModels(modelKeys, onSuccess) { | |
var cachedResults = this.cachedModels(modelKeys); | |
var remainingKeys = _.xor(modelKeys, Object.keys(cachedResults)); | |
if (remainingKeys.length > 0) { | |
this.fetcher.fetchModels(remainingKeys, (fetchedResults) => { | |
this.cacheModels(fetchedResults); | |
var mergedResults = _.merge(cachedResults, fetchedResults); | |
onSuccess(mergedResults); | |
}); | |
} else { | |
onSuccess(cachedResults); | |
} | |
} | |
cachedModels(modelKeys) { | |
var results = {}; | |
_.forEach(modelKeys, (key) => { | |
var model = this.cachedModel(key); | |
if (model) { | |
results[key] = model; | |
} | |
}); | |
return results; | |
} | |
cachedModel(key) { | |
var parts = key.split(':'); | |
const type = parts[0]; | |
const id = parseInt(parts[1]); | |
return this.store[type] ? this.store[type][id] : null; | |
} | |
cacheModels(modelHash) { | |
_.forEach(modelHash, (model, modelKey) => { | |
this.cacheModel(modelKey, model); | |
}) | |
} | |
cacheModel(modelKey, model) { | |
var parts = modelKey.split(':'); | |
const type = parts[0]; | |
const id = parseInt(parts[1]); | |
if (!this.store[type]) { | |
this.store[type] = {}; | |
} | |
this.store[type][id] = model; | |
} | |
} | |
export default ModelStore; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment