-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmercury.js
133 lines (105 loc) · 3.17 KB
/
mercury.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
'use strict';
var SingleEvent = require('geval/single');
var MultipleEvent = require('geval/multiple');
var extend = require('xtend');
/*
Pro tip: Don't require `mercury` itself.
require and depend on all these modules directly!
*/
var mercury = module.exports = {
// Entry
main: require('main-loop'),
app: app,
// Base
BaseEvent: require('value-event/base-event'),
// Input
Delegator: require('dom-delegator'),
channels: channels,
send: require('value-event/event'),
sendValue: require('value-event/value'),
sendSubmit: require('value-event/submit'),
sendChange: require('value-event/change'),
sendKey: require('value-event/key'),
sendClick: require('value-event/click'),
// State
// remove from core: favor hg.varhash instead.
array: require('observ-array'),
struct: require('observ-struct'),
// deprecated: use hg.struct instead.
hash: require('observ-struct'),
varhash: require('observ-varhash'),
value: require('observ'),
state: state,
// Render
diff: require('virtual-dom/vtree/diff'),
patch: require('virtual-dom/vdom/patch'),
partial: require('vdom-thunk'),
create: require('virtual-dom/vdom/create-element'),
h: require('virtual-dom/virtual-hyperscript'),
// Utilities
// remove from core: require computed directly instead.
computed: require('observ/computed'),
// remove from core: require watch directly instead.
watch: require('observ/watch'),
// kumavis - new
actions: actions,
};
function input(names) {
if (!names) {
return SingleEvent();
}
return MultipleEvent(names);
}
function channels(funcs, context) {
return Object.keys(funcs).reduce(createHandle, {});
function createHandle(acc, name) {
var handle = mercury.Delegator.allocateHandle(
funcs[name].bind(null, context));
acc[name] = handle;
return acc;
}
}
function app(elem, observ, render, opts) {
if (!elem) {
throw new Error(
'Element does not exist. ' +
'Mercury cannot be initialized.');
}
mercury.Delegator(opts);
var loop = mercury.main(observ(), render, extend({
diff: mercury.diff,
create: mercury.create,
patch: mercury.patch
}, opts));
elem.appendChild(loop.target);
return observ(loop.update);
}
// kumavis - new + modified
// modified - added 'actions', removed deprecated 'handles'
function state(obj) {
var copy = extend(obj);
var $channels = copy.channels;
var $actions = copy.actions;
if ($channels) {
copy.channels = mercury.value(null);
}
if ($actions) {
copy.actions = mercury.value(null);
}
var observ = mercury.struct(copy);
if ($channels) {
observ.channels.set(mercury.channels($channels, observ));
}
if ($actions) {
observ.actions.set(mercury.actions($actions, observ));
}
return observ;
}
function actions(funcs, context) {
return Object.keys(funcs).reduce(createEvent, {});
function createEvent(acc, name) {
var handle = funcs[name].bind(null, context);
acc[name] = handle;
return acc;
}
}