@livequery/react 1.0.36 → 1.0.82

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.
@@ -3,4 +3,4 @@ import { Transporter } from '@livequery/types';
3
3
  export type LiveQueryContextOption = {
4
4
  transporter: Transporter;
5
5
  };
6
- export declare const useLiveQueryContext: () => LiveQueryContextOption, LiveQueryContextProvider: ({ children, ...props }: import("react").PropsWithChildren<LiveQueryContextOption>) => JSX.Element;
6
+ export declare const useLiveQueryContext: () => LiveQueryContextOption, LiveQueryContextProvider: ({ children, ...props }: import("react").PropsWithChildren<LiveQueryContextOption>) => import("react").JSX.Element;
@@ -1,6 +1,2 @@
1
- "use strict";
2
- var _a;
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.LiveQueryContextProvider = exports.useLiveQueryContext = void 0;
5
- const createContextFromHook_1 = require("./hooks/createContextFromHook");
6
- _a = (0, createContextFromHook_1.createContextFromHook)((props) => props), exports.useLiveQueryContext = _a[0], exports.LiveQueryContextProvider = _a[1];
1
+ import { createContextFromHook } from './hooks/createContextFromHook';
2
+ export const [useLiveQueryContext, LiveQueryContextProvider] = createContextFromHook((props) => props);
@@ -1,2 +1,2 @@
1
1
  import React, { PropsWithChildren } from 'react';
2
- export declare const createContextFromHook: <T, K>(fn: (props?: T) => K) => [() => K, ({ children, ...props }: React.PropsWithChildren<T>) => JSX.Element];
2
+ export declare const createContextFromHook: <T, K>(fn: (props?: T) => K) => [() => K, ({ children, ...props }: React.PropsWithChildren<T>) => React.JSX.Element];
@@ -1,30 +1,11 @@
1
- "use strict";
2
- var __rest = (this && this.__rest) || function (s, e) {
3
- var t = {};
4
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
5
- t[p] = s[p];
6
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
7
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
8
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
9
- t[p[i]] = s[p[i]];
10
- }
11
- return t;
12
- };
13
- var __importDefault = (this && this.__importDefault) || function (mod) {
14
- return (mod && mod.__esModule) ? mod : { "default": mod };
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.createContextFromHook = void 0;
18
- const react_1 = __importDefault(require("react"));
19
- const react_2 = require("react");
20
- const createContextFromHook = (fn) => {
21
- const context = (0, react_2.createContext)({});
22
- const getContext = () => (0, react_2.useContext)(context);
23
- const Provider = (_a) => {
24
- var { children } = _a, props = __rest(_a, ["children"]);
1
+ import React from 'react';
2
+ import { createContext, useContext } from "react";
3
+ export const createContextFromHook = (fn) => {
4
+ const context = createContext({});
5
+ const getContext = () => useContext(context);
6
+ const Provider = ({ children, ...props }) => {
25
7
  const value = fn(props);
26
- return (react_1.default.createElement(context.Provider, { value: value }, children));
8
+ return (React.createElement(context.Provider, { value: value }, children));
27
9
  };
28
10
  return [getContext, Provider];
29
11
  };
30
- exports.createContextFromHook = createContextFromHook;
@@ -1,4 +1,4 @@
1
1
  /// <reference types="react" />
2
2
  export declare const createStaticContext: <T extends {}>() => [() => T, ({ children, ...props }: import("react").PropsWithChildren<{
3
3
  value: T;
4
- }>) => JSX.Element];
4
+ }>) => import("react").JSX.Element];
@@ -1,6 +1,2 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createStaticContext = void 0;
4
- const createContextFromHook_1 = require("./createContextFromHook");
5
- const createStaticContext = () => (0, createContextFromHook_1.createContextFromHook)((props) => props.value);
6
- exports.createStaticContext = createStaticContext;
1
+ import { createContextFromHook } from './createContextFromHook';
2
+ export const createStaticContext = () => createContextFromHook((props) => props.value);
package/build/index.js CHANGED
@@ -1,22 +1,6 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./useDocumentData"), exports);
18
- __exportStar(require("./useCollectionData"), exports);
19
- __exportStar(require("./useMonitor"), exports);
20
- __exportStar(require("./LiveQueryContext"), exports);
21
- __exportStar(require("./hooks/createContextFromHook"), exports);
22
- __exportStar(require("./hooks/createStaticContext"), exports);
1
+ export * from "./useDocumentData";
2
+ export * from './useCollectionData';
3
+ export * from './useMonitor';
4
+ export * from "./LiveQueryContext";
5
+ export * from './hooks/createContextFromHook';
6
+ export * from './hooks/createStaticContext';
@@ -1,6 +1,6 @@
1
- import { QueryOption } from "@livequery/types";
1
+ import { LivequeryBaseEntity, QueryOption } from "@livequery/types";
2
2
  import { CollectionOption } from "@livequery/client";
3
- export type useCollectionDataOptions<T = any> = CollectionOption<T> & {
3
+ export type useCollectionDataOptions<T extends LivequeryBaseEntity = LivequeryBaseEntity> = CollectionOption<T> & {
4
4
  lazy?: boolean;
5
5
  filters: Partial<QueryOption<T>>;
6
6
  load_all: boolean;
@@ -14,14 +14,16 @@ export declare const useCollectionData: <T extends {
14
14
  has_more: boolean;
15
15
  empty: boolean;
16
16
  filters: Partial<QueryOption<T>>;
17
- add: (payload: T) => Promise<T>;
17
+ add: (payload: Partial<T>) => Promise<{
18
+ data: {
19
+ item: T;
20
+ };
21
+ }>;
18
22
  fetch_more: () => void;
19
23
  filter: (filters: Partial<QueryOption<T>>) => void;
20
24
  reload: () => void;
21
25
  reset: () => void;
22
26
  trigger: <T_1>(name: string, payload?: object, trigger_document_id?: string) => Promise<T_1>;
23
- update: ({ id: update_payload_id, ...payload }: Partial<T & {
24
- id: string;
25
- }>) => Promise<T>;
27
+ update: ({ id: update_payload_id, ...payload }: Partial<T>) => Promise<any>;
26
28
  $changes: import("rxjs").Subject<import("@livequery/types").UpdatedData<T>>;
27
29
  };
@@ -1,21 +1,18 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useCollectionData = void 0;
4
- const react_1 = require("react");
5
- const LiveQueryContext_1 = require("./LiveQueryContext");
6
- const useObservable_1 = require("./useObservable");
7
- const client_1 = require("@livequery/client");
1
+ import { useEffect, useMemo } from "react";
2
+ import { useLiveQueryContext } from "./LiveQueryContext";
3
+ import { useObservable } from "./useObservable";
4
+ import { CollectionObservable } from "@livequery/client";
8
5
  function assert(fn, thiss) {
9
6
  return (fn || (() => { })).bind(thiss);
10
7
  }
11
- const useCollectionData = (ref, collection_options = {}) => {
12
- const { transporter } = (0, LiveQueryContext_1.useLiveQueryContext)();
13
- const client = (0, react_1.useMemo)(() => ref && new client_1.CollectionObservable(ref, Object.assign({ transporter }, collection_options)), [ref]);
14
- const { loading, has_more, error, items, options } = (0, useObservable_1.useObservable)(client, { options: {}, items: [], has_more: false, loading: collection_options.lazy ? false : true });
15
- (0, react_1.useEffect)(() => {
16
- ref && !(collection_options === null || collection_options === void 0 ? void 0 : collection_options.lazy) && client.fetch_more();
8
+ export const useCollectionData = (ref, collection_options = {}) => {
9
+ const { transporter } = useLiveQueryContext();
10
+ const client = useMemo(() => ref && new CollectionObservable(ref, { transporter, ...collection_options }), [ref]);
11
+ const { loading, has_more, error, items, options } = useObservable(client, { options: {}, items: [], has_more: false, loading: collection_options.lazy ? false : true });
12
+ useEffect(() => {
13
+ ref && !collection_options?.lazy && client.fetch_more();
17
14
  }, [ref]);
18
- (0, react_1.useEffect)(() => {
15
+ useEffect(() => {
19
16
  collection_options.load_all && !loading && has_more && items.length > 0 && client.fetch_more();
20
17
  }, [loading]);
21
18
  return {
@@ -25,14 +22,13 @@ const useCollectionData = (ref, collection_options = {}) => {
25
22
  has_more,
26
23
  empty: ref && !error && Object.keys(items).length == 0 && loading === false,
27
24
  filters: (options || {}),
28
- add: assert(client === null || client === void 0 ? void 0 : client.add, client),
29
- fetch_more: assert(client === null || client === void 0 ? void 0 : client.fetch_more, client),
30
- filter: assert(client === null || client === void 0 ? void 0 : client.filter, client),
31
- reload: assert(client === null || client === void 0 ? void 0 : client.reload, client),
32
- reset: assert(client === null || client === void 0 ? void 0 : client.reset, client),
33
- trigger: assert(client === null || client === void 0 ? void 0 : client.trigger, client),
34
- update: assert(client === null || client === void 0 ? void 0 : client.update, client),
35
- $changes: client.$changes
25
+ add: assert(client?.add, client),
26
+ fetch_more: assert(client?.fetch_more, client),
27
+ filter: assert(client?.filter, client),
28
+ reload: assert(client?.reload, client),
29
+ reset: assert(client?.reset, client),
30
+ trigger: assert(client?.trigger, client),
31
+ update: assert(client?.update, client),
32
+ $changes: client?.$changes
36
33
  };
37
34
  };
38
- exports.useCollectionData = useCollectionData;
@@ -1,5 +1,6 @@
1
1
  import { CollectionOption } from "@livequery/client";
2
- export type useDocumentDataOptions<T = any> = Omit<CollectionOption<T>, 'filters'> & {
2
+ import { LivequeryBaseEntity } from "@livequery/types";
3
+ export type useDocumentDataOptions<T extends LivequeryBaseEntity = LivequeryBaseEntity> = Omit<CollectionOption<T>, 'filters'> & {
3
4
  lazy?: boolean;
4
5
  };
5
6
  export declare const useDocumentData: <T extends {
@@ -1,9 +1,6 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useDocumentData = void 0;
4
- const useCollectionData_1 = require("./useCollectionData");
5
- const useDocumentData = (ref, options) => {
6
- const { items, loading, error, reload, $changes } = (0, useCollectionData_1.useCollectionData)(ref, options);
1
+ import { useCollectionData } from "./useCollectionData";
2
+ export const useDocumentData = (ref, options) => {
3
+ const { items, loading, error, reload, $changes } = useCollectionData(ref, options);
7
4
  return {
8
5
  item: items[0],
9
6
  loading,
@@ -12,4 +9,3 @@ const useDocumentData = (ref, options) => {
12
9
  $changes
13
10
  };
14
11
  };
15
- exports.useDocumentData = useDocumentData;
@@ -1,28 +1,15 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.useMonitor = void 0;
13
- const react_1 = require("react");
14
- const useMonitor = (fn) => {
15
- const [{ error, data, loading }, update] = (0, react_1.useState)({});
16
- const excute = ((...args) => __awaiter(void 0, void 0, void 0, function* () {
1
+ import { useState } from "react";
2
+ export const useMonitor = (fn) => {
3
+ const [{ error, data, loading }, update] = useState({});
4
+ const excute = (async (...args) => {
17
5
  update({ data: null, loading: true, error: null });
18
6
  try {
19
- update({ data: yield fn(...args), error: null, loading: false });
7
+ update({ data: await fn(...args), error: null, loading: false });
20
8
  }
21
9
  catch (error) {
22
10
  update({ data: null, error, loading: false });
23
11
  }
24
- }));
12
+ });
25
13
  const vaild = !error && !loading;
26
14
  return { excute, loading, data, error, vaild };
27
15
  };
28
- exports.useMonitor = useMonitor;
@@ -1,19 +1,15 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useObservable = void 0;
4
- const react_1 = require("react");
5
- const useObservable = (o, default_value) => {
1
+ import { useEffect, useState } from 'react';
2
+ export const useObservable = (o, default_value) => {
6
3
  let mounting = true;
7
- const [s, ss] = (0, react_1.useState)(default_value);
8
- (0, react_1.useEffect)(() => {
4
+ const [s, ss] = useState(default_value);
5
+ useEffect(() => {
9
6
  if (!o)
10
7
  return;
11
- const subcription = o === null || o === void 0 ? void 0 : o.subscribe(d => ss(Object.assign({}, d)));
8
+ const subcription = o?.subscribe(d => ss({ ...d }));
12
9
  return () => {
13
- subcription === null || subcription === void 0 ? void 0 : subcription.unsubscribe();
10
+ subcription?.unsubscribe();
14
11
  mounting = false;
15
12
  };
16
13
  }, [o]);
17
14
  return s;
18
15
  };
19
- exports.useObservable = useObservable;
package/package.json CHANGED
@@ -3,7 +3,8 @@
3
3
  "repository": {
4
4
  "url": "https://github.com/livequery/react"
5
5
  },
6
- "version": "1.0.36",
6
+ "type": "module",
7
+ "version": "1.0.82",
7
8
  "description": "",
8
9
  "main": "build/index.js",
9
10
  "types": "build/index.d.ts",