Skip to content

Instantly share code, notes, and snippets.

declare function searchForMovies(query?: string): Promise<Movie[]>
declare function MovieList(props: { movies: Movie[] }): JSX.Element
declare function LoadingIndicator(props: { loadingSince: Date }): JSX.Element
declare function ErrorMessageUI(props: { error: Error }): JSX.Element
type Movie = { title: string, id: string }
type ViewState = (
{ status: 'loading', loadingSince: Date } |
{ status: 'done', searchResults: Movie[] } |
declare function searchForMovies(query?: string): Promise<Movie[]>
declare function MovieList({ movies }: { movies: Movie[] }): JSX.Element
declare function LoadingIndicator(): JSX.Element
declare function ErrorMessageUI({ message }: { message: string }): JSX.Element
type Movie = { title: string, id: string }
type ViewState = {
loading: boolean,
error?: Error,
searchResults?: Movie[],
declare function searchForMovies(query?: string): Promise<Movie[]>
declare function MovieList(props: { movies: Movie[] }): JSX.Element
declare function LoadingIndicator(): JSX.Element
declare function ErrorMessageUI(props: { message: string }): JSX.Element
type Movie = { title: string, id: string }
export function MovieSearch() {
const [searchTerm, setSearchTerm] = useState<string>('')
const [searchResults, setSearchResults] = useState<Movie[]>()
type ThunkAction<T> = (dispatch: Dispatch<T>) => T
type AsyncDispatch<T> = Dispatch<T | ThunkAction<T>>
function wrapAsync<T>(dispatch: Dispatch<T>): AsyncDispatch<T> {
return function(action: T | ThunkAction) {
if (action instanceof Function) {
return action(dispatch)
}
return dispatch(action)
}
type AsyncDispatch<T> = Dispatch<T | Promise<T>>
function wrapAsync<T>(dispatch: Dispatch<T>): AsyncDispatch<T> {
return (action: T | Promise<T>) => {
if (action instanceof Promise) {
return action.then(dispatch)
}
return dispatch(action)
}
}
/**
* SomeUtilFile.tsx
*
* Generic wrapper for async dispatch
*/
export function useAsyncReducer<T>(initial: T, reducer: Reducer<T, Action>): [T, AsyncDispatch<T>] {
let [state, dispatch] = useReducer<T>(reducer, initial)
let wrappedDispatch = useMemo(() => wrappedDispatch(dispatch), [dispatch])
let stateWithWrappedDispatch = useMemo(() => [state, wrappedDispatch], [state, wrappedDispatch])
/**
* SomeUtilFile.tsx
*
* Generic wrapper for creating store reducer and connecting actions
*/
export function useAsyncReducer<T>(initial: T, reducer: Reducer<T, ReducerAction<T>>, actions: AsyncActions<T>): [T, ConnectedActions<T>] {
let [state, dispatch] = useReducer<T>(reducer, initial)
let connectedActions = useMemo(() => connectActionsToDispatch(actions, dispatch), [dispatch, actions])
let stateAndConnectedActions = useMemo(() => [state, connectedActions], [state, connectedActions])
@solomon-gumball
solomon-gumball / ScrollWithSequential
Last active August 29, 2015 14:15
Scrollview w/ Sequential Layout
define(function(require, exports, module) {
var Engine = require('famous/core/Engine');
var Surface = require('famous/core/Surface');
var ScrollView = require('famous/views/ScrollView');
var SequentialLayout = require('famous/views/SequentialLayout');
var mainContext = Engine.createContext();
mainContext.setPerspective(1000);
// Create top surface
@solomon-gumball
solomon-gumball / gist:28c6ac175845447f1508
Created November 8, 2014 00:06
RenderController inAlignFrom and outAlignFrom test.
define(function(require, exports, module) {
var Engine = require('famous/core/Engine');
var Modifier = require('famous/core/Modifier');
var Surface = require('famous/core/Surface');
var RenderController = require('famous/views/RenderController');
var context = Engine.createContext();
var rc = new RenderController();
@solomon-gumball
solomon-gumball / CustomTransitionable
Created September 15, 2014 20:26
A basic pauseable transitionable.
define(function(require, exports, module) {
var Transitionable = require('famous/transitions/Transitionable');
function CustomTransitionable() {
Transitionable.apply(this, arguments);
}
CustomTransitionable.prototype = Object.create(Transitionable.prototype);
CustomTransitionable.prototype.constructor = CustomTransitionable;