Re: Passing a "callback" name to ixsl:schedule-action

Michael Kay <mike-JkSD5nQpfvpWk0Htik3J/[email protected]>
Newsgroups gmane.text.xml.saxon.help
Message-ID <[email protected]>
<xsl:variable name="callback" select="apl:resource-typeahead-callback#0"/>

if it has 0 parameters, apl:resource-typeahead-callback#1 if it has one parameter, etc.

A global function (declared with xsl:function) won't have any data in its closure. If you want to capture state in the function, you need an anonymous function:

<xsl:variable name="someData" select="doc('abc.xml')//something"/>
<xsl:variable name="callback" select="function(){my:do-something-with($someData)}"/>

Here the value of $someData is "remembered" by the function object (in its "closure") and can therefore be referenced later, when the function is executed (i.e. in your case, when the HTTP request is completed).

This doesn't play well with mutable objects, so try to avoid holding references to the HTML page in the closure.

Michael Kay
Saxonica

> On 28 Aug 2020, at 11:23, Martynas Jusevičius <martynas-tyFqBaoSXPC1Z/[email protected]> wrote:
> 
> Thanks. I'm not yet familiar with higher-order functions in XSLT.
> 
> Lets say I want to use <xsl:function
> name="apl:resource-typeahead-callback"> as the callback -- how do I
> supply it as $callback?
> 
> On Fri, Aug 28, 2020 at 11:23 AM Michael Kay <mike-JkSD5nQpfvpWk0Htik3J/[email protected]> wrote:
>> 
>> This is very much in the whole area of my Balisage 2020 paper an Asynchronous XSLT, and of course that describes the limitations of the current design which this use case illustrates well.
>> 
>> You're constrained to invoke a statically-known template within ixsl:schedule-action, but the parameters to that template can include a function item which is invoked dynamically. So you can do something like
>> 
>> <ixsl:schedule-action http-request="....">
>>  <xsl:call-template name="http-completion">
>>     <xsl:with-param name="action" as="function(*)" select="$callback"/>
>>  </xsl:call-template>
>> </ixsl:schedule-action>
>> 
>> <xsl:template name="http-completion">
>>  <xsl:param name="action" as="function(*)"/>
>>  <xsl:sequence select="$action()"/>
>> </xsl:template>
>> 
>> Michael Kay
>> Saxonica
>> 
>> On 28 Aug 2020, at 10:13, Martynas Jusevičius <martynas-tyFqBaoSXPC1Z/[email protected]> wrote:
>> 
>> Hi,
>> 
>> with Saxon-CE I've used a typeahead "library" which would accept both
>> the JS function to be called and the callback as params:
>> 
>> <!-- caller -->
>> 
>> <xsl:call-template name="typeahead:load-xml">
>>   <xsl:with-param name="element" select="."/>
>>   <xsl:with-param name="query" select="$text"/>
>>   <xsl:with-param name="uri" select="$results-uri"/>
>>   <xsl:with-param name="js-function" select="$js-function"/>
>>   <xsl:with-param name="callback" select="ixsl:get(ixsl:window(),
>> 'onresourceTypeaheadCallback')"/>
>> </xsl:call-template>
>> 
>> <!-- library -->
>> 
>> <xsl:template name="typeahead:load-xml">
>>   <xsl:param name="element" as="element()"/>
>>   <xsl:param name="uri" as="xs:anyURI"/>
>>   <xsl:param name="query" as="xs:string"/>
>>   <xsl:param name="js-function" as="xs:string"/>
>>   <xsl:param name="callback"/>
>>   <!-- if the value hasn't changed during the delay -->
>>   <xsl:if test="$query = $element/ixsl:get(., 'value')">
>>       <xsl:value-of select="ixsl:call(ixsl:window(), $js-function, [
>> ixsl:event(), $uri, $callback ])"/>
>>   </xsl:if>
>> </xsl:template>
>> 
>> Now I want to replace all custom HTTP-related functions with
>> <ixsl:schedule-action http-request="">. So I picture something like
>> this:
>> 
>> <xsl:template name="typeahead:load-xml">
>>   <xsl:param name="element" as="element()"/>
>>   <xsl:param name="uri" as="xs:anyURI"/>
>>   <xsl:param name="query" as="xs:string"/>
>>   <!-- if the value hasn't changed during the delay -->
>>   <xsl:if test="$query = $element/ixsl:get(., 'value')">
>>       <xsl:schedule-action http-request="map{ 'method': 'GET',
>> 'href': $uri, 'headers': map{ 'Accept': 'application/rdf+xml' } }">
>>           <xsl:call-template name="?????????"/>
>>       </ixsl:schedule action>
>>   </xsl:if>
>> </xsl:template>
>> 
>> So <xsl:call-template> functions like a callback here, but I cannot
>> pass template name as a param. Of course I could hardcode some name
>> like "typeahead:xml-loaded", but I would prefer not doing that.
>> 
>> Is there an idiomatic Saxon-JS solution to this?
>> 
>> 
>> _______________________________________________
>> saxon-help mailing list archived at http://saxon.markmail.org/
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/saxon-help
>> 
>> 
>> _______________________________________________
>> saxon-help mailing list archived at http://saxon.markmail.org/
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/saxon-help
> 
> 
> _______________________________________________
> saxon-help mailing list archived at http://saxon.markmail.org/
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/saxon-help

_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
[email protected]
https://lists.sourceforge.net/lists/listinfo/saxon-help
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.