Skip to content

Commit

Permalink
fix(@rallie/block): remove the ability to get the trigger of event or…
Browse files Browse the repository at this point in the history
… methods
  • Loading branch information
run-nan committed Mar 4, 2023
1 parent 0ac37e1 commit 718ff08
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 52 deletions.
6 changes: 3 additions & 3 deletions packages/block/src/base-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ export class BaseBlock<T extends BlockType> {

#socket: Socket

constructor(name: string, triggerName: string, socket: Socket) {
constructor(name: string, socket: Socket) {
this.name = name
this.#socket = socket
this.events = this.#socket.createBroadcaster(() => triggerName)
this.methods = this.#socket.createUnicaster(() => triggerName)
this.events = this.#socket.createBroadcaster()
this.methods = this.#socket.createUnicaster()
Reflect.defineProperty(this, 'state', {
get: () => this.#socket.getState<T['state'], T['state']>(constant.stateNamespace(this.name)),
set: () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/block/src/created-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class CreatedBlock<T extends BlockType> extends BaseBlock<T> {
constructor(name: string, globalBus: Bus, globalSocket: Socket, isEntry: boolean) {
const [bus] = touchBus(constant.privateBus(name))
const socket = bus.createSocket()
super(name, name, socket)
super(name, socket)
this.#socket = socket
this.#globalBus = globalBus
this.#globalSocket = globalSocket
Expand Down Expand Up @@ -58,7 +58,7 @@ export class CreatedBlock<T extends BlockType> extends BaseBlock<T> {
if (!this.#connectedBlocks[name]) {
const [bus] = touchBus(constant.privateBus(name))
const socket = bus.createSocket()
this.#connectedBlocks[name] = new BaseBlock<P>(name, this.name, socket)
this.#connectedBlocks[name] = new BaseBlock<P>(name, socket)
}
return this.#connectedBlocks[name] as BaseBlock<P>
}
Expand Down
36 changes: 0 additions & 36 deletions packages/block/test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,4 @@ describe('Test errors and warnings', () => {
}
}).toThrowError(errors.stateIsReadonly('case2'))
})

test('#case3: trigger', () => {
let normalFuncMethodTrigger = ''
let normalFuncEventTrigger = ''
type BlockType = {
methods: {
normalFunc: () => void
arrowFunc: () => void
}
events: {
normalFunc: () => void
arrowFunc: () => void
}
}
const block = createBlock<BlockType>('case3')
block.addMethods({
normalFunc() {
normalFuncMethodTrigger = arguments[arguments.length - 1]
},
})
block.listenEvents({
normalFunc() {
normalFuncEventTrigger = arguments[arguments.length - 1]
},
})
const tester = createBlock('tester')
const connectedBlock = tester.connect<BlockType>(block.name)
connectedBlock.methods.normalFunc()
expect(normalFuncMethodTrigger).toEqual('tester')
connectedBlock.events.normalFunc()
expect(normalFuncEventTrigger).toEqual('tester')
block.methods.normalFunc()
expect(normalFuncMethodTrigger).toEqual('case3')
block.events.normalFunc()
expect(normalFuncEventTrigger).toEqual('case3')
})
})
14 changes: 4 additions & 10 deletions packages/core/src/lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,8 @@ export class Socket {
return new Proxy<T>({} as any, {
get: (target, eventName) => {
return (...args: any[]) => {
return this.#eventEmitter.emitBroadcast(
eventName as string,
...args,
logger?.(eventName as string),
)
logger?.(eventName as string)
return this.#eventEmitter.emitBroadcast(eventName as string, ...args)
}
},
set: () => {
Expand All @@ -94,11 +91,8 @@ export class Socket {
return new Proxy<T>({} as any, {
get: (target, eventName) => {
return (...args: any[]) => {
return this.#eventEmitter.emitUnicast(
eventName as string,
...args,
logger?.(eventName as string),
)
logger?.(eventName as string)
return this.#eventEmitter.emitUnicast(eventName as string, ...args)
}
},
set: () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/load-html/test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ describe('Test utils', () => {
expect(styles[0].innerHTML.trim()).toEqual('background-color: "green";')
expect(styles[1].innerHTML.trim()).toEqual('color: "red";')
const { root: root2 } = utils.parseHtml(html, '#app')
expect(root2.innerHTML.trim()).toEqual('app')
expect(root2?.innerHTML.trim()).toEqual('app')

console.error = jest.fn()
const result = utils.parseHtml('<div>xxxx<div>')
Expand Down

0 comments on commit 718ff08

Please sign in to comment.