Skip to content

Commit

Permalink
Merge pull request #602 from finsharp/signalling
Browse files Browse the repository at this point in the history
Emit Signal method and delegate to provide a hook into events
  • Loading branch information
dolauli authored Jun 29, 2020
2 parents f54ee7b + 5380df3 commit 7c643f1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
5 changes: 5 additions & 0 deletions powershell/cmdlets/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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...
});
Expand Down
32 changes: 31 additions & 1 deletion powershell/module/module-class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,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],
Expand All @@ -128,8 +129,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();';
Expand All @@ -152,6 +168,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 <see cref="${System.Threading.Tasks.Task()}" /> 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) {
Expand Down

0 comments on commit 7c643f1

Please sign in to comment.