forked from goatslacker/alt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decorators.js
80 lines (66 loc) · 1.9 KB
/
decorators.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
import { assign } from './functions'
/* istanbul ignore next */
function NoopClass() { }
const builtInProto = Object.getOwnPropertyNames(NoopClass.prototype)
function addMeta(description, decoration) {
description.value.alt = description.value.alt || {}
assign(description.value.alt, decoration)
return description
}
export function decorate(context) {
return (Store) => {
const proto = Store.prototype
const publicMethods = {}
const bindListeners = {}
Object.getOwnPropertyNames(proto).forEach((name) => {
if (builtInProto.indexOf(name) !== -1) return
const meta = proto[name].alt
if (!meta) {
return
}
/* istanbul ignore else */
if (meta.actions) {
bindListeners[name] = meta.actions
} else if (meta.actionsWithContext) {
bindListeners[name] = meta.actionsWithContext(context)
} else if (meta.publicMethod) {
publicMethods[name] = proto[name]
}
})
Store.config = assign({
bindListeners,
publicMethods
}, Store.config)
return Store
}
}
export function createActions(alt, ...args) {
return function (Actions) {
return alt.createActions(Actions, {}, ...args)
}
}
export function createStore(alt, ...args) {
return function (Store) {
return alt.createStore(decorate(alt)(Store), undefined, ...args)
}
}
export function bind(...actionIds) {
return (obj, name, description) => {
return addMeta(description, { actions: actionIds })
}
}
export function bindWithContext(fn) {
return (obj, name, description) => {
return addMeta(description, { actionsWithContext: fn })
}
}
export function expose(obj, name, description) {
return addMeta(description, { publicMethod: true })
}
export function datasource(...sources) {
const source = assign(...sources)
return function (Store) {
Store.config = assign({ datasource: source }, Store.config)
return Store
}
}