@cnstra/core 2.0.0 → 2.1.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.
package/dist/index.js CHANGED
@@ -948,6 +948,58 @@ var CNS = class {
948
948
  }
949
949
  };
950
950
 
951
+ // src/CNSPersistOptionsRegistry.ts
952
+ var CNSPersistOptionsRegistry = class {
953
+ constructor() {
954
+ this.neurons = /* @__PURE__ */ new Map();
955
+ this.neuronNames = /* @__PURE__ */ new WeakMap();
956
+ this.collaterals = /* @__PURE__ */ new Map();
957
+ this.stimulations = /* @__PURE__ */ new Map();
958
+ }
959
+ addNeuron(neuron2, options) {
960
+ this.neurons.set(options.name, neuron2);
961
+ this.neuronNames.set(neuron2, options.name);
962
+ }
963
+ getNeuron(name) {
964
+ return this.neurons.get(name);
965
+ }
966
+ getNeuronName(neuron2) {
967
+ return this.neuronNames.get(neuron2);
968
+ }
969
+ getNamedNeurons() {
970
+ return this.neurons;
971
+ }
972
+ getCollateralName(collateral2) {
973
+ for (const [name, col] of this.collaterals) {
974
+ if (col === collateral2) return name;
975
+ }
976
+ return void 0;
977
+ }
978
+ removeNeuron(name) {
979
+ const neuron2 = this.neurons.get(name);
980
+ if (neuron2) this.neuronNames.delete(neuron2);
981
+ this.neurons.delete(name);
982
+ }
983
+ addCollateral(collateral2, options) {
984
+ this.collaterals.set(options.name, collateral2);
985
+ }
986
+ getCollateral(name) {
987
+ return this.collaterals.get(name);
988
+ }
989
+ removeCollateral(name) {
990
+ this.collaterals.delete(name);
991
+ }
992
+ addStimulation(stimulation, options) {
993
+ this.stimulations.set(options.stimulationId, stimulation);
994
+ }
995
+ getStimulation(stimulationId) {
996
+ return this.stimulations.get(stimulationId);
997
+ }
998
+ removeStimulation(stimulationId) {
999
+ this.stimulations.delete(stimulationId);
1000
+ }
1001
+ };
1002
+
951
1003
  // src/CNSCollateral.ts
952
1004
  var CNSCollateral = class {
953
1005
  createSignal(payload) {
@@ -955,6 +1007,55 @@ var CNSCollateral = class {
955
1007
  }
956
1008
  };
957
1009
 
1010
+ // src/utils/CNSStimulationGate.ts
1011
+ var CNSStimulationGate = class {
1012
+ constructor(guardOptions) {
1013
+ this.guardOptions = guardOptions;
1014
+ }
1015
+ isDraining() {
1016
+ return this.currentDrain !== void 0;
1017
+ }
1018
+ getCurrentStimulation() {
1019
+ return this.currentStimulation;
1020
+ }
1021
+ drain() {
1022
+ if (this.currentDrain) return this.currentDrain;
1023
+ const stimulation = this.guardOptions.cns.stimulate(
1024
+ this.guardOptions.signal,
1025
+ this.createStimulationOptions()
1026
+ );
1027
+ const drain = stimulation.waitUntilComplete().finally(() => {
1028
+ if (this.currentDrain !== drain) return;
1029
+ this.currentDrain = void 0;
1030
+ this.currentStimulation = void 0;
1031
+ this.currentAbortController = void 0;
1032
+ });
1033
+ this.currentStimulation = stimulation;
1034
+ this.currentDrain = drain;
1035
+ return drain;
1036
+ }
1037
+ abort(reason) {
1038
+ if (!this.currentAbortController) return false;
1039
+ if (this.currentAbortController.signal.aborted) return false;
1040
+ this.currentAbortController.abort(reason);
1041
+ return true;
1042
+ }
1043
+ createStimulationOptions() {
1044
+ const options = this.guardOptions.options;
1045
+ if (options?.abortSignal) {
1046
+ return options;
1047
+ }
1048
+ if (typeof AbortController === "undefined") {
1049
+ return options ?? {};
1050
+ }
1051
+ this.currentAbortController = new AbortController();
1052
+ return {
1053
+ ...options,
1054
+ abortSignal: this.currentAbortController.signal
1055
+ };
1056
+ }
1057
+ };
1058
+
958
1059
  // src/factory/index.ts
959
1060
  var collateral = () => new CNSCollateral();
960
1061
  var neuron = (axon) => {
@@ -1097,7 +1198,20 @@ var modality = (afferentPaths) => {
1097
1198
  afferentPaths
1098
1199
  };
1099
1200
  };
1201
+ var createPersistRegistry = (namedNeurons) => {
1202
+ const registry = new CNSPersistOptionsRegistry();
1203
+ for (const [name, neuron2] of Object.entries(namedNeurons)) {
1204
+ registry.addNeuron(neuron2, { name, neuron: neuron2 });
1205
+ for (const [key, col] of Object.entries(neuron2.axon)) {
1206
+ registry.addCollateral(col, {
1207
+ name: key,
1208
+ collateral: col
1209
+ });
1210
+ }
1211
+ }
1212
+ return registry;
1213
+ };
1100
1214
 
1101
- export { CNS, CNSCollateral, CNSStimulationContextStore, afferentPath, collateral, modality, neuron, withCtx };
1215
+ export { CNS, CNSCollateral, CNSPersistOptionsRegistry, CNSStimulationContextStore, CNSStimulationGate, afferentPath, collateral, createPersistRegistry, modality, neuron, withCtx };
1102
1216
  //# sourceMappingURL=index.js.map
1103
1217
  //# sourceMappingURL=index.js.map