forked from Raynos/mercury
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
126 lines (107 loc) · 3.45 KB
/
index.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
'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'),
// deprecated: use hg.channels instead.
input: input,
// deprecated: use hg.channels instead.
handles: channels,
channels: channels,
// deprecated: use hg.send instead.
event: require('value-event/event'),
send: require('value-event/event'),
// deprecated: use hg.sendValue instead.
valueEvent: require('value-event/value'),
sendValue: require('value-event/value'),
// deprecated: use hg.sendSubmit instead.
submitEvent: require('value-event/submit'),
sendSubmit: require('value-event/submit'),
// deprecated: use hg.sendChange instead.
changeEvent: require('value-event/change'),
sendChange: require('value-event/change'),
// deprecated: use hg.sendKey instead.
keyEvent: require('value-event/key'),
sendKey: require('value-event/key'),
// deprecated use hg.sendClick instead.
clickEvent: require('value-event/click'),
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')
};
function input(names) {
if (!names) {
return SingleEvent();
}
return MultipleEvent(names);
}
function state(obj) {
var copy = extend(obj);
var $channels = copy.channels;
var $handles = copy.handles;
if ($channels) {
copy.channels = mercury.value(null);
} else if ($handles) {
copy.handles = mercury.value(null);
}
var observ = mercury.struct(copy);
if ($channels) {
observ.channels.set(mercury.channels($channels, observ));
} else if ($handles) {
observ.handles.set(mercury.channels($handles, observ));
}
return observ;
}
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);
}