Skip to content

Commit

Permalink
fix(core): fixed extension typing
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewcourtice committed Jun 27, 2022
1 parent 011c375 commit c66c0bf
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
7 changes: 5 additions & 2 deletions core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ function emitCreated(store: InternalStore, state: any): void {
eventEmitter.once(EVENTS.core.installed, created);
}

function getExtendedStore<TState extends BaseState, TExtensions extends Extension<TState>[]>(store: InternalStore<TState>, extensions: TExtensions): ReturnType<Extension<TState>> {
function getExtendedStore<TState extends BaseState, TExtensions extends Extension<TState>[]>(
store: InternalStore<TState>,
extensions: TExtensions
): ReturnType<Extension<TState>> {
return extensions.reduce((output, extension) => {
let result = {};

Expand Down Expand Up @@ -114,7 +117,7 @@ function installPlugin(plugin: HarlemPlugin, app: App): void {
export const on = eventEmitter.on.bind(eventEmitter);
export const once = eventEmitter.once.bind(eventEmitter);

export function createStore<TState extends BaseState, TExtensions extends Extension<TState>[]>(
export function createStore<TState extends BaseState, TExtensions extends Extension<TState>[] = []>(
name: string,
state: TState,
options?: Partial<StoreOptions<TState, TExtensions>>
Expand Down
10 changes: 4 additions & 6 deletions core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import type {

type UnionToIntersection<TValue> = (TValue extends any ? (arg: TValue) => any : never) extends ((arg: infer I) => void) ? I : never;

//export type BaseState = object;
export type BaseState = Record<PropertyKey, any>;
export type StoreProvider<TState extends BaseState> = keyof StoreProviders<TState>;
export type ReadState<TState extends BaseState> = DeepReadonly<TState>;
Expand All @@ -23,11 +22,10 @@ export type EventHandler<TData = any> = (payload?: EventPayload<TData>) => void;
export type TriggerHandler<TEventData extends TriggerEventData> = (data: TEventData) => void;
export type Trigger<TEventData extends TriggerEventData> = (name: string | string[], handler: TriggerHandler<TEventData>) => EventListener;
export type BranchAccessor<TState extends BaseState, TBranchState extends BaseState> = (state: ReadState<TState> | WriteState<TState>) => TBranchState;
export type InternalStores = Map<string, InternalStore<any>>;
export type InternalStores = Map<string, InternalStore<BaseState>>;
export type Extension<TState extends BaseState> = (store: InternalStore<TState>) => Record<string, any>;
export type ExtensionAPIs<TExtensions extends Extension<any>[]> = UnionToIntersection<ReturnType<TExtensions[number]>>;
export type PublicStore<TState extends BaseState, TExtensions extends Extension<TState>[]> = Store<TState> & ExtensionAPIs<TExtensions>;
// export type PublicStore<TState extends BaseState, TExtensions extends Extension<TState>[]> = Omit<Store<TState>, keyof ExtensionAPIs<TExtensions>> & ExtensionAPIs<TExtensions>
export type ExtensionAPIs<TExtensions extends Extension<BaseState>[]> = UnionToIntersection<ReturnType<TExtensions[number]>>;
export type PublicStore<TState extends BaseState, TExtensions extends Extension<TState>[]> = Omit<Store<TState>, keyof ExtensionAPIs<TExtensions>> & ExtensionAPIs<TExtensions>

export interface Emittable {
on(event: string, handler: EventHandler): EventListener;
Expand Down Expand Up @@ -158,7 +156,7 @@ export interface StoreProviders<TState extends BaseState> {
payload<TPayload>(payload: TPayload): TPayload;
}

export interface InternalStore<TState extends BaseState = any> extends StoreBase<TState> {
export interface InternalStore<TState extends BaseState = BaseState> extends StoreBase<TState> {
/**
* A boolean indicating whether this store allows overwriting duplicate registrations
*/
Expand Down
12 changes: 4 additions & 8 deletions core/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import Harlem, {
} from '../src';

import {
App,
isRef,
Plugin,
ref,
} from 'vue';

Expand Down Expand Up @@ -46,12 +48,6 @@ function getStore() {
[internalKey]: 10,
}, {
allowOverwrite: false,
// extensions: [
// store => ({
// blah: 5,
// //action: () => 6
// }),
// ],
});

const fullName = getter('fullname', ({ details }) => `${details.firstName} ${details.lastName}`);
Expand Down Expand Up @@ -88,12 +84,12 @@ describe('Harlem Core', () => {

beforeAll(() => {
const app = {
use: (plugin: any, options?: any) => {
use: (plugin: Plugin, options?: any) => {
if (plugin && plugin.install){
plugin.install(app, options);
}
},
};
} as App;

app.use(Harlem);
});
Expand Down

0 comments on commit c66c0bf

Please sign in to comment.