@omegup/msync 0.0.39 → 0.0.41

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.esm.js +16 -6
  2. package/index.js +16 -6
  3. package/package.json +1 -1
package/index.esm.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { UUID, MongoClient } from 'mongodb';
2
2
  import { SynchronousPromise } from 'synchronous-promise';
3
3
  import crypto$1 from 'crypto';
4
+ import { log as log$1 } from 'console';
4
5
  import { writeFile } from 'fs/promises';
5
6
 
6
7
  const asExprRaw = (raw) => ({ get: () => raw });
@@ -1132,15 +1133,22 @@ const restart = (sources) => {
1132
1133
  return map(sources, x => x.stop());
1133
1134
  };
1134
1135
  const race = (sources) => {
1135
- const promises = Object.values(map(sources, ({ next }, key) => next.then(frame => ({ key, sources, frame }))));
1136
- return SynchronousPromise.any(promises);
1136
+ const promises = Object.values(map(sources, ({ next }, key) => next.then(frame => ({ key, frame }), error => new Error(error))));
1137
+ return SynchronousPromise.any(promises).then((result) => {
1138
+ if (result instanceof Error) {
1139
+ throw result;
1140
+ }
1141
+ else {
1142
+ return result;
1143
+ }
1144
+ });
1137
1145
  };
1138
1146
 
1139
1147
  const nextWinner = (previousWinner, previousWinnerNextFrame, sources, interrupt) => {
1140
1148
  const { frame: previousFrame, key } = previousWinner;
1141
1149
  if (!interrupt?.(key) && previousFrame.info.job) {
1142
- return previousWinnerNextFrame.then((previousFrameSuccessor) => {
1143
- if (previousFrameSuccessor.info.job) {
1150
+ return previousWinnerNextFrame.then(previousFrameSuccessor => {
1151
+ if (previousFrame.info.job === previousFrameSuccessor.info.job) {
1144
1152
  return { frame: previousFrameSuccessor, key };
1145
1153
  }
1146
1154
  return race(sources);
@@ -1206,7 +1214,7 @@ class Machine {
1206
1214
  return runCont(run, cb);
1207
1215
  }
1208
1216
  }
1209
- const wrap = (root) => root;
1217
+ const wrap = (root) => new Machine(root.runner());
1210
1218
  const runCont = async (it, cb) => {
1211
1219
  const { next, stop, clear } = it();
1212
1220
  const res = await next.then(next => ({ ok: true, next }), err => ({ ok: false, err }));
@@ -1452,6 +1460,8 @@ const makeWatchStream = (db, { collection, projection: p, hardMatch: m }, startA
1452
1460
  const doc = await stream.tryNext();
1453
1461
  if (doc)
1454
1462
  await new Promise(resolve => setTimeout(resolve, 100));
1463
+ if (doc)
1464
+ log$1('detected', doc);
1455
1465
  return doc;
1456
1466
  };
1457
1467
  return { tryNext, close: () => stream.close() };
@@ -1626,7 +1636,7 @@ const executes$1 = (view, input, streamName) => {
1626
1636
  const step8 = (l) => {
1627
1637
  return nextData(l.aggResult.cursor.firstBatch)(() => l.stream
1628
1638
  .tryNext()
1629
- .catch(_ => 1)
1639
+ .catch(err => { log('restarting', err); return 1; })
1630
1640
  .then(doc => (doc ? next(step2, 'restart') : step8(l))), 'wait for change');
1631
1641
  };
1632
1642
  return stop;
package/index.js CHANGED
@@ -3,6 +3,7 @@
3
3
  var mongodb = require('mongodb');
4
4
  var synchronousPromise = require('synchronous-promise');
5
5
  var crypto$1 = require('crypto');
6
+ var console$1 = require('console');
6
7
  var promises = require('fs/promises');
7
8
 
8
9
  const asExprRaw = (raw) => ({ get: () => raw });
@@ -1134,15 +1135,22 @@ const restart = (sources) => {
1134
1135
  return map(sources, x => x.stop());
1135
1136
  };
1136
1137
  const race = (sources) => {
1137
- const promises = Object.values(map(sources, ({ next }, key) => next.then(frame => ({ key, sources, frame }))));
1138
- return synchronousPromise.SynchronousPromise.any(promises);
1138
+ const promises = Object.values(map(sources, ({ next }, key) => next.then(frame => ({ key, frame }), error => new Error(error))));
1139
+ return synchronousPromise.SynchronousPromise.any(promises).then((result) => {
1140
+ if (result instanceof Error) {
1141
+ throw result;
1142
+ }
1143
+ else {
1144
+ return result;
1145
+ }
1146
+ });
1139
1147
  };
1140
1148
 
1141
1149
  const nextWinner = (previousWinner, previousWinnerNextFrame, sources, interrupt) => {
1142
1150
  const { frame: previousFrame, key } = previousWinner;
1143
1151
  if (!interrupt?.(key) && previousFrame.info.job) {
1144
- return previousWinnerNextFrame.then((previousFrameSuccessor) => {
1145
- if (previousFrameSuccessor.info.job) {
1152
+ return previousWinnerNextFrame.then(previousFrameSuccessor => {
1153
+ if (previousFrame.info.job === previousFrameSuccessor.info.job) {
1146
1154
  return { frame: previousFrameSuccessor, key };
1147
1155
  }
1148
1156
  return race(sources);
@@ -1208,7 +1216,7 @@ class Machine {
1208
1216
  return runCont(run, cb);
1209
1217
  }
1210
1218
  }
1211
- const wrap = (root) => root;
1219
+ const wrap = (root) => new Machine(root.runner());
1212
1220
  const runCont = async (it, cb) => {
1213
1221
  const { next, stop, clear } = it();
1214
1222
  const res = await next.then(next => ({ ok: true, next }), err => ({ ok: false, err }));
@@ -1454,6 +1462,8 @@ const makeWatchStream = (db, { collection, projection: p, hardMatch: m }, startA
1454
1462
  const doc = await stream.tryNext();
1455
1463
  if (doc)
1456
1464
  await new Promise(resolve => setTimeout(resolve, 100));
1465
+ if (doc)
1466
+ console$1.log('detected', doc);
1457
1467
  return doc;
1458
1468
  };
1459
1469
  return { tryNext, close: () => stream.close() };
@@ -1628,7 +1638,7 @@ const executes$1 = (view, input, streamName) => {
1628
1638
  const step8 = (l) => {
1629
1639
  return nextData(l.aggResult.cursor.firstBatch)(() => l.stream
1630
1640
  .tryNext()
1631
- .catch(_ => 1)
1641
+ .catch(err => { log('restarting', err); return 1; })
1632
1642
  .then(doc => (doc ? next(step2, 'restart') : step8(l))), 'wait for change');
1633
1643
  };
1634
1644
  return stop;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "module": "index.esm.js",
4
4
  "typings": "index.d.ts",
5
5
  "name": "@omegup/msync",
6
- "version": "0.0.39",
6
+ "version": "0.0.41",
7
7
  "dependencies": {
8
8
  "dayjs": "^1.11.9",
9
9
  "dotenv": "^16.3.1",