treble-hook
Advanced tools
Comparing version 2.0.0-alpha.4 to 2.0.0-alpha.5
import React, { Dispatch, SetStateAction } from 'react'; | ||
declare const _default: { | ||
addTopic<T>(topicName: string, defaultValue: T): void; | ||
addTopic<T>(topicName: string, defaultValue: T, initWithSessionStorage?: boolean): void; | ||
getPublisher(topics?: string[] | undefined): React.FunctionComponent<{ | ||
@@ -5,0 +5,0 @@ children?: React.ReactNode; |
@@ -14,6 +14,13 @@ "use strict"; | ||
return { | ||
addTopic(topicName, defaultValue) { | ||
addTopic(topicName, defaultValue, initWithSessionStorage = false) { | ||
if (!topicsCache[topicName]) { | ||
const context = createPublishContext(); | ||
const provider = createPublishProvider(context, defaultValue); | ||
let normDefaultValue = defaultValue; | ||
if (initWithSessionStorage) { | ||
const checkValue = sessionStorage.getItem(topicName); | ||
if (checkValue !== null) { | ||
normDefaultValue = checkValue; | ||
} | ||
} | ||
const provider = createPublishProvider(topicName, context, normDefaultValue, initWithSessionStorage); | ||
topicsCache[topicName] = { | ||
@@ -65,5 +72,11 @@ context, | ||
} | ||
function createPublishProvider(TrebleHookContext, defaultValue) { | ||
function createPublishProvider(topicName, TrebleHookContext, defaultValue, initWithSessionStorage = false) { | ||
return (props) => { | ||
const contextState = react_1.useState(defaultValue); | ||
const [stateValue] = contextState; | ||
react_1.useEffect(() => { | ||
if (initWithSessionStorage) { | ||
sessionStorage.setItem(topicName, String(stateValue)); | ||
} | ||
}, [stateValue]); | ||
return react_1.default.createElement(TrebleHookContext.Provider, Object.assign({ value: contextState }, props)); | ||
@@ -70,0 +83,0 @@ }; |
{ | ||
"name": "treble-hook", | ||
"version": "2.0.0-alpha.4", | ||
"version": "2.0.0-alpha.5", | ||
"description": "Get hooked on simple subscribe-and-publish in ReactJS.", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12276
101