Skip to content

Commit

Permalink
patch: remove state init symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Oct 18, 2020
1 parent dfe1965 commit c6b36f2
Showing 1 changed file with 17 additions and 25 deletions.
42 changes: 17 additions & 25 deletions packages/vest/src/core/state/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
import context from '../context';

const SYMBOL_ADD_TO_STATE = Symbol();

export default (function createState() {
function registerHandler(initialValue) {
let key;
function use(patcher) {
const { stateRef } = context.use();
const { stateRef, reg } = context.use();

// Register state handler
if (reg) {
key = reg.key;
if (!Object.prototype.hasOwnProperty.call(stateRef.current(), key)) {
stateRef.set(
key,
typeof initialValue === 'function'
? initialValue.apply(null, reg.args)
: initialValue
);
}
return;
}

if (typeof patcher === 'function') {
update(patcher);
Expand All @@ -25,20 +37,6 @@ export default (function createState() {
return [stateRef.current()[key], update];
}

use[SYMBOL_ADD_TO_STATE] = function (stateKey, args) {
const { stateRef } = context.use();
key = stateKey;

if (!Object.prototype.hasOwnProperty.call(stateRef.current(), key)) {
stateRef.set(
key,
typeof initialValue === 'function'
? initialValue.apply(null, args)
: initialValue
);
}
};

return use;
}

Expand Down Expand Up @@ -71,15 +69,9 @@ export default (function createState() {
};

for (const key in handlers) {
const [handler, args] = [].concat(handlers[key]);
registeredHandlers.push(
Array.isArray(handlers[key])
? context.bind(
{ stateRef },
handlers[key][0][SYMBOL_ADD_TO_STATE],
key,
handlers[key][1]
)
: context.bind({ stateRef }, handlers[key][SYMBOL_ADD_TO_STATE], key)
context.bind({ stateRef, reg: { key, args } }, handler)
);
}

Expand Down

0 comments on commit c6b36f2

Please sign in to comment.