floppy-disk 3.7.0 → 3.7.1-beta.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.
@@ -1,4 +1,4 @@
1
- import { type InitStoreOptions, type StoreApi } from "../vanilla.mjs";
1
+ import { type InitStoreOptions, type SetStateInput } from "../vanilla.mjs";
2
2
  import type { StoreKey } from "./create-stores.mjs";
3
3
  type StreamDataState<TData, TError> = {
4
4
  state: "INITIAL";
@@ -68,6 +68,7 @@ type AdditionalStoreApi<TConnection> = {
68
68
  data: {
69
69
  reset: () => void;
70
70
  };
71
+ delete: () => boolean;
71
72
  };
72
73
  export type StreamOptions<TConnection, TData, TError = Error> = InitStoreOptions<StreamState<TData, TError>, AdditionalStoreApi<TConnection>> & {
73
74
  connection?: {
@@ -84,5 +85,20 @@ export declare const experimental_createStream: <TConnection, TData, TVariable e
84
85
  error: (error: TError) => void;
85
86
  }) => TConnection, disconnect: (connection: TConnection) => void, options?: StreamOptions<TConnection, TData, TError>) => (variable?: TVariable) => ((options?: {
86
87
  initialData?: TData;
87
- }) => StreamState<TData, TError>) & StoreApi<StreamState<TData, TError>> & AdditionalStoreApi<TConnection>;
88
+ }) => StreamState<TData, TError>) & {
89
+ setState: (value: SetStateInput<StreamState<TData, TError>>) => void;
90
+ getState: () => StreamState<TData, TError>;
91
+ subscribe: (subscriber: import("../vanilla.d.mts").Subscriber<StreamState<TData, TError>>) => () => void;
92
+ getSubscriberCount: () => number;
93
+ variableHash: string;
94
+ connection: {
95
+ get: () => Readonly<TConnection> | undefined;
96
+ reconnect: () => void;
97
+ disconnect: () => void;
98
+ };
99
+ data: {
100
+ reset: () => void;
101
+ };
102
+ delete: () => boolean;
103
+ };
88
104
  export {};
package/esm/react.mjs CHANGED
@@ -785,7 +785,7 @@ const experimental_createStream = (connect, disconnect, options = {}) => {
785
785
  const connections = /* @__PURE__ */ new WeakMap();
786
786
  const disconnectFns = /* @__PURE__ */ new WeakMap();
787
787
  const disconnectTimeoutIds = /* @__PURE__ */ new WeakMap();
788
- const clearDataTimeoutIds = /* @__PURE__ */ new WeakMap();
788
+ const gcTimeoutIds = /* @__PURE__ */ new WeakMap();
789
789
  const configureStoreEvents = () => ({
790
790
  ...options,
791
791
  onFirstSubscribe: (state, store) => {
@@ -842,10 +842,11 @@ const experimental_createStream = (connect, disconnect, options = {}) => {
842
842
  }
843
843
  connections.delete(store);
844
844
  disconnectFns.delete(store);
845
- clearDataTimeoutIds.set(
845
+ gcTimeoutIds.set(
846
846
  store,
847
847
  setTimeout(() => {
848
- store.data.reset();
848
+ if (store.getSubscriberCount()) store.data.reset();
849
+ else store.delete();
849
850
  }, gcTime)
850
851
  );
851
852
  };
@@ -922,14 +923,30 @@ const experimental_createStream = (connect, disconnect, options = {}) => {
922
923
  errorUpdatedAt: void 0
923
924
  });
924
925
  };
926
+ store.delete = () => {
927
+ if (store.getSubscriberCount() > 0) {
928
+ console.warn(
929
+ "Cannot delete store while it still has active subscribers. Unsubscribe all listeners before deleting the store."
930
+ );
931
+ return false;
932
+ }
933
+ store.setState(initialState);
934
+ return stores.delete(variableHash);
935
+ };
925
936
  }
926
937
  const useStore = (options2) => useStoreState(store, {
927
938
  initialState: { data: options2 == null ? void 0 : options2.initialData }
928
939
  });
929
- return Object.assign(useStore, store);
940
+ return Object.assign(useStore, {
941
+ ...store,
942
+ setState: (value) => {
943
+ console.debug("Manual setState (not via provided actions) on stream store");
944
+ store.setState(value);
945
+ }
946
+ });
930
947
  };
931
948
  const triggerReconnect = (store, trigger) => {
932
- clearTimeout(clearDataTimeoutIds.get(store));
949
+ clearTimeout(gcTimeoutIds.get(store));
933
950
  const disconnectTimeoutIds_ = disconnectTimeoutIds.get(store);
934
951
  if (disconnectTimeoutIds_) {
935
952
  clearTimeout(disconnectTimeoutIds_["last-unsubscribe"]);
package/package.json CHANGED
@@ -2,7 +2,10 @@
2
2
  "name": "floppy-disk",
3
3
  "description": "Lightweight unified state management for sync and async data.",
4
4
  "private": false,
5
- "version": "3.7.0",
5
+ "version": "3.7.1-beta.1",
6
+ "publishConfig": {
7
+ "tag": "beta"
8
+ },
6
9
  "keywords": [
7
10
  "utilities",
8
11
  "store",
@@ -1,4 +1,4 @@
1
- import { type InitStoreOptions, type StoreApi } from "../vanilla.ts";
1
+ import { type InitStoreOptions, type SetStateInput } from "../vanilla.ts";
2
2
  import type { StoreKey } from "./create-stores.ts";
3
3
  type StreamDataState<TData, TError> = {
4
4
  state: "INITIAL";
@@ -68,6 +68,7 @@ type AdditionalStoreApi<TConnection> = {
68
68
  data: {
69
69
  reset: () => void;
70
70
  };
71
+ delete: () => boolean;
71
72
  };
72
73
  export type StreamOptions<TConnection, TData, TError = Error> = InitStoreOptions<StreamState<TData, TError>, AdditionalStoreApi<TConnection>> & {
73
74
  connection?: {
@@ -84,5 +85,20 @@ export declare const experimental_createStream: <TConnection, TData, TVariable e
84
85
  error: (error: TError) => void;
85
86
  }) => TConnection, disconnect: (connection: TConnection) => void, options?: StreamOptions<TConnection, TData, TError>) => (variable?: TVariable) => ((options?: {
86
87
  initialData?: TData;
87
- }) => StreamState<TData, TError>) & StoreApi<StreamState<TData, TError>> & AdditionalStoreApi<TConnection>;
88
+ }) => StreamState<TData, TError>) & {
89
+ setState: (value: SetStateInput<StreamState<TData, TError>>) => void;
90
+ getState: () => StreamState<TData, TError>;
91
+ subscribe: (subscriber: import("../vanilla.ts").Subscriber<StreamState<TData, TError>>) => () => void;
92
+ getSubscriberCount: () => number;
93
+ variableHash: string;
94
+ connection: {
95
+ get: () => Readonly<TConnection> | undefined;
96
+ reconnect: () => void;
97
+ disconnect: () => void;
98
+ };
99
+ data: {
100
+ reset: () => void;
101
+ };
102
+ delete: () => boolean;
103
+ };
88
104
  export {};
package/react.js CHANGED
@@ -787,7 +787,7 @@ const experimental_createStream = (connect, disconnect, options = {}) => {
787
787
  const connections = /* @__PURE__ */ new WeakMap();
788
788
  const disconnectFns = /* @__PURE__ */ new WeakMap();
789
789
  const disconnectTimeoutIds = /* @__PURE__ */ new WeakMap();
790
- const clearDataTimeoutIds = /* @__PURE__ */ new WeakMap();
790
+ const gcTimeoutIds = /* @__PURE__ */ new WeakMap();
791
791
  const configureStoreEvents = () => ({
792
792
  ...options,
793
793
  onFirstSubscribe: (state, store) => {
@@ -844,10 +844,11 @@ const experimental_createStream = (connect, disconnect, options = {}) => {
844
844
  }
845
845
  connections.delete(store);
846
846
  disconnectFns.delete(store);
847
- clearDataTimeoutIds.set(
847
+ gcTimeoutIds.set(
848
848
  store,
849
849
  setTimeout(() => {
850
- store.data.reset();
850
+ if (store.getSubscriberCount()) store.data.reset();
851
+ else store.delete();
851
852
  }, gcTime)
852
853
  );
853
854
  };
@@ -924,14 +925,30 @@ const experimental_createStream = (connect, disconnect, options = {}) => {
924
925
  errorUpdatedAt: void 0
925
926
  });
926
927
  };
928
+ store.delete = () => {
929
+ if (store.getSubscriberCount() > 0) {
930
+ console.warn(
931
+ "Cannot delete store while it still has active subscribers. Unsubscribe all listeners before deleting the store."
932
+ );
933
+ return false;
934
+ }
935
+ store.setState(initialState);
936
+ return stores.delete(variableHash);
937
+ };
927
938
  }
928
939
  const useStore = (options2) => useStoreState(store, {
929
940
  initialState: { data: options2 == null ? void 0 : options2.initialData }
930
941
  });
931
- return Object.assign(useStore, store);
942
+ return Object.assign(useStore, {
943
+ ...store,
944
+ setState: (value) => {
945
+ console.debug("Manual setState (not via provided actions) on stream store");
946
+ store.setState(value);
947
+ }
948
+ });
932
949
  };
933
950
  const triggerReconnect = (store, trigger) => {
934
- clearTimeout(clearDataTimeoutIds.get(store));
951
+ clearTimeout(gcTimeoutIds.get(store));
935
952
  const disconnectTimeoutIds_ = disconnectTimeoutIds.get(store);
936
953
  if (disconnectTimeoutIds_) {
937
954
  clearTimeout(disconnectTimeoutIds_["last-unsubscribe"]);