Sample returns an Observable that emits the most recent items emitted by the source Iterable whenever the input Iterable emits an item.
(iterable Iterable, opts ...Option)
| 1849 | // Sample returns an Observable that emits the most recent items emitted by the source |
| 1850 | // Iterable whenever the input Iterable emits an item. |
| 1851 | func (o *ObservableImpl) Sample(iterable Iterable, opts ...Option) Observable { |
| 1852 | option := parseOptions(opts...) |
| 1853 | next := option.buildChannel() |
| 1854 | ctx := option.buildContext(o.parent) |
| 1855 | itCh := make(chan Item) |
| 1856 | obsCh := make(chan Item) |
| 1857 | |
| 1858 | go func() { |
| 1859 | defer close(obsCh) |
| 1860 | observe := o.Observe(opts...) |
| 1861 | for { |
| 1862 | select { |
| 1863 | case <-ctx.Done(): |
| 1864 | return |
| 1865 | case i, ok := <-observe: |
| 1866 | if !ok { |
| 1867 | return |
| 1868 | } |
| 1869 | i.SendContext(ctx, obsCh) |
| 1870 | } |
| 1871 | } |
| 1872 | }() |
| 1873 | |
| 1874 | go func() { |
| 1875 | defer close(itCh) |
| 1876 | observe := iterable.Observe(opts...) |
| 1877 | for { |
| 1878 | select { |
| 1879 | case <-ctx.Done(): |
| 1880 | return |
| 1881 | case i, ok := <-observe: |
| 1882 | if !ok { |
| 1883 | return |
| 1884 | } |
| 1885 | i.SendContext(ctx, itCh) |
| 1886 | } |
| 1887 | } |
| 1888 | }() |
| 1889 | |
| 1890 | go func() { |
| 1891 | defer close(next) |
| 1892 | var lastEmittedItem Item |
| 1893 | isItemWaitingToBeEmitted := false |
| 1894 | |
| 1895 | for { |
| 1896 | select { |
| 1897 | case _, ok := <-itCh: |
| 1898 | if ok { |
| 1899 | if isItemWaitingToBeEmitted { |
| 1900 | next <- lastEmittedItem |
| 1901 | isItemWaitingToBeEmitted = false |
| 1902 | } |
| 1903 | } else { |
| 1904 | return |
| 1905 | } |
| 1906 | case item, ok := <-obsCh: |
| 1907 | if ok { |
| 1908 | lastEmittedItem = item |
nothing calls this directly
no test coverage detected