@@ -7,14 +7,28 @@ import {
77} from "../../interfaces" ;
88import { noop } from "../../utils/noop" ;
99
10+ type CreateActionProxyOptions = {
11+ readonly context ?: string [ ] ;
12+ readonly applicable ?: boolean ;
13+ } ;
14+
1015export const createActionProxy = < T > (
1116 actionDispatcher : ActionDispatcher < AnyAction > ,
12- context : string [ ] = [ ] ,
17+ options : CreateActionProxyOptions = { } ,
1318) : T => {
14- return new Proxy ( noop , {
19+ const { context = [ ] , applicable = false } = options ;
20+
21+ let target = { } ;
22+ const handler : ProxyHandler < typeof noop > = {
1523 get : get ( actionDispatcher , context ) ,
16- apply : apply ( actionDispatcher , context ) ,
17- } ) as T ;
24+ } ;
25+
26+ if ( applicable ) {
27+ target = noop ;
28+ handler . apply = apply ( actionDispatcher , context ) ;
29+ }
30+
31+ return new Proxy ( target , handler ) as T ;
1832} ;
1933
2034const SPECIAL_PROPERTIES = new Set ( [
@@ -38,37 +52,44 @@ const SPECIAL_PROPERTIES = new Set([
3852] ) ;
3953
4054const get =
41- < T > ( actionDispatcher : ActionDispatcher < AnyAction > , context : string [ ] ) =>
55+ < T > (
56+ actionDispatcher : ActionDispatcher < AnyAction > ,
57+ context : readonly string [ ] ,
58+ ) =>
4259 ( _ : unknown , property : string | symbol ) => {
4360 if ( typeof property === "string" && SPECIAL_PROPERTIES . has ( property ) ) {
4461 return ;
4562 }
4663 if ( typeof property === "symbol" ) {
4764 return ;
4865 }
49- if ( property === "$select" ) {
50- return createActionProxy < T > ( actionDispatcher , [ ...context , property ] ) ;
66+ if ( property . startsWith ( "$" ) ) {
67+ return createActionProxy < T > ( actionDispatcher , {
68+ context : [ ...context , property ] ,
69+ applicable : true ,
70+ } ) ;
5171 }
52- return createActionProxy < T > ( actionDispatcher , [
53- ...context ,
54- snakeCase ( property ) ,
55- ] ) ;
72+ return createActionProxy < T > ( actionDispatcher , {
73+ context : [ ...context , snakeCase ( property ) ] ,
74+ applicable : true ,
75+ } ) ;
5676 } ;
5777
5878const apply =
5979 < T > ( actionDispatcher : ActionDispatcher < AnyAction > , context : string [ ] ) =>
6080 ( _1 : unknown , _2 : unknown , args : unknown [ ] ) : unknown => {
6181 const action = context . pop ( ) ;
6282
83+ /* istanbul ignore next */
6384 if ( action == undefined ) {
6485 throw new Error ( "No action specified" ) ;
6586 }
6687
6788 if ( action === "$select" ) {
68- return createActionProxy < T > ( actionDispatcher , [
69- ...context ,
70- ... ( args as string [ ] ) ,
71- ] ) ;
89+ return createActionProxy < T > ( actionDispatcher , {
90+ context : [ ...context , ... ( args as string [ ] ) ] ,
91+ applicable : true ,
92+ } ) ;
7293 }
7394
7495 const path = "/" + context . join ( "/" ) ;
0 commit comments