Skip to content

Commit

Permalink
⚡ Optimise lib size
Browse files Browse the repository at this point in the history
  • Loading branch information
exah committed Mar 22, 2020
1 parent 7b05bf1 commit ccddbe3
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 35 deletions.
18 changes: 11 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@
"main": "cjs/index.js",
"module": "esm/index.js",
"types": "types/index.d.ts",
"browser": {
"./cjs/index.js": "./cjs/index.browser.js",
"./esm/index.js": "./esm/index.browser.js"
},
"files": [
"cjs",
"esm",
"types"
"types",
"server"
],
"tags": [
"react",
Expand All @@ -28,6 +33,7 @@
"build:esm": "tsc -p tsconfig.esm.json",
"build:cjs": "tsc -p tsconfig.cjs.json",
"build:types": "tsc -p tsconfig.types.json",
"prebuild": "rimraf esm cjs types",
"test": "jest",
"lint": "eslint --ext ts --ext js src/",
"size": "size-limit",
Expand All @@ -45,16 +51,13 @@
"size-limit": [
{
"name": "[server]",
"path": "./esm/index.js",
"path": "./cjs/index.js",
"limit": "5.5Kb"
},
{
"name": "[client]",
"path": "./esm/index.js",
"limit": "1Kb",
"ignore": [
"react-ssr-prepass"
]
"path": "./esm/index.browser.js",
"limit": "580Kb"
}
],
"eslintConfig": {
Expand Down Expand Up @@ -137,6 +140,7 @@
"react": "^16.13.0",
"react-dom": "^16.13.0",
"react-is": "^16.13.0",
"rimraf": "^3.0.2",
"size-limit": "^4.0.1",
"ts-jest": "^25.2.0",
"typescript": "^3.7.5"
Expand Down
2 changes: 1 addition & 1 deletion src/context.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createContext } from 'react'
import { defaultStore } from './data-store'
import { defaultStore } from './store'

/**
* Provides `dataStore` created with {@link createDataStore} to {@link withData} components using [React Context](http://reactjs.org/docs/context.html).
Expand Down
10 changes: 4 additions & 6 deletions src/get-initial-data.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import prepass from 'react-ssr-prepass'
import { defaultStore } from './data-store'
import { RawData } from './types'
import { defaultStore } from './store'

function getInitialData(
element: React.ElementType,
store = defaultStore
): Promise<ReturnType<typeof store['get']>> {
function getInitialData(element: React.ElementType, store = defaultStore) {
store.clear()

return prepass(element).then(() => Array.from(store))
return prepass(element).then<RawData>(() => Array.from(store))
}

export { getInitialData }
3 changes: 3 additions & 0 deletions src/index.browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { DataProvider } from './context'
export { hydrateData } from './store'
export { useRUD } from './use-rud'
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { hydrateData } from './data-store'
export { DataProvider } from './context'
export { getInitialData } from './get-initial-data'
export { hydrateData } from './store'
export { useRUD } from './use-rud'
2 changes: 1 addition & 1 deletion src/data-store.ts → src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { RawData, Key } from './types'

export const defaultStore = new Map<Key, any>()

export const hydrateData = (input: RawData = []) =>
export const hydrateData = (input: RawData) =>
input.forEach(([key, value]) => {
defaultStore.set(key, value)
})
29 changes: 10 additions & 19 deletions src/use-async-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,29 @@ type Action<T> =

type Reducer<T> = (prevState: AsyncState<T>, action: Action<T>) => AsyncState<T>

const merge = <A, B>(a: A, b: B) => Object.assign({}, a, b)

const reducer: Reducer<any> = (prevState, action) => {
switch (action.type) {
case ActionTypes.START:
return {
...prevState,
isLoading: true,
error: null,
}
return merge(prevState, { isLoading: true, error: null })
case ActionTypes.FINISH: {
if (action.payload instanceof Error) {
return {
...FINISH_STATE,
isReady: false,
error: action.payload,
}
return merge(FINISH_STATE, { isReady: false, error: action.payload })
}

return {
...FINISH_STATE,
result: action.payload,
}
return merge(FINISH_STATE, { result: action.payload })
}
default:
throw new Error('Unknown action type')
}
}

export function useAsyncState<T>(initialState: AsyncState<T>) {
const [state, dispatch] = useReducer<Reducer<T>>(reducer, {
...INITIAL_STATE,
...initialState,
})
export function useAsyncState<T>(input: AsyncState<T>) {
const [state, dispatch] = useReducer<Reducer<T>>(
reducer,
merge(INITIAL_STATE, input)
)

const actions = useMemo(() => {
const start = () => {
Expand Down

0 comments on commit ccddbe3

Please sign in to comment.