From 675de3a22dd074c1e30d2637b8965b18ba7e93fd Mon Sep 17 00:00:00 2001 From: Mobydack Date: Wed, 18 Aug 2021 16:12:26 +0300 Subject: [PATCH 1/2] refactor: simplifying the function emit --- packages/dotto.x/computed/computed.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/dotto.x/computed/computed.js b/packages/dotto.x/computed/computed.js index 851a33e..3da812a 100644 --- a/packages/dotto.x/computed/computed.js +++ b/packages/dotto.x/computed/computed.js @@ -10,9 +10,7 @@ export const computed = cb => { let emit = () => { lastResult = container.call() - subscribers.forEach(subscriber => { - subscriber(lastResult) - }) + subscribers.forEach(subscriber => subscriber(lastResult)) } let off = () => { From a352e0f4b5c28986a1db7c05498ca3e1d3619cde Mon Sep 17 00:00:00 2001 From: Mobydack Date: Wed, 18 Aug 2021 17:43:42 +0300 Subject: [PATCH 2/2] refactor: minification and optimization code --- packages/dotto.x/computed/container.js | 2 +- packages/dotto.x/computed/take.js | 6 ++-- packages/dotto.x/create-store/index.js | 2 +- packages/dotto.x/lifecycle/index.js | 43 ++++++++++---------------- packages/dotto.x/query/index.js | 9 +++--- packages/dotto.x/task/index.js | 17 +++++----- packages/dotto.x/utils/set.js | 19 +++--------- packages/dotto.x/utils/walk.js | 16 ++++++---- 8 files changed, 48 insertions(+), 66 deletions(-) diff --git a/packages/dotto.x/computed/container.js b/packages/dotto.x/computed/container.js index 9ad5497..b542601 100644 --- a/packages/dotto.x/computed/container.js +++ b/packages/dotto.x/computed/container.js @@ -20,7 +20,7 @@ export const createContainer = (cb, emit, invalidate) => { get_or_create(storeOffHandlers, store, () => { let unbind = onOff(store, () => { listeners.delete(store) - invalidate(listeners.size === 0) + invalidate(!listeners.size) unbind() }) }) diff --git a/packages/dotto.x/computed/take.js b/packages/dotto.x/computed/take.js index 46050a2..45de5cf 100644 --- a/packages/dotto.x/computed/take.js +++ b/packages/dotto.x/computed/take.js @@ -3,7 +3,7 @@ import { target } from './context' export const take = (store, query) => { if (!store.set) return store.get(true) let container = target() - if (container.silent) return store.get(query) - container.add(store, query) - return store.get(query) + return container.silent + ? store.get(query) + : (container.add(store, query), store.get(query)) } diff --git a/packages/dotto.x/create-store/index.js b/packages/dotto.x/create-store/index.js index bbe209d..d8ac55e 100644 --- a/packages/dotto.x/create-store/index.js +++ b/packages/dotto.x/create-store/index.js @@ -46,7 +46,7 @@ export const createStore = (initial = {}) => { return () => { ns.splice(ns.indexOf(cb), 1) this.lc-- - if (this.lc === 0) this.off() + if (!this.lc) this.off() } }, off() { diff --git a/packages/dotto.x/lifecycle/index.js b/packages/dotto.x/lifecycle/index.js index b03c9ba..e4accb3 100644 --- a/packages/dotto.x/lifecycle/index.js +++ b/packages/dotto.x/lifecycle/index.js @@ -13,9 +13,9 @@ const lifecycle = (store, eventKey, creator, bind) => { let adapterContainer = get_or_create(adaptersStorage, store, () => ({})) if (!adapterContainer[eventKey]) { - let adapterDirector = (key, originalData, apiMethods) => { + let adapterDirector = (key, originalData, methods) => { listenerContainer[key].reduceRight((shared, h) => { - !isStoped && h(originalData, { event, methods: apiMethods, shared }) + if (!isStoped) h(originalData, { event, methods, shared }) return shared }, {}) isStoped = false @@ -24,12 +24,11 @@ const lifecycle = (store, eventKey, creator, bind) => { adapterContainer[eventKey] = 1 } - let toUnbind = bind(listenerContainer) - return toUnbind + return bind(listenerContainer) } -const on = (store, handler, key, eventHandler) => { - return lifecycle(store, key, eventHandler, listenerContainer => { +const on = (store, handler, key, eventHandler) => + lifecycle(store, key, eventHandler, listenerContainer => { if (!listenerContainer[key]) { listenerContainer[key] = [] } @@ -39,62 +38,54 @@ const on = (store, handler, key, eventHandler) => { listenerContainer[key].splice(index, 1) } }) -} -export const onCreate = (destStore, cb) => { - return on(destStore, cb, 'create', (store, handler) => { +export const onCreate = (destStore, cb) => + on(destStore, cb, 'create', (store, handler) => { let orig = store.listen.bind(store) store.listen = (...args) => { if (!store.lc) handler([...args]) return orig(...args) } }) -} -export const onOff = (destStore, cb) => { - return on(destStore, cb, 'off', (store, handler) => { +export const onOff = (destStore, cb) => + on(destStore, cb, 'off', (store, handler) => { let orig = store.off.bind(store) store.off = (...args) => { handler([...args]) return orig(...args) } }) -} -export const onSet = (destStore, cb) => { - return on(destStore, cb, 'set', (store, handler) => { +export const onSet = (destStore, cb) => + on(destStore, cb, 'set', (store, handler) => { let orig = store.set.bind(store) store.set = (...args) => { let isAborted let abort = () => (isAborted = true) handler([...args], { abort }) - if (isAborted) return - return orig(...args) + if (!isAborted) return orig(...args) } }) -} -export const onChange = (destStore, cb) => { - return on(destStore, cb, 'change', (store, handler) => { +export const onChange = (destStore, cb) => + on(destStore, cb, 'change', (store, handler) => { let orig = store._emit.bind(store) store._emit = (...args) => { let isAborted let abort = () => (isAborted = true) handler([...args], { abort }) - if (isAborted) return - return orig(...args) + if (!isAborted) return orig(...args) } }) -} -export const onGet = (destStore, cb) => { - return on(destStore, cb, 'get', (store, handler) => { +export const onGet = (destStore, cb) => + on(destStore, cb, 'get', (store, handler) => { let orig = store.get.bind(store) store.get = (...args) => { handler([...args]) return orig(...args) } }) -} diff --git a/packages/dotto.x/query/index.js b/packages/dotto.x/query/index.js index c2b3e69..7dcffa9 100644 --- a/packages/dotto.x/query/index.js +++ b/packages/dotto.x/query/index.js @@ -1,13 +1,12 @@ import { computed, take } from '../computed' -export const query = (store, queries) => { - return computed(() => { - return Object.entries(queries).reduce( +export const query = (store, queries) => + computed(() => + Object.entries(queries).reduce( (acc, [queryKey, queryParam]) => ({ ...acc, [queryKey]: take(store, queryParam) }), {} ) - }) -} + ) diff --git a/packages/dotto.x/task/index.js b/packages/dotto.x/task/index.js index 358a818..88c4e88 100644 --- a/packages/dotto.x/task/index.js +++ b/packages/dotto.x/task/index.js @@ -7,7 +7,7 @@ export function startTask() { tasks++ return () => { tasks-- - if (tasks === 0) { + if (!tasks) { run_all(resolves) resolves = [] } @@ -19,15 +19,12 @@ export function task(cb) { return cb().finally(endtask) } -export function allTasks() { - if (tasks === 0) { - return Promise.resolve() - } else { - return new Promise(resolve => { - resolves.push(resolve) - }) - } -} +export const allTasks = () => + !tasks + ? Promise.resolve() + : new Promise(resolve => { + resolves.push(resolve) + }) export function clearTasks() { tasks = 0 diff --git a/packages/dotto.x/utils/set.js b/packages/dotto.x/utils/set.js index c01c633..2d849ae 100644 --- a/packages/dotto.x/utils/set.js +++ b/packages/dotto.x/utils/set.js @@ -1,16 +1,7 @@ import { walk } from './walk' -const handle = (payload, key, value) => { - return (payload[key] = value) -} - -export const set = (object, key, value) => { - return walk( - object, - key, - ({ payload, key: localKey, isLast, value: localValue }) => { - if (!isLast && localValue === undefined) payload[localKey] = {} - if (isLast) handle(payload, localKey, value) - } - ) -} +export const set = (object, setKey, value) => + walk(object, setKey, ({ payload, key, isLast, value: localValue }) => { + if (!isLast && localValue === undefined) payload[key] = {} + if (isLast) payload[key] = value + }) diff --git a/packages/dotto.x/utils/walk.js b/packages/dotto.x/utils/walk.js index 19db363..ed8b50e 100644 --- a/packages/dotto.x/utils/walk.js +++ b/packages/dotto.x/utils/walk.js @@ -1,14 +1,18 @@ import { toChain } from '../utils/path' -const handle = (payload, key, cb, isLast) => { - cb && cb({ payload, key, isLast, value: payload[key] }) - return payload[key] -} - export const walk = (object, path, cb = null) => { let pathArray = toChain(path) return pathArray.reduce( - (acc, key, i) => acc && handle(acc, key, cb, pathArray.length - 1 === i), + (payload, key, i) => + payload && + (cb && + cb({ + payload, + key, + isLast: pathArray.length - 1 === i, + value: payload[key] + }), + payload[key]), object ) }