Skip to content

Commit

Permalink
fix(extensions): fixed typing on actions extension
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewcourtice committed Jul 13, 2021
1 parent 39ebeac commit 58f2db2
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 19 deletions.
6 changes: 3 additions & 3 deletions core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function emitCreated(store: InternalStore, state: any): void {
eventEmitter.once(EVENTS.core.installed, created);
}

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

Expand Down Expand Up @@ -105,7 +105,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 object, TExtensions extends Extension[]>(name: string, state: TState, options?: Partial<StoreOptions<TExtensions>>): Store<TState> & ExtendedStore<TExtensions> {
export function createStore<TState extends object, TExtensions extends Extension<TState>[]>(name: string, state: TState, options?: Partial<StoreOptions<TState, TExtensions>>): Store<TState> & ExtendedStore<TExtensions> {
const {
allowOverwrite,
extensions
Expand Down Expand Up @@ -140,7 +140,7 @@ export function createStore<TState extends object, TExtensions extends Extension
const onAfterMutation = getMutationHook(EVENTS.mutation.after);
const onMutationError = getMutationHook(EVENTS.mutation.error);

const extendedStore = getExtendedStore(store, extensions);
const extendedStore = getExtendedStore<TState, TExtensions>(store, extensions);

stores.set(name, store);
emitCreated(store, state);
Expand Down
6 changes: 3 additions & 3 deletions core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export type Mutation<TPayload, TResult = void> = undefined extends TPayload ? (p
export type InternalStores = Map<string, InternalStore<any>>;
export type EventHandler<TData = any> = (payload?: EventPayload<TData>) => void;
export type MutationHookHandler<TPayload, TResult> = (data: MutationEventData<TPayload, TResult>) => void;
export type Extension = (store: InternalStore) => Record<string, any>;
export type ExtendedStore<TExtensions extends Extension[]> = UnionToIntersection<ReturnType<TExtensions[number]>>;
export type Extension<TState> = (store: InternalStore<TState>) => Record<string, any>;
export type ExtendedStore<TExtensions extends Extension<any>[]> = UnionToIntersection<ReturnType<TExtensions[number]>>;

export interface Emittable {
on(event: string, handler: EventHandler): EventListener;
Expand Down Expand Up @@ -60,7 +60,7 @@ export interface InternalStoreOptions {
allowOverwrite: boolean;
}

export interface StoreOptions<TExtensions extends Extension[]> extends InternalStoreOptions {
export interface StoreOptions<TState, TExtensions extends Extension<TState>[]> extends InternalStoreOptions {
extensions?: TExtensions;
}

Expand Down
10 changes: 10 additions & 0 deletions extensions/actions/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const path = require('path');
const build = require('@harlem/build');

(async () => {
const cwd = path.resolve('.');

return build(cwd, 'harlem-actions', {
globalName: 'HarlemActionsExtension'
});
})();
7 changes: 4 additions & 3 deletions extensions/actions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"unpkg": "dist/harlem-actions.min.js",
"types": "dist/index.d.ts",
"source": "src/index.ts",
"private": true,
"keywords": [
"vue",
"state",
Expand All @@ -36,9 +37,9 @@
"vue": "^3.0.0"
},
"devDependencies": {
"@harlem/build": "^1.3.1",
"@harlem/core": "^1.3.1",
"@harlem/testing": "^1.3.1",
"@harlem/build": "^1.3.2",
"@harlem/core": "^1.3.2",
"@harlem/testing": "^1.3.2",
"vue": "^3.0.0"
}
}
16 changes: 7 additions & 9 deletions extensions/actions/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import {
computed
} from 'vue';

import {
Extension,
InternalStore,
Expand All @@ -19,7 +15,9 @@ import type {
RemoveActionInstancePayload
} from './types';

export default ((store: InternalStore<ActionStoreState>) => {
export * from './types';

export default function actionsExtension<TState>(store: InternalStore<TState>) {

store.write('$add-actions', '$actions-extension', state => {
state.$actions = {};
Expand Down Expand Up @@ -52,8 +50,8 @@ export default ((store: InternalStore<ActionStoreState>) => {
state.$actions[actionName].delete(instanceId);
});

function action<TPayload, TResult = void>(name: string, body: ActionBody<TPayload, TResult>): Action<TPayload, TResult> {
const mutate = (mutator: Mutator<ActionStoreState, undefined, void>) => store.write(name, '$actions-extension', mutator);
function action<TPayload, TResult = void>(name: string, body: ActionBody<TState, TPayload, TResult>): Action<TPayload, TResult> {
const mutate = (mutator: Mutator<TState, undefined, void>) => store.write(name, '$actions-extension', mutator);

const _action = async (payload: TPayload) => {
const id = Symbol(name);
Expand All @@ -67,7 +65,7 @@ export default ((store: InternalStore<ActionStoreState>) => {
let result: TResult;

try {
result = await body(payload);
result = await body(payload, mutate);
} finally {
removeActionInstance({
actionName: name,
Expand Down Expand Up @@ -107,4 +105,4 @@ export default ((store: InternalStore<ActionStoreState>) => {
useAction
};

});
};
6 changes: 5 additions & 1 deletion extensions/actions/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export type ActionBody<TPayload, TResult = void> = undefined extends TPayload ? (payload?: TPayload) => Promise<TResult> : (payload: TPayload) => Promise<TResult>;
import {
Mutator
} from '../../../core/dist';

export type ActionBody<TState, TPayload = undefined, TResult = void> = (payload: TPayload, mutator: (mutate: Mutator<TState, undefined, void>) => void) => Promise<TResult>;
//export type Action<TPayload, TResult = void> = undefined extends TPayload ? (payload?: TPayload) => Promise<TResult> : (payload: TPayload) => Promise<TResult>;
export type ActionPredicate<TPayload = any> = (payload?: TPayload) => boolean;
export type ComposedAction<TPayload, TResult = void> = [Action<TPayload, TResult>, (predicate: ActionPredicate<TPayload>) => boolean];
Expand Down
30 changes: 30 additions & 0 deletions extensions/actions/test/actions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {
createStore
} from '@harlem/core';

import actionsExtension from '../src/index';

describe('Actions Extension', () => {

test('Performs a root reset', () => {
const {
action
} = createStore('test', {
givenName: 'John',
surname: 'Smith'
}, {
extensions: [
actionsExtension
]
});

const loadUserInfo = action('load-user-info', async (id, mutate) => {
return new Promise(resolve => setTimeout(() => {
mutate(state => {
state.
});
}, 300));
});
});

});

0 comments on commit 58f2db2

Please sign in to comment.