@bloc-js/react-bloc
Advanced tools
Comparing version 1.2.19 to 1.2.20
import { Bloc } from "@bloc-js/bloc"; | ||
export declare type CreateBlocFn<S> = () => Bloc<any, S>; | ||
export declare function useBlocState<S>(bloc: Bloc<any, S> | CreateBlocFn<S>): S | undefined; | ||
export declare type CreateBlocFn<B> = () => B; | ||
export declare function useBlocState<S, B extends Bloc<any, S>>(blocCreator: B | CreateBlocFn<B>): S; |
@@ -1,8 +0,17 @@ | ||
import { useEffect, useState } from "react"; | ||
export function useBlocState(bloc) { | ||
var _a = useState(), state = _a[0], setState = _a[1]; | ||
import { useEffect, useState, useRef } from "react"; | ||
export function useBlocState(blocCreator) { | ||
var blocRef = useRef(); | ||
function getBloc() { | ||
if (blocRef.current) | ||
return blocRef.current; | ||
blocRef.current = | ||
typeof blocCreator === "function" ? blocCreator() : blocCreator; | ||
return blocRef.current; | ||
} | ||
var _a = useState(getBloc().currentState), state = _a[0], setState = _a[1]; | ||
useEffect(function () { | ||
var actualBloc = typeof bloc === "function" ? bloc() : bloc; | ||
setState(actualBloc.currentState); | ||
var subscription = actualBloc.state$.subscribe(function (nextState) { | ||
var shouldDispose = typeof blocCreator === "function"; | ||
var bloc = getBloc(); | ||
setState(bloc.currentState); | ||
var subscription = bloc.state$.subscribe(function (nextState) { | ||
if (state === nextState) | ||
@@ -12,3 +21,7 @@ return; | ||
}); | ||
return function () { return subscription.unsubscribe(); }; | ||
return function () { | ||
subscription.unsubscribe(); | ||
if (shouldDispose) | ||
bloc.dispose(); | ||
}; | ||
}, []); | ||
@@ -15,0 +28,0 @@ return state; |
import { Bloc } from "@bloc-js/bloc"; | ||
export declare type CreateBlocFn<S> = () => Bloc<any, S>; | ||
export declare function useBlocState<S>(bloc: Bloc<any, S> | CreateBlocFn<S>): S | undefined; | ||
export declare type CreateBlocFn<B> = () => B; | ||
export declare function useBlocState<S, B extends Bloc<any, S>>(blocCreator: B | CreateBlocFn<B>): S; |
@@ -1,8 +0,17 @@ | ||
import { useEffect, useState } from "react"; | ||
export function useBlocState(bloc) { | ||
const [state, setState] = useState(); | ||
import { useEffect, useState, useRef } from "react"; | ||
export function useBlocState(blocCreator) { | ||
const blocRef = useRef(); | ||
function getBloc() { | ||
if (blocRef.current) | ||
return blocRef.current; | ||
blocRef.current = | ||
typeof blocCreator === "function" ? blocCreator() : blocCreator; | ||
return blocRef.current; | ||
} | ||
const [state, setState] = useState(getBloc().currentState); | ||
useEffect(() => { | ||
const actualBloc = typeof bloc === "function" ? bloc() : bloc; | ||
setState(actualBloc.currentState); | ||
const subscription = actualBloc.state$.subscribe(nextState => { | ||
const shouldDispose = typeof blocCreator === "function"; | ||
const bloc = getBloc(); | ||
setState(bloc.currentState); | ||
const subscription = bloc.state$.subscribe(nextState => { | ||
if (state === nextState) | ||
@@ -12,3 +21,7 @@ return; | ||
}); | ||
return () => subscription.unsubscribe(); | ||
return () => { | ||
subscription.unsubscribe(); | ||
if (shouldDispose) | ||
bloc.dispose(); | ||
}; | ||
}, []); | ||
@@ -15,0 +28,0 @@ return state; |
import { Bloc } from "@bloc-js/bloc"; | ||
export declare type CreateBlocFn<S> = () => Bloc<any, S>; | ||
export declare function useBlocState<S>(bloc: Bloc<any, S> | CreateBlocFn<S>): S | undefined; | ||
export declare type CreateBlocFn<B> = () => B; | ||
export declare function useBlocState<S, B extends Bloc<any, S>>(blocCreator: B | CreateBlocFn<B>): S; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var react_1 = require("react"); | ||
function useBlocState(bloc) { | ||
var _a = react_1.useState(), state = _a[0], setState = _a[1]; | ||
function useBlocState(blocCreator) { | ||
var blocRef = react_1.useRef(); | ||
function getBloc() { | ||
if (blocRef.current) | ||
return blocRef.current; | ||
blocRef.current = | ||
typeof blocCreator === "function" ? blocCreator() : blocCreator; | ||
return blocRef.current; | ||
} | ||
var _a = react_1.useState(getBloc().currentState), state = _a[0], setState = _a[1]; | ||
react_1.useEffect(function () { | ||
var actualBloc = typeof bloc === "function" ? bloc() : bloc; | ||
setState(actualBloc.currentState); | ||
var subscription = actualBloc.state$.subscribe(function (nextState) { | ||
var shouldDispose = typeof blocCreator === "function"; | ||
var bloc = getBloc(); | ||
setState(bloc.currentState); | ||
var subscription = bloc.state$.subscribe(function (nextState) { | ||
if (state === nextState) | ||
@@ -14,3 +23,7 @@ return; | ||
}); | ||
return function () { return subscription.unsubscribe(); }; | ||
return function () { | ||
subscription.unsubscribe(); | ||
if (shouldDispose) | ||
bloc.dispose(); | ||
}; | ||
}, []); | ||
@@ -17,0 +30,0 @@ return state; |
{ | ||
"name": "@bloc-js/react-bloc", | ||
"version": "1.2.19", | ||
"version": "1.2.20", | ||
"description": "React components for implementing the BLoC pattern.", | ||
@@ -47,3 +47,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "50b05fa0db3c2bc55ea4c8fe478f22adac7cfc91" | ||
"gitHead": "4817eb4af65f8770d6714a9089affdd2dc65a591" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
13533
170