generated from snivilised/astrolib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref(rx): complete factory impl (#134)
- Loading branch information
1 parent
fa839c4
commit 9691cfd
Showing
16 changed files
with
1,824 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package rx | ||
|
||
import ( | ||
"context" | ||
"time" | ||
) | ||
|
||
// Infinite represents an infinite wait time | ||
var Infinite int64 = -1 | ||
|
||
// Duration represents a duration | ||
type Duration interface { | ||
duration() time.Duration | ||
} | ||
|
||
type duration struct { | ||
d time.Duration | ||
} | ||
|
||
func (d *duration) duration() time.Duration { | ||
return d.d | ||
} | ||
|
||
// WithDuration is a duration option | ||
func WithDuration(d time.Duration) Duration { | ||
return &duration{ | ||
d: d, | ||
} | ||
} | ||
|
||
type causalityDuration struct { | ||
fs []execution | ||
} | ||
|
||
type execution struct { | ||
f func() | ||
isTick bool | ||
} | ||
|
||
func timeCausality[T any](elems ...any) (context.Context, Observable[T], Duration) { | ||
ch := make(chan Item[T], 1) | ||
fs := make([]execution, len(elems)+1) | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
|
||
for i, elem := range elems { | ||
i := i | ||
elem := elem | ||
|
||
if el, ok := elem.(Item[T]); ok && el.IsTick() { | ||
fs[i] = execution{ | ||
f: func() {}, | ||
isTick: true, | ||
} | ||
} else { | ||
switch elem := elem.(type) { | ||
case Item[T]: | ||
fs[i] = execution{ | ||
f: func() { | ||
ch <- elem | ||
}, | ||
} | ||
|
||
case error: | ||
fs[i] = execution{ | ||
f: func() { | ||
ch <- Error[T](elem) | ||
}, | ||
} | ||
|
||
case T: | ||
fs[i] = execution{ | ||
f: func() { | ||
ch <- Of(elem) | ||
}, | ||
} | ||
} | ||
} | ||
} | ||
|
||
fs[len(elems)] = execution{ | ||
f: func() { | ||
cancel() | ||
}, | ||
isTick: false, | ||
} | ||
|
||
return ctx, FromChannel(ch), &causalityDuration{fs: fs} | ||
} | ||
|
||
func (d *causalityDuration) duration() time.Duration { | ||
pop := d.fs[0] | ||
pop.f() | ||
|
||
d.fs = d.fs[1:] | ||
|
||
if pop.isTick { | ||
return time.Nanosecond | ||
} | ||
|
||
return time.Minute | ||
} | ||
|
||
// type mockDuration struct { | ||
// mock.Mock | ||
// } | ||
|
||
// func (m *mockDuration) duration() time.Duration { | ||
// args := m.Called() | ||
// return args.Get(0).(time.Duration) | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package rx | ||
|
||
// IllegalInputError is triggered when the observable receives an illegal input. | ||
type IllegalInputError struct { | ||
error string | ||
} | ||
|
||
func (e IllegalInputError) Error() string { | ||
return "illegal input: " + e.error | ||
} | ||
|
||
// IndexOutOfBoundError is triggered when the observable cannot access to the specified index. | ||
type IndexOutOfBoundError struct { | ||
error string | ||
} | ||
|
||
func (e IndexOutOfBoundError) Error() string { | ||
return "index out of bound: " + e.error | ||
} |
Oops, something went wrong.