This repository has been archived by the owner on Aug 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscription.js
152 lines (136 loc) · 3.38 KB
/
subscription.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* @flow */
const cloneDeep = require('lodash/cloneDeep')
const set = require('lodash/set')
const get = require('lodash/get')
const has = require('lodash/has')
const isArray = require('lodash/isArray')
const map = require('lodash/map')
const reject = require('lodash/reject')
const concat = require('lodash/concat')
const includes = require('lodash/includes')
const toPath = require('lodash/toPath')
const forEach = require('lodash/forEach')
const merge = require('lodash/merge')
const tail = require('lodash/tail')
/**
* {@link cookie} configuration options
* @typedef {Object} SubscriptionPayload
* @property {'CREATED' | 'UPDATED' | 'DELETED'} mutation
* @property {Object | Object[]} node
* @example
* import type { SubscriptionPayload } from 'secondwheel/subscription'
*/
/** @namespace subscription */
/*::
export type SubscriptionPayload = {
mutation: 'CREATED' | 'UPDATED' | 'DELETED';
node: Object | Object[];
}
*/
const updateItem = (
result/*: Object */,
path/*: string[] */,
values/*: any */
) => {
if (path.length) {
const child = result[path[0]]
const newPath = tail(path)
if (child) {
if (isArray(child)) {
forEach(child, item => {
updateItem(item, newPath, values)
})
} else {
updateItem(child, newPath, values)
}
}
} else {
forEach(values, value => {
if (
has(result, 'id') &&
has(value, 'id') &&
result.id == value.id // eslint-disable-line eqeqeq
) {
merge(result, value)
}
})
}
}
const updateList = (
result/*: Object */,
path/*: string */,
callback/*: (arr: any[]) => any[] */
)/*: Object */ => {
const clone = cloneDeep(result)
return set(clone, path, callback(get(clone, path, [])))
}
/**
* updates object to reflect created nodes at path
*
* @memberof subscription
* @example
* import { created } from 'secondwheel/subscription'
*
* const newData = created(data, 'path.to.nested.list', payload)
*/
const created = (
result/*: Object */,
path/*: string */,
payload/*: SubscriptionPayload */
)/*: Object */ =>
payload.mutation === 'CREATED'
? updateList(result, path, arr => concat(arr, payload.node))
: result
exports.created = created
/**
* updates object to reflect updated nodes at path
*
* @memberof subscription
* @example
* import { updated } from 'secondwheel/subscription'
*
* const newData = updated(data, 'path', payload)
*/
const updated = (
result/*: Object */,
path/*: string */,
payload/*: SubscriptionPayload */
)/*: Object */ => {
let clone
return payload.mutation !== 'UPDATED'
? result
: (
(clone = cloneDeep(result)),
updateItem(clone, toPath(path),
isArray(payload.node) ? payload.node : [payload.node]),
clone
)
}
exports.updated = updated
/**
* updates object to reflect deleted nodes at path
*
* @memberof subscription
* @example
* import { deleted } from 'secondwheel/subscription'
*
* const newData = deleted(data, 'path', payload)
*/
const deleted = (
result/*: Object */,
path/*: string */,
payload/*: SubscriptionPayload */
)/*: Object */ => {
let ids
return payload.mutation !== 'DELETED'
? result
: (
(ids = map(isArray(payload.node) ? payload.node : [payload.node], 'id')),
updateList(
result,
path,
arr => reject(arr, ({ id }) => includes(ids, id))
)
)
}
exports.deleted = deleted