diff --git a/examples/index.js b/examples/index.js new file mode 100644 index 0000000..b884f44 --- /dev/null +++ b/examples/index.js @@ -0,0 +1,27 @@ +const { TeleportSingleton } = require("../lib/index"); + + +(function(){ + + + // Arrange + const teleport = TeleportSingleton.getInstance(); + const eventNames = ['event1', 'event2', 'event3']; + const expectedData = ['Data 1', 'Data 2', 'Data 3']; + let receivedData = []; + + // Act + setTimeout(() => { + teleport.multiReceive(eventNames, (...data) => { + receivedData = data; + console.log(data); + }); + }, 1000); + + eventNames.forEach((eventName, index) => { + teleport.emit(eventName, expectedData[index]); + }); + + + console.log(expectedData, receivedData); +}()) \ No newline at end of file diff --git a/package.json b/package.json index a552da5..39c34aa 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@mwx47/teleport", - "version": "1.1.5", + "version": "1.1.6", "description": "A library for managing and communicating events in your application through a singleton pattern. This pattern ensures that there is a single instance of the event manager, making it easy to coordinate and handle events across different parts of your codebase.", "main": "./src/index.js", "keywords": [ diff --git a/src/__tests__/teleportSingleton.test.ts b/src/__tests__/teleportSingleton.test.ts index b7ec6c7..72918c0 100644 --- a/src/__tests__/teleportSingleton.test.ts +++ b/src/__tests__/teleportSingleton.test.ts @@ -100,26 +100,21 @@ describe('TeleportSingleton', () => { // Test case for handling multiple events with a common handler - // TODO: async - // it('should handle multiple events with a common handler by async', () => { - // // Arrange - // const teleport = TeleportSingleton.getInstance(); - // const eventNames = ['event1', 'event2', 'event3']; - // const expectedData = ['Data 1', 'Data 2', 'Data 3']; - // let receivedData: any[] = []; - - // // Act - // setTimeout(() => { - // teleport.multiReceive(eventNames, (...data) => { - // receivedData = data; - // }); - // }, 1000); - - // eventNames.forEach((eventName, index) => { - // teleport.emit(eventName, expectedData[index]); - // }); - - // // Assert - // expect(receivedData).toEqual(expectedData); - // }); + it('should handle multiple events with a common handler by async', () => { + // Arrange + const teleport = TeleportSingleton.getInstance(); + const eventNames = ['event1', 'event2', 'event3']; + const expectedData = ['Data 1', 'Data 2', 'Data 3']; + + // Act + setTimeout(() => { + teleport.multiReceive(eventNames, (...data) => { + expect(data).toEqual(expectedData); + }); + }, 10); + + eventNames.forEach((eventName, index) => { + teleport.emit(eventName, expectedData[index]); + }); + }); }); diff --git a/tsconfig.json b/tsconfig.json index c091311..967004e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,46 +1,44 @@ { - "compilerOptions": { - "baseUrl": ".", - "outDir": "lib", // 输出目录 - "sourceMap": false, // 是否生成sourceMap - "target": "ES2015", // 编译目标 - "module": "CommonJS", // 模块类型 - "moduleResolution": "node", - "allowJs": false, // 是否编辑js文件 - "strict": true, // 严格模式 - "noUnusedLocals": true, // 未使用变量报错 - "experimentalDecorators": true, // 启动装饰器 - "resolveJsonModule": true, // 加载json - "esModuleInterop": true, - // "removeComments": false, // 删除注释 - - - "declaration": true, // 生成定义文件 - "declarationMap": false, // 生成定义sourceMap + "compilerOptions": { + "baseUrl": ".", + "outDir": "lib", // 输出目录 + "sourceMap": false, // 是否生成sourceMap + "target": "ES2015", // 编译目标 + "module": "CommonJS", // 模块类型 + "moduleResolution": "node", + "allowJs": false, // 是否编辑js文件 + "strict": true, // 严格模式 + "noUnusedLocals": true, // 未使用变量报错 + "experimentalDecorators": true, // 启动装饰器 + "resolveJsonModule": true, // 加载json + "esModuleInterop": true, + // "removeComments": false, // 删除注释 + "declaration": true, // 生成定义文件 + "declarationMap": false, // 生成定义sourceMap // "declarationDir": "./dist/types", // 定义文件输出目录 - - - "noImplicitAny": true, - "strictNullChecks": true, - "strictFunctionTypes": true, - "strictBindCallApply": true, - "strictPropertyInitialization": true, - "noImplicitThis": true, - "alwaysStrict": true, - "noUnusedParameters": true, - "noImplicitReturns": true, - "noFallthroughCasesInSwitch": true, - "noUncheckedIndexedAccess": true, - "noImplicitOverride": true, - "noPropertyAccessFromIndexSignature": true, - "forceConsistentCasingInFileNames": true, - "skipLibCheck": true, - }, - "include": [ - "src/*", // 导入目录 - "**/*.ts" - ], - "exclude": [ - "node_modules", "**/__tests__/*", "./lib/**/*" - ] + "noImplicitAny": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "strictBindCallApply": true, + "strictPropertyInitialization": true, + "noImplicitThis": true, + "alwaysStrict": true, + "noUnusedParameters": true, + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true, + "noUncheckedIndexedAccess": true, + "noImplicitOverride": true, + "noPropertyAccessFromIndexSignature": true, + "forceConsistentCasingInFileNames": true, + "skipLibCheck": true, + }, + "include": [ + "src/*", // 导入目录 + "**/*.ts" + ], + "exclude": [ + "node_modules", + "**/__tests__/*", + "./lib/**/*" + ] } \ No newline at end of file