Skip to content

Commit 5224d5d

Browse files
committed
feat: Model classes emit Feathers service events
Model classes are now EventEmitter instances which emit service events when received. All FeathersJS events are supported. Here’s an example of how to use it in a component: ```js export default { created() { this.$FeathersVuex.api.Todo.on(‘created’, this.handleTodoCreated) }, methods: { handleTodoCreated(todo) { console.log(todo) } } } ```
1 parent ea14837 commit 5224d5d

File tree

6 files changed

+272
-134
lines changed

6 files changed

+272
-134
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
"@types/npm": "^2.0.31",
108108
"bson-objectid": "^1.3.0",
109109
"debug": "^4.1.1",
110+
"events": "^3.0.0",
110111
"fast-copy": "^2.0.4",
111112
"fast-json-stable-stringify": "^2.0.0",
112113
"inflection": "^1.12.0",

src/service-module/make-model.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { globalModels, prepareAddModel } from './global-models'
88
import { mergeWithAccessors, checkNamespace, getId } from '../utils'
99
import _merge from 'lodash/merge'
1010
import _get from 'lodash/get'
11+
import EventEmitter from 'events'
1112

1213
// A hack to prevent error with this.constructor.preferUpdate
1314
interface Function {
@@ -385,6 +386,10 @@ export default function makeModel(options: FeathersVuexOptions) {
385386
return _merge({}, this)
386387
}
387388
}
389+
for (var n in EventEmitter.prototype) {
390+
BaseModel[n] = EventEmitter.prototype[n]
391+
}
392+
388393
addModel(BaseModel)
389394
return BaseModel
390395
}

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,30 @@ export default function prepareMakeServicePlugin(
9292
// (3^) Setup real-time events
9393
if (options.enableEvents) {
9494
// Listen to socket events when available.
95-
service.on('created', item =>
95+
service.on('created', item => {
9696
store.dispatch(`${options.namespace}/addOrUpdate`, item)
97-
)
98-
service.on('updated', item =>
97+
if (Model.emit) {
98+
Model.emit('created', item)
99+
}
100+
})
101+
service.on('updated', item => {
99102
store.dispatch(`${options.namespace}/addOrUpdate`, item)
100-
)
101-
service.on('patched', item =>
103+
if (Model.emit) {
104+
Model.emit('updated', item)
105+
}
106+
})
107+
service.on('patched', item => {
102108
store.dispatch(`${options.namespace}/addOrUpdate`, item)
103-
)
104-
service.on('removed', item =>
109+
if (Model.emit) {
110+
Model.emit('patched', item)
111+
}
112+
})
113+
service.on('removed', item => {
105114
store.commit(`${options.namespace}/removeItem`, item)
106-
)
115+
if (Model.emit) {
116+
Model.emit('removed', item)
117+
}
118+
})
107119
}
108120
}
109121
}

test/service-module/model-base.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,22 @@ describe('makeModel / BaseModel', function () {
8484
`has ${method} method`
8585
)
8686
})
87+
88+
const eventMethods = [
89+
'on',
90+
'off',
91+
'once',
92+
'emit',
93+
'addListener',
94+
'removeListener',
95+
'removeAllListeners'
96+
]
97+
eventMethods.forEach(method => {
98+
assert(
99+
typeof BaseModel[method] === 'function',
100+
`has ${method} method`
101+
)
102+
})
87103
})
88104

89105
it('allows customization through the FeathersVuexOptions', function () {

test/service-module/model-methods.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,21 @@ describe('Models - Methods', function () {
165165
assert(typeof Task.getFromStore === 'function')
166166
})
167167

168+
it('allows listening to Feathers events on Model', function (done) {
169+
const { Letter } = makeContext()
170+
171+
Letter.on('created', data => {
172+
assert(data.to === 'Santa', 'received event with data')
173+
done()
174+
})
175+
176+
// This should trigger an event from the bottom of make-service-plugin.ts
177+
const letter = new Letter({
178+
from: 'Me',
179+
to: 'Santa'
180+
}).save()
181+
})
182+
168183
it('instance.save calls create with correct arguments', function () {
169184
const { Task } = makeContext()
170185
const task = new Task({ test: true })

0 commit comments

Comments
 (0)