Comparing version 1.0.0 to 1.1.0
@@ -9,3 +9,3 @@ import { EventEmitter2 as EventEmitter } from "eventemitter2"; | ||
declare type PredicateFn = (...args: any[]) => boolean; | ||
declare type EventCreatorFn<T extends { | ||
export declare type EventCreatorFn<T extends { | ||
type: string; | ||
@@ -12,0 +12,0 @@ payload: any; |
{ | ||
"name": "ts-bus", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
export { useBusReducer } from "./useBusReducer"; | ||
export { useBusState } from "./useBusState"; | ||
export { useBus, BusProvider } from "./BusContext"; |
@@ -5,2 +5,4 @@ "use strict"; | ||
exports.useBusReducer = useBusReducer_1.useBusReducer; | ||
var useBusState_1 = require("./useBusState"); | ||
exports.useBusState = useBusState_1.useBusState; | ||
var BusContext_1 = require("./BusContext"); | ||
@@ -7,0 +9,0 @@ exports.useBus = BusContext_1.useBus; |
@@ -40,3 +40,3 @@ "use strict"; | ||
}); | ||
it("should not subscribe without unsubscribing ", function () { | ||
it("should not subscribe without unsubscribing (useBusReducer)", function () { | ||
var mockBus = mockEventBus(); | ||
@@ -65,2 +65,29 @@ // run once to subscribe to bus | ||
}); | ||
it("should not subscribe without unsubscribing (useBusState)", function () { | ||
var mockBus = mockEventBus(); | ||
var incrementEvent = EventBus_1.createEventDefinition()("increment"); | ||
// run once to subscribe to bus | ||
var hook = react_hooks_1.renderHook(function () { | ||
return react_2.useBusState(0, incrementEvent); | ||
}, { | ||
wrapper: function (_a) { | ||
var children = _a.children; | ||
return (react_1.default.createElement(react_2.BusProvider, { value: mockBus }, children)); | ||
} | ||
}); | ||
hook.unmount(); | ||
expect(mockBus.subscribe.mock.calls.length).toBe(1); | ||
expect(mockBus._unsubscribe.mock.calls.length).toBe(1); | ||
}); | ||
it("should update state", function () { | ||
var incrementEvent = EventBus_1.createEventDefinition()("increment"); | ||
var result = react_hooks_1.renderHook(function () { | ||
return react_2.useBusState(0, incrementEvent); | ||
}, { wrapper: wrapper }).result; | ||
expect(result.current).toBe(0); | ||
react_hooks_1.act(function () { | ||
bus.publish(incrementEvent(1)); | ||
}); | ||
expect(result.current).toBe(1); | ||
}); | ||
it("should reduce state", function () { | ||
@@ -67,0 +94,0 @@ var result = react_hooks_1.renderHook(function () { |
@@ -51,3 +51,3 @@ <p align="center"> | ||
- Redux - conflates state management with eventing and causes complexity around async as a result. React comes with state management out of the box these days anyway. | ||
- Redux - conflates state management with eventing and causes complexity around async as a result. Redux has a highly invasive syntax that is difficult to remove or abstract out of an application. React comes with state management out of the box these days anyway. See my article ["Life after Redux"](https://itnext.io/life-after-redux-21f33b7f189e?source=friends_link&sk=a2566ae4b3b28797505a1295d70392fe) | ||
- RxJS - could make a great event bus but feels too heavy handed for use with many projects. | ||
@@ -112,12 +112,21 @@ - Node `events` - is a little too much API for what I need here. This lib actually decorates the `EventEmitter2` package. In the future I may remove it to become dependency free. | ||
You can also provide a function to do runtime payload type checking. This might be useful if you are working in JavaScript: | ||
You can also provide a predicate to do runtime payload type checking in development. This is useful as a sanity check if you are working in JavaScript: | ||
```js | ||
import p from "pdsl"; | ||
export const taskLabelUpdated = createEventDefinition(p`{ | ||
id: String, | ||
label: String, | ||
}`)("task.label.updated"); | ||
import p from "pdsl"; | ||
// pdsl creates predicate functions | ||
const isLabel = p`{ | ||
id: string, | ||
label: string, | ||
}` | ||
export const taskLabelUpdated = createEventDefinition(isLabel)("task.label.updated"); | ||
taskLabelUpdated({id:"abc"}); // {"id":"abc"} does not match expected payload. | ||
``` | ||
These warnings are suppressed in production. | ||
### Subscribing | ||
@@ -386,1 +395,28 @@ | ||
``` | ||
### useBusState | ||
This connects state changes to bus events via a useState equivalent function. | ||
```tsx | ||
import { useBus, useBusState } from "ts-bus/react"; | ||
const setCountEvent = createEventDefinition<number>()("SET_COUNT"); | ||
function Counter() { | ||
const bus = useBus(); | ||
const count = useBusState(0, setCountEvent); | ||
return ( | ||
<> | ||
Count: {count} | ||
<button onClick={() => bus.publish(setCountEvent(count + 1))}> | ||
+ | ||
</button> | ||
<button onClick={() => bus.publish(setCountEvent(count - 1))}> | ||
- | ||
</button> | ||
</> | ||
); | ||
} | ||
``` |
Sorry, the diff of this file is not supported yet
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
280938
33
633
419