From badb953758a25db6672a04abba1583df23513037 Mon Sep 17 00:00:00 2001 From: durulkoca Date: Mon, 5 Feb 2024 19:55:39 +0300 Subject: [PATCH 1/5] Add initial data storage to publish --- src/Event.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Event.js b/src/Event.js index ef0f81f..18e065d 100644 --- a/src/Event.js +++ b/src/Event.js @@ -24,6 +24,7 @@ const subscribe = (type, callback) => { }; const publish = (type, payload) => { + map.set(type, payload); if (!subscriptions[type]) return; Object.keys(subscriptions[type]).forEach((key) => { From cccc43d780a8dc0afc050f2fc4c2155520c1b6b8 Mon Sep 17 00:00:00 2001 From: durulkoca Date: Mon, 5 Feb 2024 20:50:04 +0300 Subject: [PATCH 2/5] Add new test cases --- src/test/synapses.test.js | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/test/synapses.test.js b/src/test/synapses.test.js index 9bb3fa7..1bcb4e9 100644 --- a/src/test/synapses.test.js +++ b/src/test/synapses.test.js @@ -1,4 +1,4 @@ -import { subscribe, publish } from "../../index"; +import { publish, subscribe } from "../../index"; describe("Synapses", () => { it("subscribes and publishes events", (done) => { @@ -13,4 +13,35 @@ describe("Synapses", () => { publish("TEST_EVENT", { number: 10, string: "blue" }); }); + + it("notifies all subscribers of an event", (done) => { + let receivedByFirstSubscriber = false; + let receivedBySecondSubscriber = false; + + const checkDone = () => { + if (receivedByFirstSubscriber && receivedBySecondSubscriber) { + done(); + } + }; + + subscribe("MULTI_SUB_EVENT", (result) => { + expect(result).toMatchObject({ data: "shared" }); + receivedByFirstSubscriber = true; + checkDone(); + }); + + subscribe("MULTI_SUB_EVENT", (result) => { + expect(result).toMatchObject({ data: "shared" }); + receivedBySecondSubscriber = true; + checkDone(); + }); + + publish("MULTI_SUB_EVENT", { data: "shared" }); + }); + + it("handles events with no subscribers without errors", () => { + expect(() => + publish("NO_SUBSCRIBER_EVENT", { data: "none" }) + ).not.toThrow(); + }); }); From 51b0ed70b9c2ffaddef442beb311034ef6cac5eb Mon Sep 17 00:00:00 2001 From: Halil Ibrahim Cengel Date: Mon, 5 Feb 2024 20:59:59 +0300 Subject: [PATCH 3/5] Add reusable publish workflow --- .github/workflows/publish.yml | 9 +++++++++ .github/workflows/tag.yml | 25 ------------------------- 2 files changed, 9 insertions(+), 25 deletions(-) create mode 100644 .github/workflows/publish.yml delete mode 100644 .github/workflows/tag.yml diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..71b18d4 --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,9 @@ +name: publish +on: + push: + tags: + - v[0-9]+.[0-9]+.[0-9]+ +jobs: + deploy: + uses: NucleoidJS/actions/.github/workflows/publish.yml@main + secrets: inherit diff --git a/.github/workflows/tag.yml b/.github/workflows/tag.yml deleted file mode 100644 index 5ad861d..0000000 --- a/.github/workflows/tag.yml +++ /dev/null @@ -1,25 +0,0 @@ -name: Tag -on: - push: - tags: - - v[0-9]+.[0-9]+.[0-9]+ -jobs: - publish: - runs-on: ubuntu-20.04 - steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v2 - with: - node-version: '16.x' - registry-url: 'https://registry.npmjs.org' - - run: npm ci - - run: npm test - - run: npm publish --access public --tag latest - env: - NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} - - uses: actions/create-release@latest - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - tag_name: ${{ github.ref }} - release_name: ${{ github.ref }} From 907b331f9af3de2044f4c02ad0c9ef4deae07f7a Mon Sep 17 00:00:00 2001 From: durulkoca Date: Tue, 6 Feb 2024 16:38:16 +0300 Subject: [PATCH 4/5] Update synapses.test.js --- src/test/synapses.test.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/test/synapses.test.js b/src/test/synapses.test.js index 1bcb4e9..9229199 100644 --- a/src/test/synapses.test.js +++ b/src/test/synapses.test.js @@ -1,4 +1,4 @@ -import { publish, subscribe } from "../../index"; +import { map, publish, subscribe } from "../Event"; describe("Synapses", () => { it("subscribes and publishes events", (done) => { @@ -44,4 +44,11 @@ describe("Synapses", () => { publish("NO_SUBSCRIBER_EVENT", { data: "none" }) ).not.toThrow(); }); + + it("registers the last published event to map", () => { + publish("LAST_EVENT", { data: "test payload" }); + + const lastPublishedEvent = map.get("LAST_EVENT"); + expect(lastPublishedEvent).toEqual({ data: "test payload" }); + }); }); From 0621d42f2b94d61cf8022b28fdd34868bff7d603 Mon Sep 17 00:00:00 2001 From: durulkoca Date: Tue, 6 Feb 2024 17:00:23 +0300 Subject: [PATCH 5/5] Change project name --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 14188de..7a51808 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "@nucleoidjs/synapses", + "name": "react-event", "version": "1.0.13", "type": "module", "description": "Event-driven alternative to React Context",