treble-hook
Advanced tools
Comparing version 2.0.0-alpha.2 to 2.0.0-alpha.3
import React, { Dispatch, SetStateAction } from 'react'; | ||
declare const _default: { | ||
addTopic<T>(topicName: string, defaultValue: T): void; | ||
getPublisher(): React.FunctionComponent<{ | ||
getPublisher(topics?: string[] | undefined): React.FunctionComponent<{ | ||
children?: React.ReactNode; | ||
@@ -6,0 +6,0 @@ }>; |
@@ -11,7 +11,7 @@ "use strict"; | ||
const react_1 = __importStar(require("react")); | ||
const topics = {}; | ||
const topicsCache = {}; | ||
exports.default = (function TrebleHookPublisherFactory() { | ||
return { | ||
addTopic(topicName, defaultValue) { | ||
if (topics[topicName]) { | ||
if (topicsCache[topicName]) { | ||
throw new Error(`The topic "${topicName}" has already been added.`); | ||
@@ -21,3 +21,3 @@ } | ||
const provider = createPublishProvider(context, defaultValue); | ||
topics[topicName] = { | ||
topicsCache[topicName] = { | ||
context, | ||
@@ -27,6 +27,9 @@ provider, | ||
}, | ||
getPublisher() { | ||
getPublisher(topics) { | ||
const TrebleHookPublisher = ({ children, }) => { | ||
const ProviderNest = Object.keys(topics).reduce((tally, topicName) => { | ||
const topic = topics[topicName]; | ||
const topicNames = topics && topics.length | ||
? Object.keys(topicsCache).filter(key => topics.some(topicName => topicName === key)) | ||
: Object.keys(topicsCache); | ||
const ProviderNest = topicNames.reduce((tally, topicName) => { | ||
const topic = topicsCache[topicName]; | ||
if (!topic) { | ||
@@ -45,6 +48,6 @@ throw new Error(getNoTopicErrorMessage(topicName)); | ||
function usePubSub(topic) { | ||
if (!topics[topic]) { | ||
if (!topicsCache[topic]) { | ||
throw new Error(getNoTopicErrorMessage(topic)); | ||
} | ||
const topicDef = topics[topic]; | ||
const topicDef = topicsCache[topic]; | ||
const context = react_1.useContext(topicDef.context); | ||
@@ -51,0 +54,0 @@ if (!context) { |
{ | ||
"name": "treble-hook", | ||
"version": "2.0.0-alpha.2", | ||
"version": "2.0.0-alpha.3", | ||
"description": "Get hooked on simple subscribe-and-publish in ReactJS.", | ||
@@ -24,2 +24,3 @@ "main": "./lib/index.js", | ||
"lint": "tslint -c ./tslint.json ./src/**/*.tsx", | ||
"publishNext": "yarn publish --tag next", | ||
"prepublishOnly": "yarn test", | ||
@@ -26,0 +27,0 @@ "pretest": "yarn build", |
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
11215
89