Skip to content

Commit

Permalink
Added unsubscribe for missing subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
diyaayay authored and jdesrosiers committed Jul 12, 2024
1 parent af45af5 commit b100420
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
7 changes: 5 additions & 2 deletions language-server/src/features/deprecated.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { DiagnosticSeverity, DiagnosticTag } from "vscode-languageserver";
import * as SchemaNode from "../schema-node.js";
import { subscribe } from "../pubsub.js";
import { subscribe, unsubscribe } from "../pubsub.js";

/** @import { Feature } from "../build-server.js" */


const annotationDialectUri = "https://json-schema.org/draft/2020-12/schema";
/** @type string */
let subscriptionToken;

/** @type Feature */
export default {
load() {
subscribe("diagnostics", async (_message, { schemaDocument, diagnostics }) => {
subscriptionToken = subscribe("diagnostics", async (_message, { schemaDocument, diagnostics }) => {
for (const schemaResource of schemaDocument.schemaResources) {
for (const deprecated of SchemaNode.annotatedWith(schemaResource, "deprecated", annotationDialectUri)) {
if (SchemaNode.annotation(deprecated, "deprecated", annotationDialectUri).some((deprecated) => deprecated)) {
Expand All @@ -34,5 +36,6 @@ export default {
},

onShutdown() {
unsubscribe("diagnostics", subscriptionToken);
}
};
7 changes: 5 additions & 2 deletions language-server/src/features/if-then-completion.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { CompletionItemKind, InsertTextFormat } from "vscode-languageserver";
import * as SchemaDocument from "../schema-document.js";
import { subscribe } from "../pubsub.js";
import { subscribe, unsubscribe } from "../pubsub.js";

/** @import { Feature } from "../build-server.js" */

/** @type string */
let subscriptionToken;

/** @type Feature */
export default {
load() {
subscribe("completions", async (_message, { schemaDocument, offset, completions }) => {
subscriptionToken = subscribe("completions", async (_message, { schemaDocument, offset, completions }) => {
const currentProperty = SchemaDocument.findNodeAtOffset(schemaDocument, offset);
if (currentProperty && currentProperty.pointer.endsWith("/if") && currentProperty.type === "property") {
completions.push(...ifThenPatternCompletion);
Expand All @@ -24,6 +26,7 @@ export default {
},

onShutdown() {
unsubscribe("completions", subscriptionToken);
}
};

Expand Down
7 changes: 5 additions & 2 deletions language-server/src/features/schema-completion.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { CompletionItemKind } from "vscode-languageserver";
import { getDialectIds } from "@hyperjump/json-schema/experimental";
import * as SchemaDocument from "../schema-document.js";
import { subscribe } from "../pubsub.js";
import { subscribe, unsubscribe } from "../pubsub.js";

/** @import { Feature } from "../build-server.js"; */

/** @type string */
let subscriptionToken;

/** @type Feature */
export default {
Expand All @@ -16,7 +18,7 @@ export default {
]);
const shouldHaveTrailingHash = (/** @type string */ uri) => trailingHashDialects.has(uri);

subscribe("completions", async (_message, { schemaDocument, offset, completions }) => {
subscriptionToken = subscribe("completions", async (_message, { schemaDocument, offset, completions }) => {
const currentProperty = SchemaDocument.findNodeAtOffset(schemaDocument, offset);
if (currentProperty && currentProperty.pointer.endsWith("/$schema") && currentProperty.type === "string") {
completions.push(...getDialectIds().map((uri) => ({
Expand All @@ -35,5 +37,6 @@ export default {
},

onShutdown() {
unsubscribe("completions", subscriptionToken);
}
};
8 changes: 6 additions & 2 deletions language-server/src/features/validate-references.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as SchemaNode from "../schema-node.js";
import { subscribe } from "../pubsub.js";
import { subscribe, unsubscribe } from "../pubsub.js";
import { keywordNameFor } from "../util.js";

/**
Expand All @@ -8,10 +8,13 @@ import { keywordNameFor } from "../util.js";
*/


/** @type string */
let subscriptionToken;

/** @type Feature */
export default {
load() {
subscribe("diagnostics", async (_message, { schemaDocument, diagnostics }) => {
subscriptionToken = subscribe("diagnostics", async (_message, { schemaDocument, diagnostics }) => {
for (const schemaResource of schemaDocument.schemaResources) {
for (const node of references(schemaResource)) {
const reference = SchemaNode.value(node);
Expand All @@ -32,6 +35,7 @@ export default {
},

onShutdown() {
unsubscribe("diagnostics", subscriptionToken);
}
};

Expand Down

0 comments on commit b100420

Please sign in to comment.