bloc-react
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -28,2 +28,3 @@ import { Subscription } from 'rxjs'; | ||
readonly blocListGlobal: BlocBase<any>[]; | ||
protected _blocMapLocal: Record<string, BlocBase<any>>; | ||
private blocObservers; | ||
@@ -33,2 +34,4 @@ constructor(blocs: BlocBase<any>[], options?: ReactBlocOptions$1); | ||
addBlocObserver<T extends BlocBase<any>>(blocClass: BlocClass<T>, callback: (bloc: T, state: ValueType<T>) => unknown, scope?: BlocObserverScope): void; | ||
addLocalBloc(key: string, bloc: BlocBase<any>): void; | ||
removeLocalBloc(key: string): void; | ||
} | ||
@@ -91,3 +94,2 @@ | ||
private _contextLocalProviderKey; | ||
private _blocMapLocal; | ||
constructor(blocs: BlocBase<any>[], options?: ReactBlocOptions); | ||
@@ -94,0 +96,0 @@ useBloc: <T extends BlocBase<any>>(blocClass: BlocClass<T>, options?: BlocHookOptions<T>) => BlocHookData<T>; |
@@ -133,2 +133,3 @@ 'use strict'; | ||
this.observer = null; | ||
this._blocMapLocal = {}; | ||
this.blocObservers = []; | ||
@@ -158,2 +159,13 @@ this.blocListGlobal = blocs; | ||
} | ||
addLocalBloc(key, bloc) { | ||
this._blocMapLocal[key] = bloc; | ||
bloc.subscribe((v) => this.notify(bloc, v)); | ||
} | ||
removeLocalBloc(key) { | ||
const bloc = this._blocMapLocal[key]; | ||
if (bloc) { | ||
bloc.complete(); | ||
delete this._blocMapLocal[key]; | ||
} | ||
} | ||
} | ||
@@ -173,3 +185,2 @@ | ||
this._contextLocalProviderKey = React__default['default'].createContext(""); | ||
this._blocMapLocal = {}; | ||
this.useBloc = (blocClass, options = {}) => { | ||
@@ -201,9 +212,2 @@ const mergedOptions = { | ||
) | ||
and mame sure you add the global state provider to your app: | ||
const { GlobalBlocProvider } = state; | ||
... | ||
<GlobalBlocProvider> | ||
<App /> | ||
</GlobalBlocProvider> | ||
`); | ||
@@ -259,6 +263,3 @@ console.error(error2.error); | ||
newBloc._localProviderRef = providerKey; | ||
this._blocMapLocal[providerKey] = newBloc; | ||
if (this.debug) { | ||
newBloc.subscribe((v) => this.notify(newBloc, v)); | ||
} | ||
this.addLocalBloc(providerKey, newBloc); | ||
return newBloc; | ||
@@ -271,4 +272,3 @@ }, []); | ||
return () => { | ||
bloc.complete(); | ||
delete this._blocMapLocal[providerKey]; | ||
this.removeLocalBloc(providerKey); | ||
}; | ||
@@ -275,0 +275,0 @@ }, []); |
{ | ||
"name": "bloc-react", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"scripts": { | ||
@@ -16,2 +16,3 @@ "dev": "vite", | ||
"dependencies": { | ||
"enzyme": "^3.11.0", | ||
"nanoid": "^3.1.22", | ||
@@ -26,3 +27,3 @@ "rxjs": "^6.6.7" | ||
"@material-ui/icons": "^4.11.2", | ||
"@types/jest": "^26.0.22", | ||
"@types/jest": "^26.0.23", | ||
"@types/material-ui": "^0.21.8", | ||
@@ -29,0 +30,0 @@ "@types/react": "^17.0.3", |
@@ -7,3 +7,3 @@ import Bloc from "./Bloc"; | ||
onChange: jest.fn(), | ||
onTransition: jest.fn() | ||
onTransition: jest.fn(), | ||
}; | ||
@@ -64,3 +64,6 @@ | ||
expect(spy.onChange).toHaveBeenCalledTimes(1); | ||
expect(spy.onChange).toHaveBeenCalledWith({ currentState: false, nextState: true }); | ||
expect(spy.onChange).toHaveBeenCalledWith({ | ||
currentState: false, | ||
nextState: true, | ||
}); | ||
}); | ||
@@ -76,3 +79,3 @@ | ||
event: AuthEvent.authenticated, | ||
nextState: true | ||
nextState: true, | ||
}); | ||
@@ -79,0 +82,0 @@ }); |
@@ -21,3 +21,5 @@ import BlocBase from "./BlocBase"; | ||
} else { | ||
console.error(`"mapEventToState" not implemented for "${this.constructor.name}"`); | ||
console.error( | ||
`"mapEventToState" not implemented for "${this.constructor.name}"` | ||
); | ||
} | ||
@@ -30,5 +32,5 @@ }; | ||
event, | ||
nextState: value | ||
nextState: value, | ||
}); | ||
}; | ||
} |
import BlocBase from "./BlocBase"; | ||
import { BlocConsumer } from "./BlocConsumer"; | ||
@@ -13,2 +14,11 @@ describe("TestBloc", () => { | ||
}); | ||
it("should allow setting the consumer", () => { | ||
const bloc = new TestBloc(); | ||
try { | ||
bloc.consumer = new BlocConsumer([]); | ||
} catch (e) { | ||
fail(e); | ||
} | ||
}); | ||
}); |
@@ -23,5 +23,5 @@ import { BlocConsumer } from "./BlocConsumer"; | ||
currentState: this.state, | ||
nextState: state | ||
nextState: state, | ||
}); | ||
}; | ||
} |
@@ -9,3 +9,3 @@ import BlocBase from "./BlocBase"; | ||
type BlocObserverScope = "local" | "global" | "all"; | ||
export type BlocObserverScope = "local" | "global" | "all"; | ||
type BlocObserver = [ | ||
@@ -21,5 +21,5 @@ BlocClass<any>, | ||
readonly blocListGlobal: BlocBase<any>[]; | ||
protected _blocMapLocal: Record<string, BlocBase<any>> = {}; | ||
private blocObservers: BlocObserver[] = []; | ||
// private _blocMapLocal: Record<string, BlocBase<any>> = {}; | ||
// private _contextMapLocal: Record<string, React.Context<Cubit<any>>> = {} | ||
@@ -63,2 +63,15 @@ | ||
} | ||
public addLocalBloc(key: string, bloc: BlocBase<any>) { | ||
this._blocMapLocal[key] = bloc; | ||
bloc.subscribe((v: any) => this.notify(bloc, v)); | ||
} | ||
public removeLocalBloc(key: string) { | ||
const bloc = this._blocMapLocal[key]; | ||
if (bloc) { | ||
bloc.complete(); | ||
delete this._blocMapLocal[key]; | ||
} | ||
} | ||
} |
@@ -9,2 +9,1 @@ import { BlocOptions } from "./types"; | ||
}; | ||
@@ -5,3 +5,3 @@ import Cubit from "./Cubit"; | ||
const spy = { | ||
onChange: jest.fn() | ||
onChange: jest.fn(), | ||
}; | ||
@@ -40,5 +40,8 @@ | ||
cubit.increment(); | ||
expect(spy.onChange).toHaveBeenCalledWith({ currentState: 0, nextState: 1 }); | ||
expect(spy.onChange).toHaveBeenCalledWith({ | ||
currentState: 0, | ||
nextState: 1, | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -5,10 +5,10 @@ import * as index from "./index"; | ||
it("should export BlocReact", () => { | ||
expect(index).toHaveProperty('BlocReact'); | ||
expect(index).toHaveProperty("BlocReact"); | ||
}); | ||
it("should export Cubit", () => { | ||
expect(index).toHaveProperty('Cubit'); | ||
expect(index).toHaveProperty("Cubit"); | ||
}); | ||
it("should export Bloc", () => { | ||
expect(index).toHaveProperty('Bloc'); | ||
expect(index).toHaveProperty("Bloc"); | ||
}); | ||
}); |
@@ -38,3 +38,6 @@ import * as rxjs from "rxjs"; | ||
beforeEach(() => { | ||
localStorage.setItem(LOCAL_STORAGE_PREFIX + persistKey, `{"state": ${cachedValue}}`); | ||
localStorage.setItem( | ||
LOCAL_STORAGE_PREFIX + persistKey, | ||
`{"state": ${cachedValue}}` | ||
); | ||
}); | ||
@@ -59,3 +62,6 @@ | ||
it("should not get value from localStorage if `persistData` is false", () => { | ||
const stream = new StreamAbstraction(initialValue, { persistKey, persistData: false }); | ||
const stream = new StreamAbstraction(initialValue, { | ||
persistKey, | ||
persistData: false, | ||
}); | ||
expect(stream.state).toBe(initialValue); | ||
@@ -66,3 +72,6 @@ }); | ||
mockConsole(); | ||
localStorage.setItem(LOCAL_STORAGE_PREFIX + persistKey, `invalid json here: state": ${cachedValue}`); | ||
localStorage.setItem( | ||
LOCAL_STORAGE_PREFIX + persistKey, | ||
`invalid json here: state": ${cachedValue}` | ||
); | ||
const stream = new StreamAbstraction(initialValue, { persistKey }); | ||
@@ -78,3 +87,3 @@ expect(stream.state).toBe(initialValue); | ||
error: jest.fn(), | ||
complete: jest.fn() | ||
complete: jest.fn(), | ||
}; | ||
@@ -104,6 +113,6 @@ | ||
describe("Persist Data Methods", function() { | ||
describe("Persist Data Methods", function () { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}) | ||
}); | ||
@@ -113,7 +122,11 @@ it("should clear the data from localstorage when `clearCache` is called", () => { | ||
stream.clearCache(); | ||
expect(localStorage.removeItem).toHaveBeenCalledWith(LOCAL_STORAGE_PREFIX + persistKey); | ||
expect(localStorage.removeItem).toHaveBeenCalledWith( | ||
LOCAL_STORAGE_PREFIX + persistKey | ||
); | ||
}); | ||
it("should not do anything if there is no `persistKey` when `clearCache` is called", () => { | ||
const stream = new StreamAbstractionExposed(initialValue, { persistKey: '' }); | ||
const stream = new StreamAbstractionExposed(initialValue, { | ||
persistKey: "", | ||
}); | ||
stream.clearCache(); | ||
@@ -131,3 +144,5 @@ expect(localStorage.removeItem).toHaveBeenCalledTimes(0); | ||
it("should not update cache when the state is updated if `persistKey` key is undefined", () => { | ||
const stream = new StreamAbstractionExposed(initialValue, { persistKey: '' }); | ||
const stream = new StreamAbstractionExposed(initialValue, { | ||
persistKey: "", | ||
}); | ||
expect(localStorage.setItem).toHaveBeenCalledTimes(0); | ||
@@ -138,2 +153,2 @@ stream.next_exposed(55); | ||
}); | ||
}); | ||
}); |
@@ -46,3 +46,3 @@ import { BehaviorSubject, Subscription } from "rxjs"; | ||
this.updateCache(); | ||
} | ||
}; | ||
@@ -65,3 +65,5 @@ protected parseFromCache = (state: string): T => { | ||
} catch (e) { | ||
const error = new Error(`Failed to parse JSON in localstorage for the key: "${LOCAL_STORAGE_PREFIX}${this._options.persistKey}"`); | ||
const error = new Error( | ||
`Failed to parse JSON in localstorage for the key: "${LOCAL_STORAGE_PREFIX}${this._options.persistKey}"` | ||
); | ||
console.error(error); | ||
@@ -68,0 +70,0 @@ return error; |
@@ -22,2 +22,2 @@ import BlocBase from "./BlocBase"; | ||
persistData?: boolean; | ||
} | ||
} |
@@ -13,4 +13,4 @@ import CounterCubit from "./bloc/CounterCubit"; | ||
export const { useBloc, GlobalBlocProvider, BlocBuilder, BlocProvider } = state; | ||
export const { useBloc, BlocBuilder, BlocProvider } = state; | ||
export default state; |
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
451328
1927
3
52
+ Addedenzyme@^3.11.0
+ Addedarray-buffer-byte-length@1.0.2(transitive)
+ Addedarray.prototype.filter@1.0.4(transitive)
+ Addedarray.prototype.flat@1.3.3(transitive)
+ Addedarraybuffer.prototype.slice@1.0.4(transitive)
+ Addedasync-function@1.0.0(transitive)
+ Addedavailable-typed-arrays@1.0.7(transitive)
+ Addedboolbase@1.0.0(transitive)
+ Addedcall-bind@1.0.8(transitive)
+ Addedcall-bind-apply-helpers@1.0.1(transitive)
+ Addedcall-bound@1.0.3(transitive)
+ Addedcheerio@1.0.0(transitive)
+ Addedcheerio-select@2.1.0(transitive)
+ Addedcommander@2.20.3(transitive)
+ Addedcss-select@5.1.0(transitive)
+ Addedcss-what@6.1.0(transitive)
+ Addeddata-view-buffer@1.0.2(transitive)
+ Addeddata-view-byte-length@1.0.2(transitive)
+ Addeddata-view-byte-offset@1.0.1(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addeddefine-properties@1.2.1(transitive)
+ Addeddiscontinuous-range@1.0.0(transitive)
+ Addeddom-serializer@2.0.0(transitive)
+ Addeddomelementtype@2.3.0(transitive)
+ Addeddomhandler@5.0.3(transitive)
+ Addeddomutils@3.2.2(transitive)
+ Addeddunder-proto@1.0.1(transitive)
+ Addedencoding-sniffer@0.2.0(transitive)
+ Addedentities@4.5.0(transitive)
+ Addedenzyme@3.11.0(transitive)
+ Addedenzyme-shallow-equal@1.0.7(transitive)
+ Addedes-abstract@1.23.9(transitive)
+ Addedes-array-method-boxes-properly@1.0.0(transitive)
+ Addedes-define-property@1.0.1(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedes-object-atoms@1.1.1(transitive)
+ Addedes-set-tostringtag@2.1.0(transitive)
+ Addedes-shim-unscopables@1.0.2(transitive)
+ Addedes-to-primitive@1.3.0(transitive)
+ Addedfor-each@0.3.4(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedfunction.prototype.name@1.1.8(transitive)
+ Addedfunctions-have-names@1.2.3(transitive)
+ Addedget-intrinsic@1.2.7(transitive)
+ Addedget-proto@1.0.1(transitive)
+ Addedget-symbol-description@1.1.0(transitive)
+ Addedglobalthis@1.0.4(transitive)
+ Addedgopd@1.2.0(transitive)
+ Addedhas@1.0.4(transitive)
+ Addedhas-bigints@1.1.0(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-proto@1.2.0(transitive)
+ Addedhas-symbols@1.1.0(transitive)
+ Addedhas-tostringtag@1.0.2(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addedhtml-element-map@1.3.1(transitive)
+ Addedhtmlparser2@9.1.0(transitive)
+ Addediconv-lite@0.6.3(transitive)
+ Addedinternal-slot@1.1.0(transitive)
+ Addedis-array-buffer@3.0.5(transitive)
+ Addedis-async-function@2.1.1(transitive)
+ Addedis-bigint@1.1.0(transitive)
+ Addedis-boolean-object@1.2.1(transitive)
+ Addedis-callable@1.2.7(transitive)
+ Addedis-data-view@1.0.2(transitive)
+ Addedis-date-object@1.1.0(transitive)
+ Addedis-finalizationregistry@1.1.1(transitive)
+ Addedis-generator-function@1.1.0(transitive)
+ Addedis-map@2.0.3(transitive)
+ Addedis-number-object@1.1.1(transitive)
+ Addedis-regex@1.2.1(transitive)
+ Addedis-set@2.0.3(transitive)
+ Addedis-shared-array-buffer@1.0.4(transitive)
+ Addedis-string@1.1.1(transitive)
+ Addedis-subset@0.1.1(transitive)
+ Addedis-symbol@1.1.1(transitive)
+ Addedis-typed-array@1.1.15(transitive)
+ Addedis-weakmap@2.0.2(transitive)
+ Addedis-weakref@1.1.0(transitive)
+ Addedis-weakset@2.0.4(transitive)
+ Addedisarray@2.0.5(transitive)
+ Addedlodash.escape@4.0.1(transitive)
+ Addedlodash.flattendeep@4.4.0(transitive)
+ Addedlodash.isequal@4.5.0(transitive)
+ Addedmath-intrinsics@1.1.0(transitive)
+ Addedmoo@0.5.2(transitive)
+ Addednearley@2.20.1(transitive)
+ Addednth-check@2.1.1(transitive)
+ Addedobject-inspect@1.13.3(transitive)
+ Addedobject-is@1.1.6(transitive)
+ Addedobject-keys@1.1.1(transitive)
+ Addedobject.assign@4.1.7(transitive)
+ Addedobject.entries@1.1.8(transitive)
+ Addedobject.values@1.2.1(transitive)
+ Addedown-keys@1.0.1(transitive)
+ Addedparse5@7.2.1(transitive)
+ Addedparse5-htmlparser2-tree-adapter@7.1.0(transitive)
+ Addedparse5-parser-stream@7.1.2(transitive)
+ Addedperformance-now@2.1.0(transitive)
+ Addedpossible-typed-array-names@1.0.0(transitive)
+ Addedraf@3.4.1(transitive)
+ Addedrailroad-diagrams@1.0.0(transitive)
+ Addedrandexp@0.4.6(transitive)
+ Addedreflect.getprototypeof@1.0.10(transitive)
+ Addedregexp.prototype.flags@1.5.4(transitive)
+ Addedret@0.1.15(transitive)
+ Addedrst-selector-parser@2.2.3(transitive)
+ Addedsafe-array-concat@1.1.3(transitive)
+ Addedsafe-push-apply@1.0.0(transitive)
+ Addedsafe-regex-test@1.1.0(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedset-function-length@1.2.2(transitive)
+ Addedset-function-name@2.0.2(transitive)
+ Addedset-proto@1.0.0(transitive)
+ Addedside-channel@1.1.0(transitive)
+ Addedside-channel-list@1.0.0(transitive)
+ Addedside-channel-map@1.0.1(transitive)
+ Addedside-channel-weakmap@1.0.2(transitive)
+ Addedstring.prototype.trim@1.2.10(transitive)
+ Addedstring.prototype.trimend@1.0.9(transitive)
+ Addedstring.prototype.trimstart@1.0.8(transitive)
+ Addedtyped-array-buffer@1.0.3(transitive)
+ Addedtyped-array-byte-length@1.0.3(transitive)
+ Addedtyped-array-byte-offset@1.0.4(transitive)
+ Addedtyped-array-length@1.0.7(transitive)
+ Addedunbox-primitive@1.1.0(transitive)
+ Addedundici@6.21.1(transitive)
+ Addedwhatwg-encoding@3.1.1(transitive)
+ Addedwhatwg-mimetype@4.0.0(transitive)
+ Addedwhich-boxed-primitive@1.1.1(transitive)
+ Addedwhich-builtin-type@1.2.1(transitive)
+ Addedwhich-collection@1.0.2(transitive)
+ Addedwhich-typed-array@1.1.18(transitive)