From 499df123b03cfb146b7653699249efb409335b16 Mon Sep 17 00:00:00 2001 From: George Ndungu Date: Tue, 19 May 2020 00:37:19 +0300 Subject: [PATCH 1/2] Emit Signal method and delegate to provide a hook into events --- powershell/cmdlets/class.ts | 5 +++++ powershell/module/module-class.ts | 33 ++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/powershell/cmdlets/class.ts b/powershell/cmdlets/class.ts index 7b08dff6287..add077904bc 100644 --- a/powershell/cmdlets/class.ts +++ b/powershell/cmdlets/class.ts @@ -1023,6 +1023,11 @@ export class CmdletClass extends Class { yield `await ${$this.state.project.serviceNamespace.moduleClass.declaration}.Instance.Signal(${id.value}, ${token.value}, ${messageData.value}, (i,t,m) => ((${ClientRuntime.IEventListener})this).Signal(i,t,()=> ${ClientRuntime.EventDataConverter}.ConvertFrom( m() ) as ${ClientRuntime.EventData} ), ${$this.invocationInfo.value}, this.ParameterSetName, ${$this.correlationId.value}, ${$this.processRecordId.value}, null );`; yield If(`${token.value}.IsCancellationRequested`, Return()); } + else { + // In Non-Azure Modes, emit the Signal method without coorelation and processrecordid + yield `await ${$this.state.project.serviceNamespace.moduleClass.declaration}.Instance.Signal(${id.value}, ${token.value}, ${messageData.value}, (i,t,m) => ((${ClientRuntime.IEventListener})this).Signal(i,t,()=> ${ClientRuntime.EventDataConverter}.ConvertFrom( m() ) as ${ClientRuntime.EventData} ), ${$this.invocationInfo.value}, this.ParameterSetName, null );`; + yield If(`${token.value}.IsCancellationRequested`, Return()); + } yield `WriteDebug($"{id}: {(messageData().Message ?? ${System.String.Empty})}");`; // any handling of the signal on our side... }); diff --git a/powershell/module/module-class.ts b/powershell/module/module-class.ts index 1bed829add9..aa075ac256c 100644 --- a/powershell/module/module-class.ts +++ b/powershell/module/module-class.ts @@ -105,6 +105,7 @@ export class ModuleClass extends Class { yield $this.fPipeline.assignPrivate(ClientRuntime.HttpPipeline.new(ClientRuntime.HttpClientFactory.new(System.Net.Http.HttpClient.new()))); yield $this.fPipelineWithProxy.assignPrivate(ClientRuntime.HttpPipeline.new(ClientRuntime.HttpClientFactory.new(System.Net.Http.HttpClient.new($this.fHandler)))); + yield "Init();"; } })); @@ -113,6 +114,7 @@ export class ModuleClass extends Class { this.add(new PartialMethod('AfterCreatePipeline', dotnet.Void, { parameters: [this.pInvocationInfo, this.pPipeline] })); this.add(new PartialMethod('CustomInit', dotnet.Void)); + /* Setting the Proxy */ this.add(new Method('SetProxyConfiguration', dotnet.Void, { parameters: [this.pProxy, this.pProxyCredential, this.pUseDefaultCredentials], @@ -128,8 +130,23 @@ export class ModuleClass extends Class { } createInitAndPipeline(namespace: Namespace) { + const $this = this; - // non-azure init method + // Custom Event Listener without Azure Spefic concepts. (ProcessId and CorelationId) + const customEventListenerFunc = System.Func( + dotnet.String, + System.Threading.CancellationToken, + System.Func(System.EventArgs), + this.incomingSignalFunc, + InvocationInfo, + dotnet.String, + System.Exception, + /* returns */ System.Threading.Tasks.Task()); + + const incomingSignalDelegate = namespace.add(new Alias('SignalDelegate', this.incomingSignalFunc)); + const eventListenerDelegate = namespace.add(new Alias('EventListenerDelegate', customEventListenerFunc)); + const EventListener = this.add(new Property('EventListener', eventListenerDelegate, { description: 'A delegate that gets called for each signalled event' })); + this.initMethod.add(function* () { yield '// called at module init time...'; yield 'CustomInit();'; @@ -152,6 +169,20 @@ export class ModuleClass extends Class { }); this.add(new LambdaProperty('Name', dotnet.String, new StringExpression(this.state.project.moduleName), { description: 'The Name of this module ' })); + + // Add Signal extensibility point + const pSignal = new Parameter('signal', incomingSignalDelegate, { description: 'The callback for the event dispatcher ' }); + // Emit signal extensibility points that called EventListenerDelegate, allowing us to handle Signals emitted by the Pipeline in the Auth Module + const signalImpl = this.add(new Method('Signal', System.Threading.Tasks.Task(), { + parameters: [this.pId, this.pToken, this.pGetEventData, pSignal, this.pInvocationInfo, this.pParameterSetName, this.pException], async: Modifier.Async, + description: 'Called to dispatch events to the common module listener', + returnsDescription: `A that will be complete when handling of the event is completed.` + })); + + signalImpl.push(Using('NoSynchronizationContext', '')); + signalImpl.add(function* () { + yield `await ${EventListener.value}?.Invoke(${$this.pId.value},${$this.pToken.value},${$this.pGetEventData.value}, ${pSignal.value}, ${$this.pInvocationInfo}, ${$this.pParameterSetName},${$this.pException});`; + }); } createAzureInitAndPipeline(namespace: Namespace) { From 5380df3f56569b96301d2311cc1fd2f945094255 Mon Sep 17 00:00:00 2001 From: George Date: Wed, 24 Jun 2020 20:36:26 +0300 Subject: [PATCH 2/2] Remove Init method constructor call. --- package.json | 2 +- powershell/module/module-class.ts | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/package.json b/package.json index d6149912e79..e60ea945426 100644 --- a/package.json +++ b/package.json @@ -10,4 +10,4 @@ "show-error": "echo ====== &echo ====== & echo ====== & echo REQUIRED: Install PowerShell-core 6.1 or greater (see readme.md) & echo ====== & echo ====== & echo ====== & exit 1", "pack": "rush publish --publish --pack --include-all --tag preview" } -} \ No newline at end of file +} diff --git a/powershell/module/module-class.ts b/powershell/module/module-class.ts index aa075ac256c..63e1b38d9b4 100644 --- a/powershell/module/module-class.ts +++ b/powershell/module/module-class.ts @@ -105,7 +105,6 @@ export class ModuleClass extends Class { yield $this.fPipeline.assignPrivate(ClientRuntime.HttpPipeline.new(ClientRuntime.HttpClientFactory.new(System.Net.Http.HttpClient.new()))); yield $this.fPipelineWithProxy.assignPrivate(ClientRuntime.HttpPipeline.new(ClientRuntime.HttpClientFactory.new(System.Net.Http.HttpClient.new($this.fHandler)))); - yield "Init();"; } }));