1
+ import type Owner from '@ember/owner' ;
2
+ import type { Invoke } from '@glint/template/-private/integration' ;
3
+
1
4
export interface Stage1DecoratorDescriptor {
2
5
initializer : ( ) => unknown ;
3
6
}
@@ -7,3 +10,138 @@ export type Stage1Decorator = (
7
10
key : string | symbol ,
8
11
descriptor ?: Stage1DecoratorDescriptor ,
9
12
) => any ;
13
+
14
+ export const INTERMEDIATE_VALUE = '__Intermediate_Value__' ;
15
+ export const INTERNAL = '__INTERNAL__' ;
16
+
17
+ export interface InternalFunctionResourceConfig < Value = unknown > {
18
+ definition : ResourceFunction < Value > ;
19
+ type : 'function-based' ;
20
+ name : string ;
21
+ [ INTERNAL ] : true ;
22
+ }
23
+
24
+ export const CURRENT = Symbol ( 'ember-resources::CURRENT' ) as unknown as 'CURRENT' ;
25
+
26
+ export interface GlintRenderable {
27
+ /**
28
+ * Cells aren't inherently understood by Glint,
29
+ * so to work around that, we'll hook in to the fact that
30
+ * ContentValue (the type expected for all renderables),
31
+ * defines an interface with this signature.
32
+ *
33
+ * (SafeString)
34
+ *
35
+ * There *has* been interest in the community to formally support
36
+ * toString and toHTML APIs across all objects. An RFC needs to be
37
+ * written so that we can gather feedback / potential problems.
38
+ */
39
+ toHTML ( ) : string ;
40
+ }
41
+
42
+ // Will need to be a class for .current flattening / auto-rendering
43
+ export interface Reactive < Value > extends GlintRenderable {
44
+ current : Value ;
45
+ [ CURRENT ] : Value ;
46
+ [ Invoke ] ?: Value ;
47
+ }
48
+
49
+ /**
50
+ * This is the type of the arguments passed to the `resource` function
51
+ *
52
+ * ```ts
53
+ * import { resource, type ResourceAPI } from 'ember-resources';
54
+ *
55
+ * export const Clock = resource((api: ResourceAPI) => {
56
+ * let { on, use, owner } = api;
57
+ *
58
+ * // ...
59
+ * })
60
+ * ```
61
+ */
62
+ export type ResourceAPI = {
63
+ on : {
64
+ /**
65
+ * Optionally a function-resource can provide a cleanup function.
66
+ *
67
+ *
68
+ * Example:
69
+ * ```js
70
+ * import { resource } from 'ember-resources';
71
+ * import { TrackedObject } from 'tracked-built-ins';
72
+ *
73
+ * const load = resource(({ on }) => {
74
+ * let state = new TrackedObject({});
75
+ * let controller = new AbortController();
76
+ *
77
+ * on.cleanup(() => controller.abort());
78
+ *
79
+ * fetch(this.url, { signal: controller.signal })
80
+ * // ...
81
+ *
82
+ * return state;
83
+ * })
84
+ */
85
+ cleanup : ( destroyer : Destructor ) => void ;
86
+ } ;
87
+
88
+ /**
89
+ * Allows for composition of resources.
90
+ *
91
+ * Example:
92
+ * ```js
93
+ * let formatter = new Intl.DateTimeFormat("en-US", {
94
+ * hour: "numeric",
95
+ * minute: "numeric",
96
+ * second: "numeric",
97
+ * hour12: false,
98
+ * });
99
+ * let format = (time: Reactive<Date>) => formatter.format(time.current);
100
+ *
101
+ * const Now = resource(({ on }) => {
102
+ * let now = cell(nowDate);
103
+ * let timer = setInterval(() => now.set(Date.now()), 1000);
104
+ *
105
+ * on.cleanup(() => clearInterval(timer));
106
+ *
107
+ * return () => now.current;
108
+ * });
109
+ *
110
+ * const Stopwatch = resource(({ use }) => {
111
+ * let time = use(Now);
112
+ *
113
+ * return () => format(time);
114
+ * });
115
+ * ```
116
+ */
117
+ use : < Value > ( resource : Value ) => Reactive < Value extends Reactive < any > ? Value [ 'current' ] : Value > ;
118
+ /**
119
+ * The Application owner.
120
+ * This allows for direct access to traditional ember services.
121
+ *
122
+ * Example:
123
+ * ```js
124
+ * resource(({ owner }) => {
125
+ * owner.lookup('service:router').currentRouteName
126
+ * //...
127
+ * }
128
+ * ```
129
+ */
130
+ owner : Owner ;
131
+ } ;
132
+
133
+ /**
134
+ * Type of the callback passed to `resource`
135
+ */
136
+ export type ResourceFunction < Value = unknown > = ( hooks : ResourceAPI ) => Value | ( ( ) => Value ) ;
137
+
138
+ /**
139
+ * The perceived return value of `resource`
140
+ * This is a lie to TypeScript, because the effective value of
141
+ * of the resource is the result of the collapsed functions
142
+ * passed to `resource`
143
+ */
144
+ export type ResourceFn < Value = unknown > = ( hooks : ResourceAPI ) => Value ;
145
+
146
+ export type Destructor = ( ) => void ;
147
+ export type Cache = object ;
0 commit comments