Skip to content

Commit 55c6cc1

Browse files
Merge pull request #1158 from NullVoxPopuli/cleaning
Cleaning
2 parents 60d391e + 118e4c5 commit 55c6cc1

File tree

13 files changed

+152
-154
lines changed

13 files changed

+152
-154
lines changed

ember-resources/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,6 @@
123123
"main": "addon-main.cjs",
124124
"app-js": {}
125125
},
126-
"packageManager": "pnpm@8.15.9",
127126
"volta": {
128127
"extends": "../package.json"
129128
}

ember-resources/src/cell.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,9 @@ export function cell<Value = unknown>(initialValue?: Value): Cell<Value> {
142142
// @ts-ignore
143143
import { capabilities as helperCapabilities, setHelperManager } from '@ember/helper';
144144

145-
import { CURRENT } from './function-based/types.ts';
145+
import { CURRENT } from './types.ts';
146146

147-
import type { GlintRenderable, Reactive } from './function-based/types.ts';
147+
import type { GlintRenderable, Reactive } from './types.ts';
148148

149149
class CellManager {
150150
capabilities = helperCapabilities('3.23', {

ember-resources/src/function-based/index.ts

-3
This file was deleted.

ember-resources/src/function-based/types.ts

-137
This file was deleted.

ember-resources/src/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Public API
22
export { cell } from './cell.ts';
3-
export { resource, resourceFactory } from './function-based/index.ts';
3+
export { resourceFactory } from './immediate-invocation-manager.ts';
4+
export { resource } from './resource.ts';
45
export { registerUsable, use } from './use.ts';
56

67
// Public Type Utilities
7-
export type { ResourceAPI } from './function-based/index.ts';
8-
export type { Reactive } from './function-based/types.ts';
8+
export type { Reactive, ResourceAPI } from './types.ts';

ember-resources/src/function-based/manager.ts ember-resources/src/resource-manager.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { invokeHelper } from '@ember/helper';
88
import { capabilities as helperCapabilities } from '@ember/helper';
99
import { dependencySatisfies, importSync, macroCondition } from '@embroider/macros';
1010

11-
import { ReadonlyCell } from '../cell.ts';
11+
import { ReadonlyCell } from './cell.ts';
1212
import { CURRENT, INTERNAL } from './types.ts';
1313

1414
import type {

ember-resources/src/function-based/resource.ts ember-resources/src/resource.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { assert } from '@ember/debug';
22
// @ts-ignore
33
import { invokeHelper, setHelperManager } from '@ember/helper';
44

5-
import { registerUsable } from '../use.ts';
6-
import { ResourceManagerFactory } from './manager.ts';
5+
import { ResourceManagerFactory } from './resource-manager.ts';
76
import { INTERNAL } from './types.ts';
7+
import { registerUsable } from './use.ts';
88
import { wrapForPlainUsage } from './utils.ts';
99

1010
import type { InternalFunctionResourceConfig, ResourceFn, ResourceFunction } from './types.ts';

ember-resources/src/types.ts

+138
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import type Owner from '@ember/owner';
2+
import type { Invoke } from '@glint/template/-private/integration';
3+
14
export interface Stage1DecoratorDescriptor {
25
initializer: () => unknown;
36
}
@@ -7,3 +10,138 @@ export type Stage1Decorator = (
710
key: string | symbol,
811
descriptor?: Stage1DecoratorDescriptor,
912
) => 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;

ember-resources/src/use.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ import { invokeHelper } from '@ember/helper';
1010

1111
import { ReadonlyCell } from './cell.ts';
1212

13-
import type { INTERNAL } from './function-based/types.ts';
14-
import type { InternalFunctionResourceConfig, Reactive } from './function-based/types.ts';
15-
import type { Stage1DecoratorDescriptor } from './types.ts';
13+
import type {
14+
INTERNAL,
15+
InternalFunctionResourceConfig,
16+
Reactive,
17+
Stage1DecoratorDescriptor,
18+
} from './types.ts';
1619

1720
type Config =
1821
| { [INTERNAL]: true; type: string; definition: unknown }
File renamed without changes.

test-app-definitely-typed/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@
102102
"ember": {
103103
"edition": "octane"
104104
},
105-
"packageManager": "pnpm@8.15.9",
106105
"volta": {
107106
"extends": "../package.json"
108107
},

test-app/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@
121121
"ember": {
122122
"edition": "octane"
123123
},
124-
"packageManager": "pnpm@7.1.2",
125124
"volta": {
126125
"extends": "../package.json"
127126
}

0 commit comments

Comments
 (0)