This repository has been archived by the owner on Mar 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreate-logux-store.js
328 lines (262 loc) · 7.67 KB
/
create-logux-store.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
var Store = require('vuex').Store
var isFirstOlder = require('logux-core/is-first-older')
var CrossTabClient = require('logux-client/cross-tab-client')
var deepClone = require('./utils/deep-clone')
function warnBadUndo (id) {
var json = JSON.stringify(id)
console.warn(
'Logux can not find ' + json + ' to undo it. Maybe action was cleaned.'
)
}
function LoguxState (client, config, vuexConfig) {
var self = this
Store.call(this, deepClone(vuexConfig))
this.client = client
this.log = client.log
var init
var prevMeta
var replaying
var wait = {}
var history = {}
var actionCount = 0
var started = (function (now) {
return { id: now, time: now[0] }
})(client.log.generateId())
self.initialize = new Promise(function (resolve) {
init = resolve
})
self.commit = function commit () {
var action
var isNoObjectTypeAction
if (typeof arguments[0] === 'string') {
var type = arguments[0]
var options = Array.prototype.slice.call(arguments, 1)
isNoObjectTypeAction = true
action = {
type: type,
options: options
}
} else {
action = arguments[0]
}
var meta = {
id: client.log.generateId(),
tab: client.id,
reasons: ['tab' + client.id],
dispatch: true
}
client.log.add(action, meta)
prevMeta = meta
originCommit(action, isNoObjectTypeAction)
saveHistory(meta)
}
self.commit.local = function local (action, meta) {
meta.tab = client.id
return client.log.add(action, meta)
}
self.commit.crossTab = function crossTab (action, meta) {
return client.log.add(action, meta)
}
self.commit.sync = function sync (action, meta) {
if (!meta) meta = {}
if (!meta.reasons) meta.reasons = []
meta.sync = true
meta.reasons.push('waitForSync')
return client.log.add(action, meta)
}
client.log.on('preadd', function (action, meta) {
if (action.type === 'logux/undo' && meta.reasons.length === 0) {
meta.reasons.push('reasonsLoading')
}
if (!isFirstOlder(prevMeta, meta) && meta.reasons.length === 0) {
meta.reasons.push('replay')
}
})
var lastAdded = 0
var dispatchCalls = 0
client.on('add', function (action, meta) {
if (meta.added > lastAdded) lastAdded = meta.added
if (meta.dispatch) {
dispatchCalls += 1
if (lastAdded > config.dispatchHistory && dispatchCalls % 25 === 0) {
client.log.removeReason('tab' + client.id, {
maxAdded: lastAdded - config.dispatchHistory
})
}
return
}
process(action, meta, isFirstOlder(meta, started))
})
client.on('clean', function (action, meta) {
var key = meta.id.join('\t')
delete wait[key]
delete history[key]
})
client.sync.on('state', function () {
if (client.sync.state === 'synchronized') {
client.log.removeReason('waitForSync', { maxAdded: client.sync.lastSent })
}
})
var previous = []
var ignores = {}
client.log.each(function (action, meta) {
if (!meta.tab) {
if (action.type === 'logux/undo') {
ignores[action.id.join('\t')] = true
} else if (!ignores[meta.id.join('\t')]) {
previous.push([action, meta])
}
}
}).then(function () {
if (previous.length > 0) {
previous.forEach(function (i) {
process(i[0], i[1], true)
})
}
init()
})
// Functions
function saveHistory (meta) {
actionCount += 1
if (
config.saveStateEvery === 1 || actionCount % config.saveStateEvery === 1
) {
history[meta.id.join('\t')] = deepClone(self.state)
}
}
function originCommit (action, isNotObjectTypeAction) {
if (action.type === 'logux/state') {
self.replaceState(action.state)
return
}
var commitArgs = arguments
if (isNotObjectTypeAction) {
commitArgs = [action.type].concat(action.options)
}
if (action.type in self._mutations) {
Store.prototype.commit.apply(self, commitArgs)
}
}
function replaceState (state, actions) {
var newState = actions.reduceRight(function (prev, i) {
var changed = deepClone(prev)
if (vuexConfig.mutations[i[0].type]) {
vuexConfig.mutations[i[0].type](changed, i[0])
}
if (history[i[1]]) history[i[1]] = changed
return changed
}, state)
originCommit({ type: 'logux/state', state: newState })
}
function replay (actionId, replayIsSafe) {
var until = actionId.join('\t')
var ignore = {}
var actions = []
var replayed = false
var newAction
var collecting = true
replaying = new Promise(function (resolve) {
client.log.each(function (action, meta) {
if (meta.tab && meta.tab !== client.id) return true
var id = meta.id.join('\t')
if (collecting || !history[id]) {
if (action.type === 'logux/undo') {
ignore[action.id.join('\t')] = true
return true
}
if (!ignore[id]) actions.push([action, id])
if (id === until) {
newAction = action
collecting = false
}
return true
} else {
replayed = true
replaceState(history[id], actions)
return false
}
}).then(function () {
if (!replayed) {
if (config.onMissedHistory) config.onMissedHistory(newAction)
var full
if (replayIsSafe) {
full = actions
} else {
full = actions.slice(0)
while (actions.length > 0) {
var last = actions[actions.length - 1]
actions.pop()
if (history[last[1]]) {
replayed = true
replaceState(history[last[1]], actions.concat([
[newAction, until]
]))
break
}
}
}
if (!replayed) {
replaceState(deepClone(vuexConfig.state), full)
}
}
replaying = false
resolve()
})
})
return replaying
}
function process (action, meta, replayIsSafe) {
if (replaying) {
var key = meta.id.join('\t')
wait[key] = true
replaying.then(function () {
if (wait[key]) {
process(action, meta, replayIsSafe)
delete wait[key]
}
})
return
}
if (action.type === 'logux/undo') {
var reasons = meta.reasons
client.log.byId(action.id).then(function (result) {
if (result[0]) {
if (reasons.length === 1 && reasons[0] === 'reasonsLoading') {
client.log.changeMeta(meta.id, { reasons: result[1].reasons })
}
delete history[action.id.join('\t')]
replay(action.id)
} else {
client.log.changeMeta(meta.id, { reasons: [] })
warnBadUndo(action.id)
}
})
} else if (isFirstOlder(prevMeta, meta)) {
prevMeta = meta
originCommit(action)
if (meta.added) saveHistory(meta)
} else {
replay(meta.id, replayIsSafe).then(function () {
if (meta.reasons.indexOf('replay') !== -1) {
client.log.changeMeta(meta.id, {
reasons: meta.reasons.filter(function (i) {
return i !== 'replay'
})
})
}
})
}
}
}
LoguxState.prototype = Object.create(Store.prototype)
LoguxState.prototype.constructor = LoguxState
function createLoguxState (config) {
if (!config) config = {}
var client = new CrossTabClient(config)
return LoguxState.bind(null, client, {
dispatchHistory: config.dispatchHistory || 1000,
saveStateEvery: config.saveStateEvery || 50,
onMissedHistory: config.onMissedHistory
})
}
module.exports = createLoguxState