Comparing version 2.0.0 to 2.1.0
{ | ||
"name": "ts-bus", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -80,2 +80,17 @@ "use strict"; | ||
}); | ||
it("should update state (options configuration)", function () { | ||
var incrementEvent = EventBus_1.createEventDefinition()("counter.increment"); | ||
var result = react_hooks_1.renderHook(function () { | ||
return react_2.useBusState.configure({ subscriber: function (dispatch, bus) { | ||
return bus.subscribe("counter.**", function (v) { return dispatch(v.payload); }); | ||
} })(0); | ||
}, { | ||
wrapper: wrapper | ||
}).result; | ||
expect(result.current).toBe(0); | ||
react_hooks_1.act(function () { | ||
bus.publish(incrementEvent(1)); | ||
}); | ||
expect(result.current).toBe(1); | ||
}); | ||
it("should update state", function () { | ||
@@ -82,0 +97,0 @@ var incrementEvent = EventBus_1.createEventDefinition()("increment"); |
@@ -448,1 +448,20 @@ <p align="center"> | ||
``` | ||
#### useBusState configuration | ||
You can configure useBusState with a subscriber passing in an options object. | ||
```ts | ||
// get a new useState function | ||
const useState = useBusState.configure({ | ||
subscriber: (dispatch, bus) => bus.subscribe("**", (ev) => dispatch(ev.payload)) | ||
}); | ||
const state = useState(/*...*/); | ||
``` | ||
Available options: | ||
| Option | Description | | ||
| ---------- | ------------------- | | ||
| subscriber | Subscriber function | |
@@ -16,2 +16,3 @@ import { EventBus } from "./EventBus"; | ||
export declare type SubscribeFn<E extends BusEvent> = (dispatch: DispatchFn<E>, bus: EventBus) => UnsubscribeFn; | ||
export declare type SubscribeWithPayloadDispatchFn<E extends BusEvent> = (dispatch: DispatchFn<E["payload"]>, bus: EventBus) => UnsubscribeFn; | ||
export declare type EventCreatorFn<T extends { | ||
@@ -18,0 +19,0 @@ type: string; |
@@ -1,2 +0,9 @@ | ||
import { EventCreatorFn, BusEvent } from "./types"; | ||
import { EventCreatorFn, BusEvent, SubscribeWithPayloadDispatchFn } from './types'; | ||
export declare function useBusState<E extends BusEvent = BusEvent>(initState: E["payload"] | undefined, event: EventCreatorFn<E>): E["payload"]; | ||
export declare namespace useBusState { | ||
var configure: <E extends BusEvent<any> = BusEvent<any>>(options: UseBusStateOptions<E>) => (initState: E["payload"] | (() => E["payload"])) => E["payload"]; | ||
} | ||
declare type UseBusStateOptions<E extends BusEvent> = { | ||
subscriber: SubscribeWithPayloadDispatchFn<E>; | ||
}; | ||
export {}; |
@@ -5,16 +5,19 @@ "use strict"; | ||
var react_2 = require("./react"); | ||
function useBusState(initState, event) { | ||
var useStateCreator = function (subscriber) { return function (initState) { | ||
var bus = react_2.useBus(); | ||
var _a = react_1.useState(initState), state = _a[0], dispatch = _a[1]; | ||
react_1.useLayoutEffect(function () { | ||
var unsubscribe = bus.subscribe(event, function (v) { | ||
dispatch(v.payload); | ||
}); | ||
return function () { | ||
unsubscribe(); | ||
}; | ||
}, [dispatch, bus]); | ||
react_1.useLayoutEffect(function () { return subscriber(dispatch, bus); }, [dispatch, bus]); | ||
return state; | ||
}; }; | ||
function useBusState(initState, event) { | ||
var bus = react_2.useBus(); | ||
var useState = useStateCreator(function (dispatch, bus) { | ||
return bus.subscribe(event, function (ev) { return dispatch(ev.payload); }); | ||
}); | ||
return useState(initState); | ||
} | ||
exports.useBusState = useBusState; | ||
useBusState.configure = function (options) { | ||
return useStateCreator(options.subscriber); | ||
}; | ||
//# sourceMappingURL=useBusState.js.map |
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
285921
689
466