Skip to content

Commit 154b718

Browse files
committed
feat(app): add handeEvents to global
1 parent 53df628 commit 154b718

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

src/index.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ import prepareMakeAuthPlugin from './auth-module/make-auth-plugin'
1717
import useFind from './useFind'
1818
import useGet from './useGet'
1919

20-
import { FeathersVuexOptions } from './service-module/types'
20+
import { FeathersVuexOptions, HandleEvents } from './service-module/types'
2121
import { initAuth, hydrateApi } from './utils'
2222
import { FeathersVuex } from './vue-plugin/vue-plugin'
23+
const events = ['created', 'patched', 'updated', 'removed']
2324

24-
const defaultOptions: FeathersVuexOptions = {
25+
const defaults: FeathersVuexOptions = {
2526
autoRemove: false, // Automatically remove records missing from responses (only use with feathers-rest)
2627
addOnUpsert: false, // Add new records pushed by 'updated/patched' socketio events into store, instead of discarding them
2728
enableEvents: true, // Listens to socket.io events when available
@@ -34,6 +35,7 @@ const defaultOptions: FeathersVuexOptions = {
3435
preferUpdate: false, // When true, calling model.save() will do an update instead of a patch.
3536
replaceItems: false, // Instad of merging in changes in the store, replace the entire record.
3637
serverAlias: 'api',
38+
handleEvents: {} as HandleEvents,
3739
skipRequestIfExists: false, // For get action, if the record already exists in store, skip the remote request
3840
whitelist: [] // Custom query operators that will be allowed in the find getter.
3941
}
@@ -44,14 +46,21 @@ export default function feathersVuex(feathers, options: FeathersVuexOptions) {
4446
'The first argument to feathersVuex must be a feathers client.'
4547
)
4648
}
47-
options = Object.assign({}, defaultOptions, options)
4849

4950
if (!options.serverAlias) {
5051
throw new Error(
5152
`You must provide a 'serverAlias' in the options to feathersVuex`
5253
)
5354
}
5455

56+
// Setup the event handlers. By default they just return the value of `options.enableEvents`
57+
defaults.handleEvents = events.reduce((obj, eventName) => {
58+
obj[eventName] = () => options.enableEvents || true
59+
return obj
60+
}, {} as HandleEvents)
61+
62+
options = Object.assign({}, defaults, options)
63+
5564
addClient({ client: feathers, serverAlias: options.serverAlias })
5665

5766
const BaseModel = makeBaseModel(options)

src/service-module/make-service-plugin.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,16 @@ eslint
66
import {
77
FeathersVuexOptions,
88
MakeServicePluginOptions,
9-
HandleEvents
9+
ServiceOptionsDefaults
1010
} from './types'
1111
import makeServiceModule from './make-service-module'
1212
import { globalModels, prepareAddModel } from './global-models'
1313
import { makeNamespace, getServicePath, assignIfNotPresent } from '../utils'
1414
import _get from 'lodash/get'
1515

16-
const defaults = {
16+
const defaults: ServiceOptionsDefaults = {
1717
namespace: '', // The namespace for the Vuex module. Will generally be derived from the service.path, service.name, when available. Otherwise, it must be provided here, explicitly.
18-
nameStyle: 'short', // Determines the source of the module name. 'short', 'path', or 'explicit'
1918
servicePath: '',
20-
handleEvents: {} as HandleEvents,
2119
state: {}, // for custom state
2220
getters: {}, // for custom getters
2321
mutations: {}, // for custom mutations
@@ -42,12 +40,11 @@ export default function prepareMakeServicePlugin(
4240
* (3) Setup real-time events
4341
*/
4442
return function makeServicePlugin(config: MakeServicePluginOptions) {
45-
// Setup the event handlers. By default they just return the value of `options.enableEvents`
46-
defaults.handleEvents = events.reduce((obj, eventName) => {
47-
obj[eventName] = () => config.enableEvents || true
48-
return obj
49-
}, {} as HandleEvents)
50-
43+
if (!config.service) {
44+
throw new Error(
45+
'No service was provided. If you passed one in, check that you have configured a transport plugin on the Feathers Client. Make sure you use the client version of the transport.'
46+
)
47+
}
5148
const options = Object.assign({}, defaults, globalOptions, config)
5249
const {
5350
Model,
@@ -59,11 +56,10 @@ export default function prepareMakeServicePlugin(
5956
preferUpdate
6057
} = options
6158

62-
if (!service) {
63-
throw new Error(
64-
'No service was provided. If you passed one in, check that you have configured a transport plugin on the Feathers Client. Make sure you use the client version of the transport.'
65-
)
66-
}
59+
events.forEach(eventName => {
60+
if (options.handleEvents[eventName])
61+
options.handleEvents[eventName] = () => options.enableEvents || true
62+
})
6763

6864
// Make sure we get a service path from either the service or the options
6965
let { servicePath } = options

0 commit comments

Comments
 (0)