Skip to content

Commit

Permalink
fix(react-image): change Load to return loaded HTMLImageElement (#61)
Browse files Browse the repository at this point in the history
## change Load to return loaded HTMLImageElement

Please check the following:

- [x] I have written documents and tests, if needed.
  • Loading branch information
manudeli authored Dec 20, 2023
1 parent 46b699e commit a9d5fff
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 82 deletions.
5 changes: 5 additions & 0 deletions .changeset/khaki-trees-try.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fepack/react-image": patch
---

fix(react-image): change Load to return loaded HTMLImageElement
5 changes: 5 additions & 0 deletions .changeset/neat-trains-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fepack/image": patch
---

fix(react-image): change Load to return loaded HTMLImageElement
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ jobs:
cache: "pnpm"
cache-dependency-path: "pnpm-lock.yaml"
node-version-file: ".nvmrc"
- if: matrix.command == 'test'
run: pnpx playwright install
- run: pnpm install --frozen-lockfile
- if: matrix.command == 'test'
run: pnpm exec playwright install
- run: pnpm prepack
- run: pnpm ${{ matrix.command }}
2 changes: 1 addition & 1 deletion .github/workflows/code-coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ jobs:
cache: "pnpm"
cache-dependency-path: "pnpm-lock.yaml"
node-version-file: ".nvmrc"
- run: pnpx playwright install
- run: pnpm install --frozen-lockfile
- run: pnpm exec playwright install
- run: pnpm test
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"eslint": "^7.32.0",
"jsdom": "^22.1.0",
"packlint": "^0.2.4",
"playwright": "^1.40.1",
"prettier": "^2.5.1",
"publint": "^0.2.2",
"rimraf": "^5.0.1",
Expand Down
3 changes: 1 addition & 2 deletions packages/image/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@
"type:check": "tsc --noEmit"
},
"devDependencies": {
"@types/node": "^18.16.2",
"playwright": "^1.38.0"
"@types/node": "^18.16.2"
},
"publishConfig": {
"access": "public"
Expand Down
16 changes: 8 additions & 8 deletions packages/image/src/LoadClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { load } from "./load";

export type LoadSrc = Parameters<typeof load>[0];

export type LoadState<TLoadSrc extends LoadSrc> = {
src: TLoadSrc;
export type LoadState = {
image: HTMLImageElement;
promise?: Promise<unknown>;
error?: unknown;
};

type Notify = (...args: unknown[]) => unknown;
export class LoadClient {
private loadCache = new Map<LoadSrc, LoadState<LoadSrc>>();
private loadCache = new Map<LoadSrc, LoadState>();
private notifiesMap = new Map<LoadSrc, Notify[]>();

attach(src: LoadSrc, notify: Notify) {
Expand Down Expand Up @@ -38,17 +38,17 @@ export class LoadClient {
if (loadState?.error) {
throw loadState.error;
}
if (loadState?.src) {
return loadState as LoadState<TLoadSrc>;
if (loadState?.image) {
return loadState.image;
}
if (loadState?.promise) {
throw loadState.promise;
}

const newLoadState: LoadState<TLoadSrc> = {
src,
const newLoadState: LoadState = {
image: undefined as unknown as HTMLImageElement,
promise: load(src)
.then((image) => (newLoadState.src = image.src as TLoadSrc))
.then((image) => (newLoadState.image = image))
.catch(() => (newLoadState.error = `${src}: load error`)),
};

Expand Down
4 changes: 2 additions & 2 deletions packages/image/src/load.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { load } from ".";

describe("load", () => {
it("should load image by src", async () => {
const loadedImage = await load("src/images/test.png");
expect(loadedImage.src).toBe("http://localhost:5173/src/images/test.png");
const image = await load("src/images/test.png");
expect(image.src).toBe("http://localhost:5173/src/images/test.png");
});
});
6 changes: 2 additions & 4 deletions packages/react-image/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@
"type:check": "tsc --noEmit"
},
"dependencies": {
"@fepack/image": "workspace:0.2.5",
"use-sync-external-store": "^1.2.0"
"@fepack/image": "workspace:*"
},
"devDependencies": {
"@testing-library/jest-dom": "^5.16.5",
Expand All @@ -57,12 +56,11 @@
"@types/react": "^18.2.0",
"@types/react-dom": "^18.2.1",
"@types/testing-library__jest-dom": "^5.14.5",
"@types/use-sync-external-store": "^0.0.4",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"peerDependencies": {
"react": "^16.8 || ^17 || ^18"
"react": "^18"
},
"publishConfig": {
"access": "public"
Expand Down
9 changes: 4 additions & 5 deletions packages/react-image/src/Load.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { LoadClient, type LoadSrc, type LoadState } from "@fepack/image";
import { type FunctionComponent, createElement } from "react";
import { useSyncExternalStore } from "use-sync-external-store/shim";
import { LoadClient, type LoadSrc } from "@fepack/image";
import { ReactNode, useSyncExternalStore } from "react";

const loadClient = new LoadClient();

Expand All @@ -18,9 +17,9 @@ export const useLoad = <TLoadSrc extends LoadSrc>(

type LoadProps<TLoadSrc extends LoadSrc> = {
src: TLoadSrc;
children: FunctionComponent<LoadState<TLoadSrc>>;
children: (loadedImage: HTMLImageElement) => ReactNode;
};
export const Load = <TLoadSrc extends LoadSrc>({
src,
children,
}: LoadProps<TLoadSrc>) => createElement(children, useLoad({ src }));
}: LoadProps<TLoadSrc>) => children(useLoad({ src }));
91 changes: 37 additions & 54 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit a9d5fff

Please sign in to comment.