Phantom Stories Library - v0.0.8
    Preparing search index...

    Class Query<Q>

    Represents a loading state, typically for asynchronous operations.

    Type Parameters

    • Q

    Implements

    Index

    Constructors

    Properties

    params?: Q
    type: Query = ResourceTypes.Query

    Methods

    • Applies a function wrapped in a Resource to the Data value, supporting applicative functor operations. For Data, applies the function if the input is Data; otherwise, returns unchanged.

      Type Parameters

      • A
      • B
      • QVal

      Parameters

      Returns Resource<B, QVal>

      A new Resource with the applied result or the original non-Data/Failure state.

      const fnRes = Resource.Data((x: number) => x + 1, { id: '123' });
      const dataRes = Resource.Data(42, { id: '123' });
      const result = fnRes.ap(dataRes); // Data<43, { id: '123' }>

      const failureFn = Resource.Failure<(x: number) => number>(['fn error']);
      const result2 = failureFn.ap(dataRes); // Failure<number> (propagates failure from function resource)

      const failureVal = Resource.Failure<number>(['val error']);
      const result3 = fnRes.ap(failureVal); // Failure<number> (propagates failure from value resource)
    • Chains a function that returns a Resource, enabling monadic composition. For Data, applies the function; for others, returns unchanged.

      Type Parameters

      • R
      • P

      Parameters

      Returns Resource<R, P>

      The Resource returned by fn (for Data) or the same instance (for others).

      const data = Resource.Data(42, { id: '123' });
      const result = data.chain(d => Resource.Data(d.value * 2, d.params)); // Data<84, { id: '123' }>
    • Returns the Data value or a fallback value for non-Data states.

      Parameters

      • value: any

        Fallback value for non-Data states.

      Returns any

      The Data value or the fallback.

      const data = Resource.Data(42);
      const value = data.getDataOr(0); // 42
      const failure = Resource.Failure(['error']);
      const fallback = failure.getDataOr(0); // 0
    • Transforms the Data value using a function, preserving the Resource structure. Acts as a functor operation, similar to Array.map. Non-Data variants return unchanged.

      Type Parameters

      • R

      Parameters

      • _fn: (x: any) => R

      Returns Query<Q>

      A new Resource with the transformed value (for Data) or the same instance (for others).

      const data = Resource.Data(42, { id: '123' });
      const result = data.map(x => x + 1); // Data<43, { id: '123' }>
    • Safely transforms the Data value, catching errors and returning Failure if the function throws. Useful for error-prone operations (e.g., parsing JSON).

      Type Parameters

      • R

      Parameters

      • _fn: (x: any) => R

      Returns Query<Q>

      A new Data with the transformed value or Failure if an error occurs.

      const data = Resource.Data('{"name": "Jane"}', { id: '123' });
      const result = data.mapSafe(JSON.parse); // Data<{ name: 'Jane' }> or Failure
    • Matches the Resource state with a pattern, returning a value. Enables declarative state handling, similar to a switch statement.

      Type Parameters

      • R

      Parameters

      Returns R

      The result of the matching function.

      const resource = Resource.Data(42, { id: '123' });
      const result = resource.matchWith({
      Data: ({ value }) => value * 2,
      Query: () => 0,
      Empty: () => 0,
      Failure: () => 0,
      }); // 84
    • Matches the Resource state with a partial pattern, performing side effects.

      Parameters

      • pattern: Partial<ResourcePattern<any, Q, void>>

        Partial object with functions for some Resource states.

      Returns void

      Resource.Failure(['error']).matchWithPartial({
      Failure: ({ messages }) => console.log(messages),
      });
    • Lifts a value into a Data Resource, preserving existing params (if any). Acts as the monadic pure operation.

      Type Parameters

      • R

      Parameters

      • value: R

        Value to lift into Data.

      Returns Data<R, Q>

      A new Data Resource with the value and existing params.

      const data = Resource.Data(42, { id: '123' });
      const result = data.of(100); // Data<100, { id: '123' }>
    • Transitions to a Query state with new parameters.

      Type Parameters

      • P

      Parameters

      • params: P

        New parameters for the Query.

      Returns Query<P>

      A new Query Resource.

      const data = Resource.Data(42, { id: '123' });
      const query = data.update({ id: '456' }); // Query<{ id: '456' }>
    • Creates a new Query Resource.

      Type Parameters

      • Q

      Parameters

      • Optionalparams: Q

        Optional parameters (e.g., query params).

      Returns Query<Q>