|
@@ -21,7 +21,7 @@ class NCoreContext<T extends {} | undefined, K extends ConfigType<T>> {
|
|
|
setState: (state: Partial<T> | any) => void = () => {
|
|
setState: (state: Partial<T> | any) => void = () => {
|
|
|
};
|
|
};
|
|
|
subscribers: Array<{
|
|
subscribers: Array<{
|
|
|
- func: (state: T) => void;
|
|
|
|
|
|
|
+ func: (params: any, state: T) => void;
|
|
|
key: string;
|
|
key: string;
|
|
|
}> = [];
|
|
}> = [];
|
|
|
config: K;
|
|
config: K;
|
|
@@ -45,14 +45,20 @@ class NCoreContext<T extends {} | undefined, K extends ConfigType<T>> {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // event emitter:
|
|
|
|
|
- emit = (state: T) => {
|
|
|
|
|
|
|
+ // event emitters:
|
|
|
|
|
+ emit = (params: any, state: T) => {
|
|
|
this.subscribers.forEach(subscriber => {
|
|
this.subscribers.forEach(subscriber => {
|
|
|
- subscriber.func(state);
|
|
|
|
|
|
|
+ subscriber.func(params, state);
|
|
|
});
|
|
});
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- // listeners:
|
|
|
|
|
|
|
+ emitWithKey = (key: string, payload?: any) => {
|
|
|
|
|
+ this.subscribers.forEach(subscriber => {
|
|
|
|
|
+ if(subscriber.key === key) subscriber.func(payload, this.state);
|
|
|
|
|
+ });
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
|
|
+ // event listeners:
|
|
|
addEventListener = (key: string, func: (state: T) => void) => {
|
|
addEventListener = (key: string, func: (state: T) => void) => {
|
|
|
this.subscribers.push({
|
|
this.subscribers.push({
|
|
|
func,
|
|
func,
|
|
@@ -87,7 +93,7 @@ class NCoreContext<T extends {} | undefined, K extends ConfigType<T>> {
|
|
|
|
|
|
|
|
this.state = newState;
|
|
this.state = newState;
|
|
|
|
|
|
|
|
- this.emit(this.state);
|
|
|
|
|
|
|
+ this.emit(undefined, this.state);
|
|
|
|
|
|
|
|
if(this.config.isSaveState && this.config.onStorageUpdate) {
|
|
if(this.config.isSaveState && this.config.onStorageUpdate) {
|
|
|
this.config.onStorageUpdate(this.state);
|
|
this.config.onStorageUpdate(this.state);
|