@@ -4,11 +4,10 @@ import { assert } from '@ember/debug';
4
4
import { associateDestroyableChild , destroy , registerDestructor } from '@ember/destroyable' ;
5
5
// @ts -ignore
6
6
import { invokeHelper , setHelperManager } from '@ember/helper' ;
7
- import { dependencySatisfies , importSync , macroCondition } from '@embroider/macros' ;
8
7
9
8
import { ReadonlyCell } from './cell.ts' ;
10
9
import { ResourceManagerFactory } from './resource-manager.ts' ;
11
- import { CURRENT , INTERNAL } from './types.ts' ;
10
+ import { INTERNAL } from './types.ts' ;
12
11
import { registerUsable , TYPE_KEY } from './use.ts' ;
13
12
import { shallowFlat } from './utils.ts' ;
14
13
@@ -19,20 +18,10 @@ export const CREATE_KEY = Symbol.for('__configured-resource-key__');
19
18
export const DEBUG_NAME = Symbol . for ( 'DEBUG_NAME' ) ;
20
19
export const RESOURCE_CACHE = Symbol . for ( '__resource_cache__' ) ;
21
20
22
- let getOwner : ( context : unknown ) => Owner | undefined ;
23
- let setOwner : ( context : unknown , owner : Owner ) => void ;
24
-
25
- if ( macroCondition ( dependencySatisfies ( 'ember-source' , '>=4.12.0' ) ) ) {
26
- // In no version of ember where `@ember/owner` tried to be imported did it exist
27
- // if (macroCondition(false)) {
28
- // Using 'any' here because importSync can't lookup types correctly
29
- getOwner = ( importSync ( '@ember/owner' ) as any ) . getOwner ;
30
- setOwner = ( importSync ( '@ember/owner' ) as any ) . setOwner ;
31
- } else {
32
- // Using 'any' here because importSync can't lookup types correctly
33
- getOwner = ( importSync ( '@ember/application' ) as any ) . getOwner ;
34
- setOwner = ( importSync ( '@ember/application' ) as any ) . setOwner ;
35
- }
21
+ import { compatOwner } from './owner.ts' ;
22
+
23
+ const getOwner = compatOwner . getOwner ;
24
+ const setOwner = compatOwner . setOwner ;
36
25
37
26
/**
38
27
* The return value from resource()
@@ -71,16 +60,17 @@ export class Builder<Value> {
71
60
this . #fn = fn ;
72
61
}
73
62
74
- create ( owner : Owner ) {
75
- return new Resource ( this . #fn, owner ) ;
63
+ create ( ) {
64
+ return new Resource ( this . #fn) ;
76
65
}
77
66
}
78
67
79
68
const TYPE = 'function-based' ;
80
69
81
70
registerUsable ( TYPE , ( context : object , config : Builder < unknown > ) => {
82
- let owner = getOwner ( context ) || context ;
83
- let instance = config . create ( owner as Owner ) ;
71
+ let instance = config . create ( ) ;
72
+
73
+ instance . link ( context ) ;
84
74
85
75
return instance [ RESOURCE_CACHE ] ;
86
76
} ) ;
@@ -90,12 +80,12 @@ registerUsable(TYPE, (context: object, config: Builder<unknown>) => {
90
80
*/
91
81
export class Resource < Value > {
92
82
#originalFn: ResourceFunction < Value > ;
93
- #owner: Owner ;
83
+ #owner: Owner | undefined ;
94
84
#previousFn: object | undefined ;
95
85
#usableCache = new WeakMap < object , ReturnType < typeof invokeHelper > > ( ) ;
96
86
#cache: ReturnType < typeof invokeHelper > ;
97
87
98
- constructor ( fn : ResourceFunction < Value > , owner : Owner ) {
88
+ constructor ( fn : ResourceFunction < Value > ) {
99
89
/**
100
90
* We have to copy the `fn` in case there are multiple
101
91
* usages or invocations of the function.
@@ -104,7 +94,6 @@ export class Resource<Value> {
104
94
* destroy.
105
95
*/
106
96
this . #originalFn = fn . bind ( null ) ;
107
- this . #owner = owner ;
108
97
109
98
this . #cache = createCache ( ( ) => {
110
99
if ( this . #previousFn) {
@@ -116,6 +105,11 @@ export class Resource<Value> {
116
105
associateDestroyableChild ( this . #originalFn, currentFn ) ;
117
106
this . #previousFn = currentFn ;
118
107
108
+ assert (
109
+ `Cannot create a resource without an owner. Must have previously called .link()` ,
110
+ this . #owner,
111
+ ) ;
112
+
119
113
let maybeValue = currentFn ( {
120
114
on : {
121
115
cleanup : ( destroyer : Destructor ) => {
@@ -161,11 +155,18 @@ export class Resource<Value> {
161
155
162
156
return maybeValue ;
163
157
} ) ;
164
-
165
- setOwner ( this . #cache, this . #owner) ;
166
158
}
167
- link ( owner : Owner ) {
159
+ link ( context : object ) {
160
+ let owner = getOwner ( context ) ;
161
+
162
+ assert ( `Cannot link without an owner` , owner ) ;
163
+
168
164
this . #owner = owner ;
165
+
166
+ associateDestroyableChild ( context , this . #cache) ;
167
+ associateDestroyableChild ( context , this . #originalFn) ;
168
+
169
+ setOwner ( this . #cache, this . #owner) ;
169
170
}
170
171
171
172
get [ RESOURCE_CACHE ] ( ) : unknown {
0 commit comments