stream-executor
- functional stream programming library
- This library is inspired by RxJS
- This library is effective for
- managing and reusing processes in actions in fine-grained
- the processing in the action becomes complicated
npm i stream-executor
Usage
1. chain stream (like RxJS)
Not using stream-executor
let isSucceeded = false
const initialValue = 1
let value = 0
if (value >= 0) {
value = initialValue * 10
}
if (value > 1) {
isSucceeded = true
} else {
console.log('not succeeded')
}
if (value < 10) {
return
}
const result = value
console.log(isSucceeded)
console.log(result)
using stream-executor
import { createStream, map, which, filter, tap } from 'stream-executor'
let isSucceeded = false
const chainResult = createStream(1)
.chain(
map((it) => it * 10),
which(
(it) => it > 1,
tap((it) => (isSucceeded = true)),
tap((it) => console.log('not succeeded'))
),
filter((it) => it >= 10)
)
.execute()
console.log(isSucceeded)
console.log(chainResult)
2. batch stream (like switch without break)
not using stream-executor
let isLoading: boolean
const mammal = { no: 999, name: 'UNKNOWN', type: 'bird' }
isLoading = true
if (mammal.type === 'bird') {
calculateSize(mammal)
} else {
console.log('Not Bird')
}
if (mammal.type == 'bird' && mammal.name !== 'UNKNOWN') {
console.log('maybe new species')
registerDB(mammal)
}
isLoading = false
console.log('end')
console.log(isLoading)
using stream-executor
import { createStream, ifRight, which } from 'stream-executor'
const mammal = { no: 999, name: 'UNKNOWN', type: 'bird' }
let isLoading = true
createStream(mammal)
.batch(
(_) => (isLoading = true),
which(
({ type }) => type === 'bird',
(it) => calculateSize(it),
(_) => console.log('Not Bird')
),
ifRight(
({ type, name }) => type === 'bird' && name === 'UNKNOWN',
(mammal) => registerDB(mammal)
),
(_) => (isLoading = false),
(_) => console.log('end')
)
.execute()
console.log(isLoading)
Important
1. about createStream
- The argument of createStream is not deep copied. use
deepCopy
method if you'd like to do deep copy, please.
import { createStream, tap, deepCopy } from 'stream-executor'
const input = { value: 1 }
const result = createStream(input)
.chain(tap((it) => (it.value += 9)))
console.log(input)
console.log(result)
const input2 = { value: 1 }
const result2 = createStream(deepCopy(input2))
.chain(tap((it) => (it.value += 9)))
console.log(input2)
console.log(result2)
2. about deepCopy
- getter and function in object are removed.
import { createStream, tap, deepCopy } from 'stream-executor'
class Wrapper<T> {
value: T
constructor(value: T) {
this.value = value
}
get doubledValue() {
return this.value * 2
}
hello() {
console.log('world')
}
}
const input = new Wrapper(1)
const result = createStream(deepCopy(input))
.chain(tap((it) => (it.value += 9)))
console.log(input)
console.log(result)
3. about createStream().chain()
:
- further process is not called if
undefined
returned
import { createStream, tap, filter, map } from 'stream-executor'
const result = createStream(1)
.chain(
tap((it) => console.log(it)),
filter((it) => it > 2),
map((it) => it + 9)
)
.execute()
console.log(result)
4. abount the arguments of execute()
- set the arguments of execute method if you'd like to customize error handling, please
let error: any
createStream(1)
.batch(
(it) => console.log(it),
..
)
.execute((err: any) => {
console.error(error)
error = err
})
Utils
helper methods and those descriptions in createStream are