Compare commits
51 Commits
Author | SHA1 | Date | |
---|---|---|---|
4649af7ab0 | |||
87a561de5e | |||
ae45222791 | |||
13f9926809 | |||
a8e38e636c | |||
e8c3c574c4 | |||
3ca383ba60 | |||
1dc4f8230c | |||
ebeedc6200 | |||
9d895913eb | |||
b5cc8e7952 | |||
1a36912461 | |||
1dcbdcc06d | |||
c7d0c8723f | |||
ae377b185e | |||
eab145e736 | |||
4060d64ed4 | |||
b7dfd8b1d5 | |||
82f2675910 | |||
d1149d3877 | |||
758dcd671c | |||
4c59d38971 | |||
385acfd951 | |||
97eba85337 | |||
61936bbdd1 | |||
4ab1b917a9 | |||
f299cff4c9 | |||
a6e262bbae | |||
8a8200ca8a | |||
e7785cc31b | |||
63610b66bc | |||
d8d382b2fb | |||
7c8a2eeaeb | |||
a34e8f571d | |||
2bf398cf97 | |||
315bbffac2 | |||
52e77d1c3e | |||
9ac1fdbb63 | |||
ac43f63daf | |||
c8ce7942d5 | |||
336351b98a | |||
20dcc9dc00 | |||
f273094369 | |||
7fb3688cb9 | |||
ddf28a8d0e | |||
b26aa04388 | |||
3726e29768 | |||
e500c8a7f6 | |||
243d1f17a3 | |||
a04cafdb63 | |||
1832377126 |
137
.gitlab-ci.yml
137
.gitlab-ci.yml
@ -1,137 +0,0 @@
|
|||||||
# gitzone ci_default
|
|
||||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
|
||||||
|
|
||||||
cache:
|
|
||||||
paths:
|
|
||||||
- .npmci_cache/
|
|
||||||
key: '$CI_BUILD_STAGE'
|
|
||||||
|
|
||||||
stages:
|
|
||||||
- security
|
|
||||||
- test
|
|
||||||
- release
|
|
||||||
- metadata
|
|
||||||
|
|
||||||
# ====================
|
|
||||||
# security stage
|
|
||||||
# ====================
|
|
||||||
mirror:
|
|
||||||
stage: security
|
|
||||||
script:
|
|
||||||
- npmci git mirror
|
|
||||||
only:
|
|
||||||
- tags
|
|
||||||
tags:
|
|
||||||
- lossless
|
|
||||||
- docker
|
|
||||||
- notpriv
|
|
||||||
|
|
||||||
auditProductionDependencies:
|
|
||||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
|
||||||
stage: security
|
|
||||||
script:
|
|
||||||
- npmci npm prepare
|
|
||||||
- npmci command npm install --production --ignore-scripts
|
|
||||||
- npmci command npm config set registry https://registry.npmjs.org
|
|
||||||
- npmci command npm audit --audit-level=high --only=prod --production
|
|
||||||
tags:
|
|
||||||
- docker
|
|
||||||
|
|
||||||
auditDevDependencies:
|
|
||||||
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
|
|
||||||
stage: security
|
|
||||||
script:
|
|
||||||
- npmci npm prepare
|
|
||||||
- npmci command npm install --ignore-scripts
|
|
||||||
- npmci command npm config set registry https://registry.npmjs.org
|
|
||||||
- npmci command npm audit --audit-level=high --only=dev
|
|
||||||
tags:
|
|
||||||
- docker
|
|
||||||
allow_failure: true
|
|
||||||
|
|
||||||
# ====================
|
|
||||||
# test stage
|
|
||||||
# ====================
|
|
||||||
|
|
||||||
testStable:
|
|
||||||
stage: test
|
|
||||||
script:
|
|
||||||
- npmci npm prepare
|
|
||||||
- npmci node install stable
|
|
||||||
- npmci npm install
|
|
||||||
- npmci npm test
|
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
|
||||||
tags:
|
|
||||||
- docker
|
|
||||||
|
|
||||||
testBuild:
|
|
||||||
stage: test
|
|
||||||
script:
|
|
||||||
- npmci npm prepare
|
|
||||||
- npmci node install stable
|
|
||||||
- npmci npm install
|
|
||||||
- npmci command npm run build
|
|
||||||
coverage: /\d+.?\d+?\%\s*coverage/
|
|
||||||
tags:
|
|
||||||
- docker
|
|
||||||
|
|
||||||
release:
|
|
||||||
stage: release
|
|
||||||
script:
|
|
||||||
- npmci node install stable
|
|
||||||
- npmci npm publish
|
|
||||||
only:
|
|
||||||
- tags
|
|
||||||
tags:
|
|
||||||
- lossless
|
|
||||||
- docker
|
|
||||||
- notpriv
|
|
||||||
|
|
||||||
# ====================
|
|
||||||
# metadata stage
|
|
||||||
# ====================
|
|
||||||
codequality:
|
|
||||||
stage: metadata
|
|
||||||
allow_failure: true
|
|
||||||
only:
|
|
||||||
- tags
|
|
||||||
script:
|
|
||||||
- npmci command npm install -g tslint typescript
|
|
||||||
- npmci npm prepare
|
|
||||||
- npmci npm install
|
|
||||||
- npmci command "tslint -c tslint.json ./ts/**/*.ts"
|
|
||||||
tags:
|
|
||||||
- lossless
|
|
||||||
- docker
|
|
||||||
- priv
|
|
||||||
|
|
||||||
trigger:
|
|
||||||
stage: metadata
|
|
||||||
script:
|
|
||||||
- npmci trigger
|
|
||||||
only:
|
|
||||||
- tags
|
|
||||||
tags:
|
|
||||||
- lossless
|
|
||||||
- docker
|
|
||||||
- notpriv
|
|
||||||
|
|
||||||
pages:
|
|
||||||
stage: metadata
|
|
||||||
script:
|
|
||||||
- npmci node install lts
|
|
||||||
- npmci command npm install -g @gitzone/tsdoc
|
|
||||||
- npmci npm prepare
|
|
||||||
- npmci npm install
|
|
||||||
- npmci command tsdoc
|
|
||||||
tags:
|
|
||||||
- lossless
|
|
||||||
- docker
|
|
||||||
- notpriv
|
|
||||||
only:
|
|
||||||
- tags
|
|
||||||
artifacts:
|
|
||||||
expire_in: 1 week
|
|
||||||
paths:
|
|
||||||
- public
|
|
||||||
allow_failure: true
|
|
24
.vscode/launch.json
vendored
24
.vscode/launch.json
vendored
@ -2,28 +2,10 @@
|
|||||||
"version": "0.2.0",
|
"version": "0.2.0",
|
||||||
"configurations": [
|
"configurations": [
|
||||||
{
|
{
|
||||||
"name": "current file",
|
"command": "npm test",
|
||||||
"type": "node",
|
"name": "Run npm test",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"args": [
|
"type": "node-terminal"
|
||||||
"${relativeFile}"
|
|
||||||
],
|
|
||||||
"runtimeArgs": ["-r", "@gitzone/tsrun"],
|
|
||||||
"cwd": "${workspaceRoot}",
|
|
||||||
"protocol": "inspector",
|
|
||||||
"internalConsoleOptions": "openOnSessionStart"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"name": "test.ts",
|
|
||||||
"type": "node",
|
|
||||||
"request": "launch",
|
|
||||||
"args": [
|
|
||||||
"test/test.ts"
|
|
||||||
],
|
|
||||||
"runtimeArgs": ["-r", "@gitzone/tsrun"],
|
|
||||||
"cwd": "${workspaceRoot}",
|
|
||||||
"protocol": "inspector",
|
|
||||||
"internalConsoleOptions": "openOnSessionStart"
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -2,17 +2,33 @@
|
|||||||
"gitzone": {
|
"gitzone": {
|
||||||
"projectType": "npm",
|
"projectType": "npm",
|
||||||
"module": {
|
"module": {
|
||||||
"githost": "gitlab.com",
|
"githost": "code.foss.global",
|
||||||
"gitscope": "designestate",
|
"gitscope": "design.estate",
|
||||||
"gitrepo": "dees-comms",
|
"gitrepo": "dees-comms",
|
||||||
"shortDescription": "a comms module for communicating within the DOM",
|
"shortDescription": "a comms module for communicating within the DOM",
|
||||||
"npmPackagename": "@designestate/dees-comms",
|
"npmPackagename": "@design.estate/dees-comms",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"projectDomain": "design.estate"
|
"projectDomain": "design.estate",
|
||||||
|
"description": "A communications module for enabling DOM-based messaging and synchronization across browser tabs and workers.",
|
||||||
|
"keywords": [
|
||||||
|
"DOM communication",
|
||||||
|
"message broadcasting",
|
||||||
|
"cross-browser",
|
||||||
|
"cross-tab",
|
||||||
|
"web workers",
|
||||||
|
"typed messages",
|
||||||
|
"TypeScript",
|
||||||
|
"broadcast channel",
|
||||||
|
"client-side messaging",
|
||||||
|
"module"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"npmci": {
|
"npmci": {
|
||||||
"npmGlobalTools": [],
|
"npmGlobalTools": [],
|
||||||
"npmAccessLevel": "public"
|
"npmAccessLevel": "public"
|
||||||
|
},
|
||||||
|
"tsdoc": {
|
||||||
|
"legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n"
|
||||||
}
|
}
|
||||||
}
|
}
|
11358
package-lock.json
generated
11358
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
40
package.json
40
package.json
@ -1,28 +1,30 @@
|
|||||||
{
|
{
|
||||||
"name": "@designestate/dees-comms",
|
"name": "@design.estate/dees-comms",
|
||||||
"version": "1.0.2",
|
"version": "1.0.27",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "a comms module for communicating within the DOM",
|
"description": "A communications module for enabling DOM-based messaging and synchronization across browser tabs and workers.",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
"typings": "dist_ts/index.d.ts",
|
"typings": "dist_ts/index.d.ts",
|
||||||
|
"type": "module",
|
||||||
"author": "Lossless GmbH",
|
"author": "Lossless GmbH",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "(tstest test/ --web)",
|
"test": "(tstest test/ --web)",
|
||||||
"build": "(tsbuild --web)"
|
"build": "(tsbuild --web --allowimplicitany && tsbundle npm)"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@gitzone/tsbuild": "^2.1.25",
|
"@git.zone/tsbuild": "^2.1.66",
|
||||||
"@gitzone/tsbundle": "^1.0.78",
|
"@git.zone/tsbundle": "^2.0.8",
|
||||||
"@gitzone/tstest": "^1.0.52",
|
"@git.zone/tsrun": "^1.2.44",
|
||||||
"@pushrocks/tapbundle": "^3.2.9",
|
"@git.zone/tstest": "^1.0.77",
|
||||||
"@types/node": "^14.11.5",
|
"@push.rocks/tapbundle": "^5.0.23",
|
||||||
"tslint": "^6.1.3",
|
"@types/node": "^20.12.12"
|
||||||
"tslint-config-prettier": "^1.15.0"
|
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@apiglobal/typedrequest": "^1.0.49",
|
"@api.global/typedrequest": "^3.0.25",
|
||||||
"@apiglobal/typedrequest-interfaces": "^1.0.15"
|
"@api.global/typedrequest-interfaces": "^3.0.19",
|
||||||
|
"@push.rocks/smartdelay": "^3.0.5",
|
||||||
|
"broadcast-channel": "^7.0.0"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"ts/**/*",
|
"ts/**/*",
|
||||||
@ -38,5 +40,17 @@
|
|||||||
],
|
],
|
||||||
"browserslist": [
|
"browserslist": [
|
||||||
"last 1 chrome versions"
|
"last 1 chrome versions"
|
||||||
|
],
|
||||||
|
"keywords": [
|
||||||
|
"DOM communication",
|
||||||
|
"message broadcasting",
|
||||||
|
"cross-browser",
|
||||||
|
"cross-tab",
|
||||||
|
"web workers",
|
||||||
|
"typed messages",
|
||||||
|
"TypeScript",
|
||||||
|
"broadcast channel",
|
||||||
|
"client-side messaging",
|
||||||
|
"module"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
6711
pnpm-lock.yaml
generated
Normal file
6711
pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
1
readme.hints.md
Normal file
1
readme.hints.md
Normal file
@ -0,0 +1 @@
|
|||||||
|
|
142
readme.md
142
readme.md
@ -1,39 +1,127 @@
|
|||||||
# @designestate/dees-comms
|
# @design.estate/dees-comms
|
||||||
a comms module for communicating within the DOM
|
a comms module for communicating within the DOM
|
||||||
|
|
||||||
## Availabililty and Links
|
## Install
|
||||||
* [npmjs.org (npm package)](https://www.npmjs.com/package/@designestate/dees-comms)
|
To integrate `@design.estate/dees-comms` into your project, run the following command in your terminal:
|
||||||
* [gitlab.com (source)](https://gitlab.com/designestate/dees-comms)
|
|
||||||
* [github.com (source mirror)](https://github.com/designestate/dees-comms)
|
|
||||||
* [docs (typedoc)](https://designestate.gitlab.io/dees-comms/)
|
|
||||||
|
|
||||||
## Status for master
|
```bash
|
||||||
|
npm install @design.estate/dees-comms --save
|
||||||
|
```
|
||||||
|
|
||||||
Status Category | Status Badge
|
This will add the package to your project dependencies.
|
||||||
-- | --
|
|
||||||
GitLab Pipelines | [](https://lossless.cloud)
|
|
||||||
GitLab Pipline Test Coverage | [](https://lossless.cloud)
|
|
||||||
npm | [](https://lossless.cloud)
|
|
||||||
Snyk | [](https://lossless.cloud)
|
|
||||||
TypeScript Support | [](https://lossless.cloud)
|
|
||||||
node Support | [](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
|
||||||
Code Style | [](https://lossless.cloud)
|
|
||||||
PackagePhobia (total standalone install weight) | [](https://lossless.cloud)
|
|
||||||
PackagePhobia (package size on registry) | [](https://lossless.cloud)
|
|
||||||
BundlePhobia (total size when bundled) | [](https://lossless.cloud)
|
|
||||||
Platform support | [](https://lossless.cloud) [](https://lossless.cloud)
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
Use TypeScript for best in class intellisense
|
`@design.estate/dees-comms` facilitates communication within the DOM, especially between various workers and tabs in client-side JavaScript. Using TypeScript and the provided ESM syntax will enhance your development experience with better intellisense support and type safety.
|
||||||
|
|
||||||
## Contribution
|
### Setting Up
|
||||||
|
|
||||||
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
|
First, make sure to import the module in your TypeScript file:
|
||||||
|
|
||||||
For further information read the linked docs at the top of this readme.
|
```typescript
|
||||||
|
import { DeesComms } from '@design.estate/dees-comms';
|
||||||
|
```
|
||||||
|
|
||||||
> MIT licensed | **©** [Lossless GmbH](https://lossless.gmbh)
|
### Initializing the Communication Module
|
||||||
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
|
||||||
|
|
||||||
[](https://maintainedby.lossless.com)
|
Create an instance of the `DeesComms` class to start using the module. This instance will act as the central communication hub in your application.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const myDeesComms = new DeesComms();
|
||||||
|
```
|
||||||
|
|
||||||
|
### Sending Messages
|
||||||
|
|
||||||
|
To send messages, you'll need to create a typed request. Let's assume we are sending a message that requires a response:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// Define the message type
|
||||||
|
interface MyMessageType {
|
||||||
|
method: 'myMethod';
|
||||||
|
someData: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Creating a typed request
|
||||||
|
const myTypedRequest = myDeesComms.createTypedRequest<MyMessageType>('myMethod');
|
||||||
|
|
||||||
|
// Firing the request
|
||||||
|
myTypedRequest.fire({
|
||||||
|
someData: 'Hello, World!',
|
||||||
|
}).then(response => {
|
||||||
|
console.log('Received response:', response);
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### Receiving and Handling Messages
|
||||||
|
|
||||||
|
To handle incoming messages, you need to define handlers for the types of messages you expect to receive.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// Define the handler for the 'myMethod' message
|
||||||
|
myDeesComms.createTypedHandler<MyMessageType>('myMethod', async (incomingMessage) => {
|
||||||
|
console.log('Message received:', incomingMessage);
|
||||||
|
|
||||||
|
// Process the incoming message...
|
||||||
|
|
||||||
|
// Return a response (if needed)
|
||||||
|
return {
|
||||||
|
response: 'Message received loud and clear!'
|
||||||
|
};
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### Communication Between Tabs or Workers
|
||||||
|
|
||||||
|
`@design.estate/dees-comms` uses the Broadcast Channel API and a polyfill for environments where it's not available, enabling seamless communication between different contexts like tabs, iframes, or web workers.
|
||||||
|
|
||||||
|
### Clean Up
|
||||||
|
|
||||||
|
When you're done with communication, or before creating a new instance for any reason, ensure you clean up properly to prevent memory leaks or other unexpected behaviors:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// Currently, the cleanup should be handled according to the specific needs of your application
|
||||||
|
// e.g., closing or nullifying references to the DeesComms instance.
|
||||||
|
```
|
||||||
|
|
||||||
|
### Complete Example
|
||||||
|
|
||||||
|
Putting it all together, let's create a small application that sends a message and listens for a response:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { DeesComms } from '@design.estate/dees-comms';
|
||||||
|
|
||||||
|
const appDeesComms = new DeesComms();
|
||||||
|
|
||||||
|
// Setting up the listener
|
||||||
|
appDeesComms.createTypedHandler<{ method: 'greet'; name: string }>('greet', async (message) => {
|
||||||
|
console.log(`Greetings received from ${message.name}`);
|
||||||
|
return { reply: `Hello ${message.name}, nice to meet you!` };
|
||||||
|
});
|
||||||
|
|
||||||
|
// Sending a greeting message
|
||||||
|
const greetRequest = appDeesComms.createTypedRequest<{ method: 'greet'; name: string }>('greet');
|
||||||
|
greetRequest.fire({ name: 'Alice' }).then(response => {
|
||||||
|
console.log(response.reply); // Expected output: "Hello Alice, nice to meet you!"
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
This module provides a powerful and flexible way to handle communications within the DOM, leveraging modern JavaScript features and patterns. The usage of TypeScript ensures type safety and enhances code understandability and maintainability.
|
||||||
|
|
||||||
|
## License and Legal Information
|
||||||
|
|
||||||
|
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
|
||||||
|
|
||||||
|
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
### Trademarks
|
||||||
|
|
||||||
|
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
|
||||||
|
|
||||||
|
### Company Information
|
||||||
|
|
||||||
|
Task Venture Capital GmbH
|
||||||
|
Registered at District court Bremen HRB 35230 HB, Germany
|
||||||
|
|
||||||
|
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
|
||||||
|
|
||||||
|
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.
|
||||||
|
@ -1,10 +0,0 @@
|
|||||||
import { expect, tap } from '@pushrocks/tapbundle';
|
|
||||||
import * as deesComms from '../ts/index';
|
|
||||||
|
|
||||||
let deesCommsTest: deesComms.DeesComms;
|
|
||||||
|
|
||||||
tap.test('first test', async () => {
|
|
||||||
deesCommsTest = new deesComms.DeesComms();
|
|
||||||
});
|
|
||||||
|
|
||||||
tap.start();
|
|
38
test/test.node.ts
Normal file
38
test/test.node.ts
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
import { expect, tap } from '@push.rocks/tapbundle';
|
||||||
|
import * as deesComms from '../ts/index.js';
|
||||||
|
|
||||||
|
let deesCommsTest: deesComms.DeesComms;
|
||||||
|
let deesCommsTest2: deesComms.DeesComms;
|
||||||
|
|
||||||
|
tap.test('first test', async (tools) => {
|
||||||
|
deesCommsTest = new deesComms.DeesComms();
|
||||||
|
deesCommsTest2 = new deesComms.DeesComms();
|
||||||
|
let counter = 1;
|
||||||
|
deesCommsTest2.createTypedHandler<any>('test', async (requestData) => {
|
||||||
|
console.log(`got the request ${counter++}`);
|
||||||
|
return { hitheretoo: `greetings to ${requestData.hithere}` };
|
||||||
|
});
|
||||||
|
|
||||||
|
// lets fire a request
|
||||||
|
const typedrequest = deesCommsTest.createTypedRequest<any>('test');
|
||||||
|
const result = await typedrequest.fire({
|
||||||
|
hithere: 'hello',
|
||||||
|
});
|
||||||
|
console.log(JSON.stringify(result));
|
||||||
|
|
||||||
|
// lets fire a request
|
||||||
|
const typedrequest2 = deesCommsTest.createTypedRequest<any>('test2');
|
||||||
|
// TODO: return response after timeout
|
||||||
|
/* const result2 = await typedrequest2.fire({
|
||||||
|
hithere: 'hello',
|
||||||
|
});
|
||||||
|
console.log(JSON.stringify(result2)); */
|
||||||
|
});
|
||||||
|
|
||||||
|
tap.test('should end on nodejs', async (toolsArg) => {
|
||||||
|
if (globalThis.process) {
|
||||||
|
toolsArg.delayFor(2000).then(() => globalThis.process.exit(0));
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
tap.start();
|
8
ts/00_commitinfo_data.ts
Normal file
8
ts/00_commitinfo_data.ts
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
/**
|
||||||
|
* autocreated commitinfo by @pushrocks/commitinfo
|
||||||
|
*/
|
||||||
|
export const commitinfo = {
|
||||||
|
name: '@design.estate/dees-comms',
|
||||||
|
version: '1.0.27',
|
||||||
|
description: 'A communications module for enabling DOM-based messaging and synchronization across browser tabs and workers.'
|
||||||
|
}
|
@ -1,21 +1,36 @@
|
|||||||
import { TypedRequest } from '@apiglobal/typedrequest';
|
import * as plugins from './dees-comms.plugins.js';
|
||||||
import * as plugins from './dees-comms.plugins';
|
|
||||||
|
let BroadcastChannel = globalThis.BroadcastChannel;
|
||||||
|
if (!BroadcastChannel) {
|
||||||
|
BroadcastChannel = plugins.BroadCastChannelPolyfill as any;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* a comm class for client side communication between workers and tabs.
|
* a comm class for client side communication between workers and tabs.
|
||||||
*/
|
*/
|
||||||
export class DeesComms {
|
export class DeesComms {
|
||||||
|
private broadcastChannel = new BroadcastChannel('dees-comms');
|
||||||
|
|
||||||
// sending messages
|
// sending messages
|
||||||
private postChannel = new BroadcastChannel('dees-comms');
|
public typedrouter = new plugins.typedrequest.TypedRouter();
|
||||||
private typedrouter = new plugins.typedrequest.TypedRouter();
|
public typedtarget = new plugins.typedrequest.TypedTarget({
|
||||||
|
postMethodWithTypedRouter: async (messageArg) => {
|
||||||
private subscriptionChannel = new BroadcastChannel('dees-comms');
|
this.postMessage(messageArg);
|
||||||
|
},
|
||||||
|
typedRouterRef: this.typedrouter,
|
||||||
|
});
|
||||||
|
|
||||||
|
// receiving messages
|
||||||
constructor() {
|
constructor() {
|
||||||
this.subscriptionChannel.onmessage = (eventArg) => {
|
this.broadcastChannel.onmessage = async (eventArg) => {
|
||||||
const message = eventArg.data;
|
const message = (eventArg as any).method ? eventArg : eventArg.data;
|
||||||
this.typedrouter.routeAndAddResponse(message);
|
console.log(JSON.stringify(message));
|
||||||
|
const response = await this.typedrouter.routeAndAddResponse(message);
|
||||||
|
if (response && !response.error) {
|
||||||
|
this.postMessage(response);
|
||||||
|
} else {
|
||||||
|
// console.log(response);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -24,8 +39,8 @@ export class DeesComms {
|
|||||||
*/
|
*/
|
||||||
public createTypedRequest<T extends plugins.typedrequestInterfaces.ITypedRequest>(
|
public createTypedRequest<T extends plugins.typedrequestInterfaces.ITypedRequest>(
|
||||||
methodName: T['method']
|
methodName: T['method']
|
||||||
): TypedRequest<T> {
|
): plugins.typedrequest.TypedRequest<T> {
|
||||||
const typedrequest = new plugins.typedrequest.TypedRequest(this.postMessage, methodName, this.typedrouter);
|
const typedrequest = new plugins.typedrequest.TypedRequest(this.typedtarget, methodName);
|
||||||
return typedrequest;
|
return typedrequest;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,11 +50,18 @@ export class DeesComms {
|
|||||||
public async postMessage<T = plugins.typedrequestInterfaces.ITypedRequest>(
|
public async postMessage<T = plugins.typedrequestInterfaces.ITypedRequest>(
|
||||||
messageArg: T
|
messageArg: T
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
this.postChannel.postMessage(messageArg);
|
this.broadcastChannel.postMessage(messageArg);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* subscribe to messages
|
* subscribe to messages
|
||||||
*/
|
*/
|
||||||
public async subscribe() {}
|
public async createTypedHandler<T extends plugins.typedrequestInterfaces.ITypedRequest>(
|
||||||
|
methodArg: T['method'],
|
||||||
|
handlerFunction: plugins.typedrequest.THandlerFunction<T>
|
||||||
|
) {
|
||||||
|
this.typedrouter.addTypedHandler(
|
||||||
|
new plugins.typedrequest.TypedHandler<T>(methodArg, handlerFunction)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import * as plugins from './dees-comms.plugins';
|
import * as plugins from './dees-comms.plugins.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* a message that can be sent
|
* a message that can be sent
|
||||||
|
@ -1,5 +1,13 @@
|
|||||||
// pushrocks scope
|
// pushrocks scope
|
||||||
import * as typedrequestInterfaces from '@apiglobal/typedrequest-interfaces';
|
import * as smartdelay from '@push.rocks/smartdelay';
|
||||||
import * as typedrequest from '@apiglobal/typedrequest';
|
import * as typedrequestInterfaces from '@api.global/typedrequest-interfaces';
|
||||||
|
import * as typedrequest from '@api.global/typedrequest';
|
||||||
|
|
||||||
export { typedrequestInterfaces, typedrequest };
|
export { smartdelay, typedrequestInterfaces, typedrequest };
|
||||||
|
|
||||||
|
// third party scope
|
||||||
|
import { BroadcastChannel as BroadCastChannelPolyfill } from 'broadcast-channel';
|
||||||
|
|
||||||
|
export {
|
||||||
|
BroadCastChannelPolyfill
|
||||||
|
}
|
12
ts/index.ts
12
ts/index.ts
@ -1,11 +1 @@
|
|||||||
import * as plugins from './dees-comms.plugins';
|
export * from './dees-comms.classes.deescomms.js';
|
||||||
|
|
||||||
/**
|
|
||||||
* a comm class for client side communication between workers and tabs.
|
|
||||||
*/
|
|
||||||
export class DeesComms {
|
|
||||||
public postChannel = new BroadcastChannel('dees-comms');
|
|
||||||
public subscriptionChannel = new BroadcastChannel('dees-comms');
|
|
||||||
|
|
||||||
public async postMessage() {}
|
|
||||||
}
|
|
||||||
|
14
tsconfig.json
Normal file
14
tsconfig.json
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"experimentalDecorators": true,
|
||||||
|
"useDefineForClassFields": false,
|
||||||
|
"target": "ES2022",
|
||||||
|
"module": "NodeNext",
|
||||||
|
"moduleResolution": "NodeNext",
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"verbatimModuleSyntax": true
|
||||||
|
},
|
||||||
|
"exclude": [
|
||||||
|
"dist_*/**/*.d.ts"
|
||||||
|
]
|
||||||
|
}
|
17
tslint.json
17
tslint.json
@ -1,17 +0,0 @@
|
|||||||
{
|
|
||||||
"extends": ["tslint:latest", "tslint-config-prettier"],
|
|
||||||
"rules": {
|
|
||||||
"semicolon": [true, "always"],
|
|
||||||
"no-console": false,
|
|
||||||
"ordered-imports": false,
|
|
||||||
"object-literal-sort-keys": false,
|
|
||||||
"member-ordering": {
|
|
||||||
"options":{
|
|
||||||
"order": [
|
|
||||||
"static-method"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"defaultSeverity": "warning"
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user