Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleaning #1158

Merged
merged 2 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion ember-resources/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@
"main": "addon-main.cjs",
"app-js": {}
},
"packageManager": "[email protected]",
"volta": {
"extends": "../package.json"
}
Expand Down
4 changes: 2 additions & 2 deletions ember-resources/src/cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ export function cell<Value = unknown>(initialValue?: Value): Cell<Value> {
// @ts-ignore
import { capabilities as helperCapabilities, setHelperManager } from '@ember/helper';

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

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

class CellManager {
capabilities = helperCapabilities('3.23', {
Expand Down
3 changes: 0 additions & 3 deletions ember-resources/src/function-based/index.ts

This file was deleted.

137 changes: 0 additions & 137 deletions ember-resources/src/function-based/types.ts

This file was deleted.

6 changes: 3 additions & 3 deletions ember-resources/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Public API
export { cell } from './cell.ts';
export { resource, resourceFactory } from './function-based/index.ts';
export { resourceFactory } from './immediate-invocation-manager.ts';
export { resource } from './resource.ts';
export { registerUsable, use } from './use.ts';

// Public Type Utilities
export type { ResourceAPI } from './function-based/index.ts';
export type { Reactive } from './function-based/types.ts';
export type { Reactive, ResourceAPI } from './types.ts';
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { invokeHelper } from '@ember/helper';
import { capabilities as helperCapabilities } from '@ember/helper';
import { dependencySatisfies, importSync, macroCondition } from '@embroider/macros';

import { ReadonlyCell } from '../cell.ts';
import { ReadonlyCell } from './cell.ts';
import { CURRENT, INTERNAL } from './types.ts';

import type {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { assert } from '@ember/debug';
// @ts-ignore
import { invokeHelper, setHelperManager } from '@ember/helper';

import { registerUsable } from '../use.ts';
import { ResourceManagerFactory } from './manager.ts';
import { ResourceManagerFactory } from './resource-manager.ts';
import { INTERNAL } from './types.ts';
import { registerUsable } from './use.ts';
import { wrapForPlainUsage } from './utils.ts';

import type { InternalFunctionResourceConfig, ResourceFn, ResourceFunction } from './types.ts';
Expand Down
138 changes: 138 additions & 0 deletions ember-resources/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import type Owner from '@ember/owner';
import type { Invoke } from '@glint/template/-private/integration';

export interface Stage1DecoratorDescriptor {
initializer: () => unknown;
}
Expand All @@ -7,3 +10,138 @@ export type Stage1Decorator = (
key: string | symbol,
descriptor?: Stage1DecoratorDescriptor,
) => any;

export const INTERMEDIATE_VALUE = '__Intermediate_Value__';
export const INTERNAL = '__INTERNAL__';

export interface InternalFunctionResourceConfig<Value = unknown> {
definition: ResourceFunction<Value>;
type: 'function-based';
name: string;
[INTERNAL]: true;
}

export const CURRENT = Symbol('ember-resources::CURRENT') as unknown as 'CURRENT';

export interface GlintRenderable {
/**
* Cells aren't inherently understood by Glint,
* so to work around that, we'll hook in to the fact that
* ContentValue (the type expected for all renderables),
* defines an interface with this signature.
*
* (SafeString)
*
* There *has* been interest in the community to formally support
* toString and toHTML APIs across all objects. An RFC needs to be
* written so that we can gather feedback / potential problems.
*/
toHTML(): string;
}

// Will need to be a class for .current flattening / auto-rendering
export interface Reactive<Value> extends GlintRenderable {
current: Value;
[CURRENT]: Value;
[Invoke]?: Value;
}

/**
* This is the type of the arguments passed to the `resource` function
*
* ```ts
* import { resource, type ResourceAPI } from 'ember-resources';
*
* export const Clock = resource((api: ResourceAPI) => {
* let { on, use, owner } = api;
*
* // ...
* })
* ```
*/
export type ResourceAPI = {
on: {
/**
* Optionally a function-resource can provide a cleanup function.
*
*
* Example:
* ```js
* import { resource } from 'ember-resources';
* import { TrackedObject } from 'tracked-built-ins';
*
* const load = resource(({ on }) => {
* let state = new TrackedObject({});
* let controller = new AbortController();
*
* on.cleanup(() => controller.abort());
*
* fetch(this.url, { signal: controller.signal })
* // ...
*
* return state;
* })
*/
cleanup: (destroyer: Destructor) => void;
};

/**
* Allows for composition of resources.
*
* Example:
* ```js
* let formatter = new Intl.DateTimeFormat("en-US", {
* hour: "numeric",
* minute: "numeric",
* second: "numeric",
* hour12: false,
* });
* let format = (time: Reactive<Date>) => formatter.format(time.current);
*
* const Now = resource(({ on }) => {
* let now = cell(nowDate);
* let timer = setInterval(() => now.set(Date.now()), 1000);
*
* on.cleanup(() => clearInterval(timer));
*
* return () => now.current;
* });
*
* const Stopwatch = resource(({ use }) => {
* let time = use(Now);
*
* return () => format(time);
* });
* ```
*/
use: <Value>(resource: Value) => Reactive<Value extends Reactive<any> ? Value['current'] : Value>;
/**
* The Application owner.
* This allows for direct access to traditional ember services.
*
* Example:
* ```js
* resource(({ owner }) => {
* owner.lookup('service:router').currentRouteName
* //...
* }
* ```
*/
owner: Owner;
};

/**
* Type of the callback passed to `resource`
*/
export type ResourceFunction<Value = unknown> = (hooks: ResourceAPI) => Value | (() => Value);

/**
* The perceived return value of `resource`
* This is a lie to TypeScript, because the effective value of
* of the resource is the result of the collapsed functions
* passed to `resource`
*/
export type ResourceFn<Value = unknown> = (hooks: ResourceAPI) => Value;

export type Destructor = () => void;
export type Cache = object;
9 changes: 6 additions & 3 deletions ember-resources/src/use.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ import { invokeHelper } from '@ember/helper';

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

import type { INTERNAL } from './function-based/types.ts';
import type { InternalFunctionResourceConfig, Reactive } from './function-based/types.ts';
import type { Stage1DecoratorDescriptor } from './types.ts';
import type {
INTERNAL,
InternalFunctionResourceConfig,
Reactive,
Stage1DecoratorDescriptor,
} from './types.ts';

type Config =
| { [INTERNAL]: true; type: string; definition: unknown }
Expand Down
1 change: 0 additions & 1 deletion test-app-definitely-typed/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@
"ember": {
"edition": "octane"
},
"packageManager": "[email protected]",
"volta": {
"extends": "../package.json"
},
Expand Down
1 change: 0 additions & 1 deletion test-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@
"ember": {
"edition": "octane"
},
"packageManager": "[email protected]",
"volta": {
"extends": "../package.json"
}
Expand Down
Loading