LivelyKernel / lively4-core

A Self-supporting, Web-based Development Environment
https://lively-kernel.org/lively4/lively4-core/start.html
MIT License
77 stars 25 forks source link

Unified place to signal the system that a file updated #373

Closed onsetsu closed 5 years ago

onsetsu commented 5 years ago

Currently, we have multiple locations, in which we signal different parts of the system, that some file updated, e.g.:

-> When saving a file, both of the above are executed in (asynchronous) sequence. I wondered if we could implement a fileUpdated(url, excluded) utility that would do this in one go.

Why would this be useful? Other parts of the system could also signal updates to files, without having to use an editor or a container.

JensLincke commented 5 years ago

we have that unified place now: lively4fetchHandlers

use it like this:


if (self.lively4fetchHandlers) {  
  // remove old instances of me
  self.lively4fetchHandlers = self.lively4fetchHandlers.filter(ea => !ea.isNavbarHandler);
  self.lively4fetchHandlers.push({
    isNavbarHandler: true,
    handle(request, options) {
      // do nothing
    }, 
    async finsihed(request, options) {
      var url = (request.url || request).toString()
      var method = "GET"
      if (options && options.method) method = options.method;
      if (method == "PUT" || method == "DELETE") {
        try {
          for(var container of document.querySelectorAll("lively-container")) {
            var navbar = container.get("lively-container-navbar")
            if (navbar && navbar.onObserveURLChange) {

              await navbar.onObserveURLChange(url, method)
            }
          }        
        } catch(e) {
          console.error(e)
        }
      }
    }
  })

}