I’ve got a use case where I want to pass a list into the typeahead handler function:
def get_schema(): bikeshares = get_gbfs_list() def search_bikeshares(pattern): return search(bikeshares, pattern) return schema.Schema( version = "1", fields = [ schema.Typeahead( id = "company", name = "Search providers", desc = "A list of bikeshare providers that match search.", icon = "bicycle", handler = search_bikeshares, ), ], )
bikeshares = get_gbfs_list() fetches a list of items via an API so I just want to run this once. Then the typeahead handler should optimally be provided this list and search using a simple for loop (or just the built in filter in the typeahead module).
As you can see from my example code I’ve tried to use a nested function with a closure over the list of bike shares, but I just get an error:
2022/07/24 13:01:15 error loading applet: failed to load applet: parsing schema for apps/bikeshare/bikeshare.star: field 0 references non-existent handler "search_bikeshares"
I’ve also tried using a lambda function with much the same result. So this seems to not work in go starlark. I could fetch and assign the list in the main scope of the module, that is outside the
get_schema() call. But then I believe the list will be fetched every time the Pixlet app is run because the API call would run every time the module is loaded.
Any advice would be greatly appreciated.