This repository has been archived by the owner on Apr 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 108
Support for other storage backends for larger data storage #73
Open
nilbus
wants to merge
8
commits into
nilbus:master
Choose a base branch
from
honi:async
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
8 commits
Select commit
Hold shift + click to select a range
0d93c90
Initial async implementation.
honi a6020a2
Fixed tests (bug in spyOnLocalsync)
honi 8cb57a5
Refactored the use of LocalStorage to an async StorageAdapter.
honi def4e28
Added some comments and minor code cleanup.
honi a1e41ec
Renamed localsync and dualsync to localSync and dualSync (camelCased)…
honi 29c3da8
Added dirtyName and destroyedName cache vars in Store (DRY).
honi 8ed599f
Organized adapters in separate coffee file. Updated Makefile. Added s…
honi 2148a58
Pushing old code for review.
honi 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
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 |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# Async storage adapters classes. | ||
|
||
class LocalStorageAdapter | ||
# Reference implementation with LocalStorage. | ||
constructor: (name) -> | ||
@name = name || 'Backbone.dualStorage.LocalStorage' | ||
@store = localStorage | ||
|
||
initialize: -> | ||
$.Deferred().resolve() | ||
|
||
set: (key, value) -> | ||
@store.setItem key, JSON.stringify value | ||
$.Deferred().resolve value | ||
|
||
get: (key) -> | ||
$.Deferred().resolve JSON.parse @store.getItem key | ||
|
||
remove: (key) -> | ||
@store.removeItem key | ||
$.Deferred().resolve() | ||
|
||
clear: -> | ||
@store.clear() | ||
$.Deferred().resolve() | ||
|
||
|
||
class LawnchairStorageAdapter | ||
constructor: (name) -> | ||
@name = name || 'Backbone.dualStorage.Lawnchair' | ||
|
||
initialize: -> | ||
promise = $.Deferred() | ||
@store = new Lawnchair | ||
name: @name | ||
adapter: ['indexed-db'], -> promise.resolve() | ||
return promise | ||
|
||
set: (key, value) -> | ||
promise = $.Deferred() | ||
@store.save {key: key, value: value}, (data) -> promise.resolve data.value | ||
return promise | ||
|
||
get: (key) -> | ||
promise = $.Deferred() | ||
@store.get key, (data) -> promise.resolve data?.value | ||
return promise | ||
|
||
remove: (key) -> | ||
promise = $.Deferred() | ||
@store.remove key, -> promise.resolve() | ||
return promise | ||
|
||
clear: -> | ||
promise = $.Deferred() | ||
@store.nuke -> promise.resolve() | ||
return promise | ||
|
||
|
||
class StickyStorageAdapter | ||
constructor: (name) -> | ||
@name = name || 'Backbone.dualStorage.Sticky' | ||
|
||
initialize: -> | ||
promise = $.Deferred() | ||
@store = new StickyStore | ||
name: @name | ||
adapters: ['indexedDB'] | ||
ready: -> promise.resolve() | ||
return promise | ||
|
||
set: (key, value) -> | ||
promise = $.Deferred() | ||
@store.set {key: key, value: value}, (data) -> promise.resolve data.value | ||
return promise | ||
|
||
get: (key) -> | ||
promise = $.Deferred() | ||
@store.get key, (data) -> promise.resolve data?.value | ||
return promise | ||
|
||
remove: (key) -> | ||
promise = $.Deferred() | ||
@store.remove key, -> promise.resolve() | ||
return promise | ||
|
||
clear: -> | ||
promise = $.Deferred() | ||
@store.removeAll -> promise.resolve() | ||
return promise | ||
|
||
|
||
Backbone.storageAdapters = | ||
LocalStorageAdapter: LocalStorageAdapter | ||
LawnchairStorageAdapter: LawnchairStorageAdapter | ||
# IndexedDB store is not working with Sticky. | ||
# StickyStorageAdapter: StickyStorageAdapter |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
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.
There are lots of places where you're referring to the deferred as a promise. Deferred actually has a method
promise()
that returns a promise object that has a subset of deferred's methods - without resolve and fail for example. In general, a class that resolves its own deferreds should expose/return its the deferred's promise rather than the deferred, so that other code doesn't accidentally or intentionally resolve or fail the deferred unexpectedly on the owner's behalf.