-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
💥 Replace custom data store with
Map
- Loading branch information
Showing
11 changed files
with
53 additions
and
141 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,23 @@ | ||
import { AsyncState } from './types' | ||
|
||
export const IS_CLIENT = typeof window !== 'undefined' | ||
export const IS_SERVER = !IS_CLIENT | ||
export const INITIAL_ID = -1 | ||
|
||
export const enum ActionTypes { | ||
START = 'START', | ||
FINISH = 'FINISH', | ||
} | ||
|
||
export const INITIAL_STATE: AsyncState<null> = { | ||
isReady: false, | ||
isLoading: false, | ||
error: null, | ||
result: null, | ||
} | ||
|
||
export const FINISH_STATE: AsyncState<null> = { | ||
isReady: true, | ||
isLoading: false, | ||
error: null, | ||
result: null, | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,8 @@ | ||
import { Data, Store } from './types' | ||
import { INITIAL_ID } from './constants' | ||
import { RawData, Key } from './types' | ||
|
||
export function createDataStore<T>(initial: T): Store<T> { | ||
let store = initial | ||
let pointer = INITIAL_ID | ||
export const defaultStore = new Map<Key, any>() | ||
|
||
return { | ||
init: (value) => { | ||
pointer = INITIAL_ID | ||
store = value | ||
}, | ||
save: (id, value) => { | ||
store[id] = value | ||
}, | ||
nextId: () => { | ||
pointer += 1 | ||
return pointer | ||
}, | ||
resetIds: () => { | ||
pointer = INITIAL_ID | ||
return pointer | ||
}, | ||
exists: (id) => store[id] !== undefined, | ||
remove: (id) => delete store[id], | ||
getById: (id) => (id != null ? store[id] : null), | ||
isInitial: () => Object.keys(store).length === 0, | ||
get: () => store, | ||
} | ||
} | ||
|
||
export const defaultDataStore = createDataStore<Data>({}) | ||
export const hydrateData = (data: Data) => defaultDataStore.init(data) | ||
export const hydrateData = (input: RawData = []) => | ||
input.forEach(([key, value]) => { | ||
defaultStore.set(key, value) | ||
}) |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,13 @@ | ||
import prepass from 'react-ssr-prepass' | ||
import { Store } from './types' | ||
import { defaultDataStore } from './data-store' | ||
import { defaultStore } from './data-store' | ||
|
||
function getInitialData( | ||
element: React.ElementType, | ||
dataStore: Store = defaultDataStore | ||
): Promise<ReturnType<typeof dataStore['get']>> { | ||
dataStore.init({}) | ||
store = defaultStore | ||
): Promise<ReturnType<typeof store['get']>> { | ||
store.clear() | ||
|
||
return new Promise((resolve, reject) => | ||
prepass(element) | ||
.then(() => { | ||
dataStore.resetIds() // prepare for next render | ||
resolve(dataStore.get()) | ||
}) | ||
.catch((error: Error) => { | ||
reject(error) | ||
}) | ||
) | ||
return prepass(element).then(() => Array.from(store)) | ||
} | ||
|
||
export { getInitialData } |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
export { createDataStore, hydrateData } from './data-store' | ||
export { hydrateData } from './data-store' | ||
export { DataProvider } from './context' | ||
export { getInitialData } from './get-initial-data' | ||
export { RUD } from './rud' | ||
export { useRUD } from './use-rud' |
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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