Skip to content

Commit

Permalink
Merge branch 'main' of github.com:ribeirolabs/events into main
Browse files Browse the repository at this point in the history
BREAKING CHANGE
  • Loading branch information
igor-ribeiro committed Feb 12, 2022
2 parents 05c8aba + 41dd65e commit e2bea6f
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 51 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Type safe listener for custom/native events and dispatcher for custom events
### Listen/Unlisten

```js
import { listenEvent } from '@ribeirolabs/events';

// adds event listener
const unlisten = listenEvent('hashchange', (e) => console.log(e.name));

Expand All @@ -17,14 +19,19 @@ unlisten();
You can also use `unlistenEvent`.

```js
import { unlistenEvent } from '@ribeirolabs/events';

// removes event listener
unlistenEvent('hashchange');
```
#### React

### React

You can use the hook `useEvent`. It automatically adds the listener on mount and remove it on unmount.

```js
import { useEvent } from '@ribeirolabs/events/react';

function Component() {
const listener = useCallback((event) => {
console.log(event.name);
Expand All @@ -34,10 +41,11 @@ function Component() {
}
```


### Dispath (custom events)

```js
import { listenEvent, dispatchCustomEvent } from '@ribeirolabs/events';

listenEvent('my-event', (event) => {
console.log(event.detail.message);
});
Expand Down
41 changes: 41 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const { build } = require("esbuild");

function exit() {
process.exit(1);
}

build({
entryPoints: ["./src/index.ts"],
outfile: "./dist/index.js",
logLevel: "info",
format: "cjs",
bundle: true,
}).catch(exit);

build({
entryPoints: ["./src/index.ts"],
outfile: "./dist/index.browser.js",
logLevel: "info",
format: "iife",
globalName: "events",
platform: "browser",
minify: true,
}).catch(exit);

build({
entryPoints: ["./src/index.ts"],
outfile: "./dist/index.mjs",
logLevel: "info",
format: "esm",
platform: "browser",
minify: true,
}).catch(exit);

build({
entryPoints: ["./src/react/index.ts"],
outfile: "./dist/react/index.js",
logLevel: "info",
format: "cjs",
bundle: true,
external: ["react", "."],
}).catch(exit);
20 changes: 13 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,31 @@
"name": "@ribeirolabs/events",
"version": "0.0.0-development",
"description": "Type safe listener for custom/native events and dispatcher for custom events",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
"dist",
"README.md"
"dist"
],
"scripts": {
"prebuild": "rimraf dist",
"postbuild": "node postbuild.js",
"build": "run-p build:*",
"build:lib": "esbuild src/index.ts --format=cjs --outfile=dist/index.js",
"build:lib": "node build.js",
"build:types": "tsc",
"test": "jest --watch",
"test:ci": "jest",
"semantic-release": "semantic-release"
},
"release": {
"branches": [
"main"
]
"main",
{
"name": "beta",
"channel": "beta",
"prerelease": true
}
],
"pkgRoot": "dist"
},
"publishConfig": {
"access": "public"
Expand Down
16 changes: 16 additions & 0 deletions postbuild.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const fs = require("fs");

const packageJson = JSON.parse(fs.readFileSync("./package.json", "utf-8"));

delete packageJson.scripts;
delete packageJson.files;
delete packageJson.devDependencies;

fs.writeFileSync(
"./dist/package.json",
JSON.stringify(packageJson).replace(/dist\//g, ""),
"utf-8"
);

fs.copyFileSync("./README.md", "./dist/README.md");
fs.copyFileSync("./LICENSE", "./dist/LICENSE");
30 changes: 1 addition & 29 deletions src/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { render } from "@testing-library/react";
import React from "react";
import { dispatchCustomEvent, listenEvent, useEvent } from ".";
import { dispatchCustomEvent, listenEvent } from ".";

declare global {
interface Events {
Expand Down Expand Up @@ -57,29 +55,3 @@ it("should remove listener", () => {

expect(listener).toBeCalledTimes(1);
});

it("should add/remove listener using the hook", () => {
const listener = jest.fn();

function Component() {
useEvent("test", listener);

return null;
}

const { unmount } = render(<Component />);

dispatchCustomEvent("test", {
message: "it works",
});

expect(listener).toBeCalledTimes(1);

unmount();

dispatchCustomEvent("test", {
message: "it works",
});

expect(listener).toBeCalledTimes(1);
});
15 changes: 2 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { useEffect } from "react";

declare global {
interface Events {}
}

type EventName = keyof Events | keyof WindowEventMap;
export type EventName = keyof Events | keyof WindowEventMap;

type EventHandler<E extends string> = (
export type EventHandler<E extends string> = (
event: E extends keyof Events
? CustomEvent<Events[E]>
: E extends keyof WindowEventMap
Expand Down Expand Up @@ -36,12 +34,3 @@ export function dispatchCustomEvent<E extends keyof Events>(
) {
window.dispatchEvent(new CustomEvent(event, { detail }));
}

export function useEvent<E extends string>(
event: EventName | E,
handler: EventHandler<E>
): void {
useEffect(() => {
return listenEvent(event, handler);
}, [event, handler]);
}
38 changes: 38 additions & 0 deletions src/react/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { render } from "@testing-library/react";
import React from "react";
import { useEvent } from ".";
import { dispatchCustomEvent } from "..";

declare global {
interface Events {
test: {
message: string;
};
}
}

it("should add/remove listener using the hook", () => {
const listener = jest.fn();

function Component() {
useEvent("test", listener);

return null;
}

const { unmount } = render(<Component />);

dispatchCustomEvent("test", {
message: "it works",
});

expect(listener).toBeCalledTimes(1);

unmount();

dispatchCustomEvent("test", {
message: "it works",
});

expect(listener).toBeCalledTimes(1);
});
11 changes: 11 additions & 0 deletions src/react/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { useEffect } from "react";
import { EventHandler, EventName, listenEvent } from "./../";

export function useEvent<E extends string>(
event: EventName | E,
handler: EventHandler<E>
): void {
useEffect(() => {
return listenEvent(event, handler);
}, [event, handler]);
}

0 comments on commit e2bea6f

Please sign in to comment.