feathersjs-ecosystem / feathers-vuex

Integration of FeathersJS, Vue, and Nuxt for the artisan developer
https://vuex.feathersjs.com
MIT License
445 stars 108 forks source link

Socket filter #221

Closed barata0 closed 4 years ago

barata0 commented 5 years ago

Hi,

Feathers server has the option to filter channels dynamically. FeathersVuex should have this option too, instead of forcing the api to persist the filter rules and the client to "ask" for those events, the client could discard the undesired events.

what about allow the enableEvents prop receive a boolean or a map?

enableEvents = boolean (yes or no for all methods)

or

enableEvents = {
  create: function or boolean,
  update: function or boolean,
  patch: function or boolean,
  remove: function or boolean
}

The function returns a boolean indicating dinamically if the client want to store that record locally.

marshallswain commented 5 years ago

@barata0 are you saying you want to selectively use created or updated methods, and not listen to others?

marshallswain commented 5 years ago

Oh. never mind, I get what you're saying. We would probably leave enableEvents as a boolean and add an events option. Rather than allowing a boolean OR a function, we'd probably just go with a function.

events: {
  created: item => item.isWorthKeeping,
  updated: () => false,
  patched: item => item.hasPatchedAttribute && item.isWorthKeeping,
  removed: item => item // The default value
}
marshallswain commented 5 years ago

Actually, I feel like it would be better to call it enabledEvents.

marshallswain commented 5 years ago

I like it. Feel free to make a PR to the typescript branch.

barata0 commented 5 years ago

Shouldn't be better to call eventsFilter for the functions? Is there is no function defined it assumes true.

So you can enable and disable the vuex update events at all with the current option enableEvents and can implement a filter through

eventsFilter: {
  created: item => item.isWorthKeeping,
  updated: () => false,
  patched: item => item.hasPatchedAttribute && item.isWorthKeeping,
}

In the example the delete method is not implemented so it will delete any instance that exists on vuex store after receiving the event.

I'll try to make a PR on this.

marshallswain commented 4 years ago
handleEvents: {
  created (data, { model, models }) {
    // perform some side effects if you want, then

    // You want the record added to the store?
    return true

    // You don't want the record added to the store?
    return false
  }
}

If we want the record removed from the store, we can use the model arg and manually do model.removeFromStore(id). I don't think Model.removeFromStore() exists, yet, so I'll need to create it.

marshallswain commented 4 years ago

Actually remove should definitely remove the item from the store if it returns true. So that will be different than the rest. I now see what you were talking about.

marshallswain commented 4 years ago

This has been released in feathers-vuex@3.1.0. Thanks @barata0 for the great idea!

Read more about it in the docs: https://vuex.feathersjs.com/service-plugin.html#service-events

barata0 commented 4 years ago

Awesome!