Comparing version
type BlacObserver<S> = (newState: S, oldState: S) => void | Promise<void>; | ||
declare class BlacObservable<S> { | ||
private _observers; | ||
_observers: Set<BlacObserver<S>>; | ||
subscribe(observer: BlacObserver<S>): void; | ||
@@ -10,37 +10,57 @@ unsubscribe(observer: BlacObserver<S>): void; | ||
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; | ||
declare enum BlacEvent { | ||
BLOC_DISPOSED = "BLOC_DISPOSED", | ||
LISTENER_REMOVED = "LISTENER_REMOVED", | ||
LISTENER_ADDED = "LISTENER_ADDED" | ||
} | ||
interface BlocOptions { | ||
} | ||
declare abstract class BlocBase<S> { | ||
static allowMultipleInstances: boolean; | ||
static keepAlive: boolean; | ||
static create: <S>() => BlocBase<S>; | ||
static isBlacClass: boolean; | ||
isBlacLive: boolean; | ||
_state: S; | ||
observer: BlacObservable<S>; | ||
blac?: Blac<any, any>; | ||
observer: BlacObservable<S | any>; | ||
blac?: Blac<any>; | ||
uid: string; | ||
pluginStore: Map<string, unknown>; | ||
constructor(initialState: S, options?: BlocOptions); | ||
_state: S; | ||
get state(): S; | ||
get name(): string; | ||
onStateChange: (callback: (newState: S, oldState: S) => void) => (() => void); | ||
getGlobalBloc<B extends BlocBase<any>>(blocClass: BlocClass<B>): B | undefined; | ||
dispose(): void; | ||
private handleUnsubscribe; | ||
} | ||
type BlacGlobalState = { | ||
[key: string]: BlocBase<any>; | ||
}; | ||
interface BlacOptions<G extends BlacGlobalState> { | ||
global?: G; | ||
declare abstract class Bloc<S, A> extends BlocBase<S> { | ||
static create: () => BlocBase<any>; | ||
abstract reducer(action: A, state: S): S; | ||
emit: (action: A) => void; | ||
} | ||
declare class Blac<GS extends BlacGlobalState, O extends BlacOptions<GS>> { | ||
blocMap: Map<BlocClass<BlocBase<any>>, BlocBase<any>>; | ||
interface CubitOptions { | ||
} | ||
declare abstract class Cubit<S> extends BlocBase<S> { | ||
static create: () => Cubit<any>; | ||
emit(state: S): void; | ||
} | ||
type BlocClassNoParams<B> = new (args: never[]) => B; | ||
type BlocBaseAbstract<T> = typeof Bloc<any, any> | typeof Cubit<any>; | ||
type BlocConstructor<B> = new (...args: never[]) => B; | ||
type ValueType<B extends BlocBase<any>> = B extends BlocBase<infer U> ? U : never; | ||
interface BlacOptions { | ||
} | ||
declare class Blac<O extends BlacOptions> { | ||
blocMap: Map<BlocConstructor<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; | ||
report: (event: BlacEvent, bloc: BlocBase<any>) => void; | ||
unregisterBloc(bloc: BlocBase<any>): void; | ||
registerBloc(bloc: BlocBase<any>): void; | ||
createNewInstance<B extends BlocBase<any>>(blocClass: BlocConstructor<B>): B | undefined; | ||
getBloc<B extends BlocBase<any>>(blocClass: BlocConstructor<B>): B | undefined; | ||
addPluginKey(ref: string, value: any): void; | ||
@@ -51,20 +71,9 @@ getPluginKey(ref: string): unknown; | ||
interface Window { | ||
blac?: Blac<any, any>; | ||
blac?: Blac<any>; | ||
} | ||
interface GlobalThis { | ||
blac?: Blac<any, any>; | ||
blac?: Blac<any>; | ||
} | ||
} | ||
interface CubitOptions { | ||
} | ||
declare abstract class Cubit<S> extends BlocBase<S> { | ||
emit(state: S): void; | ||
} | ||
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 }; | ||
export { Blac, BlacEvent, BlacObservable, BlacObserver, BlacOptions, Bloc, BlocBase, BlocBaseAbstract, BlocClassNoParams, BlocConstructor, BlocOptions, Cubit, CubitOptions, ValueType }; |
@@ -1,37 +0,1 @@ | ||
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 { | ||
@@ -53,9 +17,18 @@ _observers = new Set(); | ||
var BlacEvent; | ||
(function (BlacEvent) { | ||
BlacEvent["BLOC_DISPOSED"] = "BLOC_DISPOSED"; | ||
BlacEvent["LISTENER_REMOVED"] = "LISTENER_REMOVED"; | ||
BlacEvent["LISTENER_ADDED"] = "LISTENER_ADDED"; | ||
})(BlacEvent || (BlacEvent = {})); | ||
class BlocBase { | ||
static allowMultipleInstances = false; | ||
static keepAlive = false; | ||
static create; | ||
static isBlacClass = true; | ||
isBlacLive = true; | ||
_state; | ||
observer; | ||
blac = globalThis.blac; | ||
uid = Math.random().toString(36).split('.')[1]; | ||
uid = Math.random().toString(36).split(".")[1]; | ||
pluginStore = new Map(); | ||
constructor(initialState, options) { | ||
@@ -65,2 +38,3 @@ this.observer = new BlacObservable(); | ||
} | ||
_state; | ||
get state() { | ||
@@ -74,14 +48,76 @@ return this._state; | ||
this.observer.subscribe(callback); | ||
return () => this.observer.unsubscribe(callback); | ||
this.blac?.report(BlacEvent.LISTENER_ADDED, this); | ||
return () => this.handleUnsubscribe(callback); | ||
}; | ||
getGlobalBloc(blocClass) { | ||
return this.blac?.getBloc(blocClass); | ||
} | ||
dispose() { | ||
this.isBlacLive = false; | ||
this.observer.dispose(); | ||
this.blac?.report(BlacEvent.BLOC_DISPOSED, this); | ||
} | ||
handleUnsubscribe = (callback) => { | ||
this.observer.unsubscribe(callback); | ||
this.blac?.report(BlacEvent.LISTENER_REMOVED, this); | ||
}; | ||
} | ||
class Blac { | ||
blocMap = new Map(); | ||
pluginMap = new Map(); | ||
constructor(options = {}) { | ||
globalThis.blac = this; | ||
} | ||
report = (event, bloc) => { | ||
const base = bloc.constructor; | ||
switch (event) { | ||
case BlacEvent.BLOC_DISPOSED: | ||
this.unregisterBloc(bloc); | ||
break; | ||
case BlacEvent.LISTENER_REMOVED: | ||
if (bloc.observer._observers.size === 0 && !base.keepAlive) | ||
bloc.dispose(); | ||
break; | ||
} | ||
}; | ||
unregisterBloc(bloc) { | ||
this.blocMap.delete(bloc.constructor); | ||
} | ||
registerBloc(bloc) { | ||
this.blocMap.set(bloc.constructor, bloc); | ||
} | ||
createNewInstance(blocClass) { | ||
const base = blocClass; | ||
const allowMultipleInstances = base.allowMultipleInstances; | ||
try { | ||
const hasCreateMethod = Object.prototype.hasOwnProperty.call(blocClass, "create"); | ||
const newBloc = hasCreateMethod ? base.create() : new blocClass(); | ||
if (!allowMultipleInstances) { | ||
this.registerBloc(newBloc); | ||
} | ||
return newBloc; | ||
} | ||
catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
getBloc(blocClass) { | ||
const base = blocClass; | ||
const allowMultipleInstances = base.allowMultipleInstances; | ||
if (allowMultipleInstances) { | ||
return this.createNewInstance(blocClass); | ||
} | ||
const registered = this.blocMap.get(blocClass); | ||
if (registered) | ||
return registered; | ||
return this.createNewInstance(blocClass); | ||
} | ||
addPluginKey(ref, value) { | ||
this.pluginMap.set(ref, value); | ||
} | ||
getPluginKey(ref) { | ||
return this.pluginMap.get(ref); | ||
} | ||
} | ||
class Cubit extends BlocBase { | ||
static create; | ||
emit(state) { | ||
@@ -99,2 +135,3 @@ if (state === this.state) { | ||
class Bloc extends BlocBase { | ||
static create; | ||
emit = (action) => { | ||
@@ -108,3 +145,3 @@ const oldState = this.state; | ||
export { Blac, BlacObservable, Bloc, BlocBase, Cubit }; | ||
export { Blac, BlacEvent, BlacObservable, Bloc, BlocBase, Cubit }; | ||
//# sourceMappingURL=blac.esm.js.map |
121
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 { | ||
@@ -55,9 +19,18 @@ _observers = new Set(); | ||
exports.BlacEvent = void 0; | ||
(function (BlacEvent) { | ||
BlacEvent["BLOC_DISPOSED"] = "BLOC_DISPOSED"; | ||
BlacEvent["LISTENER_REMOVED"] = "LISTENER_REMOVED"; | ||
BlacEvent["LISTENER_ADDED"] = "LISTENER_ADDED"; | ||
})(exports.BlacEvent || (exports.BlacEvent = {})); | ||
class BlocBase { | ||
static allowMultipleInstances = false; | ||
static keepAlive = false; | ||
static create; | ||
static isBlacClass = true; | ||
isBlacLive = true; | ||
_state; | ||
observer; | ||
blac = globalThis.blac; | ||
uid = Math.random().toString(36).split('.')[1]; | ||
uid = Math.random().toString(36).split(".")[1]; | ||
pluginStore = new Map(); | ||
constructor(initialState, options) { | ||
@@ -67,2 +40,3 @@ this.observer = new BlacObservable(); | ||
} | ||
_state; | ||
get state() { | ||
@@ -76,14 +50,76 @@ return this._state; | ||
this.observer.subscribe(callback); | ||
return () => this.observer.unsubscribe(callback); | ||
this.blac?.report(exports.BlacEvent.LISTENER_ADDED, this); | ||
return () => this.handleUnsubscribe(callback); | ||
}; | ||
getGlobalBloc(blocClass) { | ||
return this.blac?.getBloc(blocClass); | ||
} | ||
dispose() { | ||
this.isBlacLive = false; | ||
this.observer.dispose(); | ||
this.blac?.report(exports.BlacEvent.BLOC_DISPOSED, this); | ||
} | ||
handleUnsubscribe = (callback) => { | ||
this.observer.unsubscribe(callback); | ||
this.blac?.report(exports.BlacEvent.LISTENER_REMOVED, this); | ||
}; | ||
} | ||
class Blac { | ||
blocMap = new Map(); | ||
pluginMap = new Map(); | ||
constructor(options = {}) { | ||
globalThis.blac = this; | ||
} | ||
report = (event, bloc) => { | ||
const base = bloc.constructor; | ||
switch (event) { | ||
case exports.BlacEvent.BLOC_DISPOSED: | ||
this.unregisterBloc(bloc); | ||
break; | ||
case exports.BlacEvent.LISTENER_REMOVED: | ||
if (bloc.observer._observers.size === 0 && !base.keepAlive) | ||
bloc.dispose(); | ||
break; | ||
} | ||
}; | ||
unregisterBloc(bloc) { | ||
this.blocMap.delete(bloc.constructor); | ||
} | ||
registerBloc(bloc) { | ||
this.blocMap.set(bloc.constructor, bloc); | ||
} | ||
createNewInstance(blocClass) { | ||
const base = blocClass; | ||
const allowMultipleInstances = base.allowMultipleInstances; | ||
try { | ||
const hasCreateMethod = Object.prototype.hasOwnProperty.call(blocClass, "create"); | ||
const newBloc = hasCreateMethod ? base.create() : new blocClass(); | ||
if (!allowMultipleInstances) { | ||
this.registerBloc(newBloc); | ||
} | ||
return newBloc; | ||
} | ||
catch (e) { | ||
console.error(e); | ||
} | ||
} | ||
getBloc(blocClass) { | ||
const base = blocClass; | ||
const allowMultipleInstances = base.allowMultipleInstances; | ||
if (allowMultipleInstances) { | ||
return this.createNewInstance(blocClass); | ||
} | ||
const registered = this.blocMap.get(blocClass); | ||
if (registered) | ||
return registered; | ||
return this.createNewInstance(blocClass); | ||
} | ||
addPluginKey(ref, value) { | ||
this.pluginMap.set(ref, value); | ||
} | ||
getPluginKey(ref) { | ||
return this.pluginMap.get(ref); | ||
} | ||
} | ||
class Cubit extends BlocBase { | ||
static create; | ||
emit(state) { | ||
@@ -101,2 +137,3 @@ if (state === this.state) { | ||
class Bloc extends BlocBase { | ||
static create; | ||
emit = (action) => { | ||
@@ -103,0 +140,0 @@ const oldState = this.state; |
{ | ||
"name": "blac", | ||
"version": "1.0.1-alpha.2", | ||
"version": "1.0.1-alpha.3", | ||
"license": "MIT", | ||
@@ -21,2 +21,3 @@ "main": "dist/blac.js", | ||
"build": "rollup -c rollup.config.lib.js", | ||
"dev": "rollup -c rollup.config.lib.js --watch", | ||
"prettier": "prettier --write ./src", | ||
@@ -29,23 +30,23 @@ "test": "vitest run", | ||
"devDependencies": { | ||
"@babel/core": "^7.21.0", | ||
"@babel/preset-env": "^7.20.2", | ||
"@babel/preset-react": "^7.18.6", | ||
"@babel/preset-typescript": "^7.21.0", | ||
"@rollup/plugin-commonjs": "^24.0.1", | ||
"@rollup/plugin-node-resolve": "^15.0.1", | ||
"@typescript-eslint/eslint-plugin": "^5.53.0", | ||
"@typescript-eslint/parser": "^5.53.0", | ||
"@babel/core": "^7.22.9", | ||
"@babel/preset-env": "^7.22.9", | ||
"@babel/preset-react": "^7.22.5", | ||
"@babel/preset-typescript": "^7.22.5", | ||
"@rollup/plugin-commonjs": "^25.0.3", | ||
"@rollup/plugin-node-resolve": "^15.1.0", | ||
"@typescript-eslint/eslint-plugin": "^6.2.0", | ||
"@typescript-eslint/parser": "^6.2.0", | ||
"@vitejs/plugin-react-refresh": "^1.3.6", | ||
"@vitest/browser": "^0.29.1", | ||
"@vitest/coverage-c8": "^0.29.1", | ||
"esbuild": "^0.17.10", | ||
"eslint": "^8.34.0", | ||
"prettier": "^2.8.4", | ||
"rollup": "^3.17.2", | ||
"rollup-plugin-dts": "^5.2.0", | ||
"@vitest/browser": "^0.33.0", | ||
"@vitest/coverage-c8": "^0.33.0", | ||
"esbuild": "^0.18.17", | ||
"eslint": "^8.46.0", | ||
"prettier": "^3.0.0", | ||
"rollup": "^3.27.0", | ||
"rollup-plugin-dts": "^5.3.0", | ||
"rollup-plugin-peer-deps-external": "^2.2.4", | ||
"rollup-plugin-typescript2": "^0.34.1", | ||
"typescript": "^4.9.5", | ||
"vitest": "^0.29.1" | ||
"rollup-plugin-typescript2": "^0.35.0", | ||
"typescript": "^5.1.6", | ||
"vitest": "^0.33.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
12736
31.88%350
31.09%1
Infinity%