@@ -4,16 +4,27 @@ import { safeQSubscribe } from '../use/use-core.public';
44import type { QObject as IQObject } from './q-object.public' ;
55export const Q_OBJECT_PREFIX_SEP = ':' ;
66
7- export function _qObject < T > ( obj : T , prefix ?: string , isId : boolean = false ) : T {
7+ export function _qObject < T > ( obj : T ) : T {
88 assertEqual ( unwrapProxy ( obj ) , obj , 'Unexpected proxy at this location' ) ;
9- const id = isId
10- ? ( prefix as string )
11- : ( prefix == null ? '' : prefix + Q_OBJECT_PREFIX_SEP ) + generateId ( ) ;
12- const proxy = readWriteProxy ( obj as any as IQObject < T > , id ) ;
9+ const proxy = readWriteProxy ( obj as any as IQObject < T > , generateId ( ) ) ;
1310 Object . assign ( proxy , obj ) ;
1411 return proxy ;
1512}
1613
14+ export function _stateQObject < T > ( obj : T , prefix : string ) : T {
15+ const id = getQObjectId ( obj ) ;
16+ if ( id ) {
17+ ( obj as any ) [ QObjectIdSymbol ] = prefix + Q_OBJECT_PREFIX_SEP + id ;
18+ return obj ;
19+ } else {
20+ return readWriteProxy ( obj as any as IQObject < T > , prefix + Q_OBJECT_PREFIX_SEP + generateId ( ) ) ;
21+ }
22+ }
23+
24+ export function _restoreQObject < T > ( obj : T , id : string ) : T {
25+ return readWriteProxy ( obj as any as IQObject < T > , id ) ;
26+ }
27+
1728function QObject_notifyWrite ( id : string , doc : Document | null ) {
1829 if ( doc ) {
1930 doc . querySelectorAll ( idToComponentSelector ( id ) ) . forEach ( qNotifyRender ) ;
@@ -35,6 +46,17 @@ export function getQObjectId(obj: any): string | null {
3546 return ( obj && typeof obj === 'object' && obj [ QObjectIdSymbol ] ) || null ;
3647}
3748
49+ export function getTransient < T > ( obj : any , key : any ) : T | null {
50+ assertDefined ( getQObjectId ( obj ) ) ;
51+ return obj [ QOjectTransientsSymbol ] . get ( key ) ;
52+ }
53+
54+ export function setTransient < T > ( obj : any , key : any , value : T ) : T {
55+ assertDefined ( getQObjectId ( obj ) ) ;
56+ obj [ QOjectTransientsSymbol ] . set ( key , value ) ;
57+ return value ;
58+ }
59+
3860function idToComponentSelector ( id : string ) : any {
3961 id = id . replace ( / ( [ ^ \w \d ] ) / g, ( _ , v ) => '\\' + v ) ;
4062 return '[q\\:obj*=' + ( isStateObj ( id ) ? '' : '\\!' ) + id + ']' ;
@@ -61,6 +83,7 @@ export function readWriteProxy<T extends object>(target: T, id: string): T {
6183}
6284
6385const QOjectTargetSymbol = ':target:' ;
86+ const QOjectTransientsSymbol = ':transients:' ;
6487const QObjectIdSymbol = ':id:' ;
6588const QObjectDocumentSymbol = ':doc:' ;
6689
@@ -90,13 +113,17 @@ export function wrap<T>(value: T): T {
90113class ReadWriteProxyHandler < T extends object > implements ProxyHandler < T > {
91114 private id : string ;
92115 private doc : Document | null = null ;
116+ private transients : WeakMap < any , any > | null = null ;
93117 constructor ( id : string ) {
94118 this . id = id ;
95119 }
96120
97121 get ( target : T , prop : string ) : any {
98122 if ( prop === QOjectTargetSymbol ) return target ;
99123 if ( prop === QObjectIdSymbol ) return this . id ;
124+ if ( prop === QOjectTransientsSymbol ) {
125+ return this . transients || ( this . transients = new WeakMap ( ) ) ;
126+ }
100127 const value = ( target as any ) [ prop ] ;
101128 QObject_notifyRead ( target ) ;
102129 return wrap ( value ) ;
@@ -105,6 +132,8 @@ class ReadWriteProxyHandler<T extends object> implements ProxyHandler<T> {
105132 set ( target : T , prop : string , newValue : any ) : boolean {
106133 if ( prop === QObjectDocumentSymbol ) {
107134 this . doc = newValue ;
135+ } else if ( prop == QObjectIdSymbol ) {
136+ this . id = newValue ;
108137 } else {
109138 const unwrappedNewValue = unwrapProxy ( newValue ) ;
110139 const oldValue = ( target as any ) [ prop ] ;
0 commit comments