Skip to content

Commit 447b6ad

Browse files
committed
feat(functions): new opearator useComponentsStream for register all stream at once (can exclude)
1 parent 11487e3 commit 447b6ad

File tree

4 files changed

+51
-24
lines changed

4 files changed

+51
-24
lines changed

src/index.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ import { register, useBarnStream, useInitStream } from 'bara'
22
import App from './App'
33
import './index.css'
44
import {
5+
useComponentsStream,
56
useReactApp,
6-
useTextStream,
7-
useTouchableOpacityStream,
8-
useTouchableStream,
97
useViewStream,
108
} from './lib'
119

@@ -19,9 +17,7 @@ const BaraApp = () => {
1917
})
2018
useReactApp({ name: 'bara-app', App })
2119
useViewStream()
22-
useTouchableStream()
23-
useTouchableOpacityStream()
24-
useTextStream()
20+
useComponentsStream({TouchableOpacity: false})
2521
welcomeTrigger()
2622
}
2723

src/lib/exports/TouchableOpacity/stream.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createEmitter, useEmitter, useStream } from 'bara'
1+
import { createEmitter, useEmitter, useStream, EventType } from 'bara'
22

33
import {
44
BaraReactTouchableOpacity,
@@ -74,22 +74,22 @@ export function useTouchableOpacityStream() {
7474
)
7575
}
7676

77+
const getEmitter = (eventType: EventType) => {
78+
let emit = useEmitter(eventType)
79+
const warn = () => {
80+
// tslint:disable-next-line
81+
process.env.NODE_ENV === 'development' && console.warn(
82+
`[TouchableOpacity] No action executed because TouchableOpacity is "false" when you call "useComponentsStream", call "useTouchableOpacityStream" to register this stream or change its value to "true"!`,
83+
)
84+
}
85+
emit = emit! || warn
86+
return emit
87+
}
88+
7789
// Export and being consumed by any Touchable component
7890
export const touchableOpacityContext: BaraTouchableOpacityContext = {
79-
onPress: data => {
80-
const emit = useEmitter(ON_TOUCHABLE_OPACITY_PRESS)
81-
emit!(data)
82-
},
83-
onPressIn: data => {
84-
const emit = useEmitter(ON_TOUCHABLE_OPACITY_PRESS_IN)
85-
emit!(data)
86-
},
87-
onPressOut: data => {
88-
const emit = useEmitter(ON_TOUCHABLE_OPACITY_PRESS_OUT)
89-
emit!(data)
90-
},
91-
onLongPress: data => {
92-
const emit = useEmitter(ON_TOUCHABLE_OPACITY_LONG_PRESS)
93-
emit!(data)
94-
},
91+
onPress: data => getEmitter(ON_TOUCHABLE_OPACITY_PRESS)(data),
92+
onPressIn: data => getEmitter(ON_TOUCHABLE_OPACITY_PRESS_IN)(data),
93+
onPressOut: data => getEmitter(ON_TOUCHABLE_OPACITY_PRESS_OUT)(data),
94+
onLongPress: data => getEmitter(ON_TOUCHABLE_OPACITY_LONG_PRESS)(data),
9595
}

src/lib/functions/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './use-barn-state'
2-
export * from './barn-bridge'
2+
export * from './barn-bridge'
3+
export * from './use-components-stream'
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import * as ExportedStreams from '../exports'
2+
3+
export interface UseComponentsStreamConfig {
4+
Text?: boolean
5+
Touchable?: boolean
6+
TouchableOpacity?: boolean
7+
View?: boolean
8+
}
9+
10+
const defaultConfig: UseComponentsStreamConfig = {
11+
Text: true,
12+
Touchable: true,
13+
TouchableOpacity: true,
14+
View: true,
15+
}
16+
17+
/**
18+
* Register all components as Bara stream
19+
*/
20+
export function useComponentsStream(config?: UseComponentsStreamConfig) {
21+
const combinedConfig = { ...defaultConfig, ...config }
22+
for (const component in combinedConfig) {
23+
if ((combinedConfig as any)[component] === true) {
24+
const methodName = `use${component}Stream`
25+
if (methodName in ExportedStreams) {
26+
;(ExportedStreams as any)[methodName]()
27+
}
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)