Skip to content

Commit

Permalink
fix(@rallie/block): method and event listener can get the trigger nam…
Browse files Browse the repository at this point in the history
…e by the last argument
  • Loading branch information
ones-liupengfei committed Mar 3, 2023
1 parent 82e05f6 commit b4de50b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 13 deletions.
8 changes: 4 additions & 4 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, socket: Socket) {
constructor(name: string, triggerName: string, socket: Socket) {
this.name = name
this.#socket = socket
this.events = this.#socket.createBroadcaster()
this.methods = this.#socket.createUnicaster()
this.events = this.#socket.createBroadcaster(() => triggerName)
this.methods = this.#socket.createUnicaster(() => triggerName)
Reflect.defineProperty(this, 'state', {
get: () => this.#socket.getState<T['state'], T['state']>(constant.stateNamespace(this.name)),
set: () => {
Expand All @@ -45,6 +45,6 @@ export class BaseBlock<T extends BlockType> {
}

public listenEvents(events: Partial<T['events']>) {
return this.#socket.onBroadcast<Partial<T['events']>>(events)
return this.#socket.onBroadcast(events)
}
}
6 changes: 3 additions & 3 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, socket)
super(name, name, socket)
this.#socket = socket
this.#globalBus = globalBus
this.#globalSocket = globalSocket
Expand All @@ -36,7 +36,7 @@ export class CreatedBlock<T extends BlockType> extends BaseBlock<T> {
}

public addMethods(methods: Partial<T['methods']>) {
return this.#socket.onUnicast<Partial<T['methods']>>(methods)
return this.#socket.onUnicast(methods)
}

public relyOn(dependencies: string[]) {
Expand All @@ -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, socket)
this.#connectedBlocks[name] = new BaseBlock<P>(name, this.name, socket)
}
return this.#connectedBlocks[name] as BaseBlock<P>
}
Expand Down
36 changes: 36 additions & 0 deletions packages/block/test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,40 @@ 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')
})
})
18 changes: 12 additions & 6 deletions packages/core/src/lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,16 @@ export class Socket {
* @param logger
*/
public createBroadcaster<T extends Record<string, Function>>(
logger?: (eventName: string) => void,
logger?: (eventName: string) => any,
) {
return new Proxy<T>({} as any, {
get: (target, eventName) => {
return (...args: any[]) => {
logger?.(eventName as string)
return this.#eventEmitter.emitBroadcast(eventName as string, ...args)
return this.#eventEmitter.emitBroadcast(
eventName as string,
...args,
logger?.(eventName as string),
)
}
},
set: () => {
Expand All @@ -87,12 +90,15 @@ export class Socket {
* create a proxy to emit unicast event
* @param logger
*/
public createUnicaster<T extends Record<string, Function>>(logger?: (eventName: string) => void) {
public createUnicaster<T extends Record<string, Function>>(logger?: (eventName: string) => any) {
return new Proxy<T>({} as any, {
get: (target, eventName) => {
return (...args: any[]) => {
logger?.(eventName as string)
return this.#eventEmitter.emitUnicast(eventName as string, ...args)
return this.#eventEmitter.emitUnicast(
eventName as string,
...args,
logger?.(eventName as string),
)
}
},
set: () => {
Expand Down

0 comments on commit b4de50b

Please sign in to comment.