forked from featurist/hyperdom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
refreshEventResult.js
71 lines (60 loc) · 1.8 KB
/
refreshEventResult.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
var deprecations = require('./deprecations')
module.exports = refreshEventResult
var norefresh = {}
function norefreshFunction () {
return norefresh
}
module.exports.norefresh = norefreshFunction
function refreshEventResult (result, mount, options) {
var onlyRefreshAfterPromise = options && options.refresh === 'promise'
var componentToRefresh = options && options.component
function handlePromiseResult (result) {
var opts = cloneOptions(options)
opts.refresh = undefined
refreshEventResult(result, mount, opts)
}
if (result && typeof (result.then) === 'function') {
result.then(handlePromiseResult, handlePromiseResult)
}
if (onlyRefreshAfterPromise) {
return
}
if (isComponent(result)) {
mount.refreshComponent(result)
} else if (result instanceof Array) {
for (var i = 0; i < result.length; i++) {
refreshEventResult(result[i], mount, options)
}
} else if (componentToRefresh) {
if (componentToRefresh.refreshComponent) {
componentToRefresh.refreshComponent()
} else {
componentToRefresh.refresh()
}
} else if (result === norefresh) {
// don't refresh;
} else if (result === norefreshFunction) {
deprecations.norefresh('hyperdom.norefresh is deprecated, please use hyperdom.norefresh()')
// don't refresh;
} else {
mount.refresh()
return result
}
}
function isComponent (component) {
return component &&
((typeof component.init === 'function' &&
typeof component.update === 'function' &&
typeof component.destroy === 'function') || (typeof component.refreshComponent === 'function'))
}
function cloneOptions (options) {
if (options) {
return {
norefresh: options.norefresh,
refresh: options.refresh,
component: options.component
}
} else {
return {}
}
}