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

Make types more consistent #1157

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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: 1 addition & 0 deletions ember-resources/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"start": "pnpm vite build --watch",
"build": "pnpm vite build",
"lint": "concurrently 'npm:lint:*(!fix)' --names 'lint:'",
"lint:types": "tsc --noEmit",
"lint:fix": "concurrently 'npm:lint:*:fix' --names 'fix:'",
"lint:js": "eslint . --cache",
"lint:prettier": "prettier --check '**/*.{js,ts}'",
Expand Down
2 changes: 1 addition & 1 deletion ember-resources/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ export { resource } from './resource.ts';
export { registerUsable, use } from './use.ts';

// Public Type Utilities
export type { Reactive, ResourceAPI } from './types.ts';
export type { Reactive, Resource, ResourceAPI } from './types.ts';
10 changes: 5 additions & 5 deletions ember-resources/src/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { INTERNAL } from './types.ts';
import { registerUsable } from './use.ts';
import { wrapForPlainUsage } from './utils.ts';

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

const TYPE = 'function-based';

Expand Down Expand Up @@ -111,7 +111,7 @@ registerUsable(TYPE, (context: object, config: InternalFunctionResourceConfig) =
* </template>
* ```
*/
export function resource<Value>(setup: ResourceFunction<Value>): Value;
export function resource<Value>(setup: ResourceFunction<Value>): Resource<Value>;

/**
* `resource` is an alternative API to the class-based `Resource`.
Expand Down Expand Up @@ -167,14 +167,14 @@ export function resource<Value>(setup: ResourceFunction<Value>): Value;
* }
* ```
*/
export function resource<Value>(context: object, setup: ResourceFunction<Value>): Value;
export function resource<Value>(context: object, setup: ResourceFunction<Value>): Resource<Value>;

/**
*/
export function resource<Value>(
context: object | ResourceFunction<Value>,
setup?: ResourceFunction<Value>,
): Value | InternalFunctionResourceConfig<Value> | ResourceFn<Value> {
): Value | InternalFunctionResourceConfig<Value> | Resource<Value> {
if (!setup) {
assert(
`When using \`resource\` with @use, ` +
Expand Down Expand Up @@ -205,7 +205,7 @@ export function resource<Value>(
* using vanilla functions as resources without the resource wrapper
*
*/
return internalConfig as unknown as ResourceFn<Value>;
return internalConfig as unknown as Resource<Value>;
}

assert(
Expand Down
5 changes: 4 additions & 1 deletion ember-resources/src/type-tests/use.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { expectTypeOf } from 'expect-type';

import { cell, type Reactive, resource, resourceFactory, use } from '../index.ts';
import { cell, resource, resourceFactory, use } from '../index.ts';

import type { Reactive } from '../index.ts';

const StuckClock = resource(() => 2);

Expand Down Expand Up @@ -36,6 +38,7 @@ class DemoA {
let demoA = new DemoA();

expectTypeOf<typeof demoA.stuck>().toMatchTypeOf<number>();
expectTypeOf(demoA.clock).toMatchTypeOf<Reactive<number>>();
expectTypeOf<typeof demoA.clock>().toMatchTypeOf<Reactive<number>>();
expectTypeOf<typeof demoA.paramClock>().toMatchTypeOf<string>();
expectTypeOf<typeof demoA.paramClock2>().toMatchTypeOf<string>();
Expand Down
2 changes: 2 additions & 0 deletions ember-resources/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,7 @@ export type ResourceFunction<Value = unknown> = (hooks: ResourceAPI) => Value |
*/
export type ResourceFn<Value = unknown> = (hooks: ResourceAPI) => Value;

export type Resource<Value = unknown> = Value | Reactive<Value>;

export type Destructor = () => void;
export type Cache = object;
22 changes: 22 additions & 0 deletions test-app/app/components/implicit-current.gts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { cell,resource } from 'ember-resources';

export const Now = resource(({ on }) => {
const now = cell(Date.now());

const timer = setInterval(() => {
now.set(Date.now());
});

on.cleanup(() => {
clearInterval(timer);
});

return now;
});


<template>
It is: <time>{{Now}}</time>

It is: <time>{{Now.current}}</time>
</template>
1 change: 1 addition & 0 deletions test-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
},
"scripts": {
"lint": "concurrently 'npm:lint:*(!fix)' --names 'lint:'",
"lint:types": "glint",
"lint:fix": "concurrently 'npm:lint:*:fix' --names 'fix:'",
"lint:hbs": "ember-template-lint . --no-error-on-unmatched-pattern",
"lint:js": "eslint . --cache",
Expand Down
38 changes: 38 additions & 0 deletions test-app/tests/core/implicit-current-test.gts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { on } from '@ember/modifier';
import { click, render, settled } from '@ember/test-helpers';
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';

import { cell, resource, resourceFactory } from 'ember-resources';


module('.current and implicit .current', function (hooks) {
setupRenderingTest(hooks);

test('A resource has a current property', async function (assert) {
let change = () => 0;
const Now = resource(() => {
const now = cell(0);

change = () => now.current++;

return now;
});

await render(
<template>
<out id="implicit">{{Now}}</out>
<out id="explicit">{{Now.current}}</out>
</template>
);

assert.dom('#implicit').hasText('0');
assert.dom('explicit').hasText('0');

change();
await settled();

assert.dom('#implicit').hasText('1');
assert.dom('explicit').hasText('1');
});
});
6 changes: 5 additions & 1 deletion test-app/tests/type-tests/function-based.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { resource } from 'ember-resources';
import { type Resource, resource } from 'ember-resources';
import { expectTypeOf } from 'expect-type';
import { expectType } from 'ts-expect';

/*
Expand All @@ -13,3 +14,6 @@ import { expectType } from 'ts-expect';
*/
expectType<string>(resource(() => 'hi'));
expectType<string>(resource(() => () => 'hi'));

expectTypeOf(resource(() => 'hi')).toMatchTypeOf<string>();
expectTypeOf(resource(() => 'hi')).toMatchTypeOf<Resource<string>>();
5 changes: 5 additions & 0 deletions test-app/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
"include": ["app/**/*", "tests/**/*", "type-tests/**/*", "types/**/*"],
"compilerOptions": {
"baseUrl": ".",
"skipLibCheck": true,
"noEmit": true,
"declaration": false,
"declarationMap": false,
"emitDeclarationOnly": false,
"paths": {
"test-app/tests/*": ["tests/*"],
"test-app/*": ["app/*"],
Expand Down
Loading