Comparing version 1.0.1-alpha.5 to 1.0.1-alpha.6
type BlacObserver<S> = (newState: S, oldState: S) => void | Promise<void>; | ||
declare class BlacObservable<S> { | ||
_observers: Set<BlacObserver<S>>; | ||
subscribe(observer: BlacObserver<S>): void; | ||
private _observers; | ||
get size(): number; | ||
subscribe(observer: BlacObserver<S>): () => void; | ||
unsubscribe(observer: BlacObserver<S>): void; | ||
@@ -10,24 +11,29 @@ notify(newState: S, oldState: S): void; | ||
declare enum BlacEvent { | ||
BLOC_DISPOSED = "BLOC_DISPOSED", | ||
LISTENER_REMOVED = "LISTENER_REMOVED", | ||
LISTENER_ADDED = "LISTENER_ADDED" | ||
type BlocProps = Record<string | number, any>; | ||
declare abstract class Cubit<S, Props extends BlocProps = {}> extends BlocBase<S> { | ||
static create: () => Cubit<any, any>; | ||
props: Props; | ||
constructor(initialState: S); | ||
emit(state: S): void; | ||
patch(state: S extends object ? Partial<S> : S): void; | ||
} | ||
interface BlocOptions { | ||
} | ||
type BlocInstanceId = string | number | undefined; | ||
declare abstract class BlocBase<S> { | ||
static allowMultipleInstances: boolean; | ||
static isolated: boolean; | ||
static keepAlive: boolean; | ||
static create: <S>() => BlocBase<S>; | ||
static isBlacClass: boolean; | ||
isolated: boolean; | ||
isBlacLive: boolean; | ||
observer: BlacObservable<S | any>; | ||
blac?: Blac<any>; | ||
uid: string; | ||
pluginStore: Map<string, unknown>; | ||
constructor(initialState: S, options?: BlocOptions); | ||
observer: BlacObservable<any>; | ||
blac: Blac; | ||
id: BlocInstanceId; | ||
props: BlocProps | undefined; | ||
constructor(initialState: S); | ||
_state: S; | ||
get state(): S; | ||
get name(): string; | ||
onStateChange: (callback: (newState: S, oldState: S) => void) => (() => void); | ||
updateId: (id?: BlocInstanceId) => void; | ||
addEventListenerStateChange: (callback: (newState: S, oldState: S) => void) => (() => void); | ||
dispose(): void; | ||
@@ -43,28 +49,70 @@ private handleUnsubscribe; | ||
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 BlocBaseAbstract = 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; | ||
type CubitPropsType<B extends BlocBase<any>> = B extends Cubit<any, infer P> ? P : never; | ||
interface BlacOptions { | ||
interface BlacPlugin { | ||
name: string; | ||
onEvent<B extends BlacEvent>(event: B, bloc: BlocBase<any>, params?: EventParams[B]): void; | ||
} | ||
declare class Blac<O extends BlacOptions> { | ||
blocMap: Map<BlocConstructor<BlocBase<any>>, BlocBase<any>>; | ||
pluginMap: Map<string, any>; | ||
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; | ||
getPluginKey(ref: string): unknown; | ||
interface BlacConfig { | ||
exposeBlacInstance?: boolean; | ||
} | ||
declare enum BlacEvent { | ||
BLOC_DISPOSED = "BLOC_DISPOSED", | ||
LISTENER_REMOVED = "LISTENER_REMOVED", | ||
LISTENER_ADDED = "LISTENER_ADDED", | ||
STATE_CHANGED = "STATE_CHANGED", | ||
BLOC_CREATED = "BLOC_CREATED" | ||
} | ||
interface EventParams { | ||
[BlacEvent.BLOC_DISPOSED]: undefined; | ||
[BlacEvent.LISTENER_REMOVED]: undefined; | ||
[BlacEvent.LISTENER_ADDED]: undefined; | ||
[BlacEvent.BLOC_CREATED]: undefined; | ||
[BlacEvent.STATE_CHANGED]: { | ||
newState: any; | ||
oldState: any; | ||
}; | ||
} | ||
declare class Blac { | ||
static instance: Blac; | ||
static findAllBlocs: <B extends BlocBase<any>>(blocClass: BlocConstructor<B>) => Promise<B[]>; | ||
static addPlugin: (plugin: BlacPlugin) => void; | ||
static configure: (config: BlacConfig) => void; | ||
blocInstanceMap: Map<string, BlocBase<any>>; | ||
isolatedBlocMap: Map<Function, BlocBase<any>[]>; | ||
pluginList: BlacPlugin[]; | ||
customPropsMap: Map<Function, BlocProps>; | ||
postChangesToDocument: boolean; | ||
constructor(); | ||
static getInstance(): Blac; | ||
addPlugin: (plugin: BlacPlugin) => void; | ||
reportToPlugins: <B extends BlacEvent>(event: B, bloc: BlocBase<any>, params?: EventParams[B] | undefined) => void; | ||
report: <B extends BlacEvent>(event: B, bloc: BlocBase<any>, params?: EventParams[B] | undefined) => void; | ||
createBlocInstanceMapKey(blocClassName: string, id?: BlocInstanceId): string; | ||
unregisterBlocInstance(bloc: BlocBase<any>): void; | ||
registerBlocInstance(bloc: BlocBase<any>): void; | ||
findRegisteredBlocInstance<B extends BlocBase<any>>(blocClass: BlocConstructor<B>, id: BlocInstanceId): B | undefined; | ||
registerIsolatedBlocInstance(bloc: BlocBase<any>): void; | ||
unregisterIsolatedBlocInstance(bloc: BlocBase<any>): void; | ||
setCustomProps(blocClass: BlocBaseAbstract, props: BlocProps | undefined): void; | ||
getCustomProps(blocClass: BlocBaseAbstract): BlocProps | undefined; | ||
createNewBlocInstance<B extends BlocBase<any>>(blocClass: BlocConstructor<B>, id: BlocInstanceId, props: BlocProps | undefined): B; | ||
getBloc<B extends BlocBase<any>>(blocClass: BlocConstructor<B>, options?: { | ||
id?: BlocInstanceId; | ||
props?: BlocProps; | ||
}): B; | ||
findAllBlocs: <B extends BlocBase<any>>(blocClass: BlocConstructor<B>) => Promise<B[]>; | ||
configure: (config: BlacConfig) => void; | ||
} | ||
declare global { | ||
interface Window { | ||
__blac?: Blac; | ||
} | ||
} | ||
export { Blac, BlacEvent, BlacObservable, BlacObserver, BlacOptions, Bloc, BlocBase, BlocBaseAbstract, BlocClassNoParams, BlocConstructor, BlocOptions, Cubit, CubitOptions, ValueType }; | ||
export { Blac, BlacConfig, BlacEvent, BlacObservable, BlacObserver, BlacPlugin, Bloc, BlocBase, BlocBaseAbstract, BlocClassNoParams, BlocConstructor, BlocInstanceId, BlocProps, Cubit, CubitPropsType, EventParams, ValueType }; |
@@ -1,17 +0,86 @@ | ||
class BlacObservable { | ||
_observers = new Set(); | ||
subscribe(observer) { | ||
this._observers.add(observer); | ||
/****************************************************************************** | ||
Copyright (c) Microsoft Corporation. | ||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted. | ||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | ||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY | ||
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | ||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | ||
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR | ||
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | ||
PERFORMANCE OF THIS SOFTWARE. | ||
***************************************************************************** */ | ||
/* global Reflect, Promise, SuppressedError, Symbol */ | ||
var extendStatics = function(d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
function __extends(d, b) { | ||
if (typeof b !== "function" && b !== null) | ||
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
} | ||
var __assign = function() { | ||
__assign = Object.assign || function __assign(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
function __awaiter(thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
} | ||
function __generator(thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (g && (g = 0, op[0] && (_ = 0)), _) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
unsubscribe(observer) { | ||
this._observers.delete(observer); | ||
} | ||
notify(newState, oldState) { | ||
this._observers.forEach((observer) => observer(newState, oldState)); | ||
} | ||
dispose() { | ||
this._observers.clear(); | ||
} | ||
} | ||
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { | ||
var e = new Error(message); | ||
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; | ||
}; | ||
var BlacEvent; | ||
@@ -22,119 +91,279 @@ (function (BlacEvent) { | ||
BlacEvent["LISTENER_ADDED"] = "LISTENER_ADDED"; | ||
BlacEvent["STATE_CHANGED"] = "STATE_CHANGED"; | ||
BlacEvent["BLOC_CREATED"] = "BLOC_CREATED"; | ||
})(BlacEvent || (BlacEvent = {})); | ||
class BlocBase { | ||
static allowMultipleInstances = false; | ||
static keepAlive = false; | ||
static create; | ||
static isBlacClass = true; | ||
isBlacLive = true; | ||
observer; | ||
blac = undefined; | ||
uid = Math.random().toString(36).split(".")[1]; | ||
pluginStore = new Map(); | ||
constructor(initialState, options) { | ||
this.observer = new BlacObservable(); | ||
this._state = initialState; | ||
var Blac = /** @class */ (function () { | ||
function Blac() { | ||
var _this = this; | ||
this.blocInstanceMap = new Map(); | ||
this.isolatedBlocMap = new Map(); | ||
this.pluginList = []; | ||
this.customPropsMap = new Map(); | ||
this.postChangesToDocument = false; | ||
this.addPlugin = function (plugin) { | ||
// check if already added | ||
var index = _this.pluginList.findIndex(function (p) { return p.name === plugin.name; }); | ||
if (index !== -1) | ||
return; | ||
_this.pluginList.push(plugin); | ||
}; | ||
this.reportToPlugins = function (event, bloc, params) { | ||
_this.pluginList.forEach(function (plugin) { | ||
plugin.onEvent(event, bloc, params); | ||
}); | ||
}; | ||
this.report = function (event, bloc, params) { | ||
var base = bloc.constructor; | ||
_this.reportToPlugins(event, bloc, params); | ||
switch (event) { | ||
case BlacEvent.BLOC_DISPOSED: | ||
if (base.isolated) { | ||
_this.unregisterIsolatedBlocInstance(bloc); | ||
} | ||
else { | ||
_this.unregisterBlocInstance(bloc); | ||
} | ||
break; | ||
case BlacEvent.LISTENER_REMOVED: | ||
if (bloc.observer.size === 0 && !base.keepAlive) | ||
bloc.dispose(); | ||
break; | ||
} | ||
}; | ||
this.findAllBlocs = function (blocClass) { return __awaiter(_this, void 0, void 0, function () { | ||
var base, blocs, blocs; | ||
return __generator(this, function (_a) { | ||
base = blocClass; | ||
if (base.isolated) { | ||
blocs = this.isolatedBlocMap.get(blocClass); | ||
if (blocs) | ||
return [2 /*return*/, blocs]; | ||
} | ||
else { | ||
blocs = Array.from(this.blocInstanceMap.values()); | ||
return [2 /*return*/, blocs.filter(function (b) { return b instanceof blocClass; })]; | ||
} | ||
return [2 /*return*/, []]; | ||
}); | ||
}); }; | ||
this.configure = function (config) { | ||
if (config.exposeBlacInstance) { | ||
window.__blac = _this; | ||
} | ||
}; | ||
if (Blac.instance) { | ||
return Blac.instance; | ||
} | ||
Blac.instance = this; | ||
} | ||
_state; | ||
get state() { | ||
return this._state; | ||
} | ||
get name() { | ||
return this.constructor.name; | ||
} | ||
onStateChange = (callback) => { | ||
this.observer.subscribe(callback); | ||
this.blac?.report(BlacEvent.LISTENER_ADDED, this); | ||
return () => this.handleUnsubscribe(callback); | ||
Blac.getInstance = function () { | ||
return Blac.instance; | ||
}; | ||
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); | ||
Blac.prototype.createBlocInstanceMapKey = function (blocClassName, id) { | ||
return "".concat(blocClassName).concat(id ? id : ""); | ||
}; | ||
} | ||
class Blac { | ||
blocMap = new Map(); | ||
pluginMap = new Map(); | ||
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; | ||
Blac.prototype.unregisterBlocInstance = function (bloc) { | ||
var key = this.createBlocInstanceMapKey(bloc.name, bloc.id); | ||
this.blocInstanceMap.delete(key); | ||
}; | ||
Blac.prototype.registerBlocInstance = function (bloc) { | ||
var key = this.createBlocInstanceMapKey(bloc.name, bloc.id); | ||
this.blocInstanceMap.set(key, bloc); | ||
}; | ||
Blac.prototype.findRegisteredBlocInstance = function (blocClass, id) { | ||
var base = blocClass; | ||
if (base.isolated) | ||
return undefined; | ||
var key = this.createBlocInstanceMapKey(blocClass.name, id); | ||
return this.blocInstanceMap.get(key); | ||
}; | ||
Blac.prototype.registerIsolatedBlocInstance = function (bloc) { | ||
var blocClass = bloc.constructor; | ||
var blocs = this.isolatedBlocMap.get(blocClass); | ||
if (blocs) { | ||
blocs.push(bloc); | ||
} | ||
else { | ||
this.isolatedBlocMap.set(blocClass, [bloc]); | ||
} | ||
}; | ||
unregisterBloc(bloc) { | ||
this.blocMap.delete(bloc.constructor); | ||
} | ||
registerBloc(bloc) { | ||
this.blocMap.set(bloc.constructor, bloc); | ||
} | ||
createNewInstance(blocClass) { | ||
const base = blocClass; | ||
const allowMultipleInstances = base.allowMultipleInstances; | ||
Blac.prototype.unregisterIsolatedBlocInstance = function (bloc) { | ||
var blocClass = bloc.constructor; | ||
var blocs = this.isolatedBlocMap.get(blocClass); | ||
if (blocs) { | ||
var index = blocs.findIndex(function (b) { return b.id === bloc.id; }); | ||
blocs.splice(index, 1); | ||
} | ||
}; | ||
Blac.prototype.setCustomProps = function (blocClass, props) { | ||
if (!props) | ||
return; | ||
this.customPropsMap.set(blocClass, props); | ||
}; | ||
Blac.prototype.getCustomProps = function (blocClass) { | ||
return this.customPropsMap.get(blocClass); | ||
}; | ||
Blac.prototype.createNewBlocInstance = function (blocClass, id, props) { | ||
var base = blocClass; | ||
try { | ||
const hasCreateMethod = Object.prototype.hasOwnProperty.call(blocClass, "create"); | ||
const newBloc = hasCreateMethod ? base.create() : new blocClass(); | ||
if (!allowMultipleInstances) { | ||
this.registerBloc(newBloc); | ||
var hasCreateMethod = Object.prototype.hasOwnProperty.call(blocClass, "create"); | ||
this.setCustomProps(base, props); | ||
// base.propsProxy = props; | ||
var newBloc = hasCreateMethod ? base.create() : new blocClass(); | ||
newBloc.updateId(id); | ||
if (base.isolated) { | ||
this.registerIsolatedBlocInstance(newBloc); | ||
return newBloc; | ||
} | ||
this.registerBlocInstance(newBloc); | ||
return newBloc; | ||
} | ||
catch (e) { | ||
console.error(e); | ||
throw new Error("Failed to create instance of ".concat(blocClass.name, ". ").concat(e)); | ||
} | ||
} | ||
getBloc(blocClass) { | ||
const base = blocClass; | ||
const allowMultipleInstances = base.allowMultipleInstances; | ||
if (allowMultipleInstances) { | ||
return this.createNewInstance(blocClass); | ||
} | ||
const registered = this.blocMap.get(blocClass); | ||
}; | ||
Blac.prototype.getBloc = function (blocClass, options) { | ||
if (options === void 0) { options = {}; } | ||
var id = options.id || blocClass.name; | ||
var registered = this.findRegisteredBlocInstance(blocClass, id); | ||
if (registered) | ||
return registered; | ||
return this.createNewInstance(blocClass); | ||
return this.createNewBlocInstance(blocClass, id, options.props); | ||
}; | ||
Blac.instance = new Blac(); | ||
Blac.findAllBlocs = Blac.instance.findAllBlocs; | ||
Blac.addPlugin = Blac.instance.addPlugin; | ||
Blac.configure = Blac.instance.configure; | ||
return Blac; | ||
}()); | ||
var BlacObservable = /** @class */ (function () { | ||
function BlacObservable() { | ||
this._observers = new Set(); | ||
} | ||
addPluginKey(ref, value) { | ||
this.pluginMap.set(ref, value); | ||
Object.defineProperty(BlacObservable.prototype, "size", { | ||
get: function () { | ||
return this._observers.size; | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
BlacObservable.prototype.subscribe = function (observer) { | ||
var _this = this; | ||
this._observers.add(observer); | ||
return function () { return _this.unsubscribe(observer); }; | ||
}; | ||
BlacObservable.prototype.unsubscribe = function (observer) { | ||
this._observers.delete(observer); | ||
}; | ||
BlacObservable.prototype.notify = function (newState, oldState) { | ||
this._observers.forEach(function (observer) { return observer(newState, oldState); }); | ||
}; | ||
BlacObservable.prototype.dispose = function () { | ||
this._observers.clear(); | ||
}; | ||
return BlacObservable; | ||
}()); | ||
var BlocBase = /** @class */ (function () { | ||
function BlocBase(initialState) { | ||
var _this = this; | ||
this.isolated = false; | ||
this.isBlacLive = true; | ||
this.blac = Blac.getInstance(); | ||
this.updateId = function (id) { | ||
var originalId = _this.id; | ||
if (!id || id === originalId) | ||
return; | ||
_this.id = id; | ||
}; | ||
this.addEventListenerStateChange = function (callback) { | ||
_this.observer.subscribe(callback); | ||
_this.blac.report(BlacEvent.LISTENER_ADDED, _this); | ||
return function () { return _this.handleUnsubscribe(callback); }; | ||
}; | ||
this.handleUnsubscribe = function (callback) { | ||
_this.observer.unsubscribe(callback); | ||
_this.blac.report(BlacEvent.LISTENER_REMOVED, _this); | ||
}; | ||
this.observer = new BlacObservable(); | ||
this._state = initialState; | ||
this.blac.report(BlacEvent.BLOC_CREATED, this); | ||
this.id = this.constructor.name; | ||
this.isolated = this.constructor.isolated; | ||
} | ||
getPluginKey(ref) { | ||
return this.pluginMap.get(ref); | ||
Object.defineProperty(BlocBase.prototype, "state", { | ||
get: function () { | ||
return this._state; | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
Object.defineProperty(BlocBase.prototype, "name", { | ||
get: function () { | ||
return this.constructor.name; | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
BlocBase.prototype.dispose = function () { | ||
this.isBlacLive = false; | ||
this.observer.dispose(); | ||
this.blac.report(BlacEvent.BLOC_DISPOSED, this); | ||
}; | ||
BlocBase.isolated = false; | ||
BlocBase.keepAlive = false; | ||
BlocBase.isBlacClass = true; | ||
return BlocBase; | ||
}()); | ||
var Cubit = /** @class */ (function (_super) { | ||
__extends(Cubit, _super); | ||
function Cubit(initialState) { | ||
var _this = _super.call(this, initialState) || this; | ||
_this.props = {}; | ||
var newProps = Blac.getInstance().getCustomProps(_this.constructor); | ||
_this.props = __assign(__assign({}, _this.props), newProps); | ||
return _this; | ||
} | ||
} | ||
class Cubit extends BlocBase { | ||
static create; | ||
emit(state) { | ||
Cubit.prototype.emit = function (state) { | ||
if (state === this.state) { | ||
return; | ||
} | ||
const oldState = this.state; | ||
const newState = state; | ||
var oldState = this.state; | ||
var newState = state; | ||
this._state = state; | ||
this.observer.notify(newState, oldState); | ||
this.blac.report(BlacEvent.STATE_CHANGED, this, { | ||
newState: newState, | ||
oldState: oldState | ||
}); | ||
}; | ||
// partial object if this.state is object, otherwise same as state | ||
Cubit.prototype.patch = function (state) { | ||
this.emit(__assign(__assign({}, this.state), state)); | ||
}; | ||
return Cubit; | ||
}(BlocBase)); | ||
var Bloc = /** @class */ (function (_super) { | ||
__extends(Bloc, _super); | ||
function Bloc() { | ||
var _this = _super !== null && _super.apply(this, arguments) || this; | ||
_this.emit = function (action) { | ||
var oldState = _this.state; | ||
var newState = _this.reducer(action, _this.state); | ||
_this._state = newState; | ||
_this.observer.notify(newState, oldState); | ||
_this.blac.report(BlacEvent.STATE_CHANGED, _this, { | ||
newState: newState, | ||
oldState: oldState | ||
}); | ||
}; | ||
return _this; | ||
} | ||
} | ||
return Bloc; | ||
}(BlocBase)); | ||
class Bloc extends BlocBase { | ||
static create; | ||
emit = (action) => { | ||
const oldState = this.state; | ||
const newState = this.reducer(action, this.state); | ||
this._state = newState; | ||
this.observer.notify(newState, oldState); | ||
}; | ||
} | ||
export { Blac, BlacEvent, BlacObservable, Bloc, BlocBase, Cubit }; | ||
//# sourceMappingURL=blac.esm.js.map |
439
dist/blac.js
'use strict'; | ||
class BlacObservable { | ||
_observers = new Set(); | ||
subscribe(observer) { | ||
this._observers.add(observer); | ||
/****************************************************************************** | ||
Copyright (c) Microsoft Corporation. | ||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted. | ||
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | ||
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY | ||
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | ||
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | ||
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR | ||
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | ||
PERFORMANCE OF THIS SOFTWARE. | ||
***************************************************************************** */ | ||
/* global Reflect, Promise, SuppressedError, Symbol */ | ||
var extendStatics = function(d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
}; | ||
function __extends(d, b) { | ||
if (typeof b !== "function" && b !== null) | ||
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); | ||
extendStatics(d, b); | ||
function __() { this.constructor = d; } | ||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); | ||
} | ||
var __assign = function() { | ||
__assign = Object.assign || function __assign(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
function __awaiter(thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
} | ||
function __generator(thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (g && (g = 0, op[0] && (_ = 0)), _) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
unsubscribe(observer) { | ||
this._observers.delete(observer); | ||
} | ||
notify(newState, oldState) { | ||
this._observers.forEach((observer) => observer(newState, oldState)); | ||
} | ||
dispose() { | ||
this._observers.clear(); | ||
} | ||
} | ||
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { | ||
var e = new Error(message); | ||
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; | ||
}; | ||
exports.BlacEvent = void 0; | ||
@@ -24,118 +93,278 @@ (function (BlacEvent) { | ||
BlacEvent["LISTENER_ADDED"] = "LISTENER_ADDED"; | ||
BlacEvent["STATE_CHANGED"] = "STATE_CHANGED"; | ||
BlacEvent["BLOC_CREATED"] = "BLOC_CREATED"; | ||
})(exports.BlacEvent || (exports.BlacEvent = {})); | ||
class BlocBase { | ||
static allowMultipleInstances = false; | ||
static keepAlive = false; | ||
static create; | ||
static isBlacClass = true; | ||
isBlacLive = true; | ||
observer; | ||
blac = undefined; | ||
uid = Math.random().toString(36).split(".")[1]; | ||
pluginStore = new Map(); | ||
constructor(initialState, options) { | ||
this.observer = new BlacObservable(); | ||
this._state = initialState; | ||
var Blac = /** @class */ (function () { | ||
function Blac() { | ||
var _this = this; | ||
this.blocInstanceMap = new Map(); | ||
this.isolatedBlocMap = new Map(); | ||
this.pluginList = []; | ||
this.customPropsMap = new Map(); | ||
this.postChangesToDocument = false; | ||
this.addPlugin = function (plugin) { | ||
// check if already added | ||
var index = _this.pluginList.findIndex(function (p) { return p.name === plugin.name; }); | ||
if (index !== -1) | ||
return; | ||
_this.pluginList.push(plugin); | ||
}; | ||
this.reportToPlugins = function (event, bloc, params) { | ||
_this.pluginList.forEach(function (plugin) { | ||
plugin.onEvent(event, bloc, params); | ||
}); | ||
}; | ||
this.report = function (event, bloc, params) { | ||
var base = bloc.constructor; | ||
_this.reportToPlugins(event, bloc, params); | ||
switch (event) { | ||
case exports.BlacEvent.BLOC_DISPOSED: | ||
if (base.isolated) { | ||
_this.unregisterIsolatedBlocInstance(bloc); | ||
} | ||
else { | ||
_this.unregisterBlocInstance(bloc); | ||
} | ||
break; | ||
case exports.BlacEvent.LISTENER_REMOVED: | ||
if (bloc.observer.size === 0 && !base.keepAlive) | ||
bloc.dispose(); | ||
break; | ||
} | ||
}; | ||
this.findAllBlocs = function (blocClass) { return __awaiter(_this, void 0, void 0, function () { | ||
var base, blocs, blocs; | ||
return __generator(this, function (_a) { | ||
base = blocClass; | ||
if (base.isolated) { | ||
blocs = this.isolatedBlocMap.get(blocClass); | ||
if (blocs) | ||
return [2 /*return*/, blocs]; | ||
} | ||
else { | ||
blocs = Array.from(this.blocInstanceMap.values()); | ||
return [2 /*return*/, blocs.filter(function (b) { return b instanceof blocClass; })]; | ||
} | ||
return [2 /*return*/, []]; | ||
}); | ||
}); }; | ||
this.configure = function (config) { | ||
if (config.exposeBlacInstance) { | ||
window.__blac = _this; | ||
} | ||
}; | ||
if (Blac.instance) { | ||
return Blac.instance; | ||
} | ||
Blac.instance = this; | ||
} | ||
_state; | ||
get state() { | ||
return this._state; | ||
} | ||
get name() { | ||
return this.constructor.name; | ||
} | ||
onStateChange = (callback) => { | ||
this.observer.subscribe(callback); | ||
this.blac?.report(exports.BlacEvent.LISTENER_ADDED, this); | ||
return () => this.handleUnsubscribe(callback); | ||
Blac.getInstance = function () { | ||
return Blac.instance; | ||
}; | ||
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); | ||
Blac.prototype.createBlocInstanceMapKey = function (blocClassName, id) { | ||
return "".concat(blocClassName).concat(id ? id : ""); | ||
}; | ||
} | ||
class Blac { | ||
blocMap = new Map(); | ||
pluginMap = new Map(); | ||
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; | ||
Blac.prototype.unregisterBlocInstance = function (bloc) { | ||
var key = this.createBlocInstanceMapKey(bloc.name, bloc.id); | ||
this.blocInstanceMap.delete(key); | ||
}; | ||
Blac.prototype.registerBlocInstance = function (bloc) { | ||
var key = this.createBlocInstanceMapKey(bloc.name, bloc.id); | ||
this.blocInstanceMap.set(key, bloc); | ||
}; | ||
Blac.prototype.findRegisteredBlocInstance = function (blocClass, id) { | ||
var base = blocClass; | ||
if (base.isolated) | ||
return undefined; | ||
var key = this.createBlocInstanceMapKey(blocClass.name, id); | ||
return this.blocInstanceMap.get(key); | ||
}; | ||
Blac.prototype.registerIsolatedBlocInstance = function (bloc) { | ||
var blocClass = bloc.constructor; | ||
var blocs = this.isolatedBlocMap.get(blocClass); | ||
if (blocs) { | ||
blocs.push(bloc); | ||
} | ||
else { | ||
this.isolatedBlocMap.set(blocClass, [bloc]); | ||
} | ||
}; | ||
unregisterBloc(bloc) { | ||
this.blocMap.delete(bloc.constructor); | ||
} | ||
registerBloc(bloc) { | ||
this.blocMap.set(bloc.constructor, bloc); | ||
} | ||
createNewInstance(blocClass) { | ||
const base = blocClass; | ||
const allowMultipleInstances = base.allowMultipleInstances; | ||
Blac.prototype.unregisterIsolatedBlocInstance = function (bloc) { | ||
var blocClass = bloc.constructor; | ||
var blocs = this.isolatedBlocMap.get(blocClass); | ||
if (blocs) { | ||
var index = blocs.findIndex(function (b) { return b.id === bloc.id; }); | ||
blocs.splice(index, 1); | ||
} | ||
}; | ||
Blac.prototype.setCustomProps = function (blocClass, props) { | ||
if (!props) | ||
return; | ||
this.customPropsMap.set(blocClass, props); | ||
}; | ||
Blac.prototype.getCustomProps = function (blocClass) { | ||
return this.customPropsMap.get(blocClass); | ||
}; | ||
Blac.prototype.createNewBlocInstance = function (blocClass, id, props) { | ||
var base = blocClass; | ||
try { | ||
const hasCreateMethod = Object.prototype.hasOwnProperty.call(blocClass, "create"); | ||
const newBloc = hasCreateMethod ? base.create() : new blocClass(); | ||
if (!allowMultipleInstances) { | ||
this.registerBloc(newBloc); | ||
var hasCreateMethod = Object.prototype.hasOwnProperty.call(blocClass, "create"); | ||
this.setCustomProps(base, props); | ||
// base.propsProxy = props; | ||
var newBloc = hasCreateMethod ? base.create() : new blocClass(); | ||
newBloc.updateId(id); | ||
if (base.isolated) { | ||
this.registerIsolatedBlocInstance(newBloc); | ||
return newBloc; | ||
} | ||
this.registerBlocInstance(newBloc); | ||
return newBloc; | ||
} | ||
catch (e) { | ||
console.error(e); | ||
throw new Error("Failed to create instance of ".concat(blocClass.name, ". ").concat(e)); | ||
} | ||
} | ||
getBloc(blocClass) { | ||
const base = blocClass; | ||
const allowMultipleInstances = base.allowMultipleInstances; | ||
if (allowMultipleInstances) { | ||
return this.createNewInstance(blocClass); | ||
} | ||
const registered = this.blocMap.get(blocClass); | ||
}; | ||
Blac.prototype.getBloc = function (blocClass, options) { | ||
if (options === void 0) { options = {}; } | ||
var id = options.id || blocClass.name; | ||
var registered = this.findRegisteredBlocInstance(blocClass, id); | ||
if (registered) | ||
return registered; | ||
return this.createNewInstance(blocClass); | ||
return this.createNewBlocInstance(blocClass, id, options.props); | ||
}; | ||
Blac.instance = new Blac(); | ||
Blac.findAllBlocs = Blac.instance.findAllBlocs; | ||
Blac.addPlugin = Blac.instance.addPlugin; | ||
Blac.configure = Blac.instance.configure; | ||
return Blac; | ||
}()); | ||
var BlacObservable = /** @class */ (function () { | ||
function BlacObservable() { | ||
this._observers = new Set(); | ||
} | ||
addPluginKey(ref, value) { | ||
this.pluginMap.set(ref, value); | ||
Object.defineProperty(BlacObservable.prototype, "size", { | ||
get: function () { | ||
return this._observers.size; | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
BlacObservable.prototype.subscribe = function (observer) { | ||
var _this = this; | ||
this._observers.add(observer); | ||
return function () { return _this.unsubscribe(observer); }; | ||
}; | ||
BlacObservable.prototype.unsubscribe = function (observer) { | ||
this._observers.delete(observer); | ||
}; | ||
BlacObservable.prototype.notify = function (newState, oldState) { | ||
this._observers.forEach(function (observer) { return observer(newState, oldState); }); | ||
}; | ||
BlacObservable.prototype.dispose = function () { | ||
this._observers.clear(); | ||
}; | ||
return BlacObservable; | ||
}()); | ||
var BlocBase = /** @class */ (function () { | ||
function BlocBase(initialState) { | ||
var _this = this; | ||
this.isolated = false; | ||
this.isBlacLive = true; | ||
this.blac = Blac.getInstance(); | ||
this.updateId = function (id) { | ||
var originalId = _this.id; | ||
if (!id || id === originalId) | ||
return; | ||
_this.id = id; | ||
}; | ||
this.addEventListenerStateChange = function (callback) { | ||
_this.observer.subscribe(callback); | ||
_this.blac.report(exports.BlacEvent.LISTENER_ADDED, _this); | ||
return function () { return _this.handleUnsubscribe(callback); }; | ||
}; | ||
this.handleUnsubscribe = function (callback) { | ||
_this.observer.unsubscribe(callback); | ||
_this.blac.report(exports.BlacEvent.LISTENER_REMOVED, _this); | ||
}; | ||
this.observer = new BlacObservable(); | ||
this._state = initialState; | ||
this.blac.report(exports.BlacEvent.BLOC_CREATED, this); | ||
this.id = this.constructor.name; | ||
this.isolated = this.constructor.isolated; | ||
} | ||
getPluginKey(ref) { | ||
return this.pluginMap.get(ref); | ||
Object.defineProperty(BlocBase.prototype, "state", { | ||
get: function () { | ||
return this._state; | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
Object.defineProperty(BlocBase.prototype, "name", { | ||
get: function () { | ||
return this.constructor.name; | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
BlocBase.prototype.dispose = function () { | ||
this.isBlacLive = false; | ||
this.observer.dispose(); | ||
this.blac.report(exports.BlacEvent.BLOC_DISPOSED, this); | ||
}; | ||
BlocBase.isolated = false; | ||
BlocBase.keepAlive = false; | ||
BlocBase.isBlacClass = true; | ||
return BlocBase; | ||
}()); | ||
var Cubit = /** @class */ (function (_super) { | ||
__extends(Cubit, _super); | ||
function Cubit(initialState) { | ||
var _this = _super.call(this, initialState) || this; | ||
_this.props = {}; | ||
var newProps = Blac.getInstance().getCustomProps(_this.constructor); | ||
_this.props = __assign(__assign({}, _this.props), newProps); | ||
return _this; | ||
} | ||
} | ||
class Cubit extends BlocBase { | ||
static create; | ||
emit(state) { | ||
Cubit.prototype.emit = function (state) { | ||
if (state === this.state) { | ||
return; | ||
} | ||
const oldState = this.state; | ||
const newState = state; | ||
var oldState = this.state; | ||
var newState = state; | ||
this._state = state; | ||
this.observer.notify(newState, oldState); | ||
this.blac.report(exports.BlacEvent.STATE_CHANGED, this, { | ||
newState: newState, | ||
oldState: oldState | ||
}); | ||
}; | ||
// partial object if this.state is object, otherwise same as state | ||
Cubit.prototype.patch = function (state) { | ||
this.emit(__assign(__assign({}, this.state), state)); | ||
}; | ||
return Cubit; | ||
}(BlocBase)); | ||
var Bloc = /** @class */ (function (_super) { | ||
__extends(Bloc, _super); | ||
function Bloc() { | ||
var _this = _super !== null && _super.apply(this, arguments) || this; | ||
_this.emit = function (action) { | ||
var oldState = _this.state; | ||
var newState = _this.reducer(action, _this.state); | ||
_this._state = newState; | ||
_this.observer.notify(newState, oldState); | ||
_this.blac.report(exports.BlacEvent.STATE_CHANGED, _this, { | ||
newState: newState, | ||
oldState: oldState | ||
}); | ||
}; | ||
return _this; | ||
} | ||
} | ||
return Bloc; | ||
}(BlocBase)); | ||
class Bloc extends BlocBase { | ||
static create; | ||
emit = (action) => { | ||
const oldState = this.state; | ||
const newState = this.reducer(action, this.state); | ||
this._state = newState; | ||
this.observer.notify(newState, oldState); | ||
}; | ||
} | ||
exports.Blac = Blac; | ||
@@ -142,0 +371,0 @@ exports.BlacObservable = BlacObservable; |
{ | ||
"name": "blac", | ||
"version": "1.0.1-alpha.5", | ||
"version": "1.0.1-alpha.6", | ||
"license": "MIT", | ||
@@ -28,17 +28,17 @@ "main": "dist/blac.js", | ||
"devDependencies": { | ||
"@babel/core": "^7.22.9", | ||
"@babel/preset-env": "^7.22.9", | ||
"@babel/core": "^7.22.10", | ||
"@babel/preset-env": "^7.22.10", | ||
"@babel/preset-react": "^7.22.5", | ||
"@babel/preset-typescript": "^7.22.5", | ||
"@rollup/plugin-commonjs": "^25.0.3", | ||
"@rollup/plugin-commonjs": "^25.0.4", | ||
"@rollup/plugin-node-resolve": "^15.1.0", | ||
"@typescript-eslint/eslint-plugin": "^6.2.1", | ||
"@typescript-eslint/parser": "^6.2.1", | ||
"@typescript-eslint/eslint-plugin": "^6.3.0", | ||
"@typescript-eslint/parser": "^6.3.0", | ||
"@vitejs/plugin-react-refresh": "^1.3.6", | ||
"@vitest/browser": "^0.34.1", | ||
"@vitest/coverage-c8": "^0.33.0", | ||
"esbuild": "^0.18.17", | ||
"eslint": "^8.46.0", | ||
"prettier": "^3.0.0", | ||
"rollup": "^3.27.0", | ||
"esbuild": "^0.19.1", | ||
"eslint": "^8.47.0", | ||
"prettier": "^3.0.1", | ||
"rollup": "^3.28.0", | ||
"rollup-plugin-dts": "^5.3.1", | ||
@@ -45,0 +45,0 @@ "rollup-plugin-peer-deps-external": "^2.2.4", |
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
78089
8
824
1