Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

14 update getstate to return an array which could be extracted like react usestate #21

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/astral.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/astral.min.js.map

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions specs/context.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ describe('ContextService', () => {
it('getState should initialize and return the state', () => {
const stateKey = 'myState';

const state = ContextService.getState(stateKey);
const [stateValue] = ContextService.getState(stateKey);

expect(state.value).toBeNull();
expect(stateValue).toBeNull();
});

it('getState should return the existing state', () => {
Expand All @@ -20,9 +20,9 @@ describe('ContextService', () => {
// Initialize the state
ContextService.updateState(stateKey, stateValue);

const state = ContextService.getState(stateKey);
const [state] = ContextService.getState(stateKey);

expect(state.value).toBe(stateValue);
expect(state).toBe(stateValue);
});

it('getState should trigger onchange callback upon state change', () => {
Expand All @@ -31,8 +31,8 @@ describe('ContextService', () => {

const onChangeCallback = jest.fn();

const state = ContextService.getState(stateKey);
state.onchange(onChangeCallback);
const [_, onStateChange] = ContextService.getState(stateKey);
onStateChange(onChangeCallback);

// Update the state value
ContextService.updateState(stateKey, stateValue);
Expand All @@ -51,8 +51,8 @@ describe('ContextService', () => {

const onChangeCallback = jest.fn();

const state = ContextService.getState(stateKey);
state.onchange(onChangeCallback);
const [_, onStateChange] = ContextService.getState(stateKey);
onStateChange(onChangeCallback);

// Update the state value
ContextService.updateState(stateKey, newState);
Expand All @@ -73,10 +73,10 @@ describe('ContextService', () => {
[stateKey2]: stateValue2,
});

const state1 = ContextService.getState(stateKey1);
const state2 = ContextService.getState(stateKey2);
const [state1value] = ContextService.getState(stateKey1);
const [state2value] = ContextService.getState(stateKey2);

expect(state1.value).toBe(stateValue1);
expect(state2.value).toBe(stateValue2);
expect(state1value).toBe(stateValue1);
expect(state2value).toBe(stateValue2);
});
});
53 changes: 28 additions & 25 deletions src/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,22 @@ import event from './event';
* @example Astral.context.updateState("myState", "myValue");
* @example Astral.context.setDebugMode(true);
* @example Astral.context.setDebugMode(false);
* @example Astral.context.getState("myState").onchange(function(prevState, newState) { console.log(prevState, newState) });
* @example Astral.context.getState("myState").value;
* @example
* var [value, onchange] = Astral.context.getState("myState");
* onchange(function(prevState, newState) { console.log(prevState, newState) });
* @memberof! Astral
* @public
*/
export default (function () {
/**
* @typedef {Object} State
* @typedef {Array} State
* @property {function} onchange - Callback function to trigger upon state change
* @property {*} value - Current state value
* @memberof! Astral.context
* @public
* @example Astral.context.getState("myState");
* @example Astral.context.getState("myState").onchange(function(prevState, newState) { console.log(prevState, newState) });
* @example Astral.context.getState("myState").value;
* @example
* var [value, onchange] = Astral.context.getState("myState");
* onchange(function(prevState, newState) { console.log(prevState, newState) });
*/

/**
Expand All @@ -31,16 +32,19 @@ export default (function () {
* @property {*} newState - New state value
* @memberof! Astral.context
* @public
* @example Astral.context.getState("myState");
* @example Astral.context.getState("myState").onchange(function(prevState, newState) { console.log(prevState, newState) });
* @example
* var [value, onchange] = Astral.context.getState("myState");
* onchange(function(prevState, newState) { console.log(prevState, newState) });
*/

/**
* @typedef {Object} Listener
* @property {function} onchange - Callback function to trigger upon state change
* @memberof! Astral.context
* @public
* @example Astral.context.getState("myState").onchange(function(prevState, newState) { console.log(prevState, newState) });
* @example
* var [value, onchange] = Astral.context.getState("myState");
* onchange(function(prevState, newState) { console.log(prevState, newState) });
*/

/**
Expand All @@ -51,17 +55,16 @@ export default (function () {
* @name store
*/

/**
* @desc Debug mode
* @memberof! Astral.context
* @private
* @type {Boolean}
* @name _debugMode
* @default false
*/

// initialize store and debug mode
var store = {},
/**
* @desc Debug mode
* @memberof! Astral.context
* @private
* @type {Boolean}
* @name _debugMode
* @default false
*/
_debugMode = false;

/**
Expand All @@ -86,15 +89,15 @@ export default (function () {
* @param {String} key State key
* @public
* @memberof! Astral.context
* @example Astral.context.getState("myState");
* @example Astral.context.getState("myState").onchange(function(prevState, newState) { console.log(prevState, newState) });
* @example Astral.context.getState("myState").value;
* @example
* var [value, onchange] = Astral.context.getState("myState");
* onchange(function(prevState, newState) { console.log(prevState, newState) });
* @returns {State}
*/
function getState(key) {
if (typeof store[key] === 'undefined') _initState(key, null);
var listener = new ListenerConstructor(key);
return { value: store[key], onchange: listener };
return [store[key], listener];
}

/**
Expand Down Expand Up @@ -190,12 +193,12 @@ export default (function () {
* @property {function} getState - Get a state
* @property {function} updateState - Update a state
* @property {function} setDebugMode - Set debug mode
* @example Astral.context.getState("myState");
* @example
* var [value, onchange] = Astral.context.getState("myState");
* onchange(function(prevState, newState) { console.log(prevState, newState) });
* @example Astral.context.updateState("myState", "myValue");
* @example Astral.context.setDebugMode(true);
* @example Astral.context.setDebugMode(false);
* @example Astral.context.getState("myState").onchange(function(prevState, newState) { console.log(prevState, newState) });
* @example Astral.context.getState("myState").value;
*/

return {
Expand Down
Loading