@koolbase/core 12.9.0 → 12.10.0

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.
@@ -0,0 +1,2 @@
1
+ export { createUseCollection, type UseCollectionResult } from './use-collection.js';
2
+ export { createUseRecord, type UseRecordResult } from './use-record.js';
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createUseRecord = exports.createUseCollection = void 0;
4
+ // React hooks over core's controllers: ONE implementation, exported to apps by
5
+ // @koolbase/react-native (from its root, as always) and @koolbase/js/react.
6
+ // React only -- nothing native, nothing DOM. Deliberately not exported from
7
+ // core's root entry, so importing @koolbase/core never loads React.
8
+ var use_collection_js_1 = require("./use-collection.js");
9
+ Object.defineProperty(exports, "createUseCollection", { enumerable: true, get: function () { return use_collection_js_1.createUseCollection; } });
10
+ var use_record_js_1 = require("./use-record.js");
11
+ Object.defineProperty(exports, "createUseRecord", { enumerable: true, get: function () { return use_record_js_1.createUseRecord; } });
@@ -0,0 +1,12 @@
1
+ import { type CollectionQuery, type CollectionState, type KoolbaseDatabase } from '../index.js';
2
+ export interface UseCollectionResult extends CollectionState {
3
+ /** Page one again, from the server. What is shown stays while it runs, and stays if it fails. */
4
+ refresh: () => Promise<void>;
5
+ /**
6
+ * The next page, appended, while hasMore. If rows are added or removed on
7
+ * the server between pages, one can be skipped until refresh(); none is
8
+ * ever shown twice.
9
+ */
10
+ loadMore: () => Promise<void>;
11
+ }
12
+ export declare function createUseCollection(getDb: () => Pick<KoolbaseDatabase, 'query'>): (collection: string, query?: CollectionQuery) => UseCollectionResult;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ // useCollection: a Koolbase collection as React state.
3
+ //
4
+ // A thin wrapper. Everything about getting the records right lives in
5
+ // @koolbase/core's KoolbaseCollectionController, which is tested there; this
6
+ // file only ties one controller's lifetime to a component's.
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.createUseCollection = createUseCollection;
9
+ const react_1 = require("react");
10
+ const index_js_1 = require("../index.js");
11
+ const noop = () => { };
12
+ const done = () => Promise.resolve();
13
+ function createUseCollection(getDb) {
14
+ return function useCollection(collection, query = {}) {
15
+ // The query as a value: an inline `where` object is a new object every
16
+ // render, and must not start a new fetch every render.
17
+ const key = (0, index_js_1.collectionQueryKey)(collection, query);
18
+ const [current, setCurrent] = (0, react_1.useState)(null);
19
+ (0, react_1.useEffect)(() => {
20
+ // Created here, not during render. Under React strict mode an effect
21
+ // runs, cleans up and runs again; each run gets its own controller, so
22
+ // the one left running is never one that was already disposed.
23
+ const controller = new index_js_1.KoolbaseCollectionController(getDb(), collection, query);
24
+ setCurrent({ key, controller });
25
+ void controller.load();
26
+ return () => controller.dispose();
27
+ // Depends on key alone: key is collection + query, as a value.
28
+ }, [key]);
29
+ // A controller from the previous key reads as a fresh load, never as
30
+ // the previous query's records.
31
+ const controller = current?.key === key ? current.controller : null;
32
+ const subscribe = (0, react_1.useCallback)((onChange) => (controller ? controller.subscribe(onChange) : noop), [controller]);
33
+ const getSnapshot = (0, react_1.useCallback)(() => (controller ? controller.getState() : index_js_1.initialCollectionState), [controller]);
34
+ const state = (0, react_1.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
35
+ const refresh = (0, react_1.useCallback)(() => (controller ? controller.refresh() : done()), [controller]);
36
+ const loadMore = (0, react_1.useCallback)(() => (controller ? controller.loadMore() : done()), [controller]);
37
+ return { ...state, refresh, loadMore };
38
+ };
39
+ }
@@ -0,0 +1,6 @@
1
+ import { type KoolbaseDatabase, type RecordState } from '../index.js';
2
+ export interface UseRecordResult extends RecordState {
3
+ /** The record again, from the server. What is shown stays while it runs, and stays if it fails. */
4
+ refresh: () => Promise<void>;
5
+ }
6
+ export declare function createUseRecord(getDb: () => Pick<KoolbaseDatabase, 'get'>): (collection: string, id: string | null | undefined) => UseRecordResult;
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ // useRecord: one Koolbase record, by id, as React state.
3
+ //
4
+ // A thin wrapper, like useCollection: everything about getting the record
5
+ // right lives in @koolbase/core's KoolbaseRecordController, tested there;
6
+ // this file only ties one controller's lifetime to a component's.
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.createUseRecord = createUseRecord;
9
+ const react_1 = require("react");
10
+ const index_js_1 = require("../index.js");
11
+ const noop = () => { };
12
+ const done = () => Promise.resolve();
13
+ function createUseRecord(getDb) {
14
+ return function useRecord(collection, id) {
15
+ const key = JSON.stringify([collection, id ?? '']);
16
+ const [current, setCurrent] = (0, react_1.useState)(null);
17
+ (0, react_1.useEffect)(() => {
18
+ // Created here, not during render: under React strict mode an effect
19
+ // runs, cleans up and runs again, and each run gets its own controller.
20
+ const controller = new index_js_1.KoolbaseRecordController(getDb(), collection, id ?? '');
21
+ setCurrent({ key, controller });
22
+ void controller.load();
23
+ return () => controller.dispose();
24
+ // Depends on key alone: key is collection + id, as a value.
25
+ }, [key]);
26
+ // A controller from the previous key reads as a fresh load, never as the
27
+ // previous record.
28
+ const controller = current?.key === key ? current.controller : null;
29
+ const subscribe = (0, react_1.useCallback)((onChange) => (controller ? controller.subscribe(onChange) : noop), [controller]);
30
+ const getSnapshot = (0, react_1.useCallback)(() => (controller ? controller.getState() : index_js_1.initialRecordState), [controller]);
31
+ const state = (0, react_1.useSyncExternalStore)(subscribe, getSnapshot, getSnapshot);
32
+ const refresh = (0, react_1.useCallback)(() => (controller ? controller.refresh() : done()), [controller]);
33
+ return { ...state, refresh };
34
+ };
35
+ }
@@ -0,0 +1,2 @@
1
+ export { createUseCollection, type UseCollectionResult } from './use-collection.js';
2
+ export { createUseRecord, type UseRecordResult } from './use-record.js';
@@ -0,0 +1,6 @@
1
+ // React hooks over core's controllers: ONE implementation, exported to apps by
2
+ // @koolbase/react-native (from its root, as always) and @koolbase/js/react.
3
+ // React only -- nothing native, nothing DOM. Deliberately not exported from
4
+ // core's root entry, so importing @koolbase/core never loads React.
5
+ export { createUseCollection } from './use-collection.js';
6
+ export { createUseRecord } from './use-record.js';
@@ -0,0 +1,12 @@
1
+ import { type CollectionQuery, type CollectionState, type KoolbaseDatabase } from '../index.js';
2
+ export interface UseCollectionResult extends CollectionState {
3
+ /** Page one again, from the server. What is shown stays while it runs, and stays if it fails. */
4
+ refresh: () => Promise<void>;
5
+ /**
6
+ * The next page, appended, while hasMore. If rows are added or removed on
7
+ * the server between pages, one can be skipped until refresh(); none is
8
+ * ever shown twice.
9
+ */
10
+ loadMore: () => Promise<void>;
11
+ }
12
+ export declare function createUseCollection(getDb: () => Pick<KoolbaseDatabase, 'query'>): (collection: string, query?: CollectionQuery) => UseCollectionResult;
@@ -0,0 +1,36 @@
1
+ // useCollection: a Koolbase collection as React state.
2
+ //
3
+ // A thin wrapper. Everything about getting the records right lives in
4
+ // @koolbase/core's KoolbaseCollectionController, which is tested there; this
5
+ // file only ties one controller's lifetime to a component's.
6
+ import { useCallback, useEffect, useState, useSyncExternalStore } from 'react';
7
+ import { KoolbaseCollectionController, collectionQueryKey, initialCollectionState, } from '../index.js';
8
+ const noop = () => { };
9
+ const done = () => Promise.resolve();
10
+ export function createUseCollection(getDb) {
11
+ return function useCollection(collection, query = {}) {
12
+ // The query as a value: an inline `where` object is a new object every
13
+ // render, and must not start a new fetch every render.
14
+ const key = collectionQueryKey(collection, query);
15
+ const [current, setCurrent] = useState(null);
16
+ useEffect(() => {
17
+ // Created here, not during render. Under React strict mode an effect
18
+ // runs, cleans up and runs again; each run gets its own controller, so
19
+ // the one left running is never one that was already disposed.
20
+ const controller = new KoolbaseCollectionController(getDb(), collection, query);
21
+ setCurrent({ key, controller });
22
+ void controller.load();
23
+ return () => controller.dispose();
24
+ // Depends on key alone: key is collection + query, as a value.
25
+ }, [key]);
26
+ // A controller from the previous key reads as a fresh load, never as
27
+ // the previous query's records.
28
+ const controller = current?.key === key ? current.controller : null;
29
+ const subscribe = useCallback((onChange) => (controller ? controller.subscribe(onChange) : noop), [controller]);
30
+ const getSnapshot = useCallback(() => (controller ? controller.getState() : initialCollectionState), [controller]);
31
+ const state = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
32
+ const refresh = useCallback(() => (controller ? controller.refresh() : done()), [controller]);
33
+ const loadMore = useCallback(() => (controller ? controller.loadMore() : done()), [controller]);
34
+ return { ...state, refresh, loadMore };
35
+ };
36
+ }
@@ -0,0 +1,6 @@
1
+ import { type KoolbaseDatabase, type RecordState } from '../index.js';
2
+ export interface UseRecordResult extends RecordState {
3
+ /** The record again, from the server. What is shown stays while it runs, and stays if it fails. */
4
+ refresh: () => Promise<void>;
5
+ }
6
+ export declare function createUseRecord(getDb: () => Pick<KoolbaseDatabase, 'get'>): (collection: string, id: string | null | undefined) => UseRecordResult;
@@ -0,0 +1,32 @@
1
+ // useRecord: one Koolbase record, by id, as React state.
2
+ //
3
+ // A thin wrapper, like useCollection: everything about getting the record
4
+ // right lives in @koolbase/core's KoolbaseRecordController, tested there;
5
+ // this file only ties one controller's lifetime to a component's.
6
+ import { useCallback, useEffect, useState, useSyncExternalStore } from 'react';
7
+ import { KoolbaseRecordController, initialRecordState, } from '../index.js';
8
+ const noop = () => { };
9
+ const done = () => Promise.resolve();
10
+ export function createUseRecord(getDb) {
11
+ return function useRecord(collection, id) {
12
+ const key = JSON.stringify([collection, id ?? '']);
13
+ const [current, setCurrent] = useState(null);
14
+ useEffect(() => {
15
+ // Created here, not during render: under React strict mode an effect
16
+ // runs, cleans up and runs again, and each run gets its own controller.
17
+ const controller = new KoolbaseRecordController(getDb(), collection, id ?? '');
18
+ setCurrent({ key, controller });
19
+ void controller.load();
20
+ return () => controller.dispose();
21
+ // Depends on key alone: key is collection + id, as a value.
22
+ }, [key]);
23
+ // A controller from the previous key reads as a fresh load, never as the
24
+ // previous record.
25
+ const controller = current?.key === key ? current.controller : null;
26
+ const subscribe = useCallback((onChange) => (controller ? controller.subscribe(onChange) : noop), [controller]);
27
+ const getSnapshot = useCallback(() => (controller ? controller.getState() : initialRecordState), [controller]);
28
+ const state = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
29
+ const refresh = useCallback(() => (controller ? controller.refresh() : done()), [controller]);
30
+ return { ...state, refresh };
31
+ };
32
+ }
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "@koolbase/core",
3
- "version": "12.9.0",
3
+ "version": "12.10.0",
4
4
  "description": "Koolbase SDK core \u2014 shared behaviour behind @koolbase/react-native and @koolbase/js. Install one of those, not this.",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "types": "./dist/esm/index.d.ts",
7
7
  "files": [
8
- "dist"
8
+ "dist",
9
+ "react"
9
10
  ],
10
11
  "scripts": {
11
12
  "build": "node ../../scripts/build-package.js core",
@@ -39,7 +40,25 @@
39
40
  "default": "./dist/cjs/index.js"
40
41
  }
41
42
  },
43
+ "./react": {
44
+ "import": {
45
+ "types": "./dist/esm/react/index.d.ts",
46
+ "default": "./dist/esm/react/index.js"
47
+ },
48
+ "require": {
49
+ "types": "./dist/cjs/react/index.d.ts",
50
+ "default": "./dist/cjs/react/index.js"
51
+ }
52
+ },
42
53
  "./package.json": "./package.json"
43
54
  },
44
- "sideEffects": false
55
+ "sideEffects": false,
56
+ "peerDependencies": {
57
+ "react": ">=18.0.0"
58
+ },
59
+ "peerDependenciesMeta": {
60
+ "react": {
61
+ "optional": true
62
+ }
63
+ }
45
64
  }
@@ -0,0 +1,7 @@
1
+ {
2
+ "//": "For resolvers that ignore \"exports\" (TypeScript node10, older bundlers): the ./react entry.",
3
+ "main": "../dist/cjs/react/index.js",
4
+ "module": "../dist/esm/react/index.js",
5
+ "types": "../dist/cjs/react/index.d.ts",
6
+ "sideEffects": false
7
+ }