Skip to content

Commit

Permalink
feat(core): added provider api
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewcourtice committed Aug 2, 2021
1 parent eb102a7 commit 202025c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
17 changes: 15 additions & 2 deletions core/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import type {
Mutator,
ReadState,
WriteState,
StoreProvider,
StoreProviders,
} from './types';

function localiseHandler(name: string, handler: EventHandler): EventHandler {
Expand All @@ -45,6 +47,7 @@ export default class Store<TState extends BaseState = any> implements InternalSt
private stack: Set<string>;
private readState: ReadState<TState>;
private writeState: WriteState<TState>;
private providers: StoreProviders<TState>;

public name: string;
public getters: Map<string, () => unknown>;
Expand All @@ -60,6 +63,11 @@ export default class Store<TState extends BaseState = any> implements InternalSt
this.writeState = reactive(state) as WriteState<TState>;
this.readState = readonly(this.writeState) as ReadState<TState>;

this.providers = {
read: value => value,
write: value => value,
};

this.name = name;
this.getters = new Map();
this.mutations = new Map();
Expand All @@ -70,7 +78,7 @@ export default class Store<TState extends BaseState = any> implements InternalSt
}

public get state(): ReadState<TState> {
return this.readState;
return this.providers.read(this.readState);
}

public emit(event: string, sender: string, data: any): void {
Expand All @@ -91,6 +99,10 @@ export default class Store<TState extends BaseState = any> implements InternalSt
return eventEmitter.once(event, localiseHandler(this.name, handler));
}

public provider<TKey extends StoreProvider<TState>>(key: TKey, value: StoreProviders<TState>[TKey]): void {
this.providers[key] = value;
}

public getter<TResult>(name: string, getter: Getter<TState, TResult>): ComputedRef<TResult> {
if (!this.allowsOverwrite && this.getters.has(name)) {
raiseOverwriteError('getter', name);
Expand Down Expand Up @@ -119,7 +131,8 @@ export default class Store<TState extends BaseState = any> implements InternalSt
this.emit(EVENTS.mutation.before, sender, eventData);

try {
result = mutator(this.writeState, payload);
const state = this.providers.write(this.writeState);
result = mutator(state, payload);
} catch (error) {
this.emit(EVENTS.mutation.error, sender, eventData);
throw error;
Expand Down
7 changes: 7 additions & 0 deletions core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type {
type UnionToIntersection<U> = (U extends any ? (arg: U) => any : never) extends ((arg: infer I) => void) ? I : never;

export type BaseState = Record<PropertyKey, unknown>;
export type StoreProvider<TState extends BaseState> = keyof StoreProviders<TState>;
export type ReadState<TState extends BaseState> = DeepReadonly<TState>;
export type WriteState<TState extends BaseState> = TState;
export type Getter<TState extends BaseState, TResult> = (state: ReadState<TState>) => TResult;
Expand Down Expand Up @@ -46,6 +47,11 @@ export interface StoreBase<TState extends BaseState> {
mutation<TPayload, TResult = void>(name: string, mutator: Mutator<TState, TPayload, TResult>): Mutation<TPayload, TResult>;
}

export interface StoreProviders<TState extends BaseState> {
read(state: ReadState<TState>): ReadState<TState>;
write(state: WriteState<TState>): WriteState<TState>;
}

export interface InternalStore<TState extends BaseState = any> extends StoreBase<TState> {
readonly allowsOverwrite: boolean;
readonly state: ReadState<TState>;
Expand All @@ -56,6 +62,7 @@ export interface InternalStore<TState extends BaseState = any> extends StoreBase
on(event: string, handler: EventHandler): EventListener;
once(event: string, handler: EventHandler): EventListener
exec<TResult = void>(name: string, payload?: any): TResult;
provider<TKey extends StoreProvider<TState>>(key: TKey, value: StoreProviders<TState>[TKey]): void;
write<TResult = void>(name: string, sender: string, mutator: Mutator<TState, undefined, TResult>): TResult;
}

Expand Down

0 comments on commit 202025c

Please sign in to comment.