bloc-react
Advanced tools
Comparing version 0.1.6 to 0.1.7
@@ -34,3 +34,7 @@ import { Subscription } from 'rxjs'; | ||
readonly addTransition: (bloc: BlocBase<any>, state: any, event: any) => void; | ||
readonly addBlocAdded: (bloc: BlocBase<any>) => void; | ||
readonly addBlocRemoved: (bloc: BlocBase<any>) => void; | ||
private readonly defaultAction; | ||
onBlocAdded: (bloc: BlocBase<any>) => void; | ||
onBlocRemoved: (bloc: BlocBase<any>) => void; | ||
private createTransitionEvent; | ||
@@ -46,2 +50,5 @@ private createChangeEvent; | ||
} | ||
interface ConsumerOptions { | ||
observer?: BlocObserver; | ||
} | ||
declare class BlocConsumer { | ||
@@ -51,3 +58,2 @@ observer: BlocObserver; | ||
providerList: ProviderItem[]; | ||
protected _blocMapLocal: Record<string, BlocBase<any>>; | ||
private blocListGlobal; | ||
@@ -57,3 +63,3 @@ private blocChangeObservers; | ||
private mockBlocs; | ||
constructor(blocs: BlocBase<any>[]); | ||
constructor(blocs: BlocBase<any>[], options?: ConsumerOptions); | ||
notifyChange(bloc: BlocBase<any>, state: any): void; | ||
@@ -73,3 +79,6 @@ notifyValueChange(bloc: BlocBase<any>): void; | ||
declare type RemoveMethods = () => void; | ||
declare class StreamAbstraction<T> { | ||
isClosed: boolean; | ||
removeListeners: Array<RemoveMethods>; | ||
protected readonly _options: BlocOptions; | ||
@@ -79,2 +88,4 @@ private _subject; | ||
get state(): T; | ||
readonly removeRemoveListener: (index: number) => void; | ||
readonly addRemoveListener: (method: RemoveMethods) => () => void; | ||
subscribe: (next?: ((value: any) => void) | undefined) => Subscription; | ||
@@ -90,12 +101,26 @@ complete: () => void; | ||
interface BlocMeta { | ||
scope: 'unknown' | 'local' | 'global'; | ||
} | ||
declare type ChangeMethod = <T>(change: ChangeEvent<T>, bloc: BlocBase<T>) => void; | ||
declare type RegisterMethod = <T>(consumer: BlocConsumer, bloc: BlocBase<T>) => void; | ||
declare type ValueChangeMethod = <T>(value: T, bloc: BlocBase<T>) => void; | ||
declare class BlocBase<T> extends StreamAbstraction<T> { | ||
_localProviderRef: string; | ||
onRegister: null | ((consumer: BlocConsumer) => void); | ||
onChange: null | ((change: ChangeEvent<T>) => void); | ||
onValueChange: null | ((value: T) => void); | ||
id: string; | ||
createdAt: number; | ||
meta: BlocMeta; | ||
changeListeners: ChangeMethod[]; | ||
registerListeners: RegisterMethod[]; | ||
valueChangeListeners: ValueChangeMethod[]; | ||
constructor(initialValue: T, blocOptions?: BlocOptions); | ||
protected _consumer: BlocConsumer | null; | ||
set consumer(consumer: BlocConsumer); | ||
protected notifyChange: (state: T) => void; | ||
protected notifyValueChange: () => void; | ||
readonly removeChangeListener: (index: number) => void; | ||
readonly addChangeListener: (method: ChangeMethod) => () => void; | ||
readonly removeValueChangeListener: (index: number) => void; | ||
readonly addValueChangeListener: (method: ValueChangeMethod) => () => void; | ||
readonly removeRegisterListener: (index: number) => void; | ||
readonly addRegisterListener: (method: RegisterMethod) => () => void; | ||
readonly notifyChange: (state: T) => void; | ||
readonly notifyValueChange: () => void; | ||
} | ||
@@ -124,5 +149,6 @@ | ||
declare class BlocReact extends BlocConsumer { | ||
private providerCount; | ||
private readonly _blocsGlobal; | ||
private _contextLocalProviderKey; | ||
constructor(blocs: BlocBase<any>[]); | ||
constructor(blocs: BlocBase<any>[], options?: ConsumerOptions); | ||
useBloc: <T extends BlocBase<any>>(blocClass: BlocClass<T>, options?: BlocHookOptions<T>) => BlocHookData<T>; | ||
@@ -129,0 +155,0 @@ BlocBuilder<T extends BlocBase<any>>(props: { |
@@ -6,2 +6,3 @@ 'use strict'; | ||
var rxjs = require('rxjs'); | ||
var nanoid = require('nanoid'); | ||
var React = require('react'); | ||
@@ -21,4 +22,17 @@ | ||
constructor(initialValue, blocOptions = {}) { | ||
this.isClosed = false; | ||
this.removeListeners = []; | ||
this.removeRemoveListener = (index) => { | ||
this.removeListeners.splice(index, 1); | ||
}; | ||
this.addRemoveListener = (method) => { | ||
const index = this.removeListeners.length; | ||
this.removeListeners.push(method); | ||
return () => this.removeRemoveListener(index); | ||
}; | ||
this.subscribe = (next) => this._subject.subscribe(next); | ||
this.complete = () => this._subject.complete(); | ||
this.complete = () => { | ||
this.isClosed = true; | ||
this._subject.complete(); | ||
}; | ||
this.clearCache = () => { | ||
@@ -80,17 +94,45 @@ const key = this._options.persistKey; | ||
super(initialValue, blocOptions); | ||
this._localProviderRef = ""; | ||
this.onRegister = null; | ||
this.onChange = null; | ||
this.onValueChange = null; | ||
this.id = nanoid.nanoid(); | ||
this.createdAt = Date.now(); | ||
this.meta = { | ||
scope: "unknown" | ||
}; | ||
this.changeListeners = []; | ||
this.registerListeners = []; | ||
this.valueChangeListeners = []; | ||
this._consumer = null; | ||
this.removeChangeListener = (index) => { | ||
this.changeListeners.splice(index, 1); | ||
}; | ||
this.addChangeListener = (method) => { | ||
const index = this.changeListeners.length; | ||
this.changeListeners.push(method); | ||
return () => this.removeChangeListener(index); | ||
}; | ||
this.removeValueChangeListener = (index) => { | ||
this.valueChangeListeners.splice(index, 1); | ||
}; | ||
this.addValueChangeListener = (method) => { | ||
const index = this.valueChangeListeners.length; | ||
this.valueChangeListeners.push(method); | ||
return () => this.removeValueChangeListener(index); | ||
}; | ||
this.removeRegisterListener = (index) => { | ||
this.registerListeners.splice(index, 1); | ||
}; | ||
this.addRegisterListener = (method) => { | ||
const index = this.registerListeners.length; | ||
this.registerListeners.push(method); | ||
return () => this.removeRegisterListener(index); | ||
}; | ||
this.notifyChange = (state) => { | ||
this._consumer?.notifyChange(this, state); | ||
this.onChange?.({ | ||
this.changeListeners.forEach((fn) => fn({ | ||
currentState: this.state, | ||
nextState: state | ||
}); | ||
}, this)); | ||
}; | ||
this.notifyValueChange = () => { | ||
this._consumer?.notifyValueChange(this); | ||
this.onValueChange?.(this.state); | ||
this.valueChangeListeners.forEach((fn) => fn(this.state, this)); | ||
}; | ||
@@ -149,4 +191,12 @@ } | ||
}; | ||
this.addBlocAdded = (bloc) => { | ||
this.onBlocAdded(bloc); | ||
}; | ||
this.addBlocRemoved = (bloc) => { | ||
this.onBlocRemoved(bloc); | ||
}; | ||
this.defaultAction = () => { | ||
}; | ||
this.onBlocAdded = this.defaultAction; | ||
this.onBlocRemoved = this.defaultAction; | ||
this.onChange = methods.onChange ? methods.onChange : this.defaultAction; | ||
@@ -171,6 +221,5 @@ this.onTransition = methods.onTransition ? methods.onTransition : this.defaultAction; | ||
class BlocConsumer { | ||
constructor(blocs) { | ||
constructor(blocs, options = {}) { | ||
this.mocksEnabled = false; | ||
this.providerList = []; | ||
this._blocMapLocal = {}; | ||
this.blocChangeObservers = []; | ||
@@ -180,9 +229,14 @@ this.blocValueChangeObservers = []; | ||
this.blocListGlobal = blocs; | ||
this.observer = new BlocObserver(); | ||
this.observer = options.observer || new BlocObserver(); | ||
for (const b of blocs) { | ||
b.consumer = this; | ||
b.onRegister?.(this); | ||
b.registerListeners.forEach((fn) => fn(this, b)); | ||
b.meta.scope = "global"; | ||
this.observer.addBlocAdded(b); | ||
} | ||
} | ||
notifyChange(bloc, state) { | ||
if (bloc.isClosed) { | ||
return; | ||
} | ||
this.observer.addChange(bloc, state); | ||
@@ -201,2 +255,5 @@ for (const [blocClass, callback, scope] of this.blocChangeObservers) { | ||
notifyValueChange(bloc) { | ||
if (bloc.isClosed) { | ||
return; | ||
} | ||
for (const [blocClass, callback, scope] of this.blocValueChangeObservers) { | ||
@@ -211,2 +268,5 @@ const isGlobal = this.blocListGlobal.indexOf(bloc) !== -1; | ||
notifyTransition(bloc, state, event) { | ||
if (bloc.isClosed) { | ||
return; | ||
} | ||
this.observer.addTransition(bloc, state, event); | ||
@@ -223,3 +283,5 @@ } | ||
item.bloc.consumer = this; | ||
item.bloc.onRegister?.(this); | ||
item.bloc.registerListeners.forEach((fn) => fn(this, item.bloc)); | ||
item.bloc.meta.scope = "local"; | ||
this.observer.addBlocAdded(item.bloc); | ||
} | ||
@@ -230,2 +292,4 @@ removeLocalBloc(id, bloc) { | ||
item.bloc.complete(); | ||
item.bloc.removeListeners.forEach((fn) => fn()); | ||
this.observer.addBlocRemoved(item.bloc); | ||
this.providerList = this.providerList.filter((i) => i !== item); | ||
@@ -291,4 +355,5 @@ } | ||
class BlocReact extends BlocConsumer { | ||
constructor(blocs) { | ||
super(blocs); | ||
constructor(blocs, options) { | ||
super(blocs, options); | ||
this.providerCount = 0; | ||
this._contextLocalProviderKey = React__default['default'].createContext(0); | ||
@@ -359,3 +424,3 @@ this.useBloc = (blocClass, options = {}) => { | ||
BlocProvider(props) { | ||
const id = Date.now(); | ||
const id = this.providerCount++; | ||
const localProviderKey = React.useContext(this._contextLocalProviderKey); | ||
@@ -362,0 +427,0 @@ const bloc = React.useMemo(() => { |
{ | ||
"name": "bloc-react", | ||
"version": "0.1.6", | ||
"version": "0.1.7", | ||
"license": "MIT", | ||
@@ -17,3 +17,5 @@ "main": "dist/bloc-react.js", | ||
"dev": "vite", | ||
"dev:tool": "cd devtools && vite", | ||
"build": "tsc && vite build", | ||
"build:tool": "cd devtools && vite build", | ||
"build:lib": "rollup -c rollup.config.lib.js", | ||
@@ -25,3 +27,4 @@ "serve": "vite preview", | ||
"dependencies": { | ||
"rxjs": "^7.0.1" | ||
"nanoid": "^3.1.23", | ||
"rxjs": "^7.1.0" | ||
}, | ||
@@ -34,12 +37,14 @@ "devDependencies": { | ||
"@material-ui/icons": "^4.11.2", | ||
"@material-ui/lab": "^4.0.0-alpha.58", | ||
"@testing-library/react": "^11.2.7", | ||
"@testing-library/react-hooks": "^6.0.0", | ||
"@testing-library/react-hooks": "^7.0.0", | ||
"@types/chrome": "^0.0.142", | ||
"@types/enzyme": "^3.10.8", | ||
"@types/jest": "^26.0.23", | ||
"@types/material-ui": "^0.21.8", | ||
"@types/react": "^17.0.6", | ||
"@types/react": "^17.0.8", | ||
"@types/react-dom": "^17.0.5", | ||
"@types/react-router-dom": "^5.1.7", | ||
"@typescript-eslint/eslint-plugin": "^4.24.0", | ||
"@typescript-eslint/parser": "^4.24.0", | ||
"@typescript-eslint/eslint-plugin": "^4.25.0", | ||
"@typescript-eslint/parser": "^4.25.0", | ||
"@vitejs/plugin-react-refresh": "^1.3.3", | ||
@@ -49,7 +54,7 @@ "@wojtekmaj/enzyme-adapter-react-17": "^0.6.1", | ||
"enzyme": "^3.11.0", | ||
"esbuild": "^0.12.1", | ||
"eslint": "^7.26.0", | ||
"esbuild": "^0.12.3", | ||
"eslint": "^7.27.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-config-standard": "^16.0.2", | ||
"eslint-plugin-import": "^2.23.2", | ||
"eslint-config-standard": "^16.0.3", | ||
"eslint-plugin-import": "^2.23.3", | ||
"eslint-plugin-node": "^11.1.0", | ||
@@ -59,4 +64,4 @@ "eslint-plugin-promise": "^5.1.0", | ||
"install": "^0.13.0", | ||
"jest": "^26.6.3", | ||
"jest-localstorage-mock": "^2.4.12", | ||
"jest": "^27.0.1", | ||
"jest-localstorage-mock": "^2.4.13", | ||
"jest-mock-console": "^1.1.0", | ||
@@ -67,7 +72,7 @@ "prettier": "^2.3.0", | ||
"react-router-dom": "^5.2.0", | ||
"rollup": "^2.48.0", | ||
"rollup": "^2.50.1", | ||
"rollup-plugin-dts": "^3.0.2", | ||
"rollup-plugin-esbuild": "^4.2.3", | ||
"typescript": "^4.2.4", | ||
"vite": "^2.3.3" | ||
"vite": "^2.3.4" | ||
}, | ||
@@ -74,0 +79,0 @@ "jest": { |
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
354908
22
2252
2
42
+ Addednanoid@^3.1.23
+ Addednanoid@3.3.8(transitive)
Updatedrxjs@^7.1.0