-
Notifications
You must be signed in to change notification settings - Fork 26
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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" | ||
} |
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>, | ||
); | ||
}); | ||
}); |
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}> | ||
{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; | ||
}; |
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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...
|
||
/** | ||
* 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"; |
There was a problem hiding this comment.
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... :)