This file contains hidden or 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
// 1. Define a routine | |
const FetchDataRoutine: createThunkRoutine<DataType, Error>('FETCH_DATA') | |
// 2. Create a thunk | |
const fetchData = () => async (dispatch: Dispatch, getState: () => RootState) => { | |
dispatch(FetchDataRoutine.request()); | |
try { | |
const data = await api.fetchData(); | |
dispatch(FetchDataRoutine.success(data)); | |
} catch (e) { |
This file contains hidden or 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
class WebAppViewController: UIViewController { | |
var webView: WKWebView? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
let contentController = WKUserContentController() | |
contentController.add(self, name: "native") |
This file contains hidden or 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
if let error = error { | |
let js = "window.jsCallbackBridge.resolvePromise('\(uuid)', null, '\(error.localizedDescription)')" | |
DispatchQueue.main.async { | |
self.webView?.evaluateJavaScript(js, completionHandler: nil) | |
} | |
return | |
} | |
guard let response = response as? HTTPURLResponse, (200...299).contains(response.statusCode) else { | |
let js = "window.jsCallbackBridge.resolvePromise('\(uuid)', null, 'server error')" |
This file contains hidden or 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
import _ from 'lodash'; | |
const resolvePromise = (uuid, resolveData, rejectReason) => { | |
const promise = _.get(window, `jsCallbackBridge.promises.${uuid}`); | |
if (!promise) { | |
return; | |
} | |
const { resole, reject } = {...promise}; |
This file contains hidden or 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
const sendNativePostRequest = (url, params) => { | |
return new Promise((resolve, reject) => { | |
const uuid = uuidv4(); | |
_.set(window, `jsCallbackBridge.promises.${uuid}`, { resolve, reject }); | |
const body = ''; //TODO: construct body | |
window.webkit.messageHandlers.native.postMessage({ | |
type: 'SEND_HTTP_REQUEST', | |
url, | |
body, |
This file contains hidden or 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
// @flow | |
import React from 'react'; | |
type MapValuesToProps = (values: any) => any; | |
export const connectToContext = (context: React$Context<any>) => (mapValuesToProps: MapValuesToProps) => ( | |
Comp: any | |
) => { | |
const wrapped = (props: any) => { |
This file contains hidden or 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
const discount1 = (price, discount) => { | |
const final = price * (1 - discount); | |
console.log(`V1 -- Original Price: ${price}, Discount: ${discount}, Final Price: ${final}`); | |
} | |
discount1(500, 0.5); | |
discount1(400, 0.5); | |
discount1(300, 0.5); | |
const halfPrice1 = price => discount1(price, 0.5); |
This file contains hidden or 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
// Reducer | |
export default (state: LoadingState = {}, action: Action<any>): LoadingState => { | |
const { type } = action; | |
const matches = /(.*)\/(REQUEST|SUCCESS|FAILURE)/.exec(type); | |
// Ignore non-routine actions: | |
// A routine action should have one of three suffixes: | |
// ['/REQUEST', '/SUCCESS', '/FAILURE'] | |
if (!matches) return state; |
This file contains hidden or 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
// State | |
// To know more about Record type, see https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkt | |
export type LoadingState = Record<string, boolean>; |
This file contains hidden or 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
// Selectors | |
// Select the whole loading state object | |
export const selectLoadingState = (state: RootState) => state.ui.loading; | |
// Select whether a given routine is loading | |
export const selectLoading = (routineType: string) => { | |
return createSelector([selectLoadingState], (state: LoadingState) => { | |
return !!state[routineType]; | |
}); |