Skip to content

Commit

Permalink
✨ Add OperationalContext class for keeping track of complex operations
Browse files Browse the repository at this point in the history
  • Loading branch information
skerit committed Mar 22, 2024
1 parent c904626 commit d04e114
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* Make `Datasource#toDatasource()` work without a callback
* Make `Datasource#toApp()` work without a callback
* Print a warning when a stage takes over 10 seconds to finish
* Add `OperationalContext` class for keeping track of complex operations

## 1.4.0-alpha.3 (2024-02-25)

Expand Down
97 changes: 97 additions & 0 deletions lib/class/operational_context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
const DATA = Symbol('data'),
PARENT = Symbol('parent');

/**
* The OperationalContext class
*
* @constructor
*
* @author Jelle De Loecker <[email protected]>
* @since 1.4.0
* @version 1.4.0
*
* @param {Alchemy.OperationalContext} parent
*/
const OperationalContext = Function.inherits('Alchemy.Base', 'Alchemy.OperationalContext', function OperationalContext(parent) {

// Store the optional parent context
this[PARENT] = parent || null;

// Create the data object
this[DATA] = parent ? Object.create(parent[DATA]) : {};
});

/**
* The property creator
*
* @author Jelle De Loecker <[email protected]>
* @since 1.4.0
* @version 1.4.0
*/
OperationalContext.setStatic(function setContextProperty(name, getter, setter) {

let camelized = name.camelize(),
set_method = 'set' + camelized,
get_method = 'get' + camelized;

if (setter) {
this.setMethod(set_method, function set(value) {
this[DATA][name] = setter.call(this, value);
return this;
});
} else {
this.setMethod(set_method, function set(value) {
this[DATA][name] = value;
return this;
});
}

if (getter) {
this.setMethod(get_method, function get() {
return getter.call(this, this[DATA][name]);
});
} else {
this.setMethod(get_method, function get() {
return this[DATA][name];
});
}
});

/**
* Get the parent
*
* @author Jelle De Loecker <[email protected]>
* @since 1.4.0
* @version 1.4.0
*
* @return {Alchemy.OperationalContext}
*/
OperationalContext.setMethod(function getParent() {
return this[PARENT];
});

/**
* Set the parent
*
* @author Jelle De Loecker <[email protected]>
* @since 1.4.0
* @version 1.4.0
*
* @param {Alchemy.OperationalContext} context
*/
OperationalContext.setMethod(function setParent(context) {
return this[PARENT] = context;
});

/**
* Create a new child instance
*
* @author Jelle De Loecker <[email protected]>
* @since 1.4.0
* @version 1.4.0
*
* @return {OperationalContext}
*/
OperationalContext.setMethod(function createChild() {
return new this.constructor(this);
});
9 changes: 9 additions & 0 deletions lib/stages/00-load_core.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,15 @@ const core_classes = load_core.createStage('core_classes', () => {
*/
alchemy.hawkejs.load(resolveCorePath('class', 'schema_client'), CLIENT_HAWKEJS_OPTIONS);

/**
* Require the operational_context class on the client side too
*
* @author Jelle De Loecker <[email protected]>
* @since 1.4.0
* @version 1.4.0
*/
alchemy.hawkejs.load(resolveCorePath('class', 'operational_context'), CLIENT_HAWKEJS_OPTIONS);

/**
* Require the setting classes on the client side too
*
Expand Down

0 comments on commit d04e114

Please sign in to comment.