-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement initial default adapter for upcoming adapter pattern #51
Changes from 16 commits
c9c2a13
396fded
594737f
eb7f531
4dda476
daf3c01
f3c7017
ab39813
5807638
3ee071f
4fe6296
31f9d98
2656bf3
5e50c93
a0ea814
ab98fe0
201b48f
a1be14f
2785ac7
b0d6aba
424dd71
1c56c06
e13303e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
const { StorageManager } = require('brainstem-js'); | ||
const defaultAdapter = require('../adapters/default'); | ||
|
||
const xhrs = {}; | ||
|
||
|
@@ -9,24 +9,30 @@ module.exports = { | |
postFetchAction, | ||
preFetchAction, | ||
trackKey, | ||
adapter = defaultAdapter, | ||
} = options; | ||
|
||
const storageManager = StorageManager.get(); | ||
|
||
if (xhrs[trackKey] && xhrs[trackKey].state() === 'pending') xhrs[trackKey].abort(); | ||
|
||
return (dispatch) => { | ||
return (dispatch, getState) => { | ||
if (preFetchAction) dispatch(preFetchAction); | ||
|
||
const xhr = storageManager.storage(brainstemKey) | ||
.fetch(Object.assign(fetchOptions, { remove: false })); | ||
const xhr = adapter.fetchCollection(brainstemKey, { | ||
dispatch, | ||
getState, | ||
fetchOptions, | ||
}); | ||
|
||
if (postFetchAction) xhr.done(collection => dispatch(postFetchAction(collection.pluck('id')))); | ||
if (postFetchAction) { | ||
xhr.done(collection => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have our mavenlink adapter return a deferred object like so:
|
||
dispatch(postFetchAction(adapter.collectionToIds(collection))) | ||
); | ||
} | ||
|
||
if (trackKey) xhrs[trackKey] = xhr; | ||
|
||
return new Promise((resolve, reject) => xhr | ||
.done(collection => resolve(collection.map(model => model.attributes))) | ||
.done(collection => resolve(adapter.collectionToArray(collection))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the use-case for overriding There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are delegating the handling of items in a collection to the adapter. Brainstem redux is now indifferent to the shape of the collection being passed in. |
||
.fail(reject) | ||
); | ||
}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const { StorageManager } = require('brainstem-js'); | ||
|
||
module.exports = { | ||
fetchCollection: (brainstemKey, options) => { | ||
const storageManager = StorageManager.get(); | ||
return storageManager.storage(brainstemKey) | ||
.fetch(Object.assign({}, options.fetchOptions, { remove: false })); | ||
}, | ||
fetchModel: (brainstemKey, modelId, options) => { | ||
const storageManager = StorageManager.get(); | ||
const Model = storageManager.storage(brainstemKey).model; | ||
return new Model({ id: modelId }) | ||
.fetch(Object.assign({}, options.fetchOptions, { remove: false })); | ||
}, | ||
saveModel: (brainstemKey, modelId, attributes, options) => { | ||
const storageManager = StorageManager.get(); | ||
const Model = storageManager.storage(brainstemKey).model; | ||
return new Model({ id: modelId }) | ||
.save(attributes, options.saveOptions); | ||
}, | ||
destroyModel: (brainstemKey, modelId, options) => { | ||
const storageManager = StorageManager.get(); | ||
const Model = storageManager.storage(brainstemKey).model; | ||
return new Model({ id: modelId }).destroy(options.deleteOptions); | ||
}, | ||
validateModel: (brainstemKey, attributes, options) => { | ||
const storageManager = StorageManager.get(); | ||
const Model = storageManager.storage(brainstemKey).model; | ||
return new Model().validate(attributes, options.validateOptions); | ||
}, | ||
collectionToIds: collection => collection.pluck('id'), | ||
collectionToArray: collection => collection.map(model => model.attributes), | ||
modelToId: model => model.get('id'), | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module.exports = (state, action) => { | ||
const mergedCollections = {}; | ||
const { collections } = action.payload; | ||
const collectionNames = Object.keys(collections); | ||
|
||
collectionNames.forEach((collectionName) => { | ||
mergedCollections[collectionName] = Object.assign( | ||
{}, | ||
state[collectionName], | ||
collections[collectionName], | ||
); | ||
}); | ||
|
||
return Object.assign({}, state, mergedCollections); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,16 @@ const defaultTypeOptions = { | |
}; | ||
|
||
module.exports = function makeBrainstemType(brainstemKey, typeOptions = defaultTypeOptions) { | ||
const mergedOptions = { ...defaultTypeOptions, ...typeOptions }; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
const buildActionOptions = (options) => { | ||
const { adapter } = mergedOptions; | ||
return adapter ? { ...options, adapter } : options; | ||
}; | ||
|
||
function all(state) { | ||
return Object.keys(state.brainstem[brainstemKey]) | ||
.filter(id => typeOptions.filterPredicate(state.brainstem[brainstemKey][id])) | ||
.filter(id => mergedOptions.filterPredicate(state.brainstem[brainstemKey][id])) | ||
.reduce((memo, id) => { | ||
memo[id] = state.brainstem[brainstemKey][id]; // eslint-disable-line no-param-reassign | ||
return memo; | ||
|
@@ -37,19 +44,19 @@ module.exports = function makeBrainstemType(brainstemKey, typeOptions = defaultT | |
} | ||
|
||
function fetchAll(options) { | ||
return collectionActions.fetch(brainstemKey, options); | ||
return collectionActions.fetch(brainstemKey, buildActionOptions(options)); | ||
} | ||
|
||
function fetch(id, options) { | ||
return modelActions.fetch(brainstemKey, id, options); | ||
return modelActions.fetch(brainstemKey, id, buildActionOptions(options)); | ||
} | ||
|
||
function save(id, attributes, options) { | ||
return modelActions.save(brainstemKey, id, attributes, options); | ||
return modelActions.save(brainstemKey, id, attributes, buildActionOptions(options)); | ||
} | ||
|
||
function destroy(id, options) { | ||
return modelActions.destroy(brainstemKey, id, options); | ||
return modelActions.destroy(brainstemKey, id, buildActionOptions(options)); | ||
} | ||
|
||
function matchesAction({ meta, payload }) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removing the dependency to the
StorageManager
is fantastic! I know it's outta scope so I made a follow-up issue about removing it from the defaults as well:#52