MCPcopy Create free account
hub / github.com/ReactiveX/RxGo / Sample

Method Sample

observable_operator.go:1851–1920  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

1849// Sample returns an Observable that emits the most recent items emitted by the source
1850// Iterable whenever the input Iterable emits an item.
1851func (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

Callers

nothing calls this directly

Calls 7

ObserveMethod · 0.95
parseOptionsFunction · 0.85
newChannelIterableFunction · 0.85
SendContextMethod · 0.80
buildChannelMethod · 0.65
buildContextMethod · 0.65
ObserveMethod · 0.65

Tested by

no test coverage detected