Skip to content

Commit

Permalink
feat(core): remove socket.waitState
Browse files Browse the repository at this point in the history
  • Loading branch information
ones-liupengfei committed Oct 23, 2022
1 parent 6be8720 commit 29c4e86
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 81 deletions.
47 changes: 4 additions & 43 deletions packages/core/src/lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,19 @@ export class Socket {
const msg = Errors.accessUninitializedState(namespace)
throw new Error(msg)
}
let dirty = false
let flushed = false
const state: T = readonly(this.stores[namespace].state)
const watcher = new Watcher<P>(namespace, this.stores)
const runner = effect(() => getter(state), {
lazy: true,
scheduler: () => {
if (!dirty) {
dirty = true
if (!flushed) {
flushed = true
Promise.resolve().then(() => {
const watchingState = getter(state)
watcher.handler?.(watchingState, watcher.oldWatchingStates)
watcher.oldWatchingStates = watchingState
dirty = false
flushed = false
})
}
},
Expand All @@ -197,43 +197,4 @@ export class Socket {
watcher.stopEffect = () => runner.effect.stop()
return watcher
}

/**
* waiting for some states to be initialized
* @param dependencies the dependencies to be waited for
* @param timeout the time to wait
*/
public waitState(dependencies: string[], timeout = 10 * 1000): Promise<any[]> {
const allDependencies = [...dependencies]
const unreadyDependencies = dependencies.filter((namespace: string) => {
// remove all ready states first
return !this.existState(namespace)
})

if (unreadyDependencies.length === 0) {
const states = allDependencies.map((namespace: string) => this.getState(namespace))
return Promise.resolve(states)
} else {
return new Promise<any[]>((resolve, reject) => {
const timeId = setTimeout(() => {
clearTimeout(timeId)
const msg = Errors.waitStateTimeout(unreadyDependencies)
reject(new Error(msg))
}, timeout)
const stateInitialCallback = (namespace: string) => {
const index = unreadyDependencies.indexOf(namespace)
if (index !== -1) {
unreadyDependencies.splice(index, 1)
}
if (unreadyDependencies.length === 0) {
clearTimeout(timeId)
this.eventEmitter.removeBroadcastEventListener(STATE_INITIALIZED, stateInitialCallback)
const states = allDependencies.map((namespace: string) => this.getState(namespace))
resolve(states)
}
}
this.eventEmitter.addBroadcastEventListener(STATE_INITIALIZED, stateInitialCallback)
})
}
}
}
3 changes: 0 additions & 3 deletions packages/core/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ export const Errors = {
accessUninitializedState: (namespace: string) => {
return `[@rallie/core] it's not allowed to set or watch state ${namespace} before it is initialized`
},
waitStateTimeout: (namespaces: string[]) => {
return `[@rallie/core] wait for states ${JSON.stringify(namespaces)} timeout`
},
duplicatedInitial: (namespace: string) => {
return `[@rallie/core] duplicated initialized state ${namespace}`
},
Expand Down
35 changes: 0 additions & 35 deletions packages/core/test/state.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,40 +105,6 @@ describe('Test socket.setState', () => {
})
})

describe('Test socket.waitState', () => {
const bus = new Bus('innerBus')
const socket = bus.createSocket()
test('# case 1: wait for some states to be initialized, callback should be called after all states are ready', (done) => {
socket.initState('user', { value: 'user' })
socket.waitState(['user', 'theme'], 2 * 100).then(([user, theme]) => {
expect(user.value).toEqual('user')
expect(theme.value).toEqual('dark')
done()
})
const timerId = setTimeout(() => {
socket.initState('theme', { value: 'dark' })
clearTimeout(timerId)
}, 1 * 100)
})

test('# case 2: wait for some states time out, an error should be throwed', (done) => {
socket
.waitState(['gender', 'session'], 50)
.then(() => {
throw new Error('this should not be callled')
})
.catch((error) => {
expect(error.message).toEqual(Errors.waitStateTimeout(['session']))
done()
})
socket.initState('gender', { value: 'female' })
const timerId = setTimeout(() => {
socket.initState('session', { value: 'abc' })
clearTimeout(timerId)
}, 1 * 100)
})
})

describe('Test socket.watchState', () => {
const bus = new Bus('innerBus')
const socket = bus.createSocket()
Expand Down Expand Up @@ -216,7 +182,6 @@ describe('Test socket.watchState', () => {
})

test('# case 4: test the behavior like watchEffect', async () => {
await socket.waitState(['user']) // only to increase the coverage
console.log = jest.fn()
const watcher = socket.watchState('user', (state) => {
console.log(state.name)
Expand Down

0 comments on commit 29c4e86

Please sign in to comment.