feathers-plus / feathers-offline-realtime

Offline-first realtime replication, with optimistic updates while connected.
MIT License
33 stars 9 forks source link

Duplicated new post when .find() #17

Closed darioielardi closed 4 years ago

darioielardi commented 6 years ago

Actual behavior

I have two redux-thunk actions: one to fetch my user's post list, and another one to create a post. Both use an offline service, configured as indicated in docs.

const postsOffline = feathers.service('postsClient')

const fetchPosts = userId => dispatch => {
  return postsOffline.find({ author: userId })
    .then(data => dispatch(fetchPostsSuccess(data)))

const createPost = post => dispatch => {
  return postsOffline.create(post)
    .then(post => dispatch(createPostSuccess(post)))

When I create a post, createPostSuccess is dispatched with the new post ( the local copy ) as the payload, and the reducer return [...state, action.payload], so everything works fine. But when I navigate to another route and then come back to posts page, the fetchPosts action is dispatched but as the payload it has the posts list with the new one created ( by the server ) AND the local copy of the same new post, so this new post is duplicated.

Expected behavior

When fetchPosts is dispatched, posts list is returned with the new post ( the one created by the server if there's connection or the local copy if there's not, NOT BOTH ).

I can't figure out what happen, any suggestion? Thanks for the great work.

eddyystop commented 6 years ago

I don't see the above code using feathers-offline-realtime. What am I missing?

darioielardi commented 6 years ago

Sorry for not being clear, the 'postClient' service is an offline realtime service, configured with optimistic mutator:

const posts = client.service('posts')
export const postsRealtime = new Realtime(posts, { uuid: true })
client.use('postsClient', optimisticMutator({ replicator: postsRealtime }))
postsRealtime.connect()

If I use the 'posts' service directly, this behavior does not occur.