Skip to content

Commit

Permalink
- Removed global_ref from smart environment configuration
Browse files Browse the repository at this point in the history
- Updated integration tests to remove global reference checks
- Modified `merge_env_config` to skip global_ref processing
- Introduced static `create_env_getter` method for dynamic environment access
- Simplified environment configuration across multiple modules
  • Loading branch information
Brian Joseph Petro committed Feb 23, 2025
1 parent f259852 commit 5855f27
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 12 deletions.
3 changes: 0 additions & 3 deletions smart-environment/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ The Smart Environment internally normalizes the options provided in `main_env_op

### Key Components of `main_env_opts`

- **`global_ref`**: A reference to the global object (e.g., `window` in browsers). This is used to store the singleton instance of the Smart Environment.
- **`env_path`**: The base path for the environment, important for file system operations.
- **`collections`**: Defines the data collections managed by the environment.
- Each collection should have:
Expand All @@ -208,8 +207,6 @@ The Smart Environment internally normalizes the options provided in `main_env_op

```js
export const smart_env_config = {
// Global reference (window/global)
global_ref: window,

// Base path for the environment
env_path: '',
Expand Down
4 changes: 0 additions & 4 deletions smart-environment/integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class TheMain {
get smart_env_config() {
if(!this._smart_env_config){ // must be cached to allow proper unload_main()
this._smart_env_config = {
global_ref: global,
env_path: path.resolve('./test/vault'),
collections: {
the_collection: {
Expand Down Expand Up @@ -53,7 +52,6 @@ class TheMain {

class DiffMain {
smart_env_config = {
global_ref: global, // ensures reuse of the same global environment
env_path: path.resolve('./test/vault'),
collections: {
diff_collection: {
Expand Down Expand Up @@ -113,7 +111,6 @@ test.serial('SmartEnv.create() - creates a new instance if none is in the global
t.truthy(env, 'Should create a new SmartEnv instance if none is in the global ref.');
t.is(env.opts.env_path, path.resolve('./test/vault'), 'env_path should match the config.');
t.is(the_main.env, env, 'FakeMain instance "env" property should reference the newly created SmartEnv instance.');
t.is(env.global_ref, global, 'Global ref should be set to Node global.');
t.is(env.mains.length, 1, 'Should have exactly one main in the environment.');
t.is(env.mains[0], 'the_main', 'Main key should be snake_case of constructor name.');
t.truthy(env.the_collection, 'the_collection is loaded onto the environment.');
Expand Down Expand Up @@ -201,7 +198,6 @@ test.serial('SmartEnv.create() - creates a new instance if none is in the global
t.truthy(env, 'Should create a new SmartEnv instance if none is in global ref.');
t.is(env.opts.env_path, path.resolve('./test/vault'), 'env_path matches the config.');
t.is(main.env, env, 'FakeMain env property references the SmartEnv.');
t.is(env.global_ref, global, 'Global ref should be Node global.');
t.is(env.mains.length, 1, 'Exactly one main in the environment.');
t.is(env.mains[0], 'the_main', 'Key is snake_case of constructor name.');
t.truthy(env.the_collection, 'the_collection is loaded.');
Expand Down
9 changes: 8 additions & 1 deletion smart-environment/obsidian.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import { MarkdownBlockContentAdapter } from "smart-blocks/adapters/markdown_bloc
// actions architecture
import smart_block from "smart-blocks/smart_block.js";
import smart_source from "smart-sources/smart_source.js";

import { parse_blocks } from "smart-blocks/content_parsers/parse_blocks.js";
import { parse_links } from 'smart-sources/content_parsers/parse_links.js';
import { parse_metadata } from 'smart-sources/content_parsers/parse_metadata.js';

const OBSIDIAN_DEFAULTS = {
env_path: '',
Expand Down Expand Up @@ -45,6 +47,11 @@ const OBSIDIAN_DEFAULTS = {
// "canvas": MarkdownSourceContentAdapter,
// "default": MarkdownSourceContentAdapter,
},
content_parsers: [
parse_links,
parse_metadata,
parse_blocks,
],
process_embed_queue: false,
},
smart_blocks: {
Expand Down
15 changes: 12 additions & 3 deletions smart-environment/smart_env.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,19 @@ export class SmartEnv {
if(this.global_env?._config) this.global_env._config = null;
const main_key = camel_case_to_snake_case(main.constructor.name);
this.smart_env_configs[main_key] = {main, opts: main_env_opts};
Object.defineProperty(main, 'env', {
get: () => {
this.create_env_getter(main);
}
/**
* Creates a dynamic environment getter on any instance object.
* The returned 'env' property will yield the global `smart_env`.
* @param {Object} instance_to_receive_getter
*/
static create_env_getter(instance_to_receive_getter) {
Object.defineProperty(instance_to_receive_getter, 'env', {
get: function() {
return (typeof window !== 'undefined' ? window : global).smart_env;
}
},
// configurable: true
});
}
async load() {
Expand Down
1 change: 0 additions & 1 deletion smart-environment/utils/merge_env_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { deep_merge_no_overwrite } from './deep_merge_no_overwrite.js';

export function merge_env_config(target, incoming) {
for (const [key, value] of Object.entries(incoming)) {
if (key === 'global_ref') continue;
if (typeof value === 'object' && value !== null) {
if (Array.isArray(value)) {
target[key] = [...(target[key] || []), ...value];
Expand Down

0 comments on commit 5855f27

Please sign in to comment.