@graphprotocol/client-polling-live 1.0.2 → 1.1.0-alpha-20230227170621-080114d

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.
package/cjs/index.js CHANGED
@@ -4,20 +4,20 @@ const core_1 = require("@envelop/core");
4
4
  const graphql_1 = require("graphql");
5
5
  const repeater_1 = require("@repeaterjs/repeater");
6
6
  const schema_1 = require("@graphql-tools/schema");
7
- function usePollingLive({ config: { defaultInterval = 1000 } = {} } = {}) {
7
+ function usePollingLive({ config: { defaultInterval = 1000, defaultPauseOnBackground = true } = {}, } = {}) {
8
8
  return {
9
9
  onSchemaChange({ schema, replaceSchema }) {
10
10
  if (!schema.getDirective('live')) {
11
11
  replaceSchema((0, schema_1.mergeSchemas)({
12
12
  schemas: [schema],
13
13
  typeDefs: /* GraphQL */ `
14
- directive @live(interval: Int) on QUERY
14
+ directive @live(interval: Int = ${defaultInterval}, pauseOnBackground: Boolean = ${defaultPauseOnBackground}) on QUERY
15
15
  `,
16
16
  }));
17
17
  }
18
18
  },
19
19
  onExecute({ args, executeFn, setExecuteFn }) {
20
- var _a, _b;
20
+ var _a, _b, _c, _d;
21
21
  let liveDirectiveNode;
22
22
  args.document = (0, graphql_1.visit)(args.document, {
23
23
  OperationDefinition(node) {
@@ -45,31 +45,45 @@ function usePollingLive({ config: { defaultInterval = 1000 } = {} } = {}) {
45
45
  });
46
46
  if (liveDirectiveNode) {
47
47
  const intervalArgNode = (_a = liveDirectiveNode.arguments) === null || _a === void 0 ? void 0 : _a.find((argNode) => argNode.name.value === 'interval');
48
+ let pauseOnBackground = defaultPauseOnBackground;
49
+ const pauseOnBackgroundArgNode = (_b = liveDirectiveNode.arguments) === null || _b === void 0 ? void 0 : _b.find((argNode) => argNode.name.value === 'pauseOnBackground');
50
+ if (((_c = pauseOnBackgroundArgNode === null || pauseOnBackgroundArgNode === void 0 ? void 0 : pauseOnBackgroundArgNode.value) === null || _c === void 0 ? void 0 : _c.kind) === graphql_1.Kind.BOOLEAN) {
51
+ pauseOnBackground = pauseOnBackgroundArgNode.value.value;
52
+ }
48
53
  let intervalMs = defaultInterval;
49
- if (((_b = intervalArgNode === null || intervalArgNode === void 0 ? void 0 : intervalArgNode.value) === null || _b === void 0 ? void 0 : _b.kind) === graphql_1.Kind.INT) {
54
+ if (((_d = intervalArgNode === null || intervalArgNode === void 0 ? void 0 : intervalArgNode.value) === null || _d === void 0 ? void 0 : _d.kind) === graphql_1.Kind.INT) {
50
55
  intervalMs = parseInt(intervalArgNode.value.value);
51
56
  }
57
+ function checkHidden() {
58
+ var _a, _b;
59
+ if (!pauseOnBackground) {
60
+ return false;
61
+ }
62
+ return (_b = (_a = globalThis.document) === null || _a === void 0 ? void 0 : _a.hidden) !== null && _b !== void 0 ? _b : false;
63
+ }
52
64
  setExecuteFn((args) => new repeater_1.Repeater((push, stop) => {
53
65
  let finished = false;
54
66
  async function pump() {
55
67
  if (finished) {
56
68
  return;
57
69
  }
58
- const result = await executeFn(args);
59
- if ((0, core_1.isAsyncIterable)(result)) {
60
- push({
61
- data: null,
62
- errors: [new graphql_1.GraphQLError('Execution returned AsyncIterable which is not supported!')],
63
- isLive: true,
64
- });
65
- stop();
66
- return;
67
- }
68
- result.isLive = true;
69
- if (finished) {
70
- return;
70
+ if (!checkHidden()) {
71
+ const result = await executeFn(args);
72
+ if ((0, core_1.isAsyncIterable)(result)) {
73
+ push({
74
+ data: null,
75
+ errors: [new graphql_1.GraphQLError('Execution returned AsyncIterable which is not supported!')],
76
+ isLive: true,
77
+ });
78
+ stop();
79
+ return;
80
+ }
81
+ result.isLive = true;
82
+ if (finished) {
83
+ return;
84
+ }
85
+ push(result);
71
86
  }
72
- push(result);
73
87
  setTimeout(pump, intervalMs);
74
88
  }
75
89
  pump();
package/esm/index.js CHANGED
@@ -2,20 +2,20 @@ import { isAsyncIterable } from '@envelop/core';
2
2
  import { GraphQLError, Kind, visit } from 'graphql';
3
3
  import { Repeater } from '@repeaterjs/repeater';
4
4
  import { mergeSchemas } from '@graphql-tools/schema';
5
- export default function usePollingLive({ config: { defaultInterval = 1000 } = {} } = {}) {
5
+ export default function usePollingLive({ config: { defaultInterval = 1000, defaultPauseOnBackground = true } = {}, } = {}) {
6
6
  return {
7
7
  onSchemaChange({ schema, replaceSchema }) {
8
8
  if (!schema.getDirective('live')) {
9
9
  replaceSchema(mergeSchemas({
10
10
  schemas: [schema],
11
11
  typeDefs: /* GraphQL */ `
12
- directive @live(interval: Int) on QUERY
12
+ directive @live(interval: Int = ${defaultInterval}, pauseOnBackground: Boolean = ${defaultPauseOnBackground}) on QUERY
13
13
  `,
14
14
  }));
15
15
  }
16
16
  },
17
17
  onExecute({ args, executeFn, setExecuteFn }) {
18
- var _a, _b;
18
+ var _a, _b, _c, _d;
19
19
  let liveDirectiveNode;
20
20
  args.document = visit(args.document, {
21
21
  OperationDefinition(node) {
@@ -43,31 +43,45 @@ export default function usePollingLive({ config: { defaultInterval = 1000 } = {}
43
43
  });
44
44
  if (liveDirectiveNode) {
45
45
  const intervalArgNode = (_a = liveDirectiveNode.arguments) === null || _a === void 0 ? void 0 : _a.find((argNode) => argNode.name.value === 'interval');
46
+ let pauseOnBackground = defaultPauseOnBackground;
47
+ const pauseOnBackgroundArgNode = (_b = liveDirectiveNode.arguments) === null || _b === void 0 ? void 0 : _b.find((argNode) => argNode.name.value === 'pauseOnBackground');
48
+ if (((_c = pauseOnBackgroundArgNode === null || pauseOnBackgroundArgNode === void 0 ? void 0 : pauseOnBackgroundArgNode.value) === null || _c === void 0 ? void 0 : _c.kind) === Kind.BOOLEAN) {
49
+ pauseOnBackground = pauseOnBackgroundArgNode.value.value;
50
+ }
46
51
  let intervalMs = defaultInterval;
47
- if (((_b = intervalArgNode === null || intervalArgNode === void 0 ? void 0 : intervalArgNode.value) === null || _b === void 0 ? void 0 : _b.kind) === Kind.INT) {
52
+ if (((_d = intervalArgNode === null || intervalArgNode === void 0 ? void 0 : intervalArgNode.value) === null || _d === void 0 ? void 0 : _d.kind) === Kind.INT) {
48
53
  intervalMs = parseInt(intervalArgNode.value.value);
49
54
  }
55
+ function checkHidden() {
56
+ var _a, _b;
57
+ if (!pauseOnBackground) {
58
+ return false;
59
+ }
60
+ return (_b = (_a = globalThis.document) === null || _a === void 0 ? void 0 : _a.hidden) !== null && _b !== void 0 ? _b : false;
61
+ }
50
62
  setExecuteFn((args) => new Repeater((push, stop) => {
51
63
  let finished = false;
52
64
  async function pump() {
53
65
  if (finished) {
54
66
  return;
55
67
  }
56
- const result = await executeFn(args);
57
- if (isAsyncIterable(result)) {
58
- push({
59
- data: null,
60
- errors: [new GraphQLError('Execution returned AsyncIterable which is not supported!')],
61
- isLive: true,
62
- });
63
- stop();
64
- return;
65
- }
66
- result.isLive = true;
67
- if (finished) {
68
- return;
68
+ if (!checkHidden()) {
69
+ const result = await executeFn(args);
70
+ if (isAsyncIterable(result)) {
71
+ push({
72
+ data: null,
73
+ errors: [new GraphQLError('Execution returned AsyncIterable which is not supported!')],
74
+ isLive: true,
75
+ });
76
+ stop();
77
+ return;
78
+ }
79
+ result.isLive = true;
80
+ if (finished) {
81
+ return;
82
+ }
83
+ push(result);
69
84
  }
70
- push(result);
71
85
  setTimeout(pump, intervalMs);
72
86
  }
73
87
  pump();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphprotocol/client-polling-live",
3
- "version": "1.0.2",
3
+ "version": "1.1.0-alpha-20230227170621-080114d",
4
4
  "description": "",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {
@@ -1,6 +1,7 @@
1
1
  import { Plugin } from '@envelop/core';
2
- export default function usePollingLive({ config: { defaultInterval } }?: {
2
+ export default function usePollingLive({ config: { defaultInterval, defaultPauseOnBackground }, }?: {
3
3
  config?: {
4
4
  defaultInterval?: number | undefined;
5
+ defaultPauseOnBackground?: boolean | undefined;
5
6
  } | undefined;
6
7
  }): Plugin<{}>;
@@ -1,6 +1,7 @@
1
1
  import { Plugin } from '@envelop/core';
2
- export default function usePollingLive({ config: { defaultInterval } }?: {
2
+ export default function usePollingLive({ config: { defaultInterval, defaultPauseOnBackground }, }?: {
3
3
  config?: {
4
4
  defaultInterval?: number | undefined;
5
+ defaultPauseOnBackground?: boolean | undefined;
5
6
  } | undefined;
6
7
  }): Plugin<{}>;