Socket
Socket
Sign inDemoInstall

@n1ru4l/in-memory-live-query-store

Package Overview
Dependencies
5
Maintainers
1
Versions
51
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.8.0 to 0.9.0-alpha-3ba5599.0

92

esm/index.js
import { visit, visitWithTypeInfo, isNonNullType, isScalarType, execute, getOperationAST, GraphQLError, defaultFieldResolver, TypeInfo } from 'graphql';
import { mapSchema, MapperKind } from '@graphql-tools/utils';
import { makePushPullAsyncIterableIterator, isAsyncIterable } from '@n1ru4l/push-pull-async-iterable-iterator';
import { mapSchema, MapperKind, isAsyncIterable } from '@graphql-tools/utils';
import { Repeater } from '@repeaterjs/repeater';
import { getLiveDirectiveNode, getLiveDirectiveArgumentValues } from '@n1ru4l/graphql-live-query';

@@ -176,3 +176,2 @@ import { getArgumentValues } from 'graphql/execution/values.js';

var _a, _b;
const ORIGINAL_CONTEXT_SYMBOL = Symbol("ORIGINAL_CONTEXT");

@@ -207,5 +206,2 @@ const addResourceIdentifierCollectorToSchema = (schema, idFieldName) => mapSchema(schema, {

const defaultResourceIdentifierNormalizer = (params) => `${params.typename}:${params.id}`;
const nextTick = (_b = (_a = (typeof process === "object" && typeof process.nextTick === "function"
? process.nextTick
: undefined)) !== null && _a !== void 0 ? _a : setImmediate) !== null && _b !== void 0 ? _b : setTimeout;
class InMemoryLiveQueryStore {

@@ -265,14 +261,22 @@ constructor(params) {

}));
const { asyncIterableIterator: iterator, pushValue } = makePushPullAsyncIterableIterator();
// keep track that current execution is the latest in order to prevent race-conditions :)
let executionCounter = 0;
let previousIdentifier = new Set(rootFieldIdentifier);
const record = {
iterator,
pushValue,
run: () => {
const context = this;
return new Repeater(async function liveQueryRepeater(push, onStop) {
// utils for throttle
let cancelThrottle;
let run;
let executionCounter = 0;
let previousIdentifier = new Set(rootFieldIdentifier);
function scheduleRun() {
run();
}
function dispose() {
cancelThrottle === null || cancelThrottle === void 0 ? void 0 : cancelThrottle();
context._resourceTracker.release(scheduleRun, previousIdentifier);
}
onStop.then(dispose);
run = function run() {
executionCounter = executionCounter + 1;
const counter = executionCounter;
const newIdentifier = new Set(rootFieldIdentifier);
const collectResourceIdentifier = (parameter) => newIdentifier.add(this._buildResourceIdentifier(parameter));
const collectResourceIdentifier = (parameter) => newIdentifier.add(context._buildResourceIdentifier(parameter));
const addResourceIdentifier = (values) => {

@@ -304,30 +308,13 @@ if (isNone(values)) {

});
// result cannot be a AsyncIterableIterator if the `NoLiveMixedWithDeferStreamRule` was used.
// in case anyone forgot to add it we just panic and stop the execution :)
const handleAsyncIterator = (iterator) => {
var _a;
(_a = iterator.return) === null || _a === void 0 ? void 0 : _a.call(iterator);
record.pushValue({
errors: [
new GraphQLError(`"execute" returned a AsyncIterator instead of a MaybePromise<ExecutionResult>. The "NoLiveMixedWithDeferStreamRule" rule might have been skipped.`),
],
});
// delay to next tick to ensure the error is delivered to listeners.
// TODO: figure out whether there is a better way for doing this.
nextTick(() => {
record.iterator.return();
});
this._resourceTracker.release(record, previousIdentifier);
};
runWith(result, (result) => {
if (isAsyncIterable(result)) {
handleAsyncIterator(result);
onStop(new Error(`"execute" returned a AsyncIterator instead of a MaybePromise<ExecutionResult>. The "NoLiveMixedWithDeferStreamRule" rule might have been skipped.`));
return;
}
if (counter === executionCounter) {
this._resourceTracker.track(record, previousIdentifier, newIdentifier);
context._resourceTracker.track(scheduleRun, previousIdentifier, newIdentifier);
previousIdentifier = newIdentifier;
const liveResult = result;
liveResult.isLive = true;
if (this._includeIdentifierExtension === true) {
if (context._includeIdentifierExtension === true) {
if (!liveResult.extensions) {

@@ -339,25 +326,14 @@ liveResult.extensions = {};

}
record.pushValue(liveResult);
push(liveResult);
}
});
},
};
// utils for throttle
let cancelThrottle;
if (isSome(throttleValue)) {
const { run, cancel } = throttle(record.run, throttleValue);
record.run = run;
cancelThrottle = cancel;
}
this._resourceTracker.register(record, previousIdentifier);
// Execute initial query
record.run();
// TODO: figure out how we can do this stuff without monkey-patching the iterator
const originalReturn = iterator.return.bind(iterator);
iterator.return = () => {
cancelThrottle === null || cancelThrottle === void 0 ? void 0 : cancelThrottle();
this._resourceTracker.release(record, previousIdentifier);
return originalReturn();
};
return iterator;
};
if (isSome(throttleValue)) {
const throttled = throttle(run, throttleValue);
run = throttled.run;
cancelThrottle = throttled.cancel;
}
context._resourceTracker.register(scheduleRun, previousIdentifier);
scheduleRun();
});
};

@@ -404,4 +380,4 @@ /** @deprecated Please use InMemoryLiveQueryStore.makeExecute instead. */

const records = this._resourceTracker.getRecordsForIdentifiers(identifiers);
for (const record of records) {
record.run();
for (const run of records) {
run();
}

@@ -408,0 +384,0 @@ }

@@ -7,3 +7,3 @@ 'use strict';

const utils = require('@graphql-tools/utils');
const pushPullAsyncIterableIterator = require('@n1ru4l/push-pull-async-iterable-iterator');
const repeater = require('@repeaterjs/repeater');
const graphqlLiveQuery = require('@n1ru4l/graphql-live-query');

@@ -181,3 +181,2 @@ const values_js = require('graphql/execution/values.js');

var _a, _b;
const ORIGINAL_CONTEXT_SYMBOL = Symbol("ORIGINAL_CONTEXT");

@@ -212,5 +211,2 @@ const addResourceIdentifierCollectorToSchema = (schema, idFieldName) => utils.mapSchema(schema, {

const defaultResourceIdentifierNormalizer = (params) => `${params.typename}:${params.id}`;
const nextTick = (_b = (_a = (typeof process === "object" && typeof process.nextTick === "function"
? process.nextTick
: undefined)) !== null && _a !== void 0 ? _a : setImmediate) !== null && _b !== void 0 ? _b : setTimeout;
class InMemoryLiveQueryStore {

@@ -270,14 +266,22 @@ constructor(params) {

}));
const { asyncIterableIterator: iterator, pushValue } = pushPullAsyncIterableIterator.makePushPullAsyncIterableIterator();
// keep track that current execution is the latest in order to prevent race-conditions :)
let executionCounter = 0;
let previousIdentifier = new Set(rootFieldIdentifier);
const record = {
iterator,
pushValue,
run: () => {
const context = this;
return new repeater.Repeater(async function liveQueryRepeater(push, onStop) {
// utils for throttle
let cancelThrottle;
let run;
let executionCounter = 0;
let previousIdentifier = new Set(rootFieldIdentifier);
function scheduleRun() {
run();
}
function dispose() {
cancelThrottle === null || cancelThrottle === void 0 ? void 0 : cancelThrottle();
context._resourceTracker.release(scheduleRun, previousIdentifier);
}
onStop.then(dispose);
run = function run() {
executionCounter = executionCounter + 1;
const counter = executionCounter;
const newIdentifier = new Set(rootFieldIdentifier);
const collectResourceIdentifier = (parameter) => newIdentifier.add(this._buildResourceIdentifier(parameter));
const collectResourceIdentifier = (parameter) => newIdentifier.add(context._buildResourceIdentifier(parameter));
const addResourceIdentifier = (values) => {

@@ -309,30 +313,13 @@ if (isNone(values)) {

});
// result cannot be a AsyncIterableIterator if the `NoLiveMixedWithDeferStreamRule` was used.
// in case anyone forgot to add it we just panic and stop the execution :)
const handleAsyncIterator = (iterator) => {
var _a;
(_a = iterator.return) === null || _a === void 0 ? void 0 : _a.call(iterator);
record.pushValue({
errors: [
new graphql.GraphQLError(`"execute" returned a AsyncIterator instead of a MaybePromise<ExecutionResult>. The "NoLiveMixedWithDeferStreamRule" rule might have been skipped.`),
],
});
// delay to next tick to ensure the error is delivered to listeners.
// TODO: figure out whether there is a better way for doing this.
nextTick(() => {
record.iterator.return();
});
this._resourceTracker.release(record, previousIdentifier);
};
runWith(result, (result) => {
if (pushPullAsyncIterableIterator.isAsyncIterable(result)) {
handleAsyncIterator(result);
if (utils.isAsyncIterable(result)) {
onStop(new Error(`"execute" returned a AsyncIterator instead of a MaybePromise<ExecutionResult>. The "NoLiveMixedWithDeferStreamRule" rule might have been skipped.`));
return;
}
if (counter === executionCounter) {
this._resourceTracker.track(record, previousIdentifier, newIdentifier);
context._resourceTracker.track(scheduleRun, previousIdentifier, newIdentifier);
previousIdentifier = newIdentifier;
const liveResult = result;
liveResult.isLive = true;
if (this._includeIdentifierExtension === true) {
if (context._includeIdentifierExtension === true) {
if (!liveResult.extensions) {

@@ -344,25 +331,14 @@ liveResult.extensions = {};

}
record.pushValue(liveResult);
push(liveResult);
}
});
},
};
// utils for throttle
let cancelThrottle;
if (isSome(throttleValue)) {
const { run, cancel } = throttle(record.run, throttleValue);
record.run = run;
cancelThrottle = cancel;
}
this._resourceTracker.register(record, previousIdentifier);
// Execute initial query
record.run();
// TODO: figure out how we can do this stuff without monkey-patching the iterator
const originalReturn = iterator.return.bind(iterator);
iterator.return = () => {
cancelThrottle === null || cancelThrottle === void 0 ? void 0 : cancelThrottle();
this._resourceTracker.release(record, previousIdentifier);
return originalReturn();
};
return iterator;
};
if (isSome(throttleValue)) {
const throttled = throttle(run, throttleValue);
run = throttled.run;
cancelThrottle = throttled.cancel;
}
context._resourceTracker.register(scheduleRun, previousIdentifier);
scheduleRun();
});
};

@@ -409,4 +385,4 @@ /** @deprecated Please use InMemoryLiveQueryStore.makeExecute instead. */

const records = this._resourceTracker.getRecordsForIdentifiers(identifiers);
for (const record of records) {
record.run();
for (const run of records) {
run();
}

@@ -413,0 +389,0 @@ }

{
"name": "@n1ru4l/in-memory-live-query-store",
"version": "0.8.0",
"version": "0.9.0-alpha-3ba5599.0",
"peerDependencies": {

@@ -10,3 +10,3 @@ "graphql": "^15.4.0 || ^16.0.0"

"@n1ru4l/graphql-live-query": "0.9.0",
"@n1ru4l/push-pull-async-iterable-iterator": "^3.0.0"
"@repeaterjs/repeater": "^3.0.4"
},

@@ -13,0 +13,0 @@ "repository": {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc