treble-hook
Advanced tools
Comparing version 2.0.9 to 2.0.10
@@ -10,7 +10,5 @@ import React, { Dispatch, SetStateAction } from 'react'; | ||
export declare function usePubSub<T>(topic: string): [T, React.Dispatch<React.SetStateAction<T>>]; | ||
export declare function useSub<T>(topic: string): T; | ||
export declare function usePub<T>(topic: string): React.Dispatch<React.SetStateAction<T>>; | ||
export declare type Publish<T> = Dispatch<SetStateAction<T>>; | ||
export declare type PubSubTuple<T> = [T, Publish<T>]; | ||
export declare enum PubSubTupleIndex { | ||
State = 0, | ||
Publish = 1 | ||
} |
@@ -62,4 +62,3 @@ "use strict"; | ||
if (!context) { | ||
throw new Error(`The "${topic} topic must be used within the context of a TrebleHook publisher. | ||
Please wrap your App component with a TrebleHook publisher.`); | ||
throw new Error(getNoContextErrorMessage(topic)); | ||
} | ||
@@ -69,7 +68,26 @@ return context; | ||
exports.usePubSub = usePubSub; | ||
var PubSubTupleIndex; | ||
(function (PubSubTupleIndex) { | ||
PubSubTupleIndex[PubSubTupleIndex["State"] = 0] = "State"; | ||
PubSubTupleIndex[PubSubTupleIndex["Publish"] = 1] = "Publish"; | ||
})(PubSubTupleIndex = exports.PubSubTupleIndex || (exports.PubSubTupleIndex = {})); | ||
function useSub(topic) { | ||
if (!topicsCache[topic]) { | ||
throw new Error(getNoTopicErrorMessage(topic)); | ||
} | ||
const topicDef = topicsCache[topic]; | ||
const context = react_1.useContext(topicDef.context); | ||
if (!context) { | ||
throw new Error(getNoContextErrorMessage(topic)); | ||
} | ||
return context[0]; | ||
} | ||
exports.useSub = useSub; | ||
function usePub(topic) { | ||
if (!topicsCache[topic]) { | ||
throw new Error(getNoTopicErrorMessage(topic)); | ||
} | ||
const topicDef = topicsCache[topic]; | ||
const context = react_1.useContext(topicDef.context); | ||
if (!context) { | ||
throw new Error(getNoContextErrorMessage(topic)); | ||
} | ||
return context[1]; | ||
} | ||
exports.usePub = usePub; | ||
function createPublishContext() { | ||
@@ -96,2 +114,6 @@ return react_1.createContext(); | ||
} | ||
function getNoContextErrorMessage(topicName) { | ||
return `The "${topicName} topic must be used within the context of a TrebleHook publisher. | ||
Please wrap your App component with a TrebleHook publisher.`; | ||
} | ||
function getNoTopicErrorMessage(topicName) { | ||
@@ -98,0 +120,0 @@ return `The topic "${topicName}" has not been added. |
{ | ||
"name": "treble-hook", | ||
"version": "2.0.9", | ||
"version": "2.0.10", | ||
"description": "Get hooked on simple subscribe-and-publish in ReactJS.", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -38,7 +38,7 @@ | ||
```jsx | ||
import trebleHook, { usePubSub } from 'treble-hook' | ||
import trebleHook, { usePub, useSub } from 'treble-hook' | ||
// Welcome.jsx | ||
export default function Welcome() { | ||
const [guestName] = usePubSub('guest') | ||
const guestName = useSub('guest') | ||
@@ -52,3 +52,3 @@ return ( | ||
export default function GuestEntry() { | ||
const [, pubGuestName] = usePubSub('guest') | ||
const pubGuestName = usePub('guest') | ||
@@ -145,3 +145,3 @@ return ( | ||
A React hook that subscribes a component to a topic. The hook returns a tuple that is similar to the tuple returned from `useState` where the first element is the topic's current state value and the second element is the method to publish a new state value for the topic. | ||
A hook that subscribes a component to a topic with capability to publish. The hook returns a tuple that is similar to the tuple returned from `useState` where the first element is the topic's current state value and the second element is the method to publish a new state value for the topic. | ||
@@ -205,4 +205,60 @@ ```ts | ||
## <ins>usePub</ins> | ||
A hook returning a function that publishes to a topic. | ||
```ts | ||
usePub<T>(topic: string): (value: T) => void | ||
``` | ||
<ins>Example:</ins> | ||
```tsx | ||
import { usePub } from 'treble-hook' | ||
function FruitVendor() { | ||
const pubApples = usePub<number>('apples') | ||
return ( | ||
<div> | ||
<button | ||
onClick={() => { | ||
pubApples(100) | ||
}} | ||
> | ||
Reset apples | ||
</button> | ||
</div> | ||
) | ||
} | ||
``` | ||
<br/> | ||
## <ins>useSub</ins> | ||
A hook returning a function that subscribes to a topic. | ||
```ts | ||
useSub<T>(topic: string): T | ||
``` | ||
<ins>Example:</ins> | ||
```tsx | ||
import { useSub } from 'treble-hook' | ||
function FruitVendor() { | ||
const apples = useSub<number>('apples') | ||
return ( | ||
<div> | ||
Apply quantity: {apples} | ||
</div> | ||
) | ||
} | ||
``` | ||
# Liscense | ||
### MIT |
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
17433
132
261