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

Data middleware and resource #672

Merged
merged 19 commits into from
Mar 18, 2020
Merged

Data middleware and resource #672

merged 19 commits into from
Mar 18, 2020

Conversation

tomdye
Copy link
Member

@tomdye tomdye commented Feb 6, 2020

Type: bug / feature

The following has been addressed in the PR:

Description:

Adds a data middleware capable of making widgets data aware and able to deal with a resource.

  • When using the middleware, resource will be added to the widget's properties. This can be used to pass a resource to be interacted with.
  • Provides createDataMiddleware function which takes a generic to determine the type of data the widget requires, this adds a transform function to the widget properties which uses the generic to ensure the correct data type is supplied.

Data middleware API interface:

  • getOrRead(options: ResourceOptions): any;
  • getTotal(options: ResourceOptions): number | undefined;
  • isLoading(options: ResourceOptions): boolean;
  • isFailed(options: ResourceOptions): boolean;
  • isLoading(options: ResourceOptions): boolean;
  • isFailed(options: ResourceOptions): boolean;
  • isLoading(options: ResourceOptions): boolean;
  • isFailed(options: ResourceOptions): boolean;
  • setOptions(newOptions: ResourceOptions): void;
  • getOptions(): ResourceOptions;
  • readonly resource: ResourceWrapper;
  • shared(): ResourceWrapper;

ResourceOptions are made up of:

  • pageNumber?: number
  • pageSize?: number
  • query?: string

When a widget using the middleware is destroyed, the middleware will disconnect from the resource.

The Resource will need to provide the following API

  • getOrRead(options: ResourceOptions: any
  • getTotal(options: ResourceOptions): number | undefined
  • unsubscribe(invalidator: () => void): void
  • subscribe(type: SubscriptionType, options: ResourceOptions, invalidator: () => void): void;
  • isLoading(options: ResourceOptions): boolean;
  • isFailed(options: ResourceOptions): boolean;

Both API

Will work in future with resources.

Resolves #660

Codesandbox of it in use with dummy resource: https://codesandbox.io/s/hardcore-swartz-kezpc

@codesandbox-ci
Copy link

codesandbox-ci bot commented Feb 6, 2020

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit cba69a8:

Sandbox Source
hopeful-thompson-ph8in PR

src/core/middleware/data.ts Outdated Show resolved Hide resolved
src/core/middleware/data.ts Outdated Show resolved Hide resolved
src/core/middleware/data.ts Outdated Show resolved Hide resolved
src/core/middleware/data.ts Outdated Show resolved Hide resolved
src/core/middleware/data.ts Outdated Show resolved Hide resolved
src/core/middleware/data.ts Outdated Show resolved Hide resolved
});
afterEach(() => {
resolvers.restore();
sb.resetHistory();
Copy link
Contributor

Choose a reason for hiding this comment

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

do we use this abbreviation elsewhere? sb?

Copy link
Member Author

Choose a reason for hiding this comment

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

believe so

Copy link
Member Author

Choose a reason for hiding this comment

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

Screenshot 2020-02-07 at 17 00 48

everywhere

@tomdye tomdye force-pushed the data-middleware branch 2 times, most recently from adedc32 to 4595172 Compare February 11, 2020 20:18
@tomdye tomdye changed the title Data middleware Data middleware and resource Feb 24, 2020
@agubler
Copy link
Member

agubler commented Mar 18, 2020

For APIs that do not provide a total the current implementation can be problematic, which means they will become less useful for using externally controlled/managed APIs. For example without a real total, returning a partial last page or an empty array (for APIs where there are no results) causes an infinite loop calling the user resource. It would be good is we could make the total option in some way to support this use case.

Additionally it would be beneficial if the data api could be used without providing pagination details, for example if a user wants to return a single resource.

Here is an implementation of the HNPWA using data/resources that demonstrates these two scenarios https://github.com/agubler/dojo-resource-hnpwa

The resources are defined in App.tsx https://github.com/agubler/dojo-resource-hnpwa/blob/master/src/App.tsx

@tomdye tomdye dismissed matt-gadd’s stale review March 18, 2020 16:47

matt is unavailable

@tomdye tomdye merged commit a160728 into dojo:master Mar 18, 2020
@agubler agubler mentioned this pull request Apr 17, 2020
13 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Data middleware for function based widgets
3 participants