/// <summary> /// Registers the router to a service. /// </summary> /// <remarks>An exception is throw if the router is already registered to a service, or mounted to another router.</remarks> /// <param name="service"></param> public void Register(ResService service) { if (parent != null) { throw new InvalidOperationException("Router is already mounted."); } if (this.service != null) { throw new InvalidOperationException("Router is already registered to a service."); } this.service = service; callOnRegister(); }
/// <summary> /// Traverses the node tree for all handlers, and calls any OnRegister callback. /// If the router or its ancestors are not registered to a service, it will do nothing. /// </summary> private void callOnRegister() { ResService serv = registeredService(); if (serv == null) { return; } string pattern = FullPattern; traverse(root, new List <string>(32), 0, (n, path, mountIdx) => { if (n.Handler != null) { n.Handler.OnRegister(serv, MergePattern(pattern, PathToPattern(n, path, mountIdx))); } }); }
/// <summary> /// Sends a custom event on the resource with payload. /// Throws an exception if the event is one of the pre-defined or reserved events, /// "change", "delete", "add", "remove", "patch", "reaccess", "unsubscribe", or "query". /// For pre-defined events, the matching method, ChangeEvent, AddEvent, /// RemoveEvent, or ReaccessEvent should be used instead. /// </summary> /// <remarks> /// See the protocol specification for more information: /// https://github.com/resgateio/resgate/blob/master/docs/res-service-protocol.md#custom-event /// </remarks> /// <param name="eventName">Name of the event.</param> /// <param name="payload">JSON serializable payload. May be null.</param> public async Task EventAsync(string eventName, object payload) { switch (eventName) { case "change": throw new ArgumentException("Use ChangeEvent to send change events"); case "delete": throw new ArgumentException("Use DeleteEvent to send delete events"); case "add": throw new ArgumentException("Use AddEvent to send add events"); case "remove": throw new ArgumentException("Use RemoveEvent to send remove events"); case "patch": throw new ArgumentException("Reserved event name: \"patch\""); case "reaccess": throw new ArgumentException("Use ReaccessEvent to send reaccess events"); case "unsubscribe": throw new ArgumentException("Reserved event name: \"unsubscribe\""); case "query": throw new ArgumentException("Reserved event name: \"query\""); } if (!ResService.IsValidPart(eventName)) { throw new ArgumentException("Invalid event name: " + eventName); } var ev = new CustomEventArgs(eventName, payload); await Handler.Apply(this, ev); sendEvent(eventName, payload); eventHandler?.Invoke(this, ev); }
/// <summary> /// Called when the handler is registered to a service. /// </summary> /// <param name="service">Service which the handler is registered to.</param> /// <param name="pattern">Full resource id pattern being handled.</param> public virtual void OnRegister(ResService service, String pattern) { handler.OnRegister(service, pattern); }
/// <summary> /// Called when the handler is registered to a service. /// </summary> /// <param name="service">Service which the handler is registered to.</param> /// <param name="pattern">Full resource id pattern being handled.</param> public virtual void OnRegister(ResService service, String pattern) { Service = service; FullPattern = pattern; }
/// <summary> /// Initializes a new instance of the ResourceContext class without any resource specific context. /// </summary> /// <param name="service">Service to which the resource belong.</param> public ResourceContext(ResService service) { Service = service; }