-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update portal implementation and utility functions
- Loading branch information
Showing
15 changed files
with
237 additions
and
339 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export * from "./behaviorSubject"; | ||
export * from "./usePortalImplementation"; | ||
export * from "./usePortal"; | ||
export * from "./makePortal"; | ||
export * from "./portal"; |
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 |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import { | ||
Config, | ||
CookieConfig, | ||
GetValueByPath, | ||
Paths, | ||
PortalOptions, | ||
} from "@/definition"; | ||
import { getResolvedState, getValue } from "@/utilities"; | ||
|
||
import { Portal } from "./portal"; | ||
import { useCookieImplementation } from "./useCookieImplementation"; | ||
import { useLocalImplementation } from "./useLocalImplementation"; | ||
import { usePortalImplementation } from "./usePortalImplementation"; | ||
import { useSessionImplementation } from "./useSessionImplementation"; | ||
|
||
function makePortal<Store extends Record<string, any>>(register: Store) { | ||
/** | ||
* Represents a mapping of keys (stringified) to portal entries. | ||
* @type {PortalMap} | ||
*/ | ||
const store = new Portal(); | ||
|
||
/** | ||
* A map of portal entries. | ||
* @type {PortalMap} | ||
*/ | ||
const portal = { | ||
/** | ||
* A hook for managing the portal states. | ||
* | ||
* @param path The path of the portal's state | ||
* @param {PortalOptions<State>} [options] The options of the portal's state | ||
* | ||
* @returns {PortalState<State, Data>} A tuple containing the current state and a function to update the state. | ||
*/ | ||
use: function usePortalWithStore< | ||
Path extends Paths<Store>, | ||
State extends GetValueByPath<Store, Path>, | ||
Data = State | ||
>(path: Path, options?: PortalOptions<State, Data>) { | ||
const initialState = options?.state | ||
? getResolvedState(options.state) | ||
: getValue(register, path); | ||
|
||
return usePortalImplementation<Store, Path, State, Data>({ | ||
path, | ||
initialState, | ||
options, | ||
store, | ||
}); | ||
}, | ||
|
||
/** | ||
* A hook for managing the portal states with local storage. | ||
* | ||
* @param path The path of the portal's state | ||
* @param {Config<State>} [config] The config of the portal's state | ||
* | ||
* @returns {PortalState<State, Data>} A tuple containing the current state and a function to update the state. | ||
*/ | ||
local: function useLocalWithStore< | ||
Path extends Paths<Store>, | ||
State extends GetValueByPath<Store, Path>, | ||
Data = State | ||
>(path: Path, config?: Config<State, Data>) { | ||
const initialState = config?.state | ||
? getResolvedState(config.state) | ||
: getValue(register, path); | ||
|
||
return useLocalImplementation<Store, Path, State, Data>({ | ||
path, | ||
initialState, | ||
config, | ||
store, | ||
}); | ||
}, | ||
|
||
/** | ||
* A hook for managing the portal states with session storage. | ||
* | ||
* @param path The path of the portal's state | ||
* @param {Config<State>} [config] The config of the portal's state | ||
* | ||
* @returns {PortalState<State, Data>} A tuple containing the current state and a function to update the state. | ||
*/ | ||
session: function useSessionWithStore< | ||
Path extends Paths<Store>, | ||
State extends GetValueByPath<Store, Path>, | ||
Data = State | ||
>(path: Path, config?: Config<State, Data>) { | ||
const initialState = config?.state | ||
? getResolvedState(config.state) | ||
: getValue(register, path); | ||
|
||
return useSessionImplementation<Store, Path, State, Data>({ | ||
path, | ||
initialState, | ||
config, | ||
store, | ||
}); | ||
}, | ||
|
||
/** | ||
* A hook for managing the portal states with cookie storage. | ||
* | ||
* @param path The path of the portal's state | ||
* @param {CookieConfig<State>} [config] The config of the portal's state | ||
* | ||
* @returns {PortalState<State, Data>} A tuple containing the current state and a function to update the state. | ||
*/ | ||
cookie: function useCookieWithStore< | ||
Path extends Paths<Store>, | ||
State extends GetValueByPath<Store, Path>, | ||
Data = State | ||
>(path: Path, config?: CookieConfig<State, Data>) { | ||
const initialState = config?.state | ||
? getResolvedState(config.state) | ||
: getValue(register, path); | ||
|
||
return useCookieImplementation<Store, Path, State, Data>({ | ||
path, | ||
initialState, | ||
config, | ||
store, | ||
}); | ||
}, | ||
|
||
make: makePortal, | ||
entries: store.entries, | ||
removeItem: store.removeItem, | ||
hasItem: store.hasItem, | ||
getItem: store.getItem, | ||
setItem: store.setItem, | ||
clear: store.clear, | ||
}; | ||
|
||
return portal; | ||
} | ||
|
||
export const portal = makePortal({} as Record<string, any>); |
Oops, something went wrong.