-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexamples.vim
272 lines (260 loc) · 8.44 KB
/
examples.vim
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
let s:i = 0
function! s:log(x) abort
let s:i += 1
echom 'log ' . s:i . ' ' . json_encode(a:x)
endfunction
function! callbag#demo() abort
call callbag#pipe(
\ callbag#interval(1000),
\ callbag#take(10),
\ callbag#map({x-> x + 1}),
\ callbag#filter({x-> x % 2 == 0}),
\ callbag#map({x-> x * 1000}),
\ callbag#forEach({x -> s:log(x) }),
\ )
call callbag#pipe(
\ callbag#fromEvent(['TextChangedI', 'TextChangedP']),
\ callbag#debounceTime(250),
\ callbag#forEach({x -> s:log('text changed') }),
\ )
call callbag#pipe(
\ callbag#fromEvent('InsertLeave'),
\ callbag#forEach({x -> s:log('InsertLeave') }),
\ )
call callbag#pipe(
\ callbag#empty(),
\ callbag#subscribe({
\ 'next': {x->s:log('next will never be called')},
\ 'error': {e->s:log('error will never be called')},
\ 'complete': {->s:log('complete will be called')},
\ }),
\ )
call callbag#pipe(
\ callbag#never(),
\ callbag#subscribe(
\ {x->s:log('next will not be called')},
\ {e->s:log('error will not be called')},
\ {->s:log('complete will not be called')},
\ ),
\ )
call callbag#pipe(
\ callbag#create({next,error,done->next('next')}),
\ callbag#subscribe({
\ 'next': {x->s:log('next')},
\ 'error': {e->s:log('error')},
\ 'complete': {->s:log('complete')},
\ }),
\ )
call callbag#pipe(
\ callbag#create({next,error,done->next('val')}),
\ callbag#forEach({x->s:log('next value is ' . x)}),
\ )
call callbag#pipe(
\ callbag#lazy({->2*10}),
\ callbag#subscribe({
\ 'next':{x->s:log('next ' . x)},
\ 'complete': {->s:log('complete')},
\ }),
\ )
call callbag#pipe(
\ callbag#throwError('my dummy error'),
\ callbag#subscribe({
\ 'next': {x->s:log('next will never be called')},
\ 'error': {e->s:log('error called with ' . e)},
\ 'complete': {->s:log('complete will never be called')},
\ }),
\ )
call callbag#pipe(
\ callbag#of(1, 2, 3, 4),
\ callbag#subscribe({
\ 'next': {x->s:log('next value is ' . x)},
\ 'complete': {->s:log('completed of')},
\ }),
\ )
call callbag#pipe(
\ callbag#merge(
\ callbag#fromEvent('InsertEnter'),
\ callbag#fromEvent('InsertLeave'),
\ ),
\ callbag#forEach({x->s:log('InsertEnter or InsertLeave')}),
\ )
call callbag#pipe(
\ callbag#fromEvent('TextChangedI', 'text_change_autocmd_group_name'),
\ callbag#takeUntil(
\ callbag#fromEvent('InsertLeave', 'insert_leave_autocmd_group_name'),
\ ),
\ callbag#debounceTime(250),
\ callbag#subscribe({
\ 'next': {x->s:log('next')},
\ 'error': {x->s:log('error')},
\ 'complete': {->s:log('complete')},
\ }),
\ )
call callbag#pipe(
\ callbag#fromEvent('InsertEnter'),
\ callbag#delay(2000),
\ callbag#forEach({x->s:log('next')}),
\ )
call callbag#pipe(
\ callbag#of(1,2,3,4,5,6,7,8,9),
\ callbag#group(3),
\ callbag#forEach({x->s:log(x)}),
\ )
call callbag#pipe(
\ callbag#fromEvent('InsertEnter'),
\ callbag#map({x->callbag#interval(1000)}),
\ callbag#flatten(),
\ callbag#forEach({x->s:log(x)}),
\ )
call callbag#pipe(
\ callbag#of(1,2,3,4,5),
\ callbag#scan({prev, x-> prev + x}, 0),
\ callbag#forEach({x->s:log(x)}),
\ )
call callbag#pipe(
\ callbag#of(1,2,3,4,5),
\ callbag#reduce({prev, x-> prev + x}, 0),
\ callbag#forEach({x->s:log(x)}),
\ )
call callbag#pipe(
\ callbag#concat(
\ callbag#of(1,2,3),
\ callbag#of(4,5,6),
\ ),
\ callbag#subscribe({
\ 'next':{x->s:log('next ' . x)},
\ 'complete': {->s:log('complete')},
\ }),
\ )
call callbag#pipe(
\ callbag#combine(
\ callbag#interval(100),
\ callbag#interval(350),
\ ),
\ callbag#take(10),
\ callbag#skip(2),
\ callbag#subscribe({
\ 'next':{x->s:log('next '. x[0] . ' ' . x[1])},
\ 'complete': {->s:log('complete')},
\ }),
\ )
call callbag#pipe(
\ callbag#of(1, 2, 3, 4, 5),
\ callbag#takeWhile({x -> x != 4}),
\ callbag#subscribe({
\ 'next':{x->s:log('next ' . x)},
\ 'complete': {->s:log('complete')},
\ }),
\ )
let l:MapAndTake3 = callbag#operate(
\ callbag#map({x->x*10}),
\ callbag#take(3),
\ )
call callbag#pipe(
\ callbag#of(1,2,3,4,5,6,7,8,9),
\ l:MapAndTake3,
\ callbag#subscribe({
\ 'next': {x->s:log(x)},
\ 'complete': {->s:log('complete')},
\ })
\ )
call callbag#pipe(
\ callbag#fromList([1,2,3,4]),
\ callbag#subscribe({
\ 'next': {x->s:log(x)},
\ 'complete': {->s:log('complete')},
\ }),
\ )
let l:Subject = callbag#makeSubject()
let s:Dispose = callbag#pipe(
\ l:Subject,
\ callbag#subscribe({
\ 'next': {d->s:log(d)},
\ 'error': {e->s:log(e)},
\ 'complete': {->s:Dispose()},
\ })
\ )
call l:Subject(1, 'hello')
call l:Subject(1, 'world')
call l:Subject(2, callbag#undefined())
let l:ShareSouce = callbag#share(callbag#interval(1000))
call callbag#pipe(
\ l:ShareSouce,
\ callbag#subscribe({x->s:log('first ' . x)})
\ )
call timer_start(3500, {->callbag#pipe(
\ l:ShareSouce,
\ callbag#subscribe({x->s:log('second ' . x)})
\ )})
call callbag#pipe(
\ callbag#of(1, 1, 2, 3, 3, 3, 4, 1, 5),
\ callbag#distinctUntilChanged(),
\ callbag#subscribe({
\ 'next': {x->s:log('next ' . x)},
\ 'complete': {-> s:log('complete')},
\ }),
\ )
call callbag#pipe(
\ callbag#of('hi'),
\ callbag#switchMap({->callbag#of(10, 20, 30)}, {char, num-> char . num}),
\ callbag#subscribe({
\ 'next':{x->s:log(x)},
\ 'error':{e->s:log(e)},
\ 'complete':{->s:log('complete')},
\ }),
\ )
call callbag#pipe(
\ callbag#of(1, 2, 3),
\ callbag#tap({'next':{x->s:log(x)}, 'complete':{->s:log('complete')}}),
\ callbag#subscribe(),
\ )
call callbag#pipe(
\ callbag#of(1, 2, 3),
\ callbag#materialize(),
\ callbag#subscribe({
\ 'next':{x->s:log(['next', x, callbag#isNextNotification(x), callbag#isErrorNotification(x), callbag#isCompleteNotification(x)])},
\ 'error':{x->s:log(['error', x])},
\ 'complete':{->s:log('complete')},
\ })
\ )
let l:undefined = callbag#undefined()
echom callbag#isUndefined(l:undefined)
echom callbag#isUndefined({})
try
let l:result = callbag#pipe(
\ callbag#interval(250),
\ callbag#take(3),
\ callbag#toList(),
\ ).wait({ 'sleep': 1, 'timeout': 5000 })
echom l:result
catch
" error may be thrown due to timeout or if it emits error
echom v:exception . ' ' . v:throwpoint
endtry
" let s:Stdin = callbag#makeSubject()
" call callbag#pipe(
" \ callbag#spawn(['bash', '-c', 'read i; echo $i'], { 'stdin': s:Stdin }),
" \ callbag#subscribe({
" \ 'next':{x->s:log(['next', x])},
" \ 'complete':{->s:log('complete')},
" \ 'error':{x->s:log(['error', x])},
" \ }),
" \ )
" call s:Stdin(1, 'hello')
" call s:Stdin(2, callbag#undefined())
" Plug 'vim-jp/vital.vim'
"
" call callbag#pipe(
" \ callbag#fromPromise(s:promiseWait(2000)),
" \ callbag#subscribe({
" \ 'next': {x->s:log('next')},
" \ 'complete':{->s:log('complete')},
" \ 'error': {e->s:log('error')},
" \ })
" \ )
endfunction
function! s:promiseWait(ms)
let s:V = vital#vital#new()
let s:Promise = s:V.import('Async.Promise')
return s:Promise.new({resolve -> timer_start(a:ms, resolve)})
endfunction