@bloc-js/react-bloc
Advanced tools
Comparing version 3.3.3 to 3.3.4
import { Bloc } from "@bloc-js/bloc"; | ||
import { BlocGetter, TBlocFactory } from "./registry"; | ||
export declare function useBloc<S>(fn: BlocGetter<S>): Bloc<S>; | ||
import { BlocGetter, TBlocFactory, BlocCompleter } from "./registry"; | ||
export declare function useBloc<S>(fn: BlocGetter<S>, ...args: any[]): Bloc<S>; | ||
export declare function useBlocScoped<S>(fn: BlocGetter<S>, done: BlocCompleter, ...args: any[]): Bloc<S>; | ||
export declare function useBlocState<S>(bloc: Bloc<S>): S; | ||
export declare function createHooks<S>(id: string, factory: TBlocFactory<S>): { | ||
getBloc: BlocGetter<S>; | ||
useBloc: () => Bloc<S>; | ||
useBloc: (...args: any[]) => Bloc<S>; | ||
useBlocScoped: (...args: any[]) => Bloc<S>; | ||
useState: () => S; | ||
}; |
@@ -0,19 +1,38 @@ | ||
var __spreadArrays = (this && this.__spreadArrays) || function () { | ||
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; | ||
for (var r = Array(s), k = 0, i = 0; i < il; i++) | ||
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) | ||
r[k] = a[j]; | ||
return r; | ||
}; | ||
import * as React from "react"; | ||
import { useEffect, useState } from "react"; | ||
import { BlocRootContext } from "./bloc-root"; | ||
import { blocGetter } from "./registry"; | ||
import { blocGetter, } from "./registry"; | ||
export function useBloc(fn) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
var registry = React.useContext(BlocRootContext); | ||
var bloc = React.useMemo(function () { return fn(registry); }, [registry]); | ||
var bloc = React.useMemo(function () { return fn.apply(void 0, __spreadArrays([registry], args)); }, __spreadArrays([registry], args)); | ||
return bloc; | ||
} | ||
export function useBlocScoped(fn, done) { | ||
var args = []; | ||
for (var _i = 2; _i < arguments.length; _i++) { | ||
args[_i - 2] = arguments[_i]; | ||
} | ||
var registry = React.useContext(BlocRootContext); | ||
var bloc = React.useMemo(function () { return fn.apply(void 0, __spreadArrays([registry], args)); }, __spreadArrays([registry], args)); | ||
React.useEffect(function () { | ||
return function () { return done(registry); }; | ||
}, [registry]); | ||
return bloc; | ||
} | ||
export function useBlocState(bloc) { | ||
var _a = useState(bloc.value), state = _a[0], setState = _a[1]; | ||
useEffect(function () { | ||
var subscription = bloc.subscribe(function (nextState) { | ||
setState(nextState); | ||
}); | ||
return function () { | ||
subscription.unsubscribe(); | ||
}; | ||
var subscription = bloc.subscribe(setState); | ||
return function () { return subscription.unsubscribe(); }; | ||
}, [bloc]); | ||
@@ -23,7 +42,25 @@ return state; | ||
export function createHooks(id, factory) { | ||
var getBloc = blocGetter(id, factory); | ||
var useBlocHook = function () { return useBloc(getBloc); }; | ||
var _a = blocGetter(id, factory), getBloc = _a[0], done = _a[1]; | ||
var useBlocHook = function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
return useBloc.apply(void 0, __spreadArrays([getBloc], args)); | ||
}; | ||
var useBlocScopedHook = function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
return useBlocScoped.apply(void 0, __spreadArrays([getBloc, done], args)); | ||
}; | ||
var useState = function () { return useBlocState(useBlocHook()); }; | ||
return { getBloc: getBloc, useBloc: useBlocHook, useState: useState }; | ||
return { | ||
getBloc: getBloc, | ||
useBloc: useBlocHook, | ||
useBlocScoped: useBlocScopedHook, | ||
useState: useState, | ||
}; | ||
} | ||
//# sourceMappingURL=hooks.js.map |
@@ -9,6 +9,8 @@ import { Bloc } from "@bloc-js/bloc"; | ||
getState(): {}; | ||
register<S>(id: string, factory: TBlocFactory<S>): Bloc<S>; | ||
register<S>(id: string, factory: TBlocFactory<S>, args: any[]): Bloc<S>; | ||
complete(id: string): void; | ||
} | ||
export declare type TBlocFactory<S> = (r: BlocRegistry, is?: S) => Bloc<S>; | ||
export declare type BlocGetter<S> = (registry: BlocRegistry) => Bloc<S>; | ||
export declare function blocGetter<S>(id: string, factory: TBlocFactory<S>): BlocGetter<S>; | ||
export declare type TBlocFactory<S> = (r: BlocRegistry, is?: S, ...args: any[]) => Bloc<S>; | ||
export declare type BlocGetter<S> = (registry: BlocRegistry, ...args: any[]) => Bloc<S>; | ||
export declare type BlocCompleter = (registry: BlocRegistry) => void; | ||
export declare function blocGetter<S>(id: string, factory: TBlocFactory<S>): [BlocGetter<S>, BlocCompleter]; |
@@ -12,2 +12,9 @@ var __assign = (this && this.__assign) || function () { | ||
}; | ||
var __spreadArrays = (this && this.__spreadArrays) || function () { | ||
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; | ||
for (var r = Array(s), k = 0, i = 0; i < il; i++) | ||
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) | ||
r[k] = a[j]; | ||
return r; | ||
}; | ||
var BlocRegistry = /** @class */ (function () { | ||
@@ -26,3 +33,3 @@ function BlocRegistry(initialState) { | ||
}; | ||
BlocRegistry.prototype.register = function (id, factory) { | ||
BlocRegistry.prototype.register = function (id, factory, args) { | ||
if (this.blocs[id]) { | ||
@@ -32,6 +39,12 @@ return this.blocs[id]; | ||
var state = this.initialState[id]; | ||
var bloc = factory(this, state); | ||
var bloc = factory.apply(void 0, __spreadArrays([this, state], args)); | ||
this.blocs[id] = bloc; | ||
return bloc; | ||
}; | ||
BlocRegistry.prototype.complete = function (id) { | ||
if (!this.blocs[id]) | ||
return; | ||
this.blocs[id].complete(); | ||
delete this.blocs[id]; | ||
}; | ||
return BlocRegistry; | ||
@@ -41,4 +54,13 @@ }()); | ||
export function blocGetter(id, factory) { | ||
return function (registry) { return registry.register(id, factory); }; | ||
return [ | ||
function (registry) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
return registry.register(id, factory, args); | ||
}, | ||
function (registry) { return registry.complete(id); }, | ||
]; | ||
} | ||
//# sourceMappingURL=registry.js.map |
import { Bloc } from "@bloc-js/bloc"; | ||
import { BlocGetter, TBlocFactory } from "./registry"; | ||
export declare function useBloc<S>(fn: BlocGetter<S>): Bloc<S>; | ||
import { BlocGetter, TBlocFactory, BlocCompleter } from "./registry"; | ||
export declare function useBloc<S>(fn: BlocGetter<S>, ...args: any[]): Bloc<S>; | ||
export declare function useBlocScoped<S>(fn: BlocGetter<S>, done: BlocCompleter, ...args: any[]): Bloc<S>; | ||
export declare function useBlocState<S>(bloc: Bloc<S>): S; | ||
export declare function createHooks<S>(id: string, factory: TBlocFactory<S>): { | ||
getBloc: BlocGetter<S>; | ||
useBloc: () => Bloc<S>; | ||
useBloc: (...args: any[]) => Bloc<S>; | ||
useBlocScoped: (...args: any[]) => Bloc<S>; | ||
useState: () => S; | ||
}; |
import * as React from "react"; | ||
import { useEffect, useState } from "react"; | ||
import { BlocRootContext } from "./bloc-root"; | ||
import { blocGetter } from "./registry"; | ||
export function useBloc(fn) { | ||
import { blocGetter, } from "./registry"; | ||
export function useBloc(fn, ...args) { | ||
const registry = React.useContext(BlocRootContext); | ||
const bloc = React.useMemo(() => fn(registry), [registry]); | ||
const bloc = React.useMemo(() => fn(registry, ...args), [registry, ...args]); | ||
return bloc; | ||
} | ||
export function useBlocScoped(fn, done, ...args) { | ||
const registry = React.useContext(BlocRootContext); | ||
const bloc = React.useMemo(() => fn(registry, ...args), [registry, ...args]); | ||
React.useEffect(() => { | ||
return () => done(registry); | ||
}, [registry]); | ||
return bloc; | ||
} | ||
export function useBlocState(bloc) { | ||
const [state, setState] = useState(bloc.value); | ||
useEffect(() => { | ||
const subscription = bloc.subscribe(nextState => { | ||
setState(nextState); | ||
}); | ||
return () => { | ||
subscription.unsubscribe(); | ||
}; | ||
const subscription = bloc.subscribe(setState); | ||
return () => subscription.unsubscribe(); | ||
}, [bloc]); | ||
@@ -23,7 +27,13 @@ return state; | ||
export function createHooks(id, factory) { | ||
const getBloc = blocGetter(id, factory); | ||
const useBlocHook = () => useBloc(getBloc); | ||
const [getBloc, done] = blocGetter(id, factory); | ||
const useBlocHook = (...args) => useBloc(getBloc, ...args); | ||
const useBlocScopedHook = (...args) => useBlocScoped(getBloc, done, ...args); | ||
const useState = () => useBlocState(useBlocHook()); | ||
return { getBloc, useBloc: useBlocHook, useState }; | ||
return { | ||
getBloc, | ||
useBloc: useBlocHook, | ||
useBlocScoped: useBlocScopedHook, | ||
useState, | ||
}; | ||
} | ||
//# sourceMappingURL=hooks.js.map |
@@ -9,6 +9,8 @@ import { Bloc } from "@bloc-js/bloc"; | ||
getState(): {}; | ||
register<S>(id: string, factory: TBlocFactory<S>): Bloc<S>; | ||
register<S>(id: string, factory: TBlocFactory<S>, args: any[]): Bloc<S>; | ||
complete(id: string): void; | ||
} | ||
export declare type TBlocFactory<S> = (r: BlocRegistry, is?: S) => Bloc<S>; | ||
export declare type BlocGetter<S> = (registry: BlocRegistry) => Bloc<S>; | ||
export declare function blocGetter<S>(id: string, factory: TBlocFactory<S>): BlocGetter<S>; | ||
export declare type TBlocFactory<S> = (r: BlocRegistry, is?: S, ...args: any[]) => Bloc<S>; | ||
export declare type BlocGetter<S> = (registry: BlocRegistry, ...args: any[]) => Bloc<S>; | ||
export declare type BlocCompleter = (registry: BlocRegistry) => void; | ||
export declare function blocGetter<S>(id: string, factory: TBlocFactory<S>): [BlocGetter<S>, BlocCompleter]; |
@@ -12,3 +12,3 @@ export class BlocRegistry { | ||
} | ||
register(id, factory) { | ||
register(id, factory, args) { | ||
if (this.blocs[id]) { | ||
@@ -18,10 +18,19 @@ return this.blocs[id]; | ||
const state = this.initialState[id]; | ||
const bloc = factory(this, state); | ||
const bloc = factory(this, state, ...args); | ||
this.blocs[id] = bloc; | ||
return bloc; | ||
} | ||
complete(id) { | ||
if (!this.blocs[id]) | ||
return; | ||
this.blocs[id].complete(); | ||
delete this.blocs[id]; | ||
} | ||
} | ||
export function blocGetter(id, factory) { | ||
return registry => registry.register(id, factory); | ||
return [ | ||
(registry, ...args) => registry.register(id, factory, args), | ||
registry => registry.complete(id), | ||
]; | ||
} | ||
//# sourceMappingURL=registry.js.map |
import { Bloc } from "@bloc-js/bloc"; | ||
import { BlocGetter, TBlocFactory } from "./registry"; | ||
export declare function useBloc<S>(fn: BlocGetter<S>): Bloc<S>; | ||
import { BlocGetter, TBlocFactory, BlocCompleter } from "./registry"; | ||
export declare function useBloc<S>(fn: BlocGetter<S>, ...args: any[]): Bloc<S>; | ||
export declare function useBlocScoped<S>(fn: BlocGetter<S>, done: BlocCompleter, ...args: any[]): Bloc<S>; | ||
export declare function useBlocState<S>(bloc: Bloc<S>): S; | ||
export declare function createHooks<S>(id: string, factory: TBlocFactory<S>): { | ||
getBloc: BlocGetter<S>; | ||
useBloc: () => Bloc<S>; | ||
useBloc: (...args: any[]) => Bloc<S>; | ||
useBlocScoped: (...args: any[]) => Bloc<S>; | ||
useState: () => S; | ||
}; |
@@ -21,4 +21,11 @@ "use strict"; | ||
}; | ||
var __spreadArrays = (this && this.__spreadArrays) || function () { | ||
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; | ||
for (var r = Array(s), k = 0, i = 0; i < il; i++) | ||
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) | ||
r[k] = a[j]; | ||
return r; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createHooks = exports.useBlocState = exports.useBloc = void 0; | ||
exports.createHooks = exports.useBlocState = exports.useBlocScoped = exports.useBloc = void 0; | ||
var React = __importStar(require("react")); | ||
@@ -29,16 +36,29 @@ var react_1 = require("react"); | ||
function useBloc(fn) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
var registry = React.useContext(bloc_root_1.BlocRootContext); | ||
var bloc = React.useMemo(function () { return fn(registry); }, [registry]); | ||
var bloc = React.useMemo(function () { return fn.apply(void 0, __spreadArrays([registry], args)); }, __spreadArrays([registry], args)); | ||
return bloc; | ||
} | ||
exports.useBloc = useBloc; | ||
function useBlocScoped(fn, done) { | ||
var args = []; | ||
for (var _i = 2; _i < arguments.length; _i++) { | ||
args[_i - 2] = arguments[_i]; | ||
} | ||
var registry = React.useContext(bloc_root_1.BlocRootContext); | ||
var bloc = React.useMemo(function () { return fn.apply(void 0, __spreadArrays([registry], args)); }, __spreadArrays([registry], args)); | ||
React.useEffect(function () { | ||
return function () { return done(registry); }; | ||
}, [registry]); | ||
return bloc; | ||
} | ||
exports.useBlocScoped = useBlocScoped; | ||
function useBlocState(bloc) { | ||
var _a = react_1.useState(bloc.value), state = _a[0], setState = _a[1]; | ||
react_1.useEffect(function () { | ||
var subscription = bloc.subscribe(function (nextState) { | ||
setState(nextState); | ||
}); | ||
return function () { | ||
subscription.unsubscribe(); | ||
}; | ||
var subscription = bloc.subscribe(setState); | ||
return function () { return subscription.unsubscribe(); }; | ||
}, [bloc]); | ||
@@ -49,8 +69,26 @@ return state; | ||
function createHooks(id, factory) { | ||
var getBloc = registry_1.blocGetter(id, factory); | ||
var useBlocHook = function () { return useBloc(getBloc); }; | ||
var _a = registry_1.blocGetter(id, factory), getBloc = _a[0], done = _a[1]; | ||
var useBlocHook = function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
return useBloc.apply(void 0, __spreadArrays([getBloc], args)); | ||
}; | ||
var useBlocScopedHook = function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
return useBlocScoped.apply(void 0, __spreadArrays([getBloc, done], args)); | ||
}; | ||
var useState = function () { return useBlocState(useBlocHook()); }; | ||
return { getBloc: getBloc, useBloc: useBlocHook, useState: useState }; | ||
return { | ||
getBloc: getBloc, | ||
useBloc: useBlocHook, | ||
useBlocScoped: useBlocScopedHook, | ||
useState: useState, | ||
}; | ||
} | ||
exports.createHooks = createHooks; | ||
//# sourceMappingURL=hooks.js.map |
@@ -9,6 +9,8 @@ import { Bloc } from "@bloc-js/bloc"; | ||
getState(): {}; | ||
register<S>(id: string, factory: TBlocFactory<S>): Bloc<S>; | ||
register<S>(id: string, factory: TBlocFactory<S>, args: any[]): Bloc<S>; | ||
complete(id: string): void; | ||
} | ||
export declare type TBlocFactory<S> = (r: BlocRegistry, is?: S) => Bloc<S>; | ||
export declare type BlocGetter<S> = (registry: BlocRegistry) => Bloc<S>; | ||
export declare function blocGetter<S>(id: string, factory: TBlocFactory<S>): BlocGetter<S>; | ||
export declare type TBlocFactory<S> = (r: BlocRegistry, is?: S, ...args: any[]) => Bloc<S>; | ||
export declare type BlocGetter<S> = (registry: BlocRegistry, ...args: any[]) => Bloc<S>; | ||
export declare type BlocCompleter = (registry: BlocRegistry) => void; | ||
export declare function blocGetter<S>(id: string, factory: TBlocFactory<S>): [BlocGetter<S>, BlocCompleter]; |
@@ -13,2 +13,9 @@ "use strict"; | ||
}; | ||
var __spreadArrays = (this && this.__spreadArrays) || function () { | ||
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; | ||
for (var r = Array(s), k = 0, i = 0; i < il; i++) | ||
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) | ||
r[k] = a[j]; | ||
return r; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -29,3 +36,3 @@ exports.blocGetter = exports.BlocRegistry = void 0; | ||
}; | ||
BlocRegistry.prototype.register = function (id, factory) { | ||
BlocRegistry.prototype.register = function (id, factory, args) { | ||
if (this.blocs[id]) { | ||
@@ -35,6 +42,12 @@ return this.blocs[id]; | ||
var state = this.initialState[id]; | ||
var bloc = factory(this, state); | ||
var bloc = factory.apply(void 0, __spreadArrays([this, state], args)); | ||
this.blocs[id] = bloc; | ||
return bloc; | ||
}; | ||
BlocRegistry.prototype.complete = function (id) { | ||
if (!this.blocs[id]) | ||
return; | ||
this.blocs[id].complete(); | ||
delete this.blocs[id]; | ||
}; | ||
return BlocRegistry; | ||
@@ -44,5 +57,14 @@ }()); | ||
function blocGetter(id, factory) { | ||
return function (registry) { return registry.register(id, factory); }; | ||
return [ | ||
function (registry) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
return registry.register(id, factory, args); | ||
}, | ||
function (registry) { return registry.complete(id); }, | ||
]; | ||
} | ||
exports.blocGetter = blocGetter; | ||
//# sourceMappingURL=registry.js.map |
{ | ||
"name": "@bloc-js/react-bloc", | ||
"version": "3.3.3", | ||
"version": "3.3.4", | ||
"description": "React components for implementing the BLoC pattern.", | ||
@@ -50,3 +50,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "1b48b5547c7e41f51fb910602b11fbbe476ccb95" | ||
"gitHead": "4c0b89ccce51c3c24a1b692ab919f5ccc21c180d" | ||
} |
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
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
51796
813