javascript-event-channel

javascript event channel in node and browser

Usage no npm install needed!

<script type="module">
  import javascriptEventChannel from 'https://cdn.skypack.dev/javascript-event-channel';
</script>

README

javascript-event-channel

Установка

$ npm install javascript-event-channel

настройка в браузере

Настройте в вашем сервере резолв с node_modules/javascript-event-channel в /javascript-event-channel

<script type="importmap">
{
  "imports": {
    "javascript-event-channel": "/javascript-event-channel/library/Browser.js"
  }
}
</script>

Использование

import Channel from 'javascript-event-channel';
// node:
// import Channel from 'javascript-event-channel/librabry/Node.js';

const channel = new Channel();

channel.on('event', data => console.log('hello, ' + data));

channel.async('event', 1);
channel.send('event', 2);

// output:
// hello, 2
// hello, 1

Дополнительно (?)

Если вы используете vscode, можно настроить резолв для корректной работы самого редактора с помощью файла jsconfig.json

{
  "compilerOptions": {
    "baseUrl": "../node_modules/",
    "paths": {
      "javascript-event-channel/*": ["./javascript-event-channel/*"]
    }
  }
}