stocker 0.2.0

Stocks dashboard
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
use derivative::Derivative;
use im::{hashmap, HashMap};
use reactive_rs::{Broadcast, Stream};
use std::{cell::RefCell, hash::Hash, rc::Rc};

pub trait StreamExt<'a>: Stream<'a> {
    fn buffer(self, count: usize) -> Buffer<Self, Self::Item>
    where
        Self::Item: 'a + Clone + Sized,
    {
        Buffer {
            buf: Rc::new(RefCell::new(Vec::with_capacity(count))),
            count,
            stream: self,
        }
    }

    fn combine_latest<U, F, T>(
        self,
        other: U,
        func: F,
    ) -> CombineLatest<Self, U, NoContext<F>, Self::Item, U::Item, Self::Context>
    where
        U: Stream<'a>,
        F: 'a + FnMut(&(Self::Item, U::Item)) -> T,
        Self::Item: 'a + Clone + Sized,
        Self::Context: 'a + Clone + Sized,
        U::Item: 'a + Clone + Sized,
    {
        CombineLatest {
            buf_a: Rc::new(RefCell::new(None)),
            buf_b: Rc::new(RefCell::new(None)),
            buf_ctx: Rc::new(RefCell::new(None)),
            func: NoContext(func),
            stream_a: self,
            stream_b: other,
        }
    }

    fn distinct_until_changed(self) -> DistinctUntilChanged<Self, Self::Item>
    where
        Self::Item: 'a + Clone + PartialEq + Sized,
    {
        DistinctUntilChanged {
            buf: Rc::new(RefCell::new(None)),
            stream: self,
        }
    }

    fn group_by<F, G, K, V>(
        self,
        key_func: F,
        value_func: G,
    ) -> GroupBy<'a, Self, NoContext<F>, NoContext<G>, K, V, Self::Context>
    where
        F: 'a + FnMut(&Self::Item) -> K,
        G: 'a + FnMut(&Self::Item) -> V,
        Self::Item: 'a + Clone + Sized,
        Self::Context: 'a + Sized,
    {
        GroupBy {
            key_func: NoContext(key_func),
            key_grouped_map: Rc::new(RefCell::new(hashmap! {})),
            stream: self,
            value_func: NoContext(value_func),
        }
    }

    fn merge<U>(self, other: U) -> Merge<Self, U>
    where
        U: Stream<'a, Item = Self::Item, Context = Self::Context>,
    {
        Merge {
            stream_a: self,
            stream_b: other,
        }
    }

    fn switch(self) -> Switch<Self>
    where
        Self::Item: Stream<'a>,
    {
        Switch { stream: self }
    }

    fn with_latest_from<U, F, T>(
        self,
        other: U,
        func: F,
    ) -> WithLatestFrom<Self, U, NoContext<F>, U::Item>
    where
        U: Stream<'a>,
        F: 'a + FnMut(&(Self::Item, U::Item)) -> T,
        Self::Item: 'a + Clone + Sized,
        U::Item: 'a + Clone + Sized,
    {
        WithLatestFrom {
            buf_b: Rc::new(RefCell::new(None)),
            func: NoContext(func),
            stream_a: self,
            stream_b: other,
        }
    }
}

impl<'a, T> StreamExt<'a> for T where T: Stream<'a> {}

pub struct Buffer<S, T: Sized> {
    buf: Rc<RefCell<Vec<T>>>,
    count: usize,
    stream: S,
}

impl<'a, S, T> Stream<'a> for Buffer<S, T>
where
    S: Stream<'a, Item = T>,
    T: 'a + Clone + Sized,
{
    type Context = S::Context;
    type Item = [T];

    fn subscribe_ctx<O>(self, mut observer: O)
    where
        O: 'a + FnMut(&Self::Context, &Self::Item),
    {
        self.stream.subscribe_ctx({
            let buf = self.buf;
            let count = self.count;
            move |ctx, x| {
                let full = {
                    buf.borrow_mut().push(x.clone());
                    buf.borrow().len() == count
                };
                if full {
                    observer(ctx, &buf.borrow()[..]);
                    buf.borrow_mut().clear();
                }
            }
        });
    }
}

pub struct CombineLatest<S, U, F, A: Sized, B: Sized, C: Sized> {
    buf_a: Rc<RefCell<Option<A>>>,
    buf_b: Rc<RefCell<Option<B>>>,
    buf_ctx: Rc<RefCell<Option<C>>>,
    func: F,
    stream_a: S,
    stream_b: U,
}

impl<'a, S, U, F, A, B, C> Stream<'a> for CombineLatest<S, U, F, A, B, C>
where
    S: Stream<'a, Item = A, Context = C>,
    U: Stream<'a, Item = B>,
    F: 'a + ContextFn<C, (A, B)>,
    A: 'a + Clone + Sized,
    B: 'a + Clone + Sized,
    C: 'a + Clone + Sized,
{
    type Context = C;
    type Item = F::Output;

    fn subscribe_ctx<O>(self, mut observer: O)
    where
        O: 'a + FnMut(&Self::Context, &Self::Item),
    {
        let sink: Broadcast<C, (A, B)> = Broadcast::new();
        sink.clone().subscribe_ctx({
            let mut func = self.func;
            move |ctx, x| {
                observer(ctx, &func.call_mut(ctx, x));
            }
        });
        self.stream_a.subscribe_ctx({
            let buf_a = self.buf_a.clone();
            let buf_b = self.buf_b.clone();
            let buf_ctx = self.buf_ctx.clone();
            let sink = sink.clone();
            move |ctx, a| {
                buf_a.borrow_mut().replace(a.clone());
                buf_ctx.borrow_mut().replace(ctx.clone());
                let buf_b = buf_b.borrow();
                if let Some(b) = buf_b.as_ref() {
                    let b = b.clone();
                    drop(buf_b);
                    sink.send_ctx(ctx, &(a.clone(), b));
                }
            }
        });
        self.stream_b.subscribe({
            let buf_a = self.buf_a;
            let buf_b = self.buf_b;
            let buf_ctx = self.buf_ctx;
            move |b| {
                buf_b.borrow_mut().replace(b.clone());
                let buf_a = buf_a.borrow();
                if let Some(a) = buf_a.as_ref() {
                    let a = a.clone();
                    let buf_ctx = buf_ctx.borrow();
                    let ctx = buf_ctx.as_ref().cloned().unwrap();
                    drop(buf_a);
                    drop(buf_ctx);
                    sink.send_ctx(&ctx, &(a, b.clone()));
                }
            }
        });
    }
}

pub struct DistinctUntilChanged<S, T: Sized> {
    buf: Rc<RefCell<Option<T>>>,
    stream: S,
}

impl<'a, S, T> Stream<'a> for DistinctUntilChanged<S, T>
where
    S: Stream<'a, Item = T>,
    T: 'a + Clone + PartialEq + Sized,
{
    type Context = S::Context;
    type Item = T;

    fn subscribe_ctx<O>(self, mut observer: O)
    where
        O: 'a + FnMut(&Self::Context, &Self::Item),
    {
        self.stream.subscribe_ctx({
            let buf = self.buf;
            move |ctx, x| {
                if !matches!(&*buf.borrow(), Some(y) if x == y) {
                    buf.borrow_mut().replace(x.clone());
                    observer(ctx, x);
                }
            }
        });
    }
}

pub struct GroupBy<'a, S, F, G, K: Sized, V: Sized, C> {
    key_func: F,
    #[allow(clippy::type_complexity)]
    key_grouped_map: Rc<RefCell<HashMap<K, Grouped<'a, K, V, C>>>>,
    stream: S,
    value_func: G,
}

impl<'a, S, F, G, K, V, T, C> Stream<'a> for GroupBy<'a, S, F, G, K, V, C>
where
    S: Stream<'a, Item = T, Context = C>,
    F: 'a + ContextFn<C, T, Output = K>,
    G: 'a + ContextFn<C, T, Output = V>,
    K: 'a + Clone + Eq + Hash,
    V: 'a,
    C: 'a,
{
    type Context = C;
    type Item = Grouped<'a, K, V, C>;

    fn subscribe_ctx<O>(self, mut observer: O)
    where
        O: 'a + FnMut(&Self::Context, &Self::Item),
    {
        self.stream.subscribe_ctx({
            let mut key_func = self.key_func;
            let key_grouped_map = self.key_grouped_map;
            let mut value_func = self.value_func;
            move |ctx, x| {
                let key = key_func.call_mut(ctx, x);
                let mut key_grouped_map = key_grouped_map.borrow_mut();
                let grouped = key_grouped_map.entry(key.clone()).or_insert_with(|| {
                    let grouped = Grouped {
                        key: key.clone(),
                        sink: Broadcast::new(),
                    };
                    observer(ctx, &grouped);
                    grouped
                });
                grouped.sink.send_ctx(ctx, value_func.call_mut(ctx, x));
            }
        });
    }
}

#[derive(Derivative)]
#[derivative(Clone(bound = "K: Clone"))]
pub struct Grouped<'a, K: Sized, V: 'a + Sized, C: 'a> {
    pub key: K,
    sink: Broadcast<'a, C, V>,
}

impl<'a, K, V, C> Stream<'a> for Grouped<'a, K, V, C>
where
    V: 'a,
    C: 'a,
{
    type Context = C;
    type Item = V;

    fn subscribe_ctx<O>(self, observer: O)
    where
        O: 'a + FnMut(&Self::Context, &Self::Item),
    {
        self.sink.subscribe_ctx(observer);
    }
}

pub struct Merge<S, U> {
    stream_a: S,
    stream_b: U,
}

impl<'a, S, U, T, C> Stream<'a> for Merge<S, U>
where
    S: Stream<'a, Item = T, Context = C>,
    U: Stream<'a, Item = T, Context = C>,
    T: 'a,
    C: 'a,
{
    type Context = C;
    type Item = T;

    fn subscribe_ctx<O>(self, mut observer: O)
    where
        O: 'a + FnMut(&Self::Context, &Self::Item),
    {
        let sink = Broadcast::new();
        sink.clone().subscribe_ctx(move |ctx, x| {
            observer(ctx, x);
        });
        self.stream_a.subscribe_ctx({
            let sink = sink.clone();
            move |ctx, x| {
                sink.send_ctx(ctx, x);
            }
        });
        self.stream_b.subscribe_ctx(move |ctx, x| {
            sink.send_ctx(ctx, x);
        });
    }
}

pub struct Switch<S> {
    stream: S,
}

impl<'a, S, X, T, C> Stream<'a> for Switch<S>
where
    S: Stream<'a, Item = X>,
    X: Clone + Stream<'a, Item = T, Context = C>,
    T: 'a,
    C: 'a,
{
    type Context = C;
    type Item = T;

    fn subscribe_ctx<O>(self, mut observer: O)
    where
        O: 'a + FnMut(&Self::Context, &Self::Item),
    {
        let sink = Broadcast::new();
        sink.clone().subscribe_ctx(move |ctx, x| {
            observer(ctx, x);
        });
        self.stream.subscribe(move |inner_stream| {
            inner_stream.clone().subscribe_ctx({
                let sink = sink.clone();
                move |ctx, x| {
                    sink.send_ctx(ctx, x);
                }
            });
        });
    }
}

pub struct WithLatestFrom<S, U, F, B: Sized> {
    buf_b: Rc<RefCell<Option<B>>>,
    func: F,
    stream_a: S,
    stream_b: U,
}

impl<'a, S, U, F, A, B> Stream<'a> for WithLatestFrom<S, U, F, B>
where
    S: Stream<'a, Item = A>,
    U: Stream<'a, Item = B>,
    F: 'a + ContextFn<S::Context, (A, B)>,
    A: 'a + Clone + Sized,
    B: 'a + Clone + Sized,
{
    type Context = S::Context;
    type Item = F::Output;

    fn subscribe_ctx<O>(self, mut observer: O)
    where
        O: 'a + FnMut(&Self::Context, &Self::Item),
    {
        self.stream_a.subscribe_ctx({
            let buf_b = self.buf_b.clone();
            let mut func = self.func;
            move |ctx, a| {
                let buf_b = buf_b.borrow();
                if let Some(b) = buf_b.as_ref() {
                    let b = b.clone();
                    drop(buf_b);
                    observer(ctx, &func.call_mut(ctx, &(a.clone(), b)));
                }
            }
        });
        self.stream_b.subscribe({
            let buf_b = self.buf_b;
            move |b| {
                buf_b.borrow_mut().replace(b.clone());
            }
        });
    }
}

pub trait ContextFn<C: ?Sized, T: ?Sized> {
    type Output;

    fn call_mut(&mut self, ctx: &C, item: &T) -> Self::Output;
}

impl<C: ?Sized, T: ?Sized, V, F> ContextFn<C, T> for F
where
    F: FnMut(&C, &T) -> V,
{
    type Output = V;

    #[inline(always)]
    fn call_mut(&mut self, ctx: &C, item: &T) -> Self::Output {
        self(ctx, item)
    }
}

pub struct NoContext<F>(F);

impl<F, C: ?Sized, T: ?Sized, V> ContextFn<C, T> for NoContext<F>
where
    F: FnMut(&T) -> V,
{
    type Output = V;

    #[inline(always)]
    fn call_mut(&mut self, _ctx: &C, item: &T) -> Self::Output {
        (self.0)(item)
    }
}