floppy-disk 3.7.0-beta.3 → 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) => {
@@ -822,6 +822,34 @@ const experimental_createStream = (connect, disconnect, options = {}) => {
822
822
  }
823
823
  }
824
824
  });
825
+ const disconnectAndSetState = (store, showLog = false) => {
826
+ var _a;
827
+ if (store.getSubscriberCount() && showLog) {
828
+ console.log("Stream disconnected while there is subscriber");
829
+ }
830
+ const disconnectTimeoutIds_ = disconnectTimeoutIds.get(store);
831
+ if (disconnectTimeoutIds_) {
832
+ clearTimeout(disconnectTimeoutIds_["last-unsubscribe"]);
833
+ clearTimeout(disconnectTimeoutIds_["document-hidden"]);
834
+ clearTimeout(disconnectTimeoutIds_.offline);
835
+ }
836
+ (_a = disconnectFns.get(store)) == null ? void 0 : _a();
837
+ if (store.getState().connectionState !== "INITIAL") {
838
+ store.setState({
839
+ connectionState: "DISCONNECTED",
840
+ disconnectedAt: Date.now()
841
+ });
842
+ }
843
+ connections.delete(store);
844
+ disconnectFns.delete(store);
845
+ gcTimeoutIds.set(
846
+ store,
847
+ setTimeout(() => {
848
+ if (store.getSubscriberCount()) store.data.reset();
849
+ else store.delete();
850
+ }, gcTime)
851
+ );
852
+ };
825
853
  const getStore = (variable = {}) => {
826
854
  const variableHash = getHash(variable);
827
855
  let store;
@@ -882,33 +910,7 @@ const experimental_createStream = (connect, disconnect, options = {}) => {
882
910
  connections.set(store, connection);
883
911
  disconnectFns.set(store, () => disconnect(connection));
884
912
  };
885
- store.connection.disconnect = () => {
886
- var _a;
887
- if (store.getSubscriberCount()) {
888
- console.warn("Stream disconnected while there is subscriber");
889
- }
890
- const disconnectTimeoutIds_ = disconnectTimeoutIds.get(store);
891
- if (disconnectTimeoutIds_) {
892
- clearTimeout(disconnectTimeoutIds_["last-unsubscribe"]);
893
- clearTimeout(disconnectTimeoutIds_["document-hidden"]);
894
- clearTimeout(disconnectTimeoutIds_.offline);
895
- }
896
- (_a = disconnectFns.get(store)) == null ? void 0 : _a();
897
- if (store.getState().connectionState !== "INITIAL") {
898
- store.setState({
899
- connectionState: "DISCONNECTED",
900
- disconnectedAt: Date.now()
901
- });
902
- }
903
- connections.delete(store);
904
- disconnectFns.delete(store);
905
- clearDataTimeoutIds.set(
906
- store,
907
- setTimeout(() => {
908
- store.data.reset();
909
- }, gcTime)
910
- );
911
- };
913
+ store.connection.disconnect = () => disconnectAndSetState(store, true);
912
914
  store.data = {};
913
915
  store.data.reset = () => {
914
916
  store.setState({
@@ -921,14 +923,30 @@ const experimental_createStream = (connect, disconnect, options = {}) => {
921
923
  errorUpdatedAt: void 0
922
924
  });
923
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
+ };
924
936
  }
925
937
  const useStore = (options2) => useStoreState(store, {
926
938
  initialState: { data: options2 == null ? void 0 : options2.initialData }
927
939
  });
928
- 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
+ });
929
947
  };
930
948
  const triggerReconnect = (store, trigger) => {
931
- clearTimeout(clearDataTimeoutIds.get(store));
949
+ clearTimeout(gcTimeoutIds.get(store));
932
950
  const disconnectTimeoutIds_ = disconnectTimeoutIds.get(store);
933
951
  if (disconnectTimeoutIds_) {
934
952
  clearTimeout(disconnectTimeoutIds_["last-unsubscribe"]);
@@ -948,7 +966,7 @@ const experimental_createStream = (connect, disconnect, options = {}) => {
948
966
  if (!disconnectTimeoutIds.has(store)) disconnectTimeoutIds.set(store, {});
949
967
  const disconnectTimeoutIds_ = disconnectTimeoutIds.get(store);
950
968
  disconnectTimeoutIds_[trigger] = setTimeout(() => {
951
- store.connection.disconnect();
969
+ disconnectAndSetState(store);
952
970
  }, disconnectDelay);
953
971
  };
954
972
  const triggers = {
package/package.json CHANGED
@@ -2,7 +2,7 @@
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-beta.3",
5
+ "version": "3.7.1-beta.1",
6
6
  "publishConfig": {
7
7
  "tag": "beta"
8
8
  },
@@ -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) => {
@@ -824,6 +824,34 @@ const experimental_createStream = (connect, disconnect, options = {}) => {
824
824
  }
825
825
  }
826
826
  });
827
+ const disconnectAndSetState = (store, showLog = false) => {
828
+ var _a;
829
+ if (store.getSubscriberCount() && showLog) {
830
+ console.log("Stream disconnected while there is subscriber");
831
+ }
832
+ const disconnectTimeoutIds_ = disconnectTimeoutIds.get(store);
833
+ if (disconnectTimeoutIds_) {
834
+ clearTimeout(disconnectTimeoutIds_["last-unsubscribe"]);
835
+ clearTimeout(disconnectTimeoutIds_["document-hidden"]);
836
+ clearTimeout(disconnectTimeoutIds_.offline);
837
+ }
838
+ (_a = disconnectFns.get(store)) == null ? void 0 : _a();
839
+ if (store.getState().connectionState !== "INITIAL") {
840
+ store.setState({
841
+ connectionState: "DISCONNECTED",
842
+ disconnectedAt: Date.now()
843
+ });
844
+ }
845
+ connections.delete(store);
846
+ disconnectFns.delete(store);
847
+ gcTimeoutIds.set(
848
+ store,
849
+ setTimeout(() => {
850
+ if (store.getSubscriberCount()) store.data.reset();
851
+ else store.delete();
852
+ }, gcTime)
853
+ );
854
+ };
827
855
  const getStore = (variable = {}) => {
828
856
  const variableHash = vanilla.getHash(variable);
829
857
  let store;
@@ -884,33 +912,7 @@ const experimental_createStream = (connect, disconnect, options = {}) => {
884
912
  connections.set(store, connection);
885
913
  disconnectFns.set(store, () => disconnect(connection));
886
914
  };
887
- store.connection.disconnect = () => {
888
- var _a;
889
- if (store.getSubscriberCount()) {
890
- console.warn("Stream disconnected while there is subscriber");
891
- }
892
- const disconnectTimeoutIds_ = disconnectTimeoutIds.get(store);
893
- if (disconnectTimeoutIds_) {
894
- clearTimeout(disconnectTimeoutIds_["last-unsubscribe"]);
895
- clearTimeout(disconnectTimeoutIds_["document-hidden"]);
896
- clearTimeout(disconnectTimeoutIds_.offline);
897
- }
898
- (_a = disconnectFns.get(store)) == null ? void 0 : _a();
899
- if (store.getState().connectionState !== "INITIAL") {
900
- store.setState({
901
- connectionState: "DISCONNECTED",
902
- disconnectedAt: Date.now()
903
- });
904
- }
905
- connections.delete(store);
906
- disconnectFns.delete(store);
907
- clearDataTimeoutIds.set(
908
- store,
909
- setTimeout(() => {
910
- store.data.reset();
911
- }, gcTime)
912
- );
913
- };
915
+ store.connection.disconnect = () => disconnectAndSetState(store, true);
914
916
  store.data = {};
915
917
  store.data.reset = () => {
916
918
  store.setState({
@@ -923,14 +925,30 @@ const experimental_createStream = (connect, disconnect, options = {}) => {
923
925
  errorUpdatedAt: void 0
924
926
  });
925
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
+ };
926
938
  }
927
939
  const useStore = (options2) => useStoreState(store, {
928
940
  initialState: { data: options2 == null ? void 0 : options2.initialData }
929
941
  });
930
- 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
+ });
931
949
  };
932
950
  const triggerReconnect = (store, trigger) => {
933
- clearTimeout(clearDataTimeoutIds.get(store));
951
+ clearTimeout(gcTimeoutIds.get(store));
934
952
  const disconnectTimeoutIds_ = disconnectTimeoutIds.get(store);
935
953
  if (disconnectTimeoutIds_) {
936
954
  clearTimeout(disconnectTimeoutIds_["last-unsubscribe"]);
@@ -950,7 +968,7 @@ const experimental_createStream = (connect, disconnect, options = {}) => {
950
968
  if (!disconnectTimeoutIds.has(store)) disconnectTimeoutIds.set(store, {});
951
969
  const disconnectTimeoutIds_ = disconnectTimeoutIds.get(store);
952
970
  disconnectTimeoutIds_[trigger] = setTimeout(() => {
953
- store.connection.disconnect();
971
+ disconnectAndSetState(store);
954
972
  }, disconnectDelay);
955
973
  };
956
974
  const triggers = {