diff --git a/package.json b/package.json index 0b29b0d..569becc 100644 --- a/package.json +++ b/package.json @@ -43,7 +43,8 @@ "camelcase": 0, "block-scoped-var": 0, "no-sequences": 0, - "no-negated-condition": 0 + "no-negated-condition": 0, + "no-async-promise-executor": 0 }, "ignores": [ "docs/**" diff --git a/src/orm/hasura.js b/src/orm/hasura.js index 8afaf02..7471e37 100644 --- a/src/orm/hasura.js +++ b/src/orm/hasura.js @@ -433,6 +433,32 @@ class Hasura { return unsub; // Unsubscribe } + async subscribeToMore(parameters, callback, settings = {}) { + settings = __.mergeDeep( + { + passFirst: false, + }, + this.params.query, + settings, + ); + + callback(await this.query(parameters, settings)); + + let firstPassed = settings.passFirst; + const subSettings = __.mergeDeep({}, this.params.subscribe, settings); + return this.subscribe( + parameters, + (response) => { + if (firstPassed === true) { + callback(response); + } else { + firstPassed = true; + } + }, + subSettings, + ); + } + /* Settings flatOne diff --git a/tests/subscription.js b/tests/subscription.js index 2a64817..29ed8ba 100644 --- a/tests/subscription.js +++ b/tests/subscription.js @@ -1,6 +1,7 @@ require('dotenv').config(); const test = require('ava'); const {Hasura} = require('../src'); +const __ = require('../src/utils/helpers'); test.before(async (t) => { const orm = new Hasura({ @@ -102,6 +103,7 @@ test.serial('test sub', async (t) => { }); t.is(err, null); + __.sleep(1000); const id = response[0].id; let responseCount = 0; @@ -127,7 +129,7 @@ test.serial('test sub', async (t) => { } }, 500); - const unsub = orm.subscribe( + orm.subscribe( { _om_test: { where: { @@ -139,11 +141,14 @@ test.serial('test sub', async (t) => { }, ([err, data]) => { t.is(err, null); - t.true(Array.isArray(data)); - t.is(data[0].text, 'test_sub'); - - t.is(data[0].increment, 15 + responseCount * 10); - responseCount++; + if (data && data.length > 0) { + t.true(Array.isArray(data)); + t.is(data[0].text, 'test_sub'); + t.is(data[0].increment, 15 + responseCount * 10); + responseCount++; + } else { + t.log(data); + } if (responseCount >= 2) { resolve(); @@ -233,3 +238,83 @@ test.serial('test events connect/disconnect without lazy connection', (t) => { }, 100); }); }); + +test.serial('subscribe to more returns unsub', (t) => { + t.timeout(1000); + const orm = t.context.orm; + + return new Promise(async (resolve) => { + const unsub = await orm.subscribeToMore( + { + _om_test: {}, + }, + ([err, data]) => { + t.is(err, null); + t.true(Array.isArray(data)); + + resolve(); + }, + ); + t.true(typeof unsub === 'function'); + }); +}); + +test.serial('subscribe to more works', (t) => { + t.timeout(2000); + let events = 0; + const orm = t.context.orm; + + return new Promise(async (resolve) => { + const unsub = await orm.subscribeToMore( + { + _om_test: {}, + }, + ([err, data]) => { + events++; + t.is(err, null); + t.true(Array.isArray(data)); + + if (events === 2) resolve(); + }, + ); + t.true(typeof unsub === 'function'); + + setTimeout(async () => { + await orm.mutate({ + _om_test: { + insert: { + objects: { + text: 'test_sub', + increment: 15, + }, + }, + }, + }); + }, 1100); + }); +}); + +test.serial('subscribe to more firstPass', (t) => { + t.timeout(2000); + let events = 0; + const orm = t.context.orm; + + return new Promise(async (resolve) => { + const unsub = await orm.subscribeToMore( + { + _om_test: {}, + }, + ([err, data]) => { + events++; + t.is(err, null); + t.true(Array.isArray(data)); + + if (events === 2) resolve(); + }, + { + passFirst: true, + }, + ); + t.true(typeof unsub === 'function'); + }); +});