-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add plugin and refactor ReduxDevtoolsPlugin
- Loading branch information
1 parent
453ea2d
commit 3192f46
Showing
27 changed files
with
645 additions
and
298 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { isDevMode } from '@angular/core'; | ||
import { ThyStoreModule, ReduxDevtoolsPlugin } from '@tethys/store'; | ||
|
||
export default { | ||
imports: [ | ||
ThyStoreModule.forRoot([], { | ||
plugins: isDevMode() ? [ReduxDevtoolsPlugin] : [] | ||
}) | ||
] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { Inject, Injectable, Optional, SkipSelf, Type } from '@angular/core'; | ||
import { PLUGINS_TOKEN, StorePlugin, StorePluginFn } from './plugin'; | ||
|
||
@Injectable() | ||
export class StorePluginManager { | ||
private static pluginManager: StorePluginManager; | ||
|
||
private pluginHandlers: StorePluginFn[] = []; | ||
|
||
static get instance() { | ||
return this.pluginManager; | ||
} | ||
|
||
get rootPluginHandlers(): StorePluginFn[] { | ||
return (this.parentManager && this.parentManager.pluginHandlers) || this.pluginHandlers; | ||
} | ||
|
||
constructor( | ||
@Optional() | ||
@SkipSelf() | ||
private parentManager: StorePluginManager, | ||
@Optional() | ||
@Inject(PLUGINS_TOKEN) | ||
private plugins: StorePlugin[] | ||
) { | ||
StorePluginManager.pluginManager = this.parentManager || this; | ||
if (!this.parentManager) { | ||
this.registerPlugins(); | ||
} | ||
} | ||
|
||
private registerPlugins(): void { | ||
const pluginHandlers: StorePluginFn[] = this.getPluginHandlers(); | ||
this.rootPluginHandlers.push(...pluginHandlers); | ||
} | ||
|
||
private getPluginHandlers(): StorePluginFn[] { | ||
const plugins: StorePlugin[] = this.plugins || []; | ||
return plugins.map((plugin: StorePlugin) => (plugin.handle ? plugin.handle.bind(plugin) : plugin) as StorePluginFn); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,36 @@ | ||
export interface PluginContext { | ||
store: string; | ||
import { Store } from './store'; | ||
import { Observable } from 'rxjs'; | ||
import { InjectionToken } from '@angular/core'; | ||
|
||
export interface PluginContext<TState = unknown> { | ||
/** | ||
* 调用此 Action 的 Store | ||
*/ | ||
store: Store<TState>; | ||
/** | ||
* Action 名称 | ||
*/ | ||
action: string; | ||
state: unknown; | ||
/** | ||
* 当前 Store 调用此 Action 之前的状态 | ||
*/ | ||
state: TState; | ||
/** | ||
* 获取当前 Store 的状态 | ||
*/ | ||
getState: () => TState; | ||
/** | ||
* 获取当前所有 Store 实例的状态 | ||
*/ | ||
getAllState: () => Record<string, TState>; | ||
} | ||
|
||
export type StorePluginNextFn<T> = (ctx: PluginContext) => Observable<T>; | ||
|
||
export type StorePluginFn<T = unknown> = (ctx: PluginContext, next: StorePluginNextFn<T>) => Observable<T>; | ||
|
||
export interface StorePlugin { | ||
handle<T>(ctx: PluginContext, next: StorePluginNextFn<T>): Observable<T>; | ||
} | ||
|
||
export const PLUGINS_TOKEN = new InjectionToken('TETHYS_STORE_PLUGINS'); |
Oops, something went wrong.