Type Alias ResourceAPI

ResourceAPI: {
    on: {
        cleanup: ((destroyer: Destructor) => void);
    };
    owner: Owner;
    use: (<Value>(resource: Value) => Reactive<Value extends Reactive<any>
        ? Value["current"]
        : Value>);
}

This is the type of the arguments passed to the resource function

import { resource, type ResourceAPI } from 'ember-resources';

export const Clock = resource((api: ResourceAPI) => {
let { on, use, owner } = api;

// ...
})

Type declaration

  • on: {
        cleanup: ((destroyer: Destructor) => void);
    }
    • cleanup: ((destroyer: Destructor) => void)

      Optionally a function-resource can provide a cleanup function.

      Example:

      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;
        • (destroyer): void
        • Parameters

          • destroyer: Destructor

          Returns void

  • owner: Owner

    The Application owner. This allows for direct access to traditional ember services.

    Example:

    resource(({ owner }) => {
    owner.lookup('service:router').currentRouteName
    //...
    }
  • use: (<Value>(resource: Value) => Reactive<Value extends Reactive<any>
        ? Value["current"]
        : Value>)

    Allows for composition of resources.

    Example:

    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);
    });