Comparing version 1.0.1-alpha.0 to 1.0.1-alpha.1
@@ -1,5 +0,2 @@ | ||
/** | ||
* OBSERVABLE | ||
*/ | ||
type BlacObserver<S> = (oldState: S, newState: S) => void; | ||
type BlacObserver<S> = (newState: S, oldState: S) => void | Promise<void>; | ||
declare class BlacObservable<S> { | ||
@@ -10,21 +7,18 @@ private _observers; | ||
notify(newState: S, oldState: S): void; | ||
dispose(): void; | ||
} | ||
/** | ||
* BLAC | ||
*/ | ||
declare class Blac { | ||
globalState: any; | ||
} | ||
/** | ||
* BLOC BASE | ||
*/ | ||
type BlocClassNoParams<B> = new (args: never[]) => B; | ||
type BlocClass<B> = new (...args: never[]) => B; | ||
type ValueType<B extends BlocBase<any>> = B extends BlocBase<infer U> ? U : never; | ||
interface BlocOptions { | ||
blac?: Blac; | ||
} | ||
interface CubitOptions extends BlocOptions { | ||
} | ||
declare abstract class BlocBase<S> { | ||
static isBlacClass: boolean; | ||
isBlacLive: boolean; | ||
_state: S; | ||
observable: BlacObservable<S>; | ||
blac?: Blac; | ||
observer: BlacObservable<S>; | ||
blac?: Blac<any, any>; | ||
uid: string; | ||
constructor(initialState: S, options?: BlocOptions); | ||
@@ -34,13 +28,34 @@ get state(): S; | ||
onStateChange: (callback: (newState: S, oldState: S) => void) => (() => void); | ||
getGlobalBloc<B extends BlocBase<any>>(blocClass: BlocClass<B>): B | undefined; | ||
dispose(): void; | ||
} | ||
/** | ||
* BLOC | ||
*/ | ||
declare abstract class Bloc<S, A> extends BlocBase<S> { | ||
abstract reducer(action: A, state: S): S; | ||
emit: (action: A) => void; | ||
type BlacGlobalState = { | ||
[key: string]: BlocBase<any>; | ||
}; | ||
interface BlacOptions<G extends BlacGlobalState> { | ||
global?: G; | ||
} | ||
/** | ||
* CUBIT | ||
*/ | ||
declare class Blac<GS extends BlacGlobalState, O extends BlacOptions<GS>> { | ||
blocMap: Map<BlocClass<BlocBase<any>>, BlocBase<any>>; | ||
pluginMap: Map<string, any>; | ||
global?: GS; | ||
constructor(options?: O); | ||
registerBloc(bloc: GS[string]): void; | ||
getBloc<B extends BlocBase<any>>(blocClass: BlocClass<B>): B | undefined; | ||
isGlobalBloc(bloc: BlocBase<any>): boolean; | ||
addPluginKey(ref: string, value: any): void; | ||
getPluginKey(ref: string): unknown; | ||
} | ||
declare global { | ||
interface Window { | ||
blac?: Blac<any, any>; | ||
} | ||
interface GlobalThis { | ||
blac?: Blac<any, any>; | ||
} | ||
} | ||
interface CubitOptions { | ||
} | ||
declare abstract class Cubit<S> extends BlocBase<S> { | ||
@@ -50,2 +65,7 @@ emit(state: S): void; | ||
export { Blac, BlacObservable, BlacObserver, Bloc, BlocBase, BlocOptions, Cubit, CubitOptions }; | ||
declare abstract class Bloc<S, A> extends BlocBase<S> { | ||
abstract reducer(action: A, state: S): S; | ||
emit: (action: A) => void; | ||
} | ||
export { Blac, BlacGlobalState, BlacObservable, BlacObserver, BlacOptions, Bloc, BlocBase, BlocClass, BlocClassNoParams, BlocOptions, Cubit, CubitOptions, ValueType }; |
@@ -0,8 +1,44 @@ | ||
class Blac { | ||
blocMap = new Map(); | ||
pluginMap = new Map(); | ||
global; | ||
constructor(options = {}) { | ||
// register blac instance on global object | ||
globalThis.blac = this; | ||
if (options.global) { | ||
this.global = options.global; | ||
Object.keys(options.global).forEach((key) => { | ||
const gloBloc = options?.global?.[key]; | ||
if (gloBloc) | ||
this.registerBloc(gloBloc); | ||
}); | ||
} | ||
} | ||
registerBloc(bloc) { | ||
this.blocMap.set(bloc.constructor, bloc); | ||
} | ||
getBloc(blocClass) { | ||
return this.blocMap.get(blocClass); | ||
} | ||
isGlobalBloc(bloc) { | ||
return this.blocMap.has(bloc.constructor); | ||
} | ||
// getGlobal(bloc: BlocClass<any>): BlocBase<any> | undefined { | ||
// return this.blocMap.get(bloc.constructor as BlocClass<BlocBase<any>>); | ||
// } | ||
addPluginKey(ref, value) { | ||
this.pluginMap.set(ref, value); | ||
} | ||
getPluginKey(ref) { | ||
return this.pluginMap.get(ref); | ||
} | ||
} | ||
class BlacObservable { | ||
_observers = []; | ||
_observers = new Set(); | ||
subscribe(observer) { | ||
this._observers.push(observer); | ||
this._observers.add(observer); | ||
} | ||
unsubscribe(observer) { | ||
this._observers = this._observers.filter((obs) => obs !== observer); | ||
this._observers.delete(observer); | ||
} | ||
@@ -12,20 +48,17 @@ notify(newState, oldState) { | ||
} | ||
dispose() { | ||
this._observers.clear(); | ||
} | ||
} | ||
/** | ||
* BLAC | ||
*/ | ||
class Blac { | ||
globalState = {}; | ||
} | ||
class BlocBase { | ||
static isBlacClass = true; | ||
isBlacLive = true; | ||
_state; | ||
observable; | ||
blac; | ||
observer; | ||
blac = globalThis.blac; | ||
uid = Math.random().toString(36).split('.')[1]; | ||
constructor(initialState, options) { | ||
this.observable = new BlacObservable(); | ||
this.observer = new BlacObservable(); | ||
this._state = initialState; | ||
if (options?.blac) { | ||
this.blac = options?.blac; | ||
this.blac.globalState[this.name] = this.state; | ||
} | ||
} | ||
@@ -39,19 +72,14 @@ get state() { | ||
onStateChange = (callback) => { | ||
this.observable.subscribe(callback); | ||
return () => this.observable.unsubscribe(callback); | ||
this.observer.subscribe(callback); | ||
return () => this.observer.unsubscribe(callback); | ||
}; | ||
getGlobalBloc(blocClass) { | ||
return this.blac?.getBloc(blocClass); | ||
} | ||
dispose() { | ||
this.isBlacLive = false; | ||
this.observer.dispose(); | ||
} | ||
} | ||
/** | ||
* BLOC | ||
*/ | ||
class Bloc extends BlocBase { | ||
emit = (action) => { | ||
const newState = this.reducer(action, this.state); | ||
this.observable.notify(newState, this.state); | ||
this._state = newState; | ||
}; | ||
} | ||
/** | ||
* CUBIT | ||
*/ | ||
class Cubit extends BlocBase { | ||
@@ -62,8 +90,19 @@ emit(state) { | ||
} | ||
this.observable.notify(state, this.state); | ||
const oldState = this.state; | ||
const newState = state; | ||
this._state = state; | ||
this.observer.notify(newState, oldState); | ||
} | ||
} | ||
class Bloc extends BlocBase { | ||
emit = (action) => { | ||
const oldState = this.state; | ||
const newState = this.reducer(action, this.state); | ||
this._state = newState; | ||
this.observer.notify(newState, oldState); | ||
}; | ||
} | ||
export { Blac, BlacObservable, Bloc, BlocBase, Cubit }; | ||
//# sourceMappingURL=blac.esm.js.map |
103
dist/blac.js
'use strict'; | ||
class Blac { | ||
blocMap = new Map(); | ||
pluginMap = new Map(); | ||
global; | ||
constructor(options = {}) { | ||
// register blac instance on global object | ||
globalThis.blac = this; | ||
if (options.global) { | ||
this.global = options.global; | ||
Object.keys(options.global).forEach((key) => { | ||
const gloBloc = options?.global?.[key]; | ||
if (gloBloc) | ||
this.registerBloc(gloBloc); | ||
}); | ||
} | ||
} | ||
registerBloc(bloc) { | ||
this.blocMap.set(bloc.constructor, bloc); | ||
} | ||
getBloc(blocClass) { | ||
return this.blocMap.get(blocClass); | ||
} | ||
isGlobalBloc(bloc) { | ||
return this.blocMap.has(bloc.constructor); | ||
} | ||
// getGlobal(bloc: BlocClass<any>): BlocBase<any> | undefined { | ||
// return this.blocMap.get(bloc.constructor as BlocClass<BlocBase<any>>); | ||
// } | ||
addPluginKey(ref, value) { | ||
this.pluginMap.set(ref, value); | ||
} | ||
getPluginKey(ref) { | ||
return this.pluginMap.get(ref); | ||
} | ||
} | ||
class BlacObservable { | ||
_observers = []; | ||
_observers = new Set(); | ||
subscribe(observer) { | ||
this._observers.push(observer); | ||
this._observers.add(observer); | ||
} | ||
unsubscribe(observer) { | ||
this._observers = this._observers.filter((obs) => obs !== observer); | ||
this._observers.delete(observer); | ||
} | ||
@@ -14,20 +50,17 @@ notify(newState, oldState) { | ||
} | ||
dispose() { | ||
this._observers.clear(); | ||
} | ||
} | ||
/** | ||
* BLAC | ||
*/ | ||
class Blac { | ||
globalState = {}; | ||
} | ||
class BlocBase { | ||
static isBlacClass = true; | ||
isBlacLive = true; | ||
_state; | ||
observable; | ||
blac; | ||
observer; | ||
blac = globalThis.blac; | ||
uid = Math.random().toString(36).split('.')[1]; | ||
constructor(initialState, options) { | ||
this.observable = new BlacObservable(); | ||
this.observer = new BlacObservable(); | ||
this._state = initialState; | ||
if (options?.blac) { | ||
this.blac = options?.blac; | ||
this.blac.globalState[this.name] = this.state; | ||
} | ||
} | ||
@@ -41,19 +74,14 @@ get state() { | ||
onStateChange = (callback) => { | ||
this.observable.subscribe(callback); | ||
return () => this.observable.unsubscribe(callback); | ||
this.observer.subscribe(callback); | ||
return () => this.observer.unsubscribe(callback); | ||
}; | ||
getGlobalBloc(blocClass) { | ||
return this.blac?.getBloc(blocClass); | ||
} | ||
dispose() { | ||
this.isBlacLive = false; | ||
this.observer.dispose(); | ||
} | ||
} | ||
/** | ||
* BLOC | ||
*/ | ||
class Bloc extends BlocBase { | ||
emit = (action) => { | ||
const newState = this.reducer(action, this.state); | ||
this.observable.notify(newState, this.state); | ||
this._state = newState; | ||
}; | ||
} | ||
/** | ||
* CUBIT | ||
*/ | ||
class Cubit extends BlocBase { | ||
@@ -64,7 +92,18 @@ emit(state) { | ||
} | ||
this.observable.notify(state, this.state); | ||
const oldState = this.state; | ||
const newState = state; | ||
this._state = state; | ||
this.observer.notify(newState, oldState); | ||
} | ||
} | ||
class Bloc extends BlocBase { | ||
emit = (action) => { | ||
const oldState = this.state; | ||
const newState = this.reducer(action, this.state); | ||
this._state = newState; | ||
this.observer.notify(newState, oldState); | ||
}; | ||
} | ||
exports.Blac = Blac; | ||
@@ -71,0 +110,0 @@ exports.BlacObservable = BlacObservable; |
{ | ||
"name": "blac", | ||
"version": "1.0.1-alpha.0", | ||
"version": "1.0.1-alpha.1", | ||
"license": "MIT", | ||
@@ -23,2 +23,3 @@ "main": "dist/blac.js", | ||
"test": "vitest run", | ||
"test:watch": "vitest --watch", | ||
"coverage": "vitest run --coverage" | ||
@@ -39,3 +40,2 @@ }, | ||
"@vitest/coverage-c8": "^0.29.1", | ||
"codecov": "^3.8.3", | ||
"esbuild": "^0.17.10", | ||
@@ -42,0 +42,0 @@ "eslint": "^8.34.0", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
20
9657
6
267