Skip to content

Commit 2990af5

Browse files
author
Jérémy
committed
refactor(service.module.getters): unref instead of isRef, skip/limit optimization, replace _.values
get copies with map instead of forEach, avoid params mutation, remove lodash get
1 parent 2813d86 commit 2990af5

File tree

1 file changed

+36
-56
lines changed

1 file changed

+36
-56
lines changed

src/service-module/service-module.getters.ts

Lines changed: 36 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ eslint
44
@typescript-eslint/no-explicit-any: 0
55
*/
66
import sift from 'sift'
7-
import { _ } from '@feathersjs/commons'
87
import { filterQuery, sorter, select } from '@feathersjs/adapter-commons'
98
import { globalModels as models } from './global-models'
10-
import _get from 'lodash/get'
119
import _omit from 'lodash/omit'
12-
import { isRef } from '@vue/composition-api'
10+
import { unref } from '@vue/composition-api'
1311
import { ServiceState } from '..'
1412
import { Id } from '@feathersjs/feathers'
1513

@@ -25,71 +23,61 @@ const getCopiesById = ({
2523
if (keepCopiesInStore) {
2624
return copiesById
2725
} else {
28-
const Model = _get(models, [serverAlias, 'byServicePath', servicePath])
26+
const Model = models[serverAlias].byServicePath[servicePath]
2927

3028
return Model.copiesById
3129
}
3230
}
3331

3432
export default function makeServiceGetters() {
3533
return {
36-
list(state) {
37-
return state.ids.map(id => state.keyedById[id])
38-
},
39-
find: state => params => {
40-
if (isRef(params)) {
41-
params = params.value
42-
}
43-
params = { ...params } || {}
44-
45-
// Set params.temps to true to include the tempsById records
46-
params.temps = params.hasOwnProperty('temps') ? params.temps : false
47-
48-
// Set params.copies to true to include the copiesById records
49-
params.copies = params.hasOwnProperty('copies') ? params.copies : false
50-
51-
const { paramsForServer, whitelist, keyedById } = state
34+
list: (state) => Object.values(state.keyedById),
35+
find: (state) => (_params) => {
36+
const params = unref(_params) || {}
37+
38+
const {
39+
paramsForServer,
40+
whitelist,
41+
keyedById,
42+
idField,
43+
tempsById
44+
} = state
5245
const q = _omit(params.query || {}, paramsForServer)
5346

5447
const { query, filters } = filterQuery(q, {
5548
operators: additionalOperators.concat(whitelist)
5649
})
57-
let values = _.values(keyedById)
5850

59-
if (params.temps) {
60-
values.push(..._.values(state.tempsById))
51+
let values = Object.values(keyedById) as any
52+
53+
if (params.hasOwnProperty('temps') && params.temps) {
54+
values.push(...(Object.values(tempsById) as any))
6155
}
6256

6357
values = values.filter(sift(query))
6458

65-
if (params.copies) {
66-
const { idField } = state
59+
if (params.hasOwnProperty('copies') && params.copies) {
6760
const copiesById = getCopiesById(state)
68-
values.forEach((val, i, arr) => {
69-
const copy = copiesById[val[idField]]
70-
if (copy) {
71-
// replace keyedById value with existing clone value
72-
arr[i] = copy
73-
}
74-
})
61+
// replace keyedById value with existing clone value
62+
values = values.map(
63+
(value, index) => copiesById[value[idField]] || value
64+
)
7565
}
7666

7767
const total = values.length
7868

79-
if (filters.$sort) {
69+
if (filters.$sort !== undefined) {
8070
values.sort(sorter(filters.$sort))
8171
}
8272

83-
if (filters.$skip) {
84-
values = values.slice(filters.$skip)
85-
}
86-
87-
if (typeof filters.$limit !== 'undefined') {
88-
values = values.slice(0, filters.$limit)
73+
if (filters.$skip !== undefined && filters.$limit !== undefined) {
74+
values = values.slice(filters.$skip, filters.$limit + filters.$skip)
75+
} else if (filters.$skip !== undefined || filters.$limit !== undefined) {
76+
values = values.slice(filters.$skip, filters.$limit)
8977
}
9078

9179
if (filters.$select) {
92-
values = values.map(value => _.pick(value, ...filters.$select.slice()))
80+
values = select(params)(values)
9381
}
9482

9583
return {
@@ -99,29 +87,21 @@ export default function makeServiceGetters() {
9987
data: values
10088
}
10189
},
102-
count: (state, getters) => params => {
103-
if (isRef(params)) {
104-
params = params.value
105-
}
106-
if (!params.query) {
107-
throw 'params must contain a query-object'
108-
}
90+
count: (state, getters) => (_params) => {
91+
const params = unref(_params) || {}
10992

11093
const cleanQuery = _omit(params.query, FILTERS)
11194
params.query = cleanQuery
11295

11396
return getters.find(params).total
11497
},
11598
get: ({ keyedById, tempsById, idField, tempIdField }) => (
116-
id,
117-
params = {}
99+
_id,
100+
_params = {}
118101
) => {
119-
if (isRef(id)) {
120-
id = id.value
121-
}
122-
if (isRef(params)) {
123-
params = params.value
124-
}
102+
const id = unref(_id)
103+
const params = unref(_params)
104+
125105
const record = keyedById[id] && select(params, idField)(keyedById[id])
126106
if (record) {
127107
return record
@@ -131,7 +111,7 @@ export default function makeServiceGetters() {
131111

132112
return tempRecord || null
133113
},
134-
getCopyById: state => id => {
114+
getCopyById: (state) => (id) => {
135115
const copiesById = getCopiesById(state)
136116
return copiesById[id]
137117
},

0 commit comments

Comments
 (0)