Skip to content

Instantly share code, notes, and snippets.

@isavch
Last active January 16, 2018 13:56
Show Gist options
  • Select an option

  • Save isavch/4c70d5edc05ab0050ac0e4ae62c5b592 to your computer and use it in GitHub Desktop.

Select an option

Save isavch/4c70d5edc05ab0050ac0e4ae62c5b592 to your computer and use it in GitHub Desktop.
Create API

There are basicly 2 scenarious for create:

  • Online and everything is successful
  • Offline, request failed

First case

 {
    posts: {
        6c84fb90-12c4-11e1-840d-7b25c5ee775a: {
           status: 'requested',
           data: { id: '6c84fb90-12c4-11e1-840d-7b25c5ee775a' .....},
           ......
      }
    }
 }
  • success action: we replace object with temporary id, with object from server
    {
       posts: {
        'id from server here': {
             data: { 'id': 'id from servere here' .....},
             ......
         }
       }
    }

Second case So if we look closer into second scenario, I see the following actions should follow:

  • requested action: same as success scenario
  • fail action:
    • we keep local object with random id, and return it to client and add error info
 {
    posts: {
        6c84fb90-12c4-11e1-840d-7b25c5ee775a: {
           status: 'error',
           data: { id: '6c84fb90-12c4-11e1-840d-7b25c5ee775a' .....},
           ......
      }
    }
 }

does it mean catch for create/update/delete never fire?

  • replay action
    • online:
      • we save object from server, replacing local,
      • we still keeping random id because it can be used by client
      • we store server id, for future operations (update/delete), it can't be performed with local random id, we can store id in serverId field
 {
    posts: {
        6c84fb90-12c4-11e1-840d-7b25c5ee775a: {
           data: { id: '6c84fb90-12c4-11e1-840d-7b25c5ee775a' .....},
           ref: 'real-id'
           ......
      }
    }
 }
  • offline
    • user can do next actions with object like update and delete
    • if he updates object then when we become online we can just dispatch one create action with updated object istead of 2
    • if he deletes object nothing should be ddispatched

There possible problem with duplicated data, when you have one local object and same object from server with different id. Other solution can be always have one instance of object, but in that case if client is using offline copy of object we need to notify it, that data must be refreshed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment