Skip to content

Commit

Permalink
Merge branch 'metarhia:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
lostcodder authored May 21, 2024
2 parents d25e895 + 6199ac6 commit fd130cd
Show file tree
Hide file tree
Showing 9 changed files with 612 additions and 996 deletions.
6 changes: 1 addition & 5 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
name: Testing CI

on: pull_request

jobs:
build:
runs-on: ${{ matrix.os }}

strategy:
matrix:
node:
- 18
- 20
- 21
os:
- ubuntu-latest

services:
redis:
image: redis:7
ports:
- 6379:6379

steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node }}
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020-2023 Metarhia contributors
Copyright (c) 2020-2024 Metarhia contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,5 @@ let's start with

## License

Copyright (c) 2020-2023 Metarhia contributors.
Copyright (c) 2020-2024 Metarhia contributors.
This starter kit is [MIT licensed](./LICENSE).
6 changes: 4 additions & 2 deletions application/db/redis/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ async () => {
}
const client = npm.redis.createClient(config.redis);
db.redis.client = client;
client.on('error', () => {
client.on('error', async (error) => {
if (application.worker.id === 'W1') {
console.warn('No redis service detected, so quit client');
const err = new Error('No redis', { cause: error });
console.error(err);
await client.disconnect();
}
client.quit();
});
await client.connect();
};
15 changes: 8 additions & 7 deletions application/static/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class EventEmitter {
once(name, fn) {
const dispose = (...args) => {
this.remove(name, dispose);
return fn(...args);
return void fn(...args);
};
this.on(name, dispose);
}
Expand All @@ -50,17 +50,18 @@ class EventEmitter {
remove(name, fn) {
const event = this.events.get(name);
if (!event) return;
if (event.has(fn)) event.delete(fn);
event.delete(fn);
}

clear(name) {
if (!name) return void this.events.clear();
this.events.delete(name);
}

static once(emitter, name) {
return new Promise((resolve) => emitter.once(name, resolve));
}
}

export default EventEmitter;
const once = (emitter, name) =>
new Promise((resolve) => {
emitter.once(name, resolve);
});

export { EventEmitter, once };
20 changes: 10 additions & 10 deletions application/static/metacom.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import EventEmitter from './events.js';
import { EventEmitter } from './events.js';
import { chunkDecode, MetaReadable, MetaWritable } from './streams.js';

const CALL_TIMEOUT = 7 * 1000;
Expand All @@ -7,11 +7,13 @@ const RECONNECT_TIMEOUT = 2 * 1000;

const connections = new Set();

window.addEventListener('online', () => {
for (const connection of connections) {
if (!connection.connected) connection.open();
}
});
if (window) {
window.addEventListener('online', () => {
for (const connection of connections) {
if (!connection.connected) connection.open();
}
});
}

class MetacomError extends Error {
constructor({ message, code }) {
Expand Down Expand Up @@ -62,9 +64,8 @@ class Metacom extends EventEmitter {

createStream(name, size) {
const id = ++this.streamId;
const initData = { type: 'stream', id, name, size };
const transport = this;
return new MetaWritable(transport, initData);
return new MetaWritable(id, name, size, transport);
}

createBlobUploader(blob) {
Expand Down Expand Up @@ -121,8 +122,7 @@ class Metacom extends EventEmitter {
if (stream) {
console.error(new Error(`Stream ${name} is already initialized`));
} else {
const streamData = { id, name, size };
const stream = new MetaReadable(streamData);
const stream = new MetaReadable(id, name, size);
this.streams.set(id, stream);
}
} else if (!stream) {
Expand Down
10 changes: 5 additions & 5 deletions application/static/streams.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import EventEmitter from '/events.js';
import { EventEmitter } from './events.js';

const ID_LENGTH = 4;

Expand Down Expand Up @@ -132,12 +132,12 @@ class MetaReadable extends EventEmitter {
}

class MetaWritable extends EventEmitter {
constructor(transport, options = {}) {
constructor(id, name, size, transport) {
super();
this.id = id;
this.name = name;
this.size = size;
this.transport = transport;
this.id = options.id;
this.name = options.name;
this.size = options.size;
this.init();
}

Expand Down
Loading

0 comments on commit fd130cd

Please sign in to comment.