-
Notifications
You must be signed in to change notification settings - Fork 16
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
Fixes #133, creates API for Maps and Lists for consistent data checking #154
Open
imjoshdean
wants to merge
6
commits into
master
Choose a base branch
from
133-isFromCache
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b9ce9ee
Add attribute for Maps and Lists when using fall-through-cache and c…
imjoshdean 00adf7e
Fix linting issues.
imjoshdean cc6f540
Call base connection's init.
imjoshdean 921bb4b
Add inconsistencyReason attribute to maps and lists.
imjoshdean 87d4a8b
Fix documentation issues
imjoshdean 900d493
Forgot group fix
imjoshdean File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
* @parent can-connect.behaviors | ||
* @group can-connect/fall-through-cache/fall-through-cache.data Data Callbacks | ||
* @group can-connect/fall-through-cache/fall-through-cache.hydrators Hydrators | ||
* @group can-connect/fall-through-cache/fall-through-cache.mixins mixins Mixins | ||
* | ||
* A fall through cache that checks another `cacheConnection`. | ||
* | ||
|
@@ -72,9 +73,112 @@ | |
var connect = require("can-connect"); | ||
var sortedSetJSON = require("../helpers/sorted-set-json"); | ||
|
||
var canEvent = require("can-event"); | ||
var Observation = require("can-observation"); | ||
|
||
module.exports = connect.behavior("fall-through-cache",function(baseConnect){ | ||
var setExpando = function(map, prop, value) { | ||
if("attr" in map) { | ||
map[prop] = value; | ||
} else { | ||
map._data[prop] = value; | ||
} | ||
}; | ||
var getExpando = function(map, prop) { | ||
if("attr" in map) { | ||
return map[prop]; | ||
} else { | ||
return map._data[prop]; | ||
} | ||
}; | ||
|
||
var overwrite = function( connection, Constructor, prototype, statics) { | ||
var prop; | ||
|
||
for(prop in prototype) { | ||
Constructor.prototype[prop] = prototype[prop](Constructor.prototype[prop], connection); | ||
} | ||
if(statics) { | ||
for(prop in statics) { | ||
Constructor[prop] = statics[prop](Constructor[prop], connection); | ||
} | ||
} | ||
}; | ||
|
||
var addIsConsistent = function(connection, Constructor) { | ||
overwrite(connection, Constructor, { | ||
|
||
_setInconsistencyReason: function(base, connection) { | ||
return function(error) { | ||
var oldError = getExpando(this, "inconsistencyReason"); | ||
|
||
setExpando(this, "inconsistencyReason", error); | ||
canEvent.dispatch.call(this, "inconsistencyReason", [error, oldError]); | ||
} | ||
}, | ||
|
||
_setIsConsistent: function(base, connection) { | ||
return function(isConsistent) { | ||
setExpando(this, "_isConsistent", isConsistent); | ||
canEvent.dispatch.call(this, "_isConsistent", [isConsistent, !isConsistent]); | ||
}; | ||
}, | ||
/** | ||
* @function can-connect/fall-through-cache/fall-through-cache.isConsistent isConsistent | ||
* @parent can-connect/fall-through-cache/fall-through-cache.mixins | ||
* | ||
* Returns whether or not the data is consistent between the server and | ||
* the fall-through-cache data. | ||
* | ||
* @signature `[map|list].isConsistent()` | ||
* | ||
* Returns true if the data has been successfully returned from the | ||
* server and in sync with the fall-through-cache. Returns false if the | ||
* data is from the cache. | ||
* | ||
* @return {Boolean} | ||
*/ | ||
isConsistent: function(base, connection) { | ||
return function() { | ||
Observation.add(this,"_isConsistent"); | ||
return !!getExpando(this, "_isConsistent"); | ||
}; | ||
} | ||
/** | ||
* @function can-connect/fall-through-cache/fall-through-cache.inconsistencyReason inconsistencyReason | ||
* @parent can-connect/fall-through-cache/fall-through-cache.mixins | ||
* | ||
* Returns the error of the AJAX call from the base data layer. | ||
* | ||
* @signature `[map|list].inconsistencyReason` | ||
* | ||
* Returns the error fo the base data layer's AJAX call if it's promise | ||
* was rejected. If there isn't an inconsistency issue between the server | ||
* and fall-through-cache layer, this will be undefined. | ||
* | ||
* @return {Object} | ||
*/ | ||
}); | ||
}; | ||
|
||
var behavior = { | ||
init: function() { | ||
// If List and Map are on the behavior, then we go ahead and add the | ||
// isConsistent API information. | ||
if(this.List) { | ||
addIsConsistent(this, this.List); | ||
} | ||
if(this.Map) { | ||
addIsConsistent(this, this.Map); | ||
} | ||
|
||
baseConnect.init.apply(this, arguments); | ||
}, | ||
_setIsConsistent: function(instance, isConsistent) { | ||
if(instance._setIsConsistent) { | ||
instance._setIsConsistent(isConsistent); | ||
} | ||
}, | ||
/** | ||
* @function can-connect/fall-through-cache/fall-through-cache.hydrateList hydrateList | ||
* @parent can-connect/fall-through-cache/fall-through-cache.hydrators | ||
|
@@ -147,22 +251,25 @@ module.exports = connect.behavior("fall-through-cache",function(baseConnect){ | |
set = set || {}; | ||
var self = this; | ||
return this.cacheConnection.getListData(set).then(function(data){ | ||
|
||
// get the list that is going to be made | ||
// it might be possible that this never gets called, but not right now | ||
self._getHydrateList(set, function(list){ | ||
|
||
self._setIsConsistent(list, false); | ||
self.addListReference(list, set); | ||
|
||
setTimeout(function(){ | ||
baseConnect.getListData.call(self, set).then(function(listData){ | ||
|
||
self._setInconsistencyReason(list, undefined); | ||
self.cacheConnection.updateListData(listData, set); | ||
self.updatedList(list, listData, set); | ||
self._setIsConsistent(list, true); | ||
self.deleteListReference(list, set); | ||
|
||
}, function(e){ | ||
// what do we do here? self.rejectedUpdatedList ? | ||
console.log("REJECTED", e); | ||
console.error("baseConnect.getListData rejected", e); | ||
self.rejectedUpdatedInstance(list, e); | ||
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. I think this should be 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. I don't, "instance" was the best neutral name I could come up with to apply to both a map and a list. |
||
}); | ||
},1); | ||
}); | ||
|
@@ -254,16 +361,21 @@ module.exports = connect.behavior("fall-through-cache",function(baseConnect){ | |
self._getMakeInstance(self.id(instanceData) || self.id(params), function(instance){ | ||
self.addInstanceReference(instance); | ||
|
||
self._setIsConsistent(instance, false); | ||
|
||
setTimeout(function(){ | ||
baseConnect.getData.call(self, params).then(function(instanceData2){ | ||
|
||
|
||
self._setInconsistencyReason(instance, undefined); | ||
self.cacheConnection.updateData(instanceData2); | ||
self.updatedInstance(instance, instanceData2); | ||
self._setIsConsistent(instance, true); | ||
self.deleteInstanceReference(instance); | ||
|
||
}, function(e){ | ||
// what do we do here? self.rejectedUpdatedList ? | ||
console.log("REJECTED", e); | ||
console.error("baseConnect.getData rejected", e); | ||
self.rejectedUpdatedInstance(instance, e); | ||
}); | ||
},1); | ||
}); | ||
|
@@ -277,6 +389,16 @@ module.exports = connect.behavior("fall-through-cache",function(baseConnect){ | |
|
||
return listData; | ||
}); | ||
}, | ||
rejectedUpdatedInstance: function(instance, error) { | ||
this._setIsConsistent(instance, false); | ||
this._setInconsistencyReason(instance, error); | ||
}, | ||
|
||
_setInconsistencyReason: function(instance, error) { | ||
if(instance._setInconsistencyReason) { | ||
instance._setInconsistencyReason(error); | ||
} | ||
} | ||
|
||
}; | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
We should probably avoid having fall-through-cache know anything about
can-connect/can/map
. Can you think of a way to avoid this?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.
Off the top of my head no. Based on the suggested requirements, these features would be specific to maps and lists, and made sense to me that if we wanted to have these function features available in templates.
Do you have any suggestions?