Skip to content

Commit 6861f77

Browse files
committed
fix problems with actions
1 parent 8f241ba commit 6861f77

File tree

12 files changed

+85
-163
lines changed

12 files changed

+85
-163
lines changed

docs/docs/api-reference/Scope.mdx

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ Create a new PlexusJS plugin
1111
- [.computed(item)](#Scope+computed)
1212
- [.event()](#Scope+event)
1313
- [.collection(config)](#Scope+collection)
14-
- [.action(fn)](#Scope+action)
15-
- [.batchAction(fn)](#Scope+batchAction)
1614
- [.batch(fn)](#Scope+batch)
1715
- [.preaction(fn)](#Scope+preaction)
1816

@@ -60,30 +58,6 @@ Create a new PlexusJS plugin
6058
| ------ | ------------------------------------------- |
6159
| config | <p>The configuration for the collection</p> |
6260

63-
<a name='Scope+action'></a>
64-
65-
### .action(fn) ⇒
66-
67-
<p>Generate a Plexus Action</p>
68-
69-
**Returns**: <p>The intended return value of fn, or null if an error is caught</p>
70-
71-
| Param | Description |
72-
| ----- | ---------------------------------------- |
73-
| fn | <p>The Plexus action function to run</p> |
74-
75-
<a name='Scope+batchAction'></a>
76-
77-
### .batchAction(fn) ⇒
78-
79-
<p>Generate a Plexus Action</p>
80-
81-
**Returns**: <p>The intended return value of fn, or null if an error is caught</p>
82-
83-
| Param | Description |
84-
| ----- | ---------------------------------------- |
85-
| fn | <p>The Plexus action function to run</p> |
86-
8761
<a name='Scope+batch'></a>
8862

8963
### .batch(fn)

docs/docs/api-reference/action.mdx

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,3 @@ Generate a Plexus Action
1717
| Param | Description |
1818
| ----- | ---------------------------------------- |
1919
| fn | <p>The Plexus action function to run</p> |
20-
21-
<a name='Scope+action'></a>
22-
23-
## .action(fn) ⇒
24-
25-
<p>Generate a Plexus Action</p>
26-
27-
**Returns**: <p>The intended return value of fn, or null if an error is caught</p>
28-
29-
| Param | Description |
30-
| ----- | ---------------------------------------- |
31-
| fn | <p>The Plexus action function to run</p> |

docs/docs/api-reference/batchAction.mdx

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,3 @@ Generate a Plexus Action
1717
| Param | Description |
1818
| ----- | ---------------------------------------- |
1919
| fn | <p>The Plexus action function to run</p> |
20-
21-
<a name='Scope+batchAction'></a>
22-
23-
## .batchAction(fn) ⇒
24-
25-
<p>Generate a Plexus Action</p>
26-
27-
**Returns**: <p>The intended return value of fn, or null if an error is caught</p>
28-
29-
| Param | Description |
30-
| ----- | ---------------------------------------- |
31-
| fn | <p>The Plexus action function to run</p> |

packages/plexus-core/src/action.ts

Lines changed: 26 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,23 @@ export interface PlexusActionHooks {
1111
ignoreInit(): void
1212
batch<ReturnType = any>(fn: () => ReturnType): ReturnType
1313
}
14+
export type ActionFunction<
15+
Params extends any[] | [] = any[],
16+
Response = any
17+
> = (
18+
helpers: PlexusActionHooks,
19+
...args: Params
20+
) => Response | Promise<Response>
21+
22+
export type InnerFunction<InputFn extends ActionFunction> = InputFn extends (
23+
helpers: PlexusActionHooks,
24+
...args: infer Params
25+
) => ReturnType<InputFn>
26+
? (...args: Params) => ReturnType<InputFn>
27+
: never
28+
29+
export type PlexusAction = typeof _action
30+
1431
/**
1532
* The action helpers for a defined plexus action
1633
*/
@@ -94,40 +111,11 @@ class PlexusActionHelpers {
94111
}
95112
}
96113

97-
export type FunctionArgs = [helpers: PlexusActionHooks, ...args: any[]]
98-
export type InnerFunctionArgs = [helpers?: PlexusActionHooks, ...args: any[]]
99-
100-
export type FunctionType<Response = any> = (
101-
...args: FunctionArgs
102-
) => Response | Promise<Response>
103-
104-
export type InnerFunction<ResultFn extends FunctionType> = ResultFn extends (
105-
helpers: PlexusActionHooks,
106-
...args: infer Params
107-
) => ReturnType<ResultFn>
108-
? (...args: Params) => ReturnType<ResultFn>
109-
: never
110-
111-
// export type PlexusAction = <ReturnData=FunctionType>(fn: FunctionType) => (...args: any) => ReturnData | Promise<ReturnData>
112-
export type PlexusAction = typeof _action
113-
114-
export function _action<Response, Fn extends FunctionType>(
115-
instance: () => PlexusInstance,
116-
fn: ((...args: FunctionArgs) => Response) & Fn,
117-
batched?: boolean
118-
): (...args: InnerFunctionArgs) => Response
119-
120-
export function _action<Response, Fn extends FunctionType>(
121-
instance: () => PlexusInstance,
122-
fn: ((...args: FunctionArgs) => Promise<Response>) & Fn,
123-
batched?: boolean
124-
): (...args: InnerFunctionArgs) => Promise<Response>
125-
126-
export function _action<Response, Fn extends FunctionType>(
114+
export function _action<Returns, Fn extends ActionFunction>(
127115
instance: () => PlexusInstance,
128-
fn: ((...args: FunctionArgs) => Promise<Response> | Response) & Fn,
116+
fn: Fn & ((helpers: PlexusActionHooks, ...args: any[]) => Returns),
129117
batched?: boolean
130-
) {
118+
): InnerFunction<Fn> {
131119
const helpers = new PlexusActionHelpers(instance)
132120

133121
console.log('function constructor', fn.constructor.name, isAsyncFunction(fn))
@@ -185,8 +173,7 @@ export function _action<Response, Fn extends FunctionType>(
185173
}
186174
}
187175
// return the proxy function
188-
return newAction as InnerFunction<Fn>
189-
// return proxyFn as InnerFunction<Fn>
176+
return newAction as InnerFunction<typeof fn>
190177

191178
// const newAction = async (...args) => {
192179
// try {
@@ -223,29 +210,17 @@ export function _action<Response, Fn extends FunctionType>(
223210
* @param fn The Plexus action function to run
224211
* @returns The intended return value of fn, or null if an error is caught
225212
*/
226-
export function action<Response>(
227-
fn: (...args: FunctionArgs) => Response
228-
): (...args: InnerFunctionArgs) => Response
229-
export function action<Response>(
230-
fn: (...args: FunctionArgs) => Promise<Response>
231-
): (...args: InnerFunctionArgs) => Promise<Response>
232-
export function action<Response>(
233-
fn: (...args: FunctionArgs) => Promise<Response> | Response
234-
) {
235-
return _action(() => instance(), fn)
213+
export function action<Fn extends ActionFunction>(fn: Fn): InnerFunction<Fn> {
214+
return _action(() => instance(), fn) as InnerFunction<Fn>
236215
}
237216

238217
/**
239218
* Generate a Plexus Action
240219
* @param fn The Plexus action function to run
241220
* @returns The intended return value of fn, or null if an error is caught
242221
*/
243-
export function batchAction<Response>(fn: (...args: FunctionArgs) => Response)
244-
export function batchAction<Response>(
245-
fn: (...args: FunctionArgs) => Promise<Response>
246-
)
247-
export function batchAction<Response>(
248-
fn: (...args: FunctionArgs) => Promise<Response> | Response
249-
) {
222+
export function batchAction<Fn extends ActionFunction>(
223+
fn: Fn
224+
): InnerFunction<Fn> {
250225
return _action(() => instance(), fn, true)
251226
}

packages/plexus-core/src/collection/collection.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ interface PlexusCollectionStore<DataType extends Record<string, any>> {
105105
export type PlexusCollectionInstance<
106106
DataType extends Record<string, any> = Record<string, any>,
107107
Groups extends GroupMap<DataType> = GroupMap<DataType>,
108-
Selectors extends SelectorMap<DataType> = SelectorMap<DataType>
108+
Selectors extends SelectorMap<DataType> = SelectorMap<DataType>,
109109
> = CollectionInstance<DataType, Groups, Selectors>
110110
/**
111111
* A Collection Instance
@@ -114,7 +114,7 @@ export type PlexusCollectionInstance<
114114
export class CollectionInstance<
115115
DataTypeInput extends Record<string, any>,
116116
Groups extends GroupMap<DataTypeInput>,
117-
Selectors extends SelectorMap<DataTypeInput>
117+
Selectors extends SelectorMap<DataTypeInput>,
118118
// ForeignRefs extends boolean = this['config']['foreignKeys'] extends {} ? true : false
119119
> {
120120
private _internalStore: PlexusCollectionStore<DataTypeInput>
@@ -1078,7 +1078,7 @@ export class CollectionInstance<
10781078
export function _collection<
10791079
DataType extends { [key: string]: any },
10801080
Groups extends GroupMap<DataType> = GroupMap<DataType>,
1081-
Selectors extends SelectorMap<DataType> = SelectorMap<DataType>
1081+
Selectors extends SelectorMap<DataType> = SelectorMap<DataType>,
10821082
>(
10831083
instance: () => PlexusInstance,
10841084
_config: PlexusCollectionConfig<DataType> = { primaryKey: 'id' } as const

packages/plexus-core/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export { PlexusWatchableValueInterpreter } from '@plexusjs/utils'
1414
export {
1515
action,
1616
_action,
17-
FunctionType,
17+
ActionFunction as FunctionType,
1818
PlexusAction,
1919
PlexusActionHooks,
2020
batchAction,

packages/plexus-core/src/preaction.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { PlexusInstance, instance } from './instance/instance'
22
import {
3-
FunctionArgs,
4-
FunctionType,
5-
InnerFunctionArgs,
3+
// FunctionArgs,
4+
ActionFunction,
5+
// InnerFunctionArgs,
66
_action,
77
} from './action'
88
import { genUID } from '@plexusjs/utils'
@@ -11,7 +11,7 @@ type ErrorHandler = (error: any) => unknown
1111
export interface PlexusPreActionConfig {
1212
lazy?: boolean
1313
}
14-
export class PreActionInstance<Response> {
14+
export class PreActionInstance<Fn extends ActionFunction> {
1515
private _internalStore = {
1616
_ran: false,
1717
_id: genUID(),
@@ -20,7 +20,7 @@ export class PreActionInstance<Response> {
2020
/**
2121
* The action associated with this PreAction
2222
*/
23-
action: (...args: InnerFunctionArgs) => Promise<Response> | Response
23+
action: ReturnType<typeof _action>
2424
/**
2525
* The internal id of the PreAction
2626
*/
@@ -30,7 +30,7 @@ export class PreActionInstance<Response> {
3030

3131
constructor(
3232
private instance: () => PlexusInstance,
33-
fn: (...args: FunctionArgs) => Response | Promise<Response>,
33+
fn: Fn,
3434
config: PlexusPreActionConfig = {}
3535
) {
3636
this.action = _action(instance, fn)
@@ -65,22 +65,23 @@ export class PreActionInstance<Response> {
6565
return result
6666
}
6767
}
68-
export type PlexusPreAction<Fn = any> = PreActionInstance<Fn>
68+
export type PlexusPreAction<Fn extends ActionFunction = any> =
69+
PreActionInstance<Fn>
6970

70-
export function _preaction<Response>(
71+
export function _preaction<Fn extends ActionFunction>(
7172
instance: () => PlexusInstance,
72-
fn: (...args: FunctionArgs) => Response | Promise<Response>,
73+
fn: Fn,
7374
config?: PlexusPreActionConfig
7475
) {
75-
return new PreActionInstance<Response>(instance, fn, config)
76+
return new PreActionInstance(instance, fn, config)
7677
}
7778

7879
/**
7980
* Generate a Plexus Action
8081
* @param fn The Plexus action function to run
8182
* @returns The intended return value of fn, or null if an error is caught
8283
*/
83-
export function preaction<Fn extends FunctionType>(
84+
export function preaction<Fn extends ActionFunction>(
8485
fn: Fn,
8586
config?: PlexusPreActionConfig
8687
) {

packages/plexus-core/src/scope.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { AlmostAnything, LiteralType, TypeOrReturnType } from '@plexusjs/utils'
22
import { Watchable } from '.'
3-
import { FunctionType, _action } from './action'
3+
import { ActionFunction, _action } from './action'
44
import { PlexusCollectionConfig, _collection } from './collection/collection'
55
import { _computed } from './computed'
66
import { _event } from './event'
@@ -115,7 +115,7 @@ export class Scope {
115115
* @param fn The Plexus action function to run
116116
* @returns The intended return value of fn, or null if an error is caught
117117
*/
118-
preaction<Fn extends FunctionType>(fn: Fn, config?: PlexusPreActionConfig) {
118+
preaction<Fn extends ActionFunction>(fn: Fn, config?: PlexusPreActionConfig) {
119119
return _preaction<Fn>(this.instance, fn, config)
120120
}
121121
}

packages/plexus-react/src/useLoader.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { PlexusAction, Watchable } from '@plexusjs/core'
2-
import { FunctionType, InnerFunction } from '@plexusjs/core/dist/action'
2+
import { ActionFunction, InnerFunction } from '@plexusjs/core/dist/action'
33
import { useEffect, useState } from 'react'
44
import { usePlexus, PlexusValue, PlexusValueArray } from './usePlexus'
55

@@ -17,19 +17,19 @@ export type PlexusLoaderOptions = {
1717
}
1818

1919
// Singleton argument
20-
export function useLoader<V extends Watchable, Fn extends FunctionType>(
20+
export function useLoader<V extends Watchable, Fn extends ActionFunction>(
2121
watchables: V,
2222
action: InnerFunction<Fn>,
2323
options?: PlexusLoaderOptions
2424
): PlexusLoaderReturn<PlexusValue<V>>
2525
// array argument
26-
export function useLoader<V extends Watchable[], Fn extends FunctionType>(
26+
export function useLoader<V extends Watchable[], Fn extends ActionFunction>(
2727
watchables: V | [],
2828
action: InnerFunction<Fn>,
2929
options?: PlexusLoaderOptions
3030
): PlexusLoaderReturn<PlexusValueArray<V>>
3131

32-
export function useLoader<Fn extends FunctionType>(
32+
export function useLoader<Fn extends ActionFunction>(
3333
watchables: (typeof usePlexus.arguments)[0],
3434
action: InnerFunction<Fn>,
3535
options?: PlexusLoaderOptions

packages/plexus-utils/src/shared/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const genUID = () =>
88

99
export const isAsyncFunction = <
1010
ReturnedValue,
11-
Args extends Array<unknown> = any[]
11+
Args extends Array<unknown> = any[],
1212
>(
1313
fn: (...args: Args) => ReturnedValue | Promise<ReturnedValue>
1414
): fn is (...args: Args) => Promise<ReturnedValue> =>

0 commit comments

Comments
 (0)