vue-datocms 4.0.0 → 4.0.1

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/dist/index.cjs.js CHANGED
@@ -663,46 +663,34 @@ const DatocmsStructuredTextPlugin = {
663
663
  }
664
664
  };
665
665
 
666
- function useQuerySubscription(options) {
667
- const { enabled, initialData, ...other } = options;
666
+ const useQuerySubscription = ({ enabled = true, initialData, query, token, ...other }) => {
668
667
  const error = vueDemi.ref(null);
669
- const data = vueDemi.ref(null);
670
- const status = vueDemi.ref(
671
- enabled ? "connecting" : "closed"
672
- );
668
+ const data = vueDemi.ref(vueDemi.unref(initialData) || null);
669
+ const status = vueDemi.ref(vueDemi.unref(enabled) ? "connecting" : "closed");
673
670
  const subscribeToQueryOptions = other;
674
- vueDemi.watchEffect((onCleanup) => {
675
- if (enabled === false) {
676
- status.value = "closed";
677
- return () => {
678
- };
679
- }
680
- let unsubscribe;
681
- async function subscribe() {
682
- unsubscribe = await datocmsListen.subscribeToQuery({
671
+ vueDemi.watchEffect(async (onCleanup) => {
672
+ if (query && token && vueDemi.unref(enabled)) {
673
+ const unsubscribe = await datocmsListen.subscribeToQuery({
683
674
  ...subscribeToQueryOptions,
675
+ query,
676
+ token,
684
677
  onStatusChange: (connectionStatus) => {
685
678
  status.value = connectionStatus;
686
679
  },
687
- onUpdate: (updateData) => {
680
+ onUpdate: ({ response }) => {
688
681
  error.value = null;
689
- data.value = updateData.response.data;
682
+ data.value = response.data;
690
683
  },
691
684
  onChannelError: (errorData) => {
692
685
  data.value = null;
693
686
  error.value = errorData;
694
687
  }
695
688
  });
689
+ onCleanup(unsubscribe);
696
690
  }
697
- subscribe();
698
- onCleanup(() => {
699
- if (unsubscribe) {
700
- unsubscribe();
701
- }
702
- });
703
691
  });
704
- return { error, status, data: data || initialData };
705
- }
692
+ return { data, status, error };
693
+ };
706
694
 
707
695
  const highlightPieces = (textWithHighlightMarker) => {
708
696
  return textWithHighlightMarker.split(/\[h\](.+?)\[\/h\]/g).map((text, index) => ({
package/dist/index.d.ts CHANGED
@@ -381,26 +381,26 @@ declare const DatocmsStructuredTextPlugin: {
381
381
  declare type SubscribeToQueryOptions<QueryResult, QueryVariables> = Omit<Options<QueryResult, QueryVariables>, 'onStatusChange' | 'onUpdate' | 'onChannelError'>;
382
382
  declare type EnabledQueryListenerOptions<QueryResult, QueryVariables> = {
383
383
  /** Whether the subscription has to be performed or not */
384
- enabled?: true;
384
+ enabled?: true | Ref<boolean>;
385
385
  /** The initial data to use while the initial request is being performed */
386
386
  initialData?: QueryResult;
387
387
  } & SubscribeToQueryOptions<QueryResult, QueryVariables>;
388
388
  declare type DisabledQueryListenerOptions<QueryResult, QueryVariables> = {
389
389
  /** Whether the subscription has to be performed or not */
390
- enabled: false;
390
+ enabled: false | Ref<boolean>;
391
391
  /** The initial data to use while the initial request is being performed */
392
392
  initialData?: QueryResult;
393
393
  } & Partial<SubscribeToQueryOptions<QueryResult, QueryVariables>>;
394
394
  declare type QueryListenerOptions<QueryResult, QueryVariables> = EnabledQueryListenerOptions<QueryResult, QueryVariables> | DisabledQueryListenerOptions<QueryResult, QueryVariables>;
395
- declare function useQuerySubscription<QueryResult = any, QueryVariables = Record<string, any>>(options: QueryListenerOptions<QueryResult, QueryVariables>): {
395
+ declare const useQuerySubscription: <QueryResult = any, QueryVariables = Record<string, any>>({ enabled, initialData, query, token, ...other }: QueryListenerOptions<QueryResult, QueryVariables>) => {
396
+ data: Ref<vue_demi.UnwrapRef<NonNullable<QueryResult>> | null>;
397
+ status: Ref<ConnectionStatus>;
396
398
  error: Ref<{
397
399
  code: string;
398
400
  message: string;
399
401
  fatal: boolean;
400
402
  response?: any;
401
403
  } | null>;
402
- status: Ref<ConnectionStatus>;
403
- data: Ref<QueryResult | null>;
404
404
  };
405
405
 
406
406
  declare type SearchResultInstancesHrefSchema = {
@@ -1,5 +1,5 @@
1
1
  import hypenateStyleName from 'hyphenate-style-name';
2
- import { ref, onMounted, onBeforeUnmount, defineComponent, h, isVue2, isVue3, watchEffect, reactive, computed, toRaw } from 'vue-demi';
2
+ import { ref, onMounted, onBeforeUnmount, defineComponent, h, isVue2, isVue3, watchEffect, unref, reactive, computed, toRaw } from 'vue-demi';
3
3
  import { render, renderNodeRule, defaultMetaTransformer } from 'datocms-structured-text-generic-html-renderer';
4
4
  export { renderMarkRule, renderNodeRule, renderNodeRule as renderRule } from 'datocms-structured-text-generic-html-renderer';
5
5
  import { isRoot, isInlineItem, RenderError, isStructuredText, isItemLink, isBlock } from 'datocms-structured-text-utils';
@@ -657,46 +657,34 @@ const DatocmsStructuredTextPlugin = {
657
657
  }
658
658
  };
659
659
 
660
- function useQuerySubscription(options) {
661
- const { enabled, initialData, ...other } = options;
660
+ const useQuerySubscription = ({ enabled = true, initialData, query, token, ...other }) => {
662
661
  const error = ref(null);
663
- const data = ref(null);
664
- const status = ref(
665
- enabled ? "connecting" : "closed"
666
- );
662
+ const data = ref(unref(initialData) || null);
663
+ const status = ref(unref(enabled) ? "connecting" : "closed");
667
664
  const subscribeToQueryOptions = other;
668
- watchEffect((onCleanup) => {
669
- if (enabled === false) {
670
- status.value = "closed";
671
- return () => {
672
- };
673
- }
674
- let unsubscribe;
675
- async function subscribe() {
676
- unsubscribe = await subscribeToQuery({
665
+ watchEffect(async (onCleanup) => {
666
+ if (query && token && unref(enabled)) {
667
+ const unsubscribe = await subscribeToQuery({
677
668
  ...subscribeToQueryOptions,
669
+ query,
670
+ token,
678
671
  onStatusChange: (connectionStatus) => {
679
672
  status.value = connectionStatus;
680
673
  },
681
- onUpdate: (updateData) => {
674
+ onUpdate: ({ response }) => {
682
675
  error.value = null;
683
- data.value = updateData.response.data;
676
+ data.value = response.data;
684
677
  },
685
678
  onChannelError: (errorData) => {
686
679
  data.value = null;
687
680
  error.value = errorData;
688
681
  }
689
682
  });
683
+ onCleanup(unsubscribe);
690
684
  }
691
- subscribe();
692
- onCleanup(() => {
693
- if (unsubscribe) {
694
- unsubscribe();
695
- }
696
- });
697
685
  });
698
- return { error, status, data: data || initialData };
699
- }
686
+ return { data, status, error };
687
+ };
700
688
 
701
689
  const highlightPieces = (textWithHighlightMarker) => {
702
690
  return textWithHighlightMarker.split(/\[h\](.+?)\[\/h\]/g).map((text, index) => ({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vue-datocms",
3
- "version": "4.0.0",
3
+ "version": "4.0.1",
4
4
  "description": "A set of components and utilities to work faster with DatoCMS in Vue.js environments",
5
5
  "keywords": [
6
6
  "datocms",