robonomics-interface-vue 0.2.1 → 0.2.2

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.
@@ -1,6 +1,7 @@
1
1
  import { tools } from "robonomics-interface";
2
2
  import { isRef, onUnmounted, ref, toRefs, watch } from "vue";
3
3
  import { useLoader } from "../tools/useLoader.js";
4
+ import { usePolkadotApi } from "../usePolkadotApi.js";
4
5
  import { useQuery } from "./useQuery.js";
5
6
  /**
6
7
  * Vue сomposable for working with datalog
@@ -18,6 +19,7 @@ import { useQuery } from "./useQuery.js";
18
19
  */
19
20
  export function useDatalog(addressInit, { immediate = true } = {}) {
20
21
  const address = isRef(addressInit) ? addressInit : ref(addressInit);
22
+ const { isConnected, watchConnect } = usePolkadotApi();
21
23
  const { loader, setter, state } = useLoader();
22
24
  const { load, listen } = useQuery();
23
25
  const watcher = tools.createWatcher();
@@ -37,7 +39,14 @@ export function useDatalog(addressInit, { immediate = true } = {}) {
37
39
  watcher.set(unsubscribe);
38
40
  });
39
41
  };
40
- watch(address, run, { immediate });
42
+ const isChangedArgs = ref(immediate);
43
+ watch(address, () => {
44
+ if (isChangedArgs.value || isConnected.value) {
45
+ run();
46
+ }
47
+ isChangedArgs.value = true;
48
+ }, { immediate });
49
+ watchConnect(run, { isCheck: isChangedArgs });
41
50
  onUnmounted(watcher.stop);
42
51
  return {
43
52
  ...toRefs(state),
@@ -0,0 +1,25 @@
1
+ import { Ref } from "vue";
2
+ /**
3
+ * Vue сomposable for working with devices list
4
+ * @description Provides functionality for loading and tracking changes in devices list by address
5
+ * @param addressInit - Address for getting devices list
6
+ * @param options - Composable options
7
+ * @param [options.isWatch] - Whether to track changes in the address
8
+ * @param [options.immediate] - Whether to start loading immediately
9
+ * @returns Object with devices list state and management methods
10
+ * @example
11
+ * const { data, loading, error, load } = useDevices('5F...', { immediate: true })
12
+ *
13
+ * // With reactive address
14
+ * const address = ref('5F...')
15
+ * const { data } = useDevices(address)
16
+ */
17
+ export declare function useDevices(addressInit: string | Ref<string>, { isWatch, immediate }?: {
18
+ isWatch?: boolean | undefined;
19
+ immediate?: boolean | undefined;
20
+ }): {
21
+ load: () => void;
22
+ loading: Ref<boolean, boolean>;
23
+ error: Ref<Error | null, Error | null>;
24
+ data: Ref<string[] | null | undefined, string[] | null | undefined>;
25
+ };
@@ -0,0 +1,53 @@
1
+ import { tools } from "robonomics-interface";
2
+ import { isRef, onUnmounted, ref, toRefs, watch } from "vue";
3
+ import { useLoader } from "../tools/useLoader.js";
4
+ import { usePolkadotApi } from "../usePolkadotApi.js";
5
+ import { useQuery } from "./useQuery.js";
6
+ /**
7
+ * Vue сomposable for working with devices list
8
+ * @description Provides functionality for loading and tracking changes in devices list by address
9
+ * @param addressInit - Address for getting devices list
10
+ * @param options - Composable options
11
+ * @param [options.isWatch] - Whether to track changes in the address
12
+ * @param [options.immediate] - Whether to start loading immediately
13
+ * @returns Object with devices list state and management methods
14
+ * @example
15
+ * const { data, loading, error, load } = useDevices('5F...', { immediate: true })
16
+ *
17
+ * // With reactive address
18
+ * const address = ref('5F...')
19
+ * const { data } = useDevices(address)
20
+ */
21
+ export function useDevices(addressInit, { isWatch = true, immediate = true } = {}) {
22
+ const address = isRef(addressInit) ? addressInit : ref(addressInit);
23
+ const { isConnected, watchConnect } = usePolkadotApi();
24
+ const { loader, setter, state } = useLoader();
25
+ const { getDevices } = useQuery();
26
+ const watcher = tools.createWatcher();
27
+ /**
28
+ * Loads devices list and subscribes to changes
29
+ * @description Stops previous subscription, loads devices list and subscribes to new changes
30
+ */
31
+ const load = () => {
32
+ loader(async () => {
33
+ watcher.stop();
34
+ const unsubscribe = await getDevices(address.value, setter);
35
+ watcher.set(unsubscribe);
36
+ });
37
+ };
38
+ if (isWatch) {
39
+ const isChangedArgs = ref(immediate);
40
+ watch(address, () => {
41
+ if (isChangedArgs.value || isConnected.value) {
42
+ load();
43
+ }
44
+ isChangedArgs.value = true;
45
+ }, { immediate });
46
+ watchConnect(load, { isCheck: isChangedArgs });
47
+ }
48
+ onUnmounted(watcher.stop);
49
+ return {
50
+ ...toRefs(state),
51
+ load
52
+ };
53
+ }
@@ -1,6 +1,7 @@
1
1
  import { tools } from "robonomics-interface";
2
2
  import { isRef, onUnmounted, ref, toRefs, watch } from "vue";
3
3
  import { useLoader } from "../tools/useLoader.js";
4
+ import { usePolkadotApi } from "../usePolkadotApi.js";
4
5
  import { useQuery } from "./useQuery.js";
5
6
  /**
6
7
  * Vue сomposable for working with devices list
@@ -18,6 +19,7 @@ import { useQuery } from "./useQuery.js";
18
19
  */
19
20
  export function useDevices(addressInit, { immediate = true } = {}) {
20
21
  const address = isRef(addressInit) ? addressInit : ref(addressInit);
22
+ const { isConnected, watchConnect } = usePolkadotApi();
21
23
  const { loader, setter, state } = useLoader();
22
24
  const { getDevices } = useQuery();
23
25
  const watcher = tools.createWatcher();
@@ -32,7 +34,14 @@ export function useDevices(addressInit, { immediate = true } = {}) {
32
34
  watcher.set(unsubscribe);
33
35
  });
34
36
  };
35
- watch(address, load, { immediate });
37
+ const isChangedArgs = ref(immediate);
38
+ watch(address, () => {
39
+ if (isChangedArgs.value || isConnected.value) {
40
+ load();
41
+ }
42
+ isChangedArgs.value = true;
43
+ }, { immediate });
44
+ watchConnect(load, { isCheck: isChangedArgs });
36
45
  onUnmounted(watcher.stop);
37
46
  return {
38
47
  ...toRefs(state),
@@ -1,6 +1,7 @@
1
1
  import { tools } from "robonomics-interface";
2
- import { onUnmounted, toRaw, toRefs, unref, watch } from "vue";
2
+ import { onUnmounted, ref, toRaw, toRefs, unref, watch } from "vue";
3
3
  import { useLoader } from "../tools/useLoader.js";
4
+ import { usePolkadotApi } from "../usePolkadotApi.js";
4
5
  import { useQuery } from "./useQuery.js";
5
6
  /**
6
7
  * Vue сomposable for working with launches
@@ -19,6 +20,7 @@ import { useQuery } from "./useQuery.js";
19
20
  * filter.value = { success: true, method: 'Launch', sender: '5F...' }
20
21
  */
21
22
  export function useLaunch(filter, { immediate = true } = {}) {
23
+ const { isConnected, watchConnect } = usePolkadotApi();
22
24
  const { loader, setter, state } = useLoader();
23
25
  const { listen } = useQuery();
24
26
  const watcher = tools.createWatcher();
@@ -38,7 +40,14 @@ export function useLaunch(filter, { immediate = true } = {}) {
38
40
  watcher.set(unsubscribe);
39
41
  });
40
42
  };
41
- watch(() => filter, run, { immediate, deep: true });
43
+ const isChangedArgs = ref(immediate);
44
+ watch(() => filter, () => {
45
+ if (isChangedArgs.value || isConnected.value) {
46
+ run();
47
+ }
48
+ isChangedArgs.value = true;
49
+ }, { immediate, deep: true });
50
+ watchConnect(run, { isCheck: isChangedArgs });
42
51
  onUnmounted(watcher.stop);
43
52
  return {
44
53
  ...toRefs(state),
@@ -1,6 +1,7 @@
1
1
  import { tools } from "robonomics-interface";
2
2
  import { onUnmounted, toRefs } from "vue";
3
3
  import { useLoader } from "../tools/useLoader.js";
4
+ import { usePolkadotApi } from "../usePolkadotApi.js";
4
5
  import { useQuery } from "./useQuery.js";
5
6
  /**
6
7
  * Vue сomposable for working with available subscriptions
@@ -17,6 +18,7 @@ import { useQuery } from "./useQuery.js";
17
18
  * console.log('Available subscriptions:', data.value)
18
19
  */
19
20
  export function useAvailableSubscriptions({ immediate = true } = {}) {
21
+ const { isConnected, watchConnect } = usePolkadotApi();
20
22
  const { loader, setter, state } = useLoader();
21
23
  const { countAvailableSubscriptions } = useQuery();
22
24
  const watcher = tools.createWatcher();
@@ -32,7 +34,12 @@ export function useAvailableSubscriptions({ immediate = true } = {}) {
32
34
  });
33
35
  };
34
36
  if (immediate) {
35
- load();
37
+ if (isConnected.value) {
38
+ load();
39
+ }
40
+ else {
41
+ watchConnect(load);
42
+ }
36
43
  }
37
44
  onUnmounted(watcher.stop);
38
45
  return {
@@ -1,6 +1,7 @@
1
1
  import { tools } from "robonomics-interface";
2
2
  import { isRef, onUnmounted, ref, toRefs, watch } from "vue";
3
3
  import { useLoader } from "../tools/useLoader.js";
4
+ import { usePolkadotApi } from "../usePolkadotApi.js";
4
5
  import { useQuery } from "./useQuery.js";
5
6
  /**
6
7
  * Vue сomposable for working with subscription
@@ -18,6 +19,7 @@ import { useQuery } from "./useQuery.js";
18
19
  */
19
20
  export function useSubscription(addressInit, { immediate = true } = {}) {
20
21
  const address = isRef(addressInit) ? addressInit : ref(addressInit);
22
+ const { isConnected, watchConnect } = usePolkadotApi();
21
23
  const { loader, setter, state } = useLoader();
22
24
  const { load } = useQuery();
23
25
  const watcher = tools.createWatcher();
@@ -34,7 +36,14 @@ export function useSubscription(addressInit, { immediate = true } = {}) {
34
36
  }
35
37
  });
36
38
  };
37
- watch(address, run, { immediate });
39
+ const isChangedArgs = ref(immediate);
40
+ watch(address, () => {
41
+ if (isChangedArgs.value || isConnected.value) {
42
+ run();
43
+ }
44
+ isChangedArgs.value = true;
45
+ }, { immediate });
46
+ watchConnect(run, { isCheck: isChangedArgs });
38
47
  onUnmounted(watcher.stop);
39
48
  return {
40
49
  ...toRefs(state),
@@ -25,5 +25,5 @@ export declare function useLoader<T>(): {
25
25
  data: UnwrapRef<T> | null | undefined;
26
26
  };
27
27
  loader: (getter: () => void | Promise<void> | T | Promise<T>) => Promise<void>;
28
- setter: (data: T, error?: null | Error) => void;
28
+ setter: (data: null | undefined | T, error?: null | Error) => void;
29
29
  };
@@ -1,6 +1,7 @@
1
1
  import { tools } from "robonomics-interface";
2
2
  import { isRef, onUnmounted, ref, toRefs, watch } from "vue";
3
3
  import { useLoader } from "../tools/useLoader.js";
4
+ import { usePolkadotApi } from "../usePolkadotApi.js";
4
5
  import { useQuery } from "./useQuery.js";
5
6
  /**
6
7
  * Vue сomposable for working with individual twin
@@ -18,6 +19,7 @@ import { useQuery } from "./useQuery.js";
18
19
  */
19
20
  export function useTwin(idInit, { immediate = true } = {}) {
20
21
  const id = isRef(idInit) ? idInit : ref(idInit);
22
+ const { isConnected, watchConnect } = usePolkadotApi();
21
23
  const { loader, setter, state } = useLoader();
22
24
  const { getTwin } = useQuery();
23
25
  const watcher = tools.createWatcher();
@@ -34,7 +36,14 @@ export function useTwin(idInit, { immediate = true } = {}) {
34
36
  }
35
37
  });
36
38
  };
37
- watch(id, load, { immediate });
39
+ const isChangedArgs = ref(immediate);
40
+ watch(id, () => {
41
+ if (isChangedArgs.value || isConnected.value) {
42
+ load();
43
+ }
44
+ isChangedArgs.value = true;
45
+ }, { immediate });
46
+ watchConnect(load, { isCheck: isChangedArgs });
38
47
  onUnmounted(watcher.stop);
39
48
  return {
40
49
  ...toRefs(state),
@@ -17,11 +17,14 @@ export declare const error: import("vue").Ref<any, any>;
17
17
  * })
18
18
  */
19
19
  export declare function usePolkadotApi(): {
20
- instance: Instance | undefined;
20
+ instance: false | Instance | undefined;
21
21
  status: import("vue").Ref<any, any>;
22
22
  error: import("vue").Ref<any, any>;
23
23
  isDisconnected: import("vue").ComputedRef<boolean>;
24
24
  isConnected: import("vue").ComputedRef<boolean>;
25
25
  isConnecting: import("vue").ComputedRef<boolean>;
26
+ watchConnect: (fn: () => void, { isCheck }?: {
27
+ isCheck?: import("vue").Ref<boolean, boolean> | undefined;
28
+ }) => import("vue").WatchHandle;
26
29
  composeWithCheckConnection: <T extends (api: ApiPromise, ...args: Args) => ReturnType<T>, Args extends Parameters<T> extends [ApiPromise, ...infer R] ? R : never>(fn: T) => (...args: Args) => ReturnType<T>;
27
30
  };
@@ -1,5 +1,5 @@
1
1
  import { Instance } from "robonomics-interface";
2
- import { computed, inject, ref } from "vue";
2
+ import { computed, hasInjectionContext, inject, ref, watch } from "vue";
3
3
  /** Polkadot network connection status */
4
4
  export const status = ref();
5
5
  /** Polkadot network connection error */
@@ -17,7 +17,10 @@ export const error = ref();
17
17
  * })
18
18
  */
19
19
  export function usePolkadotApi() {
20
- const Polkadot = inject("Polkadot");
20
+ const Polkadot = hasInjectionContext() && inject("Polkadot");
21
+ if (!Polkadot) {
22
+ console.error(`Instance not found in context. Read https://vuejs.org/guide/reusability/composables.html to learn more`);
23
+ }
21
24
  /** Checks whether the client is disconnected */
22
25
  const isDisconnected = computed(() => status.value === Instance.STATUS.disconnected);
23
26
  /** Checks whether the client is connected */
@@ -34,11 +37,34 @@ export function usePolkadotApi() {
34
37
  * const result = await safeQuery() // Will throw an error if not connected
35
38
  */
36
39
  const composeWithCheckConnection = (fn) => (...args) => {
37
- if (!isConnected.value || !Polkadot || !Polkadot.api) {
40
+ if (!isConnected.value) {
41
+ if (process.env.NODE_ENV !== "production") {
42
+ console.warn("Not connected", fn, args);
43
+ }
38
44
  throw new Error("Not connected");
39
45
  }
46
+ if (!Polkadot || !Polkadot.api) {
47
+ if (process.env.NODE_ENV !== "production") {
48
+ console.warn("Not found instance", fn, args, Polkadot);
49
+ }
50
+ throw new Error("Not found instance");
51
+ }
40
52
  return fn(Polkadot.api, ...args);
41
53
  };
54
+ /**
55
+ * Watch for the connection status of the API
56
+ * @description Watches for changes in connection status and calls the provided function when the API is connected
57
+ * @param fn - Function to call when the API is connected
58
+ * @param [options.isCheck] - Options object with an optional `isCheck` ref to control if the function should be called
59
+ * @returns A watch function
60
+ */
61
+ const watchConnect = (fn, { isCheck = ref(true) } = {}) => {
62
+ return watch(isConnected, () => {
63
+ if (isCheck.value && isConnected.value) {
64
+ fn();
65
+ }
66
+ }, { immediate: true });
67
+ };
42
68
  return {
43
69
  instance: Polkadot,
44
70
  status,
@@ -46,6 +72,7 @@ export function usePolkadotApi() {
46
72
  isDisconnected,
47
73
  isConnected,
48
74
  isConnecting,
75
+ watchConnect,
49
76
  composeWithCheckConnection
50
77
  };
51
78
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "robonomics-interface-vue",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "author": "vol4tim <sas@robonomics.network>",
5
5
  "license": "BSD-3-Clause",
6
6
  "type": "module",