Skip to content

Commit

Permalink
fix(overmind-svelte): add complete typing
Browse files Browse the repository at this point in the history
  • Loading branch information
abalmos committed Sep 29, 2020
1 parent fbbfd23 commit cb3303b
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions packages/node_modules/overmind-svelte/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
import { EventType } from 'overmind'
import { EventType, Overmind, IReaction, IConfiguration } from 'overmind'
import { ITrackCallback } from 'proxy-state-tree'
import { onMount, afterUpdate, onDestroy } from 'svelte'
import { Readable } from 'svelte/store'

const IS_PRODUCTION = process.env.NODE_ENV === 'production'

let nextComponentId = 0

export function createMixin (overmind) {
export function createMixin<Config extends IConfiguration>(
overmind: Overmind<Config>
) {
const componentId = nextComponentId++
let nextComponentInstanceId = 0
let currentFlushId = 0

const subscribe = listener => {
// Svelte "store contract" https://svelte.dev/docs#Store_contract
const subscribe: Readable<any>['subscribe'] = (listener) => {
// @ts-ignore
const tree = overmind.proxyStateTree.getTrackStateTree()
const componentInstanceId = nextComponentInstanceId++
let isUpdating = false

const onUpdate = (mutations, paths, flushId) => {
const onUpdate: ITrackCallback = (_mutations, _paths, flushId) => {
tree.track(onUpdate)
currentFlushId = flushId
isUpdating = true
Expand All @@ -37,7 +43,7 @@ export function createMixin (overmind) {
componentId,
componentInstanceId,
name: '',
paths: Array.from(tree.pathDependencies)
paths: Array.from(tree.pathDependencies),
})
})

Expand All @@ -49,36 +55,39 @@ export function createMixin (overmind) {
componentInstanceId,
name: '',
flushId: currentFlushId,
paths: Array.from(tree.pathDependencies)
paths: Array.from(tree.pathDependencies),
})
}
isUpdating = false
})
}

return () => {
// @ts-ignore
overmind.proxyStateTree.disposeTree(tree)
overmind.eventHub.emitAsync(EventType.COMPONENT_REMOVE, {
componentId,
componentInstanceId: componentInstanceId,
name: ''
name: '',
})
}
}

const reaction = (...args) => {
const reaction: IReaction<Config> = (...args) => {
const dispose = overmind.reaction(...args)

onDestroy(() => {
dispose()
})

return dispose
}

return {
state: { ...overmind.state, subscribe},
state: { ...overmind.state, subscribe },
actions: overmind.actions,
effects: overmind.effects,
addMutationListener: overmind.addMutationListener,
reaction: reaction
reaction: reaction,
}
}

0 comments on commit cb3303b

Please sign in to comment.