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

Add Nova Scheduling Provider #79

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions change/@nova-react-03ccc792-9e47-40ce-a3b6-caa9d5f05c1e.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"type": "minor",
"comment": {
"title": "Adds Nova Scheduling Provider"
},
"packageName": "@nova/react",
"email": "[email protected]",
"dependentChangeType": "patch"
}
9 changes: 9 additions & 0 deletions change/@nova-types-6051ab01-fe7c-4df4-9422-1143550c1376.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"type": "minor",
"comment": {
"title": "Adds Nova Scheduling Provider"
},
"packageName": "@nova/types",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* @jest-environment jsdom
*/
import React from "react";
import { render } from "@testing-library/react";
import {
NovaSchedulingProvider,
useNovaScheduling,
} from "./nova-scheduling-provider";
import type { NovaScheduling } from "@nova/types";

describe(useNovaScheduling, () => {
it("throws without a provider", () => {
expect.assertions(1);

const TestUndefinedContextComponent: React.FC = () => {
try {
useNovaScheduling();
} catch (e) {
expect((e as Error).message).toMatch(
"Nova Scheduling provider must be initialized prior to consumption!",
);
}
return null;
};

render(<TestUndefinedContextComponent />);
});

it("is able to access the scheduling instance provided by the provider", () => {
expect.assertions(3);

const scheduling = {
schedule: jest.fn(),
cancel: jest.fn(),
} as unknown as NovaScheduling;

const TestPassedContextComponent: React.FC = () => {
const facadeFromContext = useNovaScheduling();
expect(facadeFromContext).toBe(scheduling);

facadeFromContext.schedule(
() => {
return;
},
{ priority: "background", scheduledById: "test", id: "test" },
);
expect(scheduling.schedule).toBeCalledTimes(1);

facadeFromContext.cancel(1);
expect(scheduling.cancel).toBeCalledTimes(1);

return null;
};

render(
<NovaSchedulingProvider scheduling={scheduling}>
<TestPassedContextComponent />
</NovaSchedulingProvider>,
);
});
});
32 changes: 32 additions & 0 deletions packages/nova-react/src/scheduling/nova-scheduling-provider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React from "react";
import type { NovaScheduling } from "@nova/types";
import invariant from "invariant";

// Initializing default with null to make sure providers are correctly placed in the tree
const NovaSchedulingContext = React.createContext<NovaScheduling | null>(null);

interface NovaSchedulingProviderProps {
scheduling: NovaScheduling;
}

export const NovaSchedulingProvider: React.FunctionComponent<
NovaSchedulingProviderProps
> = ({ children, scheduling }) => {
return (
<NovaSchedulingContext.Provider value={scheduling}>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to add a default value for scheduling to support hosts which simple needs? We don't have any examples in this PR of how we expect a host to hook this, or a default implementation... :)

{children}
</NovaSchedulingContext.Provider>
);
};
NovaSchedulingProvider.displayName = "NovaSchedulingProvider";

export const useNovaScheduling = (): NovaScheduling => {
const scheduling = React.useContext<NovaScheduling | null>(
NovaSchedulingContext,
);
invariant(
scheduling,
"Nova Scheduling provider must be initialized prior to consumption!",
);
return scheduling;
};
1 change: 1 addition & 0 deletions packages/nova-types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from "./nova-commanding.interface";
export * from "./nova-commanding-centralized.interface";
export * from "./nova-graphql.interface";
export * from "./nova-eventing.interface";
export * from "./nova-scheduling.interface";
43 changes: 43 additions & 0 deletions packages/nova-types/src/nova-scheduling.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Describes the scheduling contract UI components and their logical parent within a host application.
* Usage: Components should create an adapter to their scheduler of choice and provide it to the NovaSchedulingProvider.
* This interface out of the box is designed to work with the priorities of the Prioritized Task Scheduling API:
* https://developer.mozilla.org/en-US/docs/Web/API/Prioritized_Task_Scheduling_API#task_priorities
* A scheduler implementation should be able to handle sending tasks to the browser's scheduler, or to a custom scheduler, and then cancelling those tasks as needed.
*/
export interface NovaScheduling {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Take a look at the comments on the other interface files - let's add some inline documentation around what this is and how to use it. At the moment, this is not clear on how a host would fulfill its duty to a component that is using this API.

Without a default implementation, it will also be difficult for component builders to use the API in their development environments - we will need to think through what the simplest path is for someone who wants to support this in Storybook for example...
/**

  • Describes the eventing contract that is used to communicate between UI components and their logical parent within a host application.
  • Usage: components should create small independent packages that define the events they generate using this contract.
  • Logical parents can take a dependency on the event package, and use it to translate the events into appropriate actions within the larger application.
    */

/**
* Schedule a job to be run at some point in the future.
* @param job The job to be run.
* @param meta The metadata associated with the job.
* @returns A job identifier which can be used to cancel the job.
*/
schedule(job: IJob<any>, meta: IJobMetaData): number;
/**
* Cancel a job which has been scheduled.
* @param jobId The identifier of the job to be cancelled.
* @returns void
*/
cancel(jobId: number): void;
}

type IJob<TResult> = (() => Promise<TResult>) | (() => TResult);

export interface IJobMetaData {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we are currently hiding details of the scheduler API:

What are the reasons for not exposing these, and would we consider exposing them in future?
image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The interface should be kept generic to provide scheduling implementation flexibility for consumers.

Operations that aren't currently supported by the API are changing priority and exposing a completion promise. My thought was to keep the API simply at the minimal set of features needed, and then we can add more as required.

/**
* The requested priority of the job.
*/
priority: JobPriority;
/**
* The identifier of the type of job. Note that this is not globally
* unique amongst all instances of a job type.
*/
id: string;
/**
* The identifier of the type of code which scheduled the job. Note that this is
* not globally unique amongst all instances of calling code.
*/
scheduledById: string;
}

export type JobPriority = "background" | "user-visible" | "user-blocking";
Loading