Skip to content

Commit

Permalink
feat(core): added event suppression
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewcourtice committed Aug 18, 2021
1 parent a8ae1b9 commit 8f5cbd3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
16 changes: 15 additions & 1 deletion core/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
import {
clone,
} from '@harlem/utilities';

import type {
StoreProviders,
} from './types';

export const SENDER = 'core';

export const EVENTS = {
Expand All @@ -17,4 +25,10 @@ export const EVENTS = {
devtools: {
update: 'devtools:update',
},
} as const;
} as const;

export const PROVIDERS = {
read: value => value,
write: value => value,
payload: value => clone(value),
} as StoreProviders<any>;
27 changes: 16 additions & 11 deletions core/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import eventEmitter from './event-emitter';
import {
EVENTS,
SENDER,
PROVIDERS,
} from './constants';

import {
Expand All @@ -14,10 +15,6 @@ import {
EffectScope,
} from 'vue';

import {
clone,
} from '@harlem/utilities';

import type {
BaseState,
EventHandler,
Expand Down Expand Up @@ -52,6 +49,7 @@ export default class Store<TState extends BaseState = any> implements InternalSt
private options: InternalStoreOptions<TState>;
private scope: EffectScope;
private stack: Set<string>;
private isSuppressing: boolean;
private readState: ReadState<TState>;
private writeState: WriteState<TState>;

Expand All @@ -64,16 +62,15 @@ export default class Store<TState extends BaseState = any> implements InternalSt
...options,

providers: {
read: value => value,
write: value => value,
payload: value => clone(value),
...PROVIDERS,
...options?.providers,
},
};

this.name = name;
this.registrations = {};
this.stack = new Set();
this.isSuppressing = false;
this.scope = effectScope();
this.writeState = reactive(state) as WriteState<TState>;
this.readState = readonly(this.writeState) as ReadState<TState>;
Expand All @@ -85,9 +82,7 @@ export default class Store<TState extends BaseState = any> implements InternalSt

public get providers(): StoreProviders<TState> {
return {
read: value => value,
write: value => value,
payload: value => value,
...PROVIDERS,
...this.options.providers,
};
}
Expand All @@ -97,7 +92,7 @@ export default class Store<TState extends BaseState = any> implements InternalSt
}

public emit(event: string, sender: string, data: any): void {
if (!this.scope.active) {
if (!this.scope.active || this.isSuppressing) {
return;
}

Expand Down Expand Up @@ -153,6 +148,16 @@ export default class Store<TState extends BaseState = any> implements InternalSt
this.registrations[group]?.delete(name);
}

public suppress(callback: () => void): void {
this.isSuppressing = true;

try {
callback();
} finally {
this.isSuppressing = false;
}
}

public getter<TResult>(name: string, getter: Getter<TState, TResult>): ComputedRef<TResult> {
const output = this.track(() => computed(() => getter(this.state)));

Expand Down
1 change: 1 addition & 0 deletions core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export interface InternalStore<TState extends BaseState = any> extends StoreBase
on(event: string, handler: EventHandler): EventListener;
once(event: string, handler: EventHandler): EventListener
track<TResult>(callback: () => TResult): TResult;
suppress(callback: () => void): void;
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;
destroy(): void;
Expand Down

0 comments on commit 8f5cbd3

Please sign in to comment.