Skip to content

Instantly share code, notes, and snippets.

@timbertson
Created May 4, 2010 12:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timbertson/389375 to your computer and use it in GitHub Desktop.
Save timbertson/389375 to your computer and use it in GitHub Desktop.
class Sync
constructor: (reader, store) ->
@reader = reader
@store = store
pull_tags: (cb) ->
@reader.get_user_tags (tags) ->
@store.set_valid_tags(tags, cb)
pull_items: (tag_name, cb) ->
self: this
@reader.get_tag_feed tag_name, (feed) ->
FuncTools.execute_map(feed.entries,
((_single_cb) ->
entry: this
self.store.add_entry(tag_name, entry, _single_cb)
), cb)
push: (cb) ->
self: this
@store.pending_actions (actions) ->
FuncTools.execute_map(actions, ((_cb) ->
action: this
[name, key, value]: action
func: null
switch name
when 'star' then func = 'set_star'
when 'read' then func = 'set_read'
when 'share' then func = 'set_public'
else
alert("unknown action: " + name)
return
console.log("pushing state [" + name + "=" + value + "] for " + key)
self.reader[func] key, value, (success) ->
if success
@store.remove_action(action, _cb)
else
console.log("failed: " + name)
_cb()
), cb)
run: (cb) ->
self: this
self.push ->
console.log("SYNC: state pushed!")
self.store.clear()
self.pull_tags ->
self.store.get_active_tags (active_tags) ->
FuncTools.execute_map(active_tags,((_cb) ->
tag_name: @key
self.pull_items(tag_name, _cb)
), cb)
class Sync
constructor: (reader, store) ->
@reader = reader
@store = store
pull_tags: (cb) ->
tags: defer @reader.get_user_tags()
@store.set_valid_tags(tags, cb)
pull_items: (tag_name, cb) ->
feed: defer @reader.get_tag_feed tag_name
for entry in feed.entries
defer @store.add_entry tag_name, entry
cb()
push: (cb) {
actions: defer @store.pending_actions()
for action in actions:
[name, key, value]: action
func: null
switch name
when 'star' then func = 'set_star'
when 'read' then func = 'set_read'
when 'share' then func = 'set_public'
else
alert("unknown action: " + name)
return
console.log("pushing state [" + name + "=" + value + "] for " + key)
success: defer @reader[func] key, value
if success
defer @store.remove_action action
else
console.log("failed: " + name)
cb()
run: (cb) ->
defer @push()
console.log("SYNC: state pushed!")
@store.clear()
defer @pull_tags()
active_tags: defer @store.get_active_tags()
defer @pull_items(tag_name) for tag_name in active_tags
cb()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment