New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

accordant

Package Overview
Dependencies
Maintainers
0
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

accordant - npm Package Compare versions

Comparing version

to
0.2.1

src/references.js

2

package.json
{
"name": "accordant",
"version": "0.2.0",
"version": "0.2.1",
"keywords": [

@@ -5,0 +5,0 @@ "Worker",

@@ -13,4 +13,5 @@ # accordant

import { SharedWorker, Worker, broadcast, proxied } from 'accordant/main';
const sw = new SharedWorker('./shared-worker.js', {
// invoked per each broadcast
// invoked per each broadcast (except its own one)
[broadcast](...args) {

@@ -20,4 +21,5 @@ console.log('SharedWorker', ...args);

});
const w = new Worker('./worker.js', {
// invoked per each broadcast
// invoked per each broadcast (from the worker.js only)
[broadcast](...args) {

@@ -27,2 +29,3 @@ console.log('Worker', ...args);

});
// both workers export a `random` method

@@ -33,2 +36,5 @@ console.log(await sw.random());

console.log(proxied(w)); // Worker
// broadcast any data to other ports (but not sw)
sw[broadcast](1, 2, 3);
</script>

@@ -72,1 +78,26 @@ </head>

```
## broadcast
This module offers the current possibilities:
* on the **main thread**, it is possible to import the `broadcast` **symbol** to help avoiding conflicts with both exported functions (because *symbols* cannot survive a *postMessage* dance) and *SharedWorker* or *Worker* options (future proof, no name clashing). This function will be triggered when the counter *SharedWorker* or *Worker* code decides, arbitrarily, to reflect that invoke on each *main* thread/port, passing along any serializable argument
* on the **SharedWorker** or **Worker** thread, it is possible to import the `broadcast` **function**, so that a call such as `broadcast(...args)` within the *worker* context will invoke, if defined, the *main thread* callback optionally passed during instantiation. In here it is a function because polluting the global worker context with a symbol didn't feel like the right thing to do
```js
// main thread
const sw = new SharedWorker('./shared-worker.js', {
[broadcast](...args) {
// invoked when shared-worker.js calls broadcast(...args)
}
});
const w = new Worker('./worker.js', {
[broadcast](...args) {
// invoked when worker.js calls broadcast(...args)
}
});
```
Still on the **main thread**, it is also possible to `sw[broadcast](...args)` so that all other ports still listening or available on the *Shared Worker* side of affairs will receive those serialized `args`.
Please note that for feature parity it is also possible to `w[broadcast](...args)` but this does practically nothing because a worker cannot have multiple ports attached so it will silently send data to nothing but it allow code to be portable across platforms and browsers' versions.
import { broadcast, isArray, stop } from './utils.js';
import Transferable from './transferable.js';
import references from './references.js';

@@ -20,2 +21,11 @@ class EventHandler {

const [channel, id, name, args] = data;
if (id === 0 && channel === name) {
for (const [wr, eh] of [...references]) {
while (!eh[broadcast]) await forIt();
const channel = eh[broadcast];
if (channel !== name)
wr.deref()?.postMessage([channel, 0, channel, args]);
}
return;
}
const response = [channel, id];

@@ -22,0 +32,0 @@ const send = [response];

@@ -11,7 +11,12 @@ import { broadcast, isArray, stop, withResolvers } from './utils.js';

return async (...args) => {
const channel = await this.init, id = this.uid++;
const { promise, resolve, reject } = withResolvers();
this.ids.set(id, r => (r instanceof Error ? reject : resolve)(r));
port.postMessage([channel, id, name, args]);
return promise;
const channel = await this.init;
if (name === broadcast)
port.postMessage([channel, 0, channel, args]);
else {
const id = this.uid++;
const { promise, resolve, reject } = withResolvers();
this.ids.set(id, r => (r instanceof Error ? reject : resolve)(r));
port.postMessage([channel, id, name, args]);
return promise;
}
};

@@ -18,0 +23,0 @@ },

import { assign, broadcast , forIt, withResolvers } from './utils.js';
import accordant from './accordant.js';
import references from './references.js';

@@ -7,3 +8,2 @@ const { promise, resolve } = withResolvers();

const ffi = {};
const references = new Map;
const notify = connected => {

@@ -10,0 +10,0 @@ const type = `port:${connected ? 'connected' : 'disconnected'}`;

@@ -6,2 +6,18 @@ import { broadcast, exports } from '../src/shared.js';

import initSQLite from 'https://cdn.jsdelivr.net/npm/@webreflection/sql.js/database.js';
const Database = await initSQLite('accordant');
const db = new Database('test.db');
db.run('CREATE TABLE IF NOT EXISTS hello (a int, b char)');
db.each('SELECT COUNT(*) AS fields FROM hello', null, async row => {
if (!row.fields) {
db.run(`INSERT INTO hello VALUES (0, 'hello')`);
db.run(`INSERT INTO hello VALUES (1, 'world')`);
await db.save();
}
db.each('SELECT * FROM hello', null, console.log);
});
exports({

@@ -8,0 +24,0 @@ random: () => ({ SharedWorker }),

@@ -0,1 +1,2 @@

import asTemplateStringsArray from 'https://esm.run/template-strings-array';
import { broadcast, exports } from '../src/worker.js';

@@ -9,4 +10,8 @@

random: () => ({ Worker: Math.random() }),
tag(template, ...values) {
template = asTemplateStringsArray(template);
console.log({ template, values });
}
});
broadcast('worker:connected');

Sorry, the diff of this file is not supported yet