Skip to content

Commit 4bbaf83

Browse files
committed
[react] Pivot API
1 parent f1ef906 commit 4bbaf83

File tree

5 files changed

+96
-95
lines changed

5 files changed

+96
-95
lines changed

src/@types/ui-react/docs.js

Lines changed: 37 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,38 @@
33
* components to make it easy to use TinyTick in a React application.
44
*
55
* The hooks in this module primarily provide access to the data and structures
6-
* exposed by an TinyTick Manager, as initialized by the ManagerProvider
7-
* component.
6+
* exposed by an TinyTick Manager, as initialized by the Provider component.
87
* @packageDocumentation
98
* @module ui-react
109
* @since v1.1.0
1110
*/
1211
/// ui-react
1312

1413
/**
15-
* The useManager hook returns the Manager provided by the a ManagerProvider
16-
* component.
14+
* The useManager hook returns the Manager provided by the a Provider component.
1715
* @returns The current Manager, or `undefined` if called from outside an active
18-
* ManagerProvider.
16+
* Provider.
1917
* @example
20-
* This example gets the Manager from the ManagerProvider component.
18+
* This example gets the Manager from the Provider component.
2119
*
2220
* ```jsx
2321
* import React from 'react';
2422
* import {createRoot} from 'react-dom/client';
25-
* import {ManagerProvider, useManager} from 'tinytick/ui-react';
23+
* import {Provider, useManager} from 'tinytick/ui-react';
2624
*
2725
* const App = () => (
28-
* <ManagerProvider>
26+
* <Provider>
2927
* <Pane />
30-
* </ManagerProvider>
28+
* </Provider>
3129
* );
3230
* const Pane = () => (
33-
* <span>{useManager().getNow()}</span>
31+
* <span>{useManager()?.getStatus()}</span>
3432
* );
3533
*
3634
* const app = document.createElement('div');
3735
* createRoot(app).render(<App />); // !act
3836
* console.log(app.innerHTML);
39-
* // -> '<span>["color"]</span>'
37+
* // -> '<span>1</span>'
4038
*
4139
* ```
4240
* @category Lifecycle hooks
@@ -54,7 +52,7 @@
5452
* abort.
5553
* @param config The ManagerConfig to set.
5654
* @returns A reference to the Manager, or `undefined` if called from outside an
57-
* active ManagerProvider.
55+
* active Provider.
5856
* @example
5957
* This example creates a Manager object and sets its configuration.
6058
*
@@ -97,7 +95,7 @@
9795
* @param withDefaults An optional boolean indicating whether to return the full
9896
* configuration, including defaults.
9997
* @returns The configuration as a ManagerConfig or ManagerConfigWithDefaults,
100-
* or `undefined` if called from outside an active ManagerProvider.
98+
* or `undefined` if called from outside an active Provider.
10199
* @example
102100
* This example creates a Manager object and gets its default configuration.
103101
* No additional configuration has been provided, so when the `withDefaults`
@@ -130,7 +128,7 @@
130128
* @param categoryId The Id of the category to create or update.
131129
* @param config The TaskRunConfig to set.
132130
* @returns A reference to the Manager, or `undefined` if called from outside an
133-
* active ManagerProvider.
131+
* active Provider.
134132
* @example
135133
* This example creates a category called `network` with a specific maximum
136134
* duration.
@@ -180,7 +178,7 @@
180178
* configuration, including defaults.
181179
* @returns The configuration as a TaskRunConfig (or `undefined` if the category
182180
* Id does not exist) or TaskRunConfigWithDefaults, or `undefined` if called
183-
* from outside an active ManagerProvider.
181+
* from outside an active Provider.
184182
* @example
185183
* This example creates a category called `network` with a specific maximum
186184
* duration. Its configuration can be accessed with or without the defaults
@@ -219,7 +217,7 @@
219217
* The useGetCategoryIds hook is the equivalent of the Manager's getCategoryIds
220218
* method, and returns an array containing all registered category Ids.
221219
* @returns An array of category Ids, or `undefined` if called from outside an
222-
* active ManagerProvider.
220+
* active Provider.
223221
* @example
224222
* This example creates categories called `network` and `file`. Their Ids are
225223
* retrieved.
@@ -243,7 +241,7 @@
243241
* method, and deletes a category configuration.
244242
* @param categoryId The Id of the category to delete.
245243
* @returns A reference to the Manager, or `undefined` if called from outside an
246-
* active ManagerProvider.
244+
* active Provider.
247245
* @example
248246
* This example creates a category called `network` which is then deleted.
249247
*
@@ -282,7 +280,7 @@
282280
* @param categoryId The optional Id of a category to associate the task with.
283281
* @param config An optional TaskRunConfig to set for all runs of this Task.
284282
* @returns A reference to the Manager, or `undefined` if called from outside an
285-
* active ManagerProvider.
283+
* active Provider.
286284
* @example
287285
* This example registers a task called `ping` that fetches content from a
288286
* website.
@@ -351,7 +349,7 @@
351349
* configuration, including defaults.
352350
* @returns The configuration as a TaskRunConfig (or `undefined` if the Task Id
353351
* does not exist) or TaskRunConfigWithDefaults, or `undefined` if called from
354-
* outside an active ManagerProvider.
352+
* outside an active Provider.
355353
* @example
356354
* This example creates a category, and registers a task for which the
357355
* configuration is returned.
@@ -393,7 +391,7 @@
393391
* The useGetTaskIds hook is the equivalent of the Manager's getTaskIds method,
394392
* and returns an array containing all registered task Ids.
395393
* @returns An array of task Ids, or `undefined` if called from outside an
396-
* active ManagerProvider.
394+
* active Provider.
397395
* @example
398396
* This example creates tasks called `ping` and `pong`. Their Ids are
399397
* retrieved.
@@ -417,7 +415,7 @@
417415
* deletes a task registration.
418416
* @param taskId The Id of the task to delete.
419417
* @returns A reference to the Manager, or `undefined` if called from outside an
420-
* active ManagerProvider.
418+
* active Provider.
421419
* @example
422420
* This example creates a task called `ping` which is then deleted.
423421
*
@@ -465,7 +463,7 @@
465463
* run.
466464
* @param config An optional TaskRunConfig to set for this run.
467465
* @returns A new unique Id of the scheduled task run, or `undefined` if called
468-
* from outside an active ManagerProvider.
466+
* from outside an active Provider.
469467
* @example
470468
* This example registers a task that is then scheduled to run.
471469
*
@@ -538,7 +536,7 @@
538536
* configuration including defaults.
539537
* @returns The configuration as a TaskRunConfig (or `undefined` if the task run
540538
* Id does not exist) or TaskRunConfigWithDefaults, or `undefined` if called
541-
* from outside an active ManagerProvider.
539+
* from outside an active Provider.
542540
* @example
543541
* This example registers a task that has a category and that is then
544542
* scheduled to run. The configuration is then returned.
@@ -586,8 +584,7 @@
586584
* If the task run Id does not exist, this method will return `undefined`.
587585
* @param taskRunId The Id of the task run to get information for.
588586
* @returns The TaskRunInfo for the task run, or `undefined` if the task run Id
589-
* does not exist, or `undefined` if called from outside an active
590-
* ManagerProvider.
587+
* does not exist, or `undefined` if called from outside an active Provider.
591588
* @example
592589
* This example registers a task that is then scheduled to run. The info is
593590
* then returned.
@@ -630,7 +627,7 @@
630627
* and deletes a scheduled task run or aborts a running one.
631628
* @param taskRunId The Id of the task run to delete or abort.
632629
* @returns A reference to the Manager, or `undefined` if called from outside an
633-
* active ManagerProvider.
630+
* active Provider.
634631
* @example
635632
* This example registers a task that is then scheduled to run. The task run
636633
* is then deleted.
@@ -662,7 +659,7 @@
662659
* running, it will disappear from this list and appear on the list of running
663660
* task runs, accessible instead with the getRunningTaskRunIds method.
664661
* @returns An array of task run Ids, or `undefined` if called from outside an
665-
* active ManagerProvider.
662+
* active Provider.
666663
* @example
667664
* This example registers a task that is then scheduled to run twice.
668665
*
@@ -696,7 +693,7 @@
696693
* getScheduledTaskRunIds method. Once it starts running, it will instead move
697694
* to appear on this list.
698695
* @returns An array of task run Ids, or `undefined` if called from outside an
699-
* active ManagerProvider.
696+
* active Provider.
700697
* @example
701698
* This example registers a task that is then scheduled to run. Once it runs,
702699
* its Id appears on the list of running tasks.
@@ -746,7 +743,7 @@
746743
* expected that you will only start it once at the beginning of your app's
747744
* lifecycle, and then stop it when it closes and you are cleaning up.
748745
* @returns A reference to the Manager, or `undefined` if called from outside an
749-
* active ManagerProvider.
746+
* active Provider.
750747
* @example
751748
* This example registers a task that is then scheduled to run. The Manager is
752749
* then started, and one `tickInterval` later, the task run starts.
@@ -789,7 +786,7 @@
789786
* @param force Whether to stop the Manager immediately instead of waiting for
790787
* all scheduled task runs to complete.
791788
* @returns A reference to the Manager, or `undefined` if called from outside an
792-
* active ManagerProvider.
789+
* active Provider.
793790
* @example
794791
* This example registers a task that is then scheduled to run twice. The
795792
* Manager is started, and one `tickInterval` later, stopped again. With the
@@ -837,7 +834,7 @@
837834
* This returns a simple numeric value that indicates whether the Manager is
838835
* stopped (0), running (1), or stopping (2).
839836
* @returns The status of the Manager, or `undefined` if called from outside an
840-
* active ManagerProvider.
837+
* active Provider.
841838
* @category Lifecycle hooks
842839
* @since v1.1.0
843840
*/
@@ -848,7 +845,7 @@
848845
*
849846
* It is simply an alias for the JavaScript `Date.now` function.
850847
* @returns The current timestamp in milliseconds, or `undefined` if called from
851-
* outside an active ManagerProvider.
848+
* outside an active Provider.
852849
* @example
853850
* This example gets the current time in milliseconds from the Manager.
854851
*
@@ -864,35 +861,34 @@
864861
*/
865862
/// useGetNow
866863
/**
867-
* ManagerProviderProps props are used with the Manager component, so that a
868-
* TinyTick Manager can be passed into the context of an application and used
869-
* throughout.
864+
* ProviderProps props are used with the Manager component, so that a TinyTick
865+
* Manager can be passed into the context of an application and used throughout.
870866
* @category Props
871867
* @since v1.1.0
872868
*/
873-
/// ManagerProviderProps
869+
/// ProviderProps
874870
{
875871
/**
876872
* Whether the Manager should be started on first render, defaulting to
877873
* `true`.
878874
* @category Prop
879875
* @since v1.1.0
880876
*/
881-
/// ManagerProviderProps.started
877+
/// ProviderProps.started
882878
/**
883879
* Whether the Manager should be force-stopped when the context is unmounted,
884880
* defaulting to `true`.
885881
* @category Prop
886882
* @since v1.1.0
887883
*/
888-
/// ManagerProviderProps.forceStop
884+
/// ProviderProps.forceStop
889885
}
890886
/**
891-
* The ManagerProvider component is used to wrap part of an application in a
892-
* context that provides a Manager to be used by hooks and components within.
887+
* The Provider component is used to wrap part of an application in a context
888+
* that provides a Manager to be used by hooks and components within.
893889
* @param props The props for this component.
894890
* @returns A rendering of the child components.
895891
* @category Context components
896892
* @since v1.1.0
897893
*/
898-
/// ManagerProvider
894+
/// Provider

src/@types/ui-react/index.d.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ import type {
1515
TimestampMs,
1616
} from '../index.d.ts';
1717

18+
/// useCreateManager
19+
export function useCreateManager(
20+
create: () => Manager,
21+
createDeps?: React.DependencyList,
22+
): Manager;
23+
1824
/// useManager
1925
export function useManager(): Manager | undefined;
2026

@@ -111,15 +117,13 @@ export function useGetStatus(): ManagerStatus | undefined;
111117
/// useGetNow
112118
export function useGetNow(): TimestampMs | undefined;
113119

114-
/// ManagerProviderProps
115-
export type ManagerProviderProps = {
116-
/// ManagerProviderProps.started
117-
readonly started?: boolean;
118-
/// ManagerProviderProps.forceStop
119-
readonly forceStop?: boolean;
120+
/// ProviderProps
121+
export type ProviderProps = {
122+
/// ProviderProps.manager
123+
readonly manager?: Manager;
120124
};
121125

122-
/// ManagerProvider
123-
export function ManagerProvider(
124-
props: ManagerProviderProps & {children: React.ReactNode},
126+
/// Provider
127+
export function Provider(
128+
props: ProviderProps & {children: React.ReactNode},
125129
): ReactElement<any, any>;

src/ui-react/components.tsx

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,17 @@
1-
import {useEffect, useState, type ReactNode} from 'react';
2-
import type {Manager} from '../@types/index.d.ts';
1+
import {useMemo, type ReactNode} from 'react';
32
import type {
4-
ManagerProvider as ManagerProviderDecl,
5-
ManagerProviderProps,
3+
Provider as ProviderDecl,
4+
ProviderProps,
65
} from '../@types/ui-react/index.d.ts';
7-
import {createManager} from '../index.ts';
86
import {Context} from './context.ts';
97

10-
export const ManagerProvider: typeof ManagerProviderDecl = ({
11-
started = true,
12-
forceStop = true,
8+
export const Provider: typeof ProviderDecl = ({
9+
manager,
1310
children,
14-
}: ManagerProviderProps & {readonly children: ReactNode}): any => {
15-
const [manager, setManager] = useState<Manager | undefined>(undefined);
16-
17-
useEffect(() => {
18-
const manager = createManager()[started ? 'start' : 'stop']();
19-
setManager(manager);
20-
return () => {
21-
manager?.stop(forceStop);
22-
setManager(undefined);
23-
};
24-
}, [started, forceStop]);
25-
26-
return <Context.Provider value={manager}>{children}</Context.Provider>;
11+
}: ProviderProps & {readonly children: ReactNode}): any => {
12+
return (
13+
<Context.Provider value={useMemo(() => [manager], [manager])}>
14+
{children}
15+
</Context.Provider>
16+
);
2717
};

src/ui-react/context.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import {TINYTICK} from '../common/strings.ts';
77

88
const TINYTICK_CONTEXT = TINYTICK + '_uirc';
99

10-
export const Context: React.Context<Manager | undefined> = (GLOBAL as any)[
10+
export type ContextValue = [manager?: Manager];
11+
12+
export const Context: React.Context<ContextValue> = (GLOBAL as any)[
1113
TINYTICK_CONTEXT
1214
]
1315
? /*! istanbul ignore next */
1416
(GLOBAL as any)[TINYTICK_CONTEXT]
15-
: ((GLOBAL as any)[TINYTICK_CONTEXT] = createContext<Manager | undefined>(
16-
undefined,
17-
));
17+
: ((GLOBAL as any)[TINYTICK_CONTEXT] = createContext<ContextValue>([]));

0 commit comments

Comments
 (0)