Skip to content

Commit

Permalink
feat(functions): new opearator useComponentsStream for register all s…
Browse files Browse the repository at this point in the history
…tream at once (can exclude)
  • Loading branch information
nampdn committed Apr 22, 2019
1 parent 11487e3 commit 447b6ad
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 24 deletions.
8 changes: 2 additions & 6 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ import { register, useBarnStream, useInitStream } from 'bara'
import App from './App'
import './index.css'
import {
useComponentsStream,
useReactApp,
useTextStream,
useTouchableOpacityStream,
useTouchableStream,
useViewStream,
} from './lib'

Expand All @@ -19,9 +17,7 @@ const BaraApp = () => {
})
useReactApp({ name: 'bara-app', App })
useViewStream()
useTouchableStream()
useTouchableOpacityStream()
useTextStream()
useComponentsStream({TouchableOpacity: false})
welcomeTrigger()
}

Expand Down
34 changes: 17 additions & 17 deletions src/lib/exports/TouchableOpacity/stream.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createEmitter, useEmitter, useStream } from 'bara'
import { createEmitter, useEmitter, useStream, EventType } from 'bara'

import {
BaraReactTouchableOpacity,
Expand Down Expand Up @@ -74,22 +74,22 @@ export function useTouchableOpacityStream() {
)
}

const getEmitter = (eventType: EventType) => {
let emit = useEmitter(eventType)
const warn = () => {
// tslint:disable-next-line
process.env.NODE_ENV === 'development' && console.warn(
`[TouchableOpacity] No action executed because TouchableOpacity is "false" when you call "useComponentsStream", call "useTouchableOpacityStream" to register this stream or change its value to "true"!`,
)
}
emit = emit! || warn
return emit
}

// Export and being consumed by any Touchable component
export const touchableOpacityContext: BaraTouchableOpacityContext = {
onPress: data => {
const emit = useEmitter(ON_TOUCHABLE_OPACITY_PRESS)
emit!(data)
},
onPressIn: data => {
const emit = useEmitter(ON_TOUCHABLE_OPACITY_PRESS_IN)
emit!(data)
},
onPressOut: data => {
const emit = useEmitter(ON_TOUCHABLE_OPACITY_PRESS_OUT)
emit!(data)
},
onLongPress: data => {
const emit = useEmitter(ON_TOUCHABLE_OPACITY_LONG_PRESS)
emit!(data)
},
onPress: data => getEmitter(ON_TOUCHABLE_OPACITY_PRESS)(data),
onPressIn: data => getEmitter(ON_TOUCHABLE_OPACITY_PRESS_IN)(data),
onPressOut: data => getEmitter(ON_TOUCHABLE_OPACITY_PRESS_OUT)(data),
onLongPress: data => getEmitter(ON_TOUCHABLE_OPACITY_LONG_PRESS)(data),
}
3 changes: 2 additions & 1 deletion src/lib/functions/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './use-barn-state'
export * from './barn-bridge'
export * from './barn-bridge'
export * from './use-components-stream'
30 changes: 30 additions & 0 deletions src/lib/functions/use-components-stream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import * as ExportedStreams from '../exports'

export interface UseComponentsStreamConfig {
Text?: boolean
Touchable?: boolean
TouchableOpacity?: boolean
View?: boolean
}

const defaultConfig: UseComponentsStreamConfig = {
Text: true,
Touchable: true,
TouchableOpacity: true,
View: true,
}

/**
* Register all components as Bara stream
*/
export function useComponentsStream(config?: UseComponentsStreamConfig) {
const combinedConfig = { ...defaultConfig, ...config }
for (const component in combinedConfig) {
if ((combinedConfig as any)[component] === true) {
const methodName = `use${component}Stream`
if (methodName in ExportedStreams) {
;(ExportedStreams as any)[methodName]()
}
}
}
}

0 comments on commit 447b6ad

Please sign in to comment.