From 481a91956bfc820494dd8cdf2226e9c26b5e671f Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Fri, 11 Oct 2024 12:48:06 -0400 Subject: [PATCH 1/2] Only have one packageManager field --- ember-resources/package.json | 1 - test-app-definitely-typed/package.json | 1 - test-app/package.json | 1 - 3 files changed, 3 deletions(-) diff --git a/ember-resources/package.json b/ember-resources/package.json index d9cd9c99..2c548e25 100644 --- a/ember-resources/package.json +++ b/ember-resources/package.json @@ -123,7 +123,6 @@ "main": "addon-main.cjs", "app-js": {} }, - "packageManager": "pnpm@8.15.9", "volta": { "extends": "../package.json" } diff --git a/test-app-definitely-typed/package.json b/test-app-definitely-typed/package.json index 78a43cef..4bae1c28 100644 --- a/test-app-definitely-typed/package.json +++ b/test-app-definitely-typed/package.json @@ -102,7 +102,6 @@ "ember": { "edition": "octane" }, - "packageManager": "pnpm@8.15.9", "volta": { "extends": "../package.json" }, diff --git a/test-app/package.json b/test-app/package.json index 9d354951..56968fab 100644 --- a/test-app/package.json +++ b/test-app/package.json @@ -121,7 +121,6 @@ "ember": { "edition": "octane" }, - "packageManager": "pnpm@7.1.2", "volta": { "extends": "../package.json" } From 118e4c5f48158f84f8a169d1ce780c6a28178ae3 Mon Sep 17 00:00:00 2001 From: NullVoxPopuli <199018+NullVoxPopuli@users.noreply.github.com> Date: Fri, 11 Oct 2024 12:53:12 -0400 Subject: [PATCH 2/2] Flatten files (except for the type-tests --- ember-resources/src/cell.ts | 4 +- ember-resources/src/function-based/index.ts | 3 - ember-resources/src/function-based/types.ts | 137 ----------------- ...ion.ts => immediate-invocation-manager.ts} | 0 ember-resources/src/index.ts | 6 +- .../manager.ts => resource-manager.ts} | 2 +- .../src/{function-based => }/resource.ts | 4 +- ember-resources/src/types.ts | 138 ++++++++++++++++++ ember-resources/src/use.ts | 9 +- .../src/{function-based => }/utils.ts | 0 10 files changed, 152 insertions(+), 151 deletions(-) delete mode 100644 ember-resources/src/function-based/index.ts delete mode 100644 ember-resources/src/function-based/types.ts rename ember-resources/src/{function-based/immediate-invocation.ts => immediate-invocation-manager.ts} (100%) rename ember-resources/src/{function-based/manager.ts => resource-manager.ts} (99%) rename ember-resources/src/{function-based => }/resource.ts (98%) rename ember-resources/src/{function-based => }/utils.ts (100%) diff --git a/ember-resources/src/cell.ts b/ember-resources/src/cell.ts index c207a506..7aec2afd 100644 --- a/ember-resources/src/cell.ts +++ b/ember-resources/src/cell.ts @@ -142,9 +142,9 @@ export function cell(initialValue?: Value): Cell { // @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', { diff --git a/ember-resources/src/function-based/index.ts b/ember-resources/src/function-based/index.ts deleted file mode 100644 index f637795a..00000000 --- a/ember-resources/src/function-based/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -export { resourceFactory } from './immediate-invocation.ts'; -export { resource } from './resource.ts'; -export type { ResourceAPI } from './types.ts'; diff --git a/ember-resources/src/function-based/types.ts b/ember-resources/src/function-based/types.ts deleted file mode 100644 index a026dc38..00000000 --- a/ember-resources/src/function-based/types.ts +++ /dev/null @@ -1,137 +0,0 @@ -import type Owner from '@ember/owner'; -import type { Invoke } from '@glint/template/-private/integration'; - -export const INTERMEDIATE_VALUE = '__Intermediate_Value__'; -export const INTERNAL = '__INTERNAL__'; - -export interface InternalFunctionResourceConfig { - definition: ResourceFunction; - 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 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) => 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: (resource: Value) => Reactive ? 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 = (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 = (hooks: ResourceAPI) => Value; - -export type Destructor = () => void; -export type Cache = object; diff --git a/ember-resources/src/function-based/immediate-invocation.ts b/ember-resources/src/immediate-invocation-manager.ts similarity index 100% rename from ember-resources/src/function-based/immediate-invocation.ts rename to ember-resources/src/immediate-invocation-manager.ts diff --git a/ember-resources/src/index.ts b/ember-resources/src/index.ts index 4e71efdb..88e04fe1 100644 --- a/ember-resources/src/index.ts +++ b/ember-resources/src/index.ts @@ -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'; diff --git a/ember-resources/src/function-based/manager.ts b/ember-resources/src/resource-manager.ts similarity index 99% rename from ember-resources/src/function-based/manager.ts rename to ember-resources/src/resource-manager.ts index 9e9cc82d..c9d91bcc 100644 --- a/ember-resources/src/function-based/manager.ts +++ b/ember-resources/src/resource-manager.ts @@ -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 { diff --git a/ember-resources/src/function-based/resource.ts b/ember-resources/src/resource.ts similarity index 98% rename from ember-resources/src/function-based/resource.ts rename to ember-resources/src/resource.ts index a1c94318..b26310eb 100644 --- a/ember-resources/src/function-based/resource.ts +++ b/ember-resources/src/resource.ts @@ -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'; diff --git a/ember-resources/src/types.ts b/ember-resources/src/types.ts index 037c6d4e..3f38a7c6 100644 --- a/ember-resources/src/types.ts +++ b/ember-resources/src/types.ts @@ -1,3 +1,6 @@ +import type Owner from '@ember/owner'; +import type { Invoke } from '@glint/template/-private/integration'; + export interface Stage1DecoratorDescriptor { initializer: () => unknown; } @@ -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 { + definition: ResourceFunction; + 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 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) => 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: (resource: Value) => Reactive ? 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 = (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 = (hooks: ResourceAPI) => Value; + +export type Destructor = () => void; +export type Cache = object; diff --git a/ember-resources/src/use.ts b/ember-resources/src/use.ts index 59687ed5..a8c11a1a 100644 --- a/ember-resources/src/use.ts +++ b/ember-resources/src/use.ts @@ -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 } diff --git a/ember-resources/src/function-based/utils.ts b/ember-resources/src/utils.ts similarity index 100% rename from ember-resources/src/function-based/utils.ts rename to ember-resources/src/utils.ts