-
Notifications
You must be signed in to change notification settings - Fork 663
/
action.js
256 lines (190 loc) · 5.59 KB
/
action.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
import { createElement } from 'react';
import marksy from 'marksy/components';
import { A, H1, H2, H3, H4, H5, P, Li, Ol, Ul, Hr, Code, Blockquote, ArticleHeader, Video } from '~/templates/global/styled';
import { Img } from '~/templates/content/styled';
import ContentTemplate from '~/templates/content/Template';
import Example from '~/components/examples/Example';
import CodePen from '~/components/examples/CodePen';
const removeEmpty = filename => filename !== '';
const convertMarkdown = marksy({
createElement,
elements: {
a: A,
h1: ArticleHeader,
h2: H2,
h3: H3,
h4: H4,
h5: H5,
p: P,
code: Code,
li: Li,
ol: Ol,
ul: Ul,
hr: Hr,
img: Img,
blockquote: Blockquote,
},
components: {
Example,
CodePen,
Video
}
});
const content = convertMarkdown(`
# Action
Action is a simplified Rx-inspired reactive stream focused on animation.
**Every Popmotion animation and input is an action.**
When an action is started, it returns a simple interface that includes **at least** a \`stop\` method.
## Import
\`\`\`javascript
import { action } from 'popmotion';
\`\`\`
## Usage
### Definition
The \`action\` factory takes one argument, an \`init\` function.
This is a function that is provided an object of \`update\`, \`complete\`, and \`error\` functions.
Usage of these functions is optional. Your action may call all or just some of them:
\`\`\`javascript
action(({ update, complete, error }) => {
update(1);
});
\`\`\`
### Initialisation
\`action\` returns a \`start\` method. This also accepts an object of \`update\`, \`complete\`, and \`error\` functions.
When called, the \`init\` function is provided these functions, and a **new instance** of the action is created.
Calling \`start\` multiple times will create multiple, separate instances of the action.
For example:
\`\`\`javascript
const foo = action(({ update }) => {
let i = 0;
setInterval(() => update(i++), 50);
});
foo.start({
update: (v) => console.log(v)
}); // 0, 1, 2...
\`\`\`
If \`start\` is passed **only a function**, that is assigned to the \`update\` function:
\`\`\`javascript
foo.start((v) => console.log(v)); // 0, 1, 2...
\`\`\`
We can also listen for the \`complete\` event like this:
\`\`\`javascript
const foo = action(({ update, complete }) => {
let i = 0;
setInterval(() => {
update(i++);
if (i === 10) complete();
}, 50);
});
foo.start({
update: (v) => console.log(v), // ...8, 9, 10
complete: () => console.log('complete!')
});
\`\`\`
### Interface
The \`init\` function can **optionally** return an API.
For instance, we might use this to stop a timer:
\`\`\`javascript
const foo = action(({ update }) => {
const interval = setInterval(() => update('ping!'), 100);
return {
stop: () => clearInterval(interval)
};
});
const bar = foo.start(console.log);
setTimeout(() => bar.stop(), 1000);
\`\`\`
Any method returned by the action \`init\` function will be exposed when an action instance is created.
### Modification
\`action\` is **chainable**, which means it offers methods that can alter the behaviour of the base action. Currently, these are \`while\` and \`pipe\` (see [Methods](#methods)).
When an action is chained, a **new action** is returned. For instance:
\`\`\`javascript
const foo = action(({ update }) => {
let i = 0;
setInterval(() => update(i++), 50);
});
const lessThanTen = (v) => v < 10;
const log = (v) => console.log(v);
foo.start(log); // ...8, 9, 10, 11...
foo.while(lessThanTen).start(log); // ...8, 9
\`\`\`
## Methods
### \`pipe\`
\`\`\`typescript
pipe(...funcs: (v: any) => any)
\`\`\`
Returns a **new** action that passes the output of the original action's \`update\` through the provided functions, from left to right.
#### Example
\`\`\`javascript
const init = ({ update }) => update(10);
const double = (v) => v * 2;
const px = (v) => v + 'px';
action(init)
.pipe(double, px)
.start((v) => console.log(v)); // '20px'
\`\`\`
### \`start\`
\`\`\`typescript
start(update: (v: any) => void)
start({
complete? () => void,
error?: (err: any) => void,
update?: (v: any) => void
})
start(reaction)
\`\`\`
Starts the action by running its initiation function, and returning its API.
Every interface returned by a \`start\` call, **regardless of the API returned from the observable**, will return at least a \`stop\` function.
It can be provided either an \`update\` function, or an object with \`update\`, \`complete\` and \`error\` functions.
#### Example
\`\`\`javascript
// Doesn't return an API
const foo = action(({ update }) => update(1)).start();
foo.stop();
// Returns a custom API
const bar = action(({ update }) => {
let i = 0;
setInterval(() => update(i), 100);
return {
setOutput: (v) => i = v
};
}).start();
bar.setOutput(2);
setTimeout(() => bar.stop(), 1000);
\`\`\`
### \`while\`
\`\`\`typescript
while(predicate: (v: any) => boolean)
\`\`\`
Returns a new action, that will continue to run **while** the updated values match the provided predicate.
When the predicate function returns \`false\`, the action will \`complete\`.
### Example
\`\`\`javascript
let latest = 0;
const init = ({ update }) => {
let i = latest;
setInterval(() => update(i++), 50);
};
action(init)
.while((v) => v < 10)
.start({
update: (v) => latest = v,
complete: () => console.log(latest) // 9
});
\`\`\`
`);
const Page = ({ section }) => (
<ContentTemplate
id="action"
section="api"
undefined
title="Action"
description="Create a reactive stream of values."
published=""
theme="pure"
next="animations"
>
{content.tree}
</ContentTemplate>
);
export default Page;