Skip to content

Commit

Permalink
Merge pull request #19 from dottostack/refactor/minimization
Browse files Browse the repository at this point in the history
refactor: minimization and optimization code
  • Loading branch information
eddort authored Aug 18, 2021
2 parents 7029776 + a352e0f commit 4145638
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 69 deletions.
4 changes: 1 addition & 3 deletions packages/dotto.x/computed/computed.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/dotto.x/computed/container.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
})
Expand Down
6 changes: 3 additions & 3 deletions packages/dotto.x/computed/take.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
2 changes: 1 addition & 1 deletion packages/dotto.x/create-store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
43 changes: 17 additions & 26 deletions packages/dotto.x/lifecycle/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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] = []
}
Expand All @@ -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)
}
})
}
9 changes: 4 additions & 5 deletions packages/dotto.x/query/index.js
Original file line number Diff line number Diff line change
@@ -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)
}),
{}
)
})
}
)
17 changes: 7 additions & 10 deletions packages/dotto.x/task/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function startTask() {
tasks++
return () => {
tasks--
if (tasks === 0) {
if (!tasks) {
run_all(resolves)
resolves = []
}
Expand All @@ -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
Expand Down
19 changes: 5 additions & 14 deletions packages/dotto.x/utils/set.js
Original file line number Diff line number Diff line change
@@ -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
})
16 changes: 10 additions & 6 deletions packages/dotto.x/utils/walk.js
Original file line number Diff line number Diff line change
@@ -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
)
}

0 comments on commit 4145638

Please sign in to comment.