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

Refactor: remove the consumer argument from createEmbedSpec #29

Merged
merged 1 commit into from
Jun 7, 2021
Merged
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
1 change: 0 additions & 1 deletion src/__tests__/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export const createNoopEmbed = <
createEmbedSpec(
name,
fieldSpec,
() => () => null,
() => null,
() => null,
{}
Expand Down
18 changes: 8 additions & 10 deletions src/__tests__/mount.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ describe("mount", () => {
createEmbedSpec(
"testEmbed",
fieldSpec,
() => () => null,
(_, __, ___, fieldNodeViews) => {
(_, __, fieldNodeViews) => {
// Prop1 is derived from the fieldSpec
fieldNodeViews.prop1;
},
Expand All @@ -32,8 +31,7 @@ describe("mount", () => {
createEmbedSpec(
"testEmbed",
fieldSpec,
() => () => null,
(_, __, ___, fieldNodeViews) => {
(_, __, fieldNodeViews) => {
// @ts-expect-error – prop1 is not available on this object,
// as it is not defined in `fieldSpec` passed into `mount`
fieldNodeViews.prop1;
Expand All @@ -44,8 +42,8 @@ describe("mount", () => {
});
});

describe("field typesafety", () => {
it("should provide typesafe fields to its consumer", () => {
describe("validator typesafety", () => {
it("should provide typesafe fields to its validator", () => {
const fieldSpec = {
prop1: {
type: "richText",
Expand All @@ -58,14 +56,14 @@ describe("mount", () => {
createEmbedSpec(
"testEmbed",
fieldSpec,
() => () => null,
() => () => undefined,
(fields) => {
// Prop1 is derived from the fieldSpec, and is a string b/c it's a richText field
fields.prop1.toString();
// Prop2 is a boolean b/c it's a checkbox field
fields.prop2.value.valueOf();
return null;
},
() => null,
{ prop1: "text" }
);
});
Expand All @@ -79,13 +77,13 @@ describe("mount", () => {
createEmbedSpec(
"testEmbed",
fieldSpec,
() => () => null,
() => () => undefined,
(fields) => {
// @ts-expect-error – prop1 is not available on this object,
// as it is not defined in `fieldSpec` passed into `mount`
fields.doesNotExist;
return null;
},
() => null,
{ notProp1: "text" }
);
});
Expand Down
9 changes: 2 additions & 7 deletions src/embedSpec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { getNodeSpecFromFieldSpec } from "./nodeSpec";
import type { FieldNameToValueMap } from "./nodeViews/helpers";
import type { TCommandCreator, TCommands } from "./types/Commands";
import type { TConsumer } from "./types/Consumer";
import type {
EmbedSpec,
FieldNameToNodeViewSpec,
Expand Down Expand Up @@ -32,8 +31,7 @@ export type Validator<FSpec extends FieldSpec<string>> = (
fields: FieldNameToValueMap<FSpec>
) => null | Record<string, string[]>;

export type TRenderer<RendererOutput, FSpec extends FieldSpec<string>> = (
consumer: TConsumer<RendererOutput, FSpec>,
export type Renderer<FSpec extends FieldSpec<string>> = (
validate: Validator<FSpec>,
// The HTMLElement representing the node parent. The renderer can mount onto this node.
dom: HTMLElement,
Expand All @@ -52,14 +50,12 @@ export type TRenderer<RendererOutput, FSpec extends FieldSpec<string>> = (
) => void;

export const createEmbedSpec = <
RenderOutput,
FSpec extends FieldSpec<string>,
EmbedName extends string
>(
name: EmbedName,
fieldSpec: FSpec,
render: TRenderer<RenderOutput, FSpec>,
consumer: TConsumer<RenderOutput, FSpec>,
render: Renderer<FSpec>,
validate: Validator<FSpec>,
defaultState: Partial<FieldNameToValueMap<FSpec>>
): EmbedSpec<FSpec, EmbedName> => ({
Expand All @@ -69,7 +65,6 @@ export const createEmbedSpec = <
createUpdator: (dom, fields, updateState, fieldValues, commands) => {
const updater = createUpdater<FSpec>();
render(
consumer,
validate,
dom,
fields,
Expand Down
4 changes: 2 additions & 2 deletions src/renderers/react/EmbedProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React, { Component } from "react";
import type { Validator } from "../../embedSpec";
import type { FieldNameToValueMap } from "../../nodeViews/helpers";
import type { TCommands } from "../../types/Commands";
import type { TConsumer } from "../../types/Consumer";
import type { Consumer } from "../../types/Consumer";
import type { FieldNameToNodeViewSpec, FieldSpec } from "../../types/Embed";
import type { TErrors } from "../../types/Errors";
import { EmbedWrapper } from "./EmbedWrapper";
Expand All @@ -28,7 +28,7 @@ type IProps<FSpec extends FieldSpec<string>> = {
fieldValues: FieldNameToValueMap<FSpec>;
onStateChange: (fields: FieldNameToValueMap<FSpec>) => void;
validate: Validator<FSpec>;
consumer: TConsumer<ReactElement, FSpec>;
consumer: Consumer<ReactElement, FSpec>;
fields: FieldNameToNodeViewSpec<FSpec>;
};

Expand Down
18 changes: 5 additions & 13 deletions src/renderers/react/createReactEmbedSpec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import type { ReactElement } from "react";
import React from "react";
import { render } from "react-dom";
import { createEmbedSpec } from "../../embedSpec";
import type { TRenderer, Validator } from "../../embedSpec";
import type { Renderer, Validator } from "../../embedSpec";
import type { FieldNameToValueMap } from "../../nodeViews/helpers";
import type { TConsumer } from "../../types/Consumer";
import type { Consumer } from "../../types/Consumer";
import type { FieldSpec } from "../../types/Embed";
import { EmbedProvider } from "./EmbedProvider";

Expand All @@ -14,12 +14,11 @@ export const createReactEmbedSpec = <
>(
name: Name,
fieldSpec: FSpec,
consumer: TConsumer<ReactElement, FSpec>,
consumer: Consumer<ReactElement, FSpec>,
validate: Validator<FSpec>,
defaultState: FieldNameToValueMap<FSpec>
) => {
const renderer: TRenderer<ReactElement, FSpec> = (
consumer,
const renderer: Renderer<FSpec> = (
validate,
dom,
fields,
Expand All @@ -41,12 +40,5 @@ export const createReactEmbedSpec = <
dom
);

return createEmbedSpec(
name,
fieldSpec,
renderer,
consumer,
validate,
defaultState
);
return createEmbedSpec(name, fieldSpec, renderer, validate, defaultState);
};
2 changes: 1 addition & 1 deletion src/types/Consumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { FieldNameToValueMap } from "../nodeViews/helpers";
import type { FieldNameToNodeViewSpec, FieldSpec } from "./Embed";
import type { TErrors } from "./Errors";

export type TConsumer<ConsumerResult, FSpec extends FieldSpec<string>> = (
export type Consumer<ConsumerResult, FSpec extends FieldSpec<string>> = (
fieldValues: FieldNameToValueMap<FSpec>,
errors: TErrors,
updateFields: (fieldValues: FieldNameToValueMap<FSpec>) => void,
Expand Down