11import AwaitedHandler , { NotImplementedError } from 'awaited-dom/base/AwaitedHandler' ;
22import AwaitedPath , { IJsPath } from 'awaited-dom/base/AwaitedPath' ;
33import Constructable from 'awaited-dom/base/Constructable' ;
4- import IAttachedState from 'awaited-dom/base/IAttachedState ' ;
4+ import INodePointer from 'awaited-dom/base/INodePointer ' ;
55import IExecJsPathResult from '@secret-agent/interfaces/IExecJsPathResult' ;
6- import getAttachedStateFnName from '@secret-agent/interfaces/getAttachedStateFnName ' ;
6+ import getNodePointerFnName from '@secret-agent/interfaces/getNodePointerFnName ' ;
77import IAwaitedOptions from '../interfaces/IAwaitedOptions' ;
88import CoreFrameEnvironment from './CoreFrameEnvironment' ;
99
10- // Sets up AwaitedHandler initializer hooks. See Noderdom/AwaitedDOM
11- AwaitedHandler . delegate = {
10+ export const delegate = {
1211 getProperty,
1312 setProperty,
1413 construct,
1514 runMethod,
1615 runStatic,
17- loadState ,
16+ createNodePointer ,
1817} ;
1918
20- export async function getProperty < T , TClass > (
19+ // Sets up AwaitedHandler initializer hooks. See Noderdom/AwaitedDOM
20+ AwaitedHandler . delegate = delegate ;
21+
22+ async function getProperty < T , TClass > (
2123 self : AwaitedHandler < TClass > ,
2224 instance : TClass ,
2325 name : string ,
2426) : Promise < T > {
25- const state = self . getState ( instance ) ;
26- await awaitRemoteInitializer ( state ) ;
27- const awaitedPath = state . awaitedPath as AwaitedPath ;
28- const { coreFrame } = state . awaitedOptions as IAwaitedOptions ;
29- const awaitedCoreFrame = await coreFrame ;
30- const finalPath = awaitedPath . addProperty ( name ) . toJSON ( ) ;
31- const result = await execJsPath < TClass , T > ( self , awaitedCoreFrame , instance , finalPath ) ;
27+ const { awaitedPath, coreFrame } = await getAwaitedState ( self , instance ) ;
28+
29+ const finalPath = awaitedPath . addProperty ( instance as any , name ) ;
30+
31+ const result = await execJsPath < TClass , T > ( self , coreFrame , instance , finalPath . toJSON ( ) ) ;
3232
3333 return cleanResult ( self , instance , result ) ;
3434}
3535
36- export async function setProperty < T , TClass > (
36+ async function setProperty < T , TClass > (
3737 self : AwaitedHandler < TClass > ,
3838 instance : TClass ,
3939 name : string ,
@@ -43,50 +43,57 @@ export async function setProperty<T, TClass>(
4343 self . setState ( instance , { [ name ] : value } ) ;
4444}
4545
46- export async function runMethod < T , TClass > (
46+ async function runMethod < T , TClass > (
4747 self : AwaitedHandler < TClass > ,
4848 instance : TClass ,
4949 name : string ,
5050 args : any [ ] ,
5151) : Promise < T > {
52- await awaitRemoteInitializer ( instance ) ;
53- const state = self . getState ( instance ) ;
54- const awaitedPath = state . awaitedPath as AwaitedPath ;
55- const { coreFrame } = state . awaitedOptions as IAwaitedOptions ;
56- const awaitedCoreFrame = await coreFrame ;
57- const finalPath = awaitedPath . addMethod ( name , ...args ) . toJSON ( ) ;
58- const result = await execJsPath < TClass , T > ( self , awaitedCoreFrame , instance , finalPath ) ;
52+ const { awaitedPath, coreFrame } = await getAwaitedState ( self , instance ) ;
53+ const finalPath = awaitedPath . addMethod ( instance as any , name , ...args ) ;
54+
55+ const result = await execJsPath < TClass , T > ( self , coreFrame , instance , finalPath . toJSON ( ) ) ;
5956 return cleanResult ( self , instance , result ) ;
6057}
6158
62- export async function loadState < TClass > (
59+ async function createNodePointer < TClass > (
6360 self : AwaitedHandler < TClass > ,
6461 instance : TClass ,
65- properties ?: string [ ] ,
66- ) : Promise < IAttachedState > {
67- await awaitRemoteInitializer ( instance ) ;
68- const state = self . getState ( instance ) ;
69- const awaitedPath = state . awaitedPath as AwaitedPath ;
70- const { coreFrame } = state . awaitedOptions as IAwaitedOptions ;
71- const awaitedCoreFrame = await coreFrame ;
72- const finalPath = awaitedPath . addMethod ( getAttachedStateFnName , properties ) . toJSON ( ) ;
73- const result = await execJsPath < TClass , null > ( self , awaitedCoreFrame , instance , finalPath ) ;
62+ ) : Promise < INodePointer > {
63+ const { awaitedPath, coreFrame } = await getAwaitedState ( self , instance ) ;
64+ const finalPath = awaitedPath . addMethod ( instance as any , getNodePointerFnName ) . toJSON ( ) ;
65+ const result = await execJsPath < TClass , null > ( self , coreFrame , instance , finalPath ) ;
7466
75- return result ?. attachedState as IAttachedState ;
67+ return result ?. nodePointer ;
7668}
7769
78- export function runStatic < T , TClass > (
70+ function runStatic < T , TClass > (
7971 self : AwaitedHandler < TClass > ,
8072 _klass : Constructable < TClass > ,
8173 name : string ,
8274) : T {
8375 throw new NotImplementedError ( `${ self . className } .${ name } static method not implemented` ) ;
8476}
8577
86- export function construct < TClass > ( self : AwaitedHandler < TClass > ) : TClass {
78+ function construct < TClass > ( self : AwaitedHandler < TClass > ) : TClass {
8779 throw new NotImplementedError ( `${ self . className } constructor not implemented` ) ;
8880}
8981
82+ async function getAwaitedState < TClass > (
83+ self : AwaitedHandler < TClass > ,
84+ instance : TClass ,
85+ ) : Promise < {
86+ awaitedPath : AwaitedPath ;
87+ coreFrame : CoreFrameEnvironment ;
88+ } > {
89+ await awaitRemoteInitializer ( instance ) ;
90+ const state = self . getState ( instance ) ;
91+ const awaitedPath = state . awaitedPath as AwaitedPath ;
92+ const { coreFrame } = state . awaitedOptions as IAwaitedOptions ;
93+ const awaitedCoreFrame = await coreFrame ;
94+ return { awaitedPath, coreFrame : awaitedCoreFrame } ;
95+ }
96+
9097export function getAwaitedPathAsMethodArg ( awaitedPath : AwaitedPath ) : string {
9198 return `$$jsPath=${ JSON . stringify ( awaitedPath . toJSON ( ) ) } ` ;
9299}
@@ -121,12 +128,12 @@ function cleanResult<T, TClass>(
121128 result : IExecJsPathResult < T > ,
122129) : T {
123130 if ( ! result ) return null ;
124- if ( ! result ?. attachedState ) return result ?. value ;
125131
126- self . setState ( instance , {
127- attachedState : result . attachedState ,
128- } ) ;
129- delete result . attachedState ;
132+ if ( result . nodePointer ) {
133+ self . setState ( instance , {
134+ nodePointer : result . nodePointer ,
135+ } ) ;
136+ }
130137
131138 return result . value ;
132139}
0 commit comments