@plentymarkets/shop-core 1.8.3 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/module.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "shop-core",
3
3
  "configKey": "shopCore",
4
- "version": "1.8.3",
4
+ "version": "1.9.0",
5
5
  "builder": {
6
6
  "@nuxt/module-builder": "0.8.4",
7
7
  "unbuild": "2.0.0"
@@ -0,0 +1,73 @@
1
+ import { beforeEach, describe, expect, it, vi, vitest } from "vitest";
2
+ import { usePlentyEvent } from "../usePlentyEvent.js";
3
+ describe("usePlentyEvent", () => {
4
+ beforeEach(() => {
5
+ const { clear } = usePlentyEvent();
6
+ clear();
7
+ vitest.clearAllMocks();
8
+ });
9
+ it("should register an event", () => {
10
+ const { on, emit } = usePlentyEvent();
11
+ const callback = vi.fn();
12
+ on("frontend:login", callback);
13
+ emit("frontend:login", { user: { id: 1 } });
14
+ expect(callback).toHaveBeenCalledWith({ user: { id: 1 } });
15
+ });
16
+ it("should turn off an event", () => {
17
+ const { on, off, emit } = usePlentyEvent();
18
+ const callback = vi.fn();
19
+ on("frontend:login", callback);
20
+ off("frontend:login", callback);
21
+ emit("frontend:login", { user: { id: 1 } });
22
+ expect(callback).not.toHaveBeenCalled();
23
+ });
24
+ it("should register an event only once", () => {
25
+ const { once, emit } = usePlentyEvent();
26
+ const callback = vi.fn();
27
+ once("frontend:login", callback);
28
+ emit("frontend:login", { user: { id: 1 } });
29
+ emit("frontend:login", { user: { id: 1 } });
30
+ expect(callback).toHaveBeenCalledTimes(1);
31
+ });
32
+ it("should clear all events", () => {
33
+ const { on, emit, clear } = usePlentyEvent();
34
+ const callback = vi.fn();
35
+ on("frontend:login", callback);
36
+ clear();
37
+ emit("frontend:login", { user: { id: 1 } });
38
+ expect(callback).not.toHaveBeenCalled();
39
+ });
40
+ it("should throw error in event listener without breaking", () => {
41
+ const { on, emit } = usePlentyEvent();
42
+ const errorCallback = vi.fn(() => {
43
+ throw new Error("Test error");
44
+ });
45
+ const callback = vi.fn();
46
+ on("frontend:login", errorCallback);
47
+ on("frontend:login", callback);
48
+ emit("frontend:login", { user: { id: 1 } });
49
+ expect(errorCallback).toHaveBeenCalled();
50
+ expect(callback).toHaveBeenCalledWith({ user: { id: 1 } });
51
+ });
52
+ it("should not turn off event if its a different callback", () => {
53
+ const { on, off, emit } = usePlentyEvent();
54
+ const callback1 = vi.fn();
55
+ const callback2 = vi.fn();
56
+ on("frontend:login", callback1);
57
+ off("frontend:login", callback2);
58
+ emit("frontend:login", { user: { id: 1 } });
59
+ expect(callback1).toHaveBeenCalled();
60
+ expect(callback2).not.toHaveBeenCalled();
61
+ });
62
+ it("should remove all listeners for an event if no callback is provided to off", () => {
63
+ const { on, off, emit } = usePlentyEvent();
64
+ const callback1 = vi.fn();
65
+ const callback2 = vi.fn();
66
+ on("frontend:login", callback1);
67
+ on("frontend:login", callback2);
68
+ off("frontend:login");
69
+ emit("frontend:login", { user: { id: 1 } });
70
+ expect(callback1).not.toHaveBeenCalled();
71
+ expect(callback2).not.toHaveBeenCalled();
72
+ });
73
+ });
@@ -1,2 +1,10 @@
1
1
  import type { Events } from '../types/events.js';
2
- export declare function usePlentyEvent(): import("mitt").Emitter<Events>;
2
+ type EventCallback<T = any> = (data: T) => void;
3
+ export declare function usePlentyEvent(): {
4
+ on: <K extends keyof Events>(event: K, callback: EventCallback<Events[K]>) => void;
5
+ off: <K extends keyof Events>(event: K, callback?: EventCallback<Events[K]>) => void;
6
+ emit: <K extends keyof Events>(event: K, data: Events[K]) => void;
7
+ once: <K extends keyof Events>(event: K, callback: EventCallback<Events[K]>) => void;
8
+ clear: () => void;
9
+ };
10
+ export {};
@@ -1,5 +1,51 @@
1
- import mitt from "mitt";
2
- const plentyEventBus = mitt();
1
+ import { useState } from "#imports";
3
2
  export function usePlentyEvent() {
4
- return plentyEventBus;
3
+ const listeners = useState("plentyEventListeners", () => ({}));
4
+ const on = (event, callback) => {
5
+ if (import.meta.server) return;
6
+ if (!listeners.value[event]) {
7
+ listeners.value[event] = [];
8
+ }
9
+ listeners.value[event].push(callback);
10
+ };
11
+ const off = (event, callback) => {
12
+ if (import.meta.server || !listeners.value[event]) return;
13
+ if (callback) {
14
+ const index = listeners.value[event].indexOf(callback);
15
+ if (index > -1) {
16
+ listeners.value[event].splice(index, 1);
17
+ }
18
+ } else {
19
+ listeners.value[event] = [];
20
+ }
21
+ };
22
+ const emit = (event, data) => {
23
+ if (import.meta.server || !listeners.value[event]) return;
24
+ listeners.value[event].forEach((callback) => {
25
+ try {
26
+ callback(data);
27
+ } catch (error) {
28
+ console.error(`Error in event listener for ${String(event)}:`, error);
29
+ }
30
+ });
31
+ };
32
+ const once = (event, callback) => {
33
+ if (import.meta.server) return;
34
+ const onceCallback = (data) => {
35
+ callback(data);
36
+ off(event, onceCallback);
37
+ };
38
+ on(event, onceCallback);
39
+ };
40
+ const clear = () => {
41
+ if (import.meta.server) return;
42
+ listeners.value = {};
43
+ };
44
+ return {
45
+ on,
46
+ off,
47
+ emit,
48
+ once,
49
+ clear
50
+ };
5
51
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@plentymarkets/shop-core",
3
- "version": "1.8.3",
3
+ "version": "1.9.0",
4
4
  "description": "Core module for PlentyONE Shop",
5
5
  "repository": {
6
6
  "type": "git",
@@ -42,10 +42,9 @@
42
42
  "test:types": "vue-tsc --noEmit && cd playground && vue-tsc --noEmit"
43
43
  },
44
44
  "dependencies": {
45
- "@plentymarkets/shop-api": "^0.128.0",
45
+ "@plentymarkets/shop-api": "^0.130.0",
46
46
  "@vue-storefront/sdk": "^3.4.1",
47
47
  "js-sha256": "^0.11.0",
48
- "mitt": "^3.0.1",
49
48
  "resolve": "^1.22.10"
50
49
  },
51
50
  "overrides": {