Public
Edited
Nov 9, 2022
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// import { newService } from "@kotelnikov/newservice-implementation"
newService = {
const lib = await import("@statewalker/services@^0.2");
return lib.newService;
}
Insert cell
Insert cell
Insert cell
{
// Service declaration:
const FirstService = newService();

let services; // <-- here we will put the list of declared services intercepted by the service consumer

// Define a service consumer. It is notified each time when a new service appears in the system:
FirstService.newConsumer((s) => (services = s));

// Now we can declare two service providers:
const provider1 = FirstService.newProvider();
const provider2 = FirstService.newProvider();

// Each provider will push a new service object:
const stamp = Date.now();
provider1(`Service ${stamp}:1`);
provider2(`Service ${stamp}:2`);

// Close the service when this cell is invalidated:
invalidation.then(FirstService.close);

return services;
}
Insert cell
Insert cell
Insert cell
MyExtension = {
// Service declaration:
const service = newService();
// Close the service (providers and consumers) when this cell is invalidated:
invalidation.then(service.close);
// Return the service:
return service;
}
Insert cell
Insert cell
Insert cell
Insert cell
Generators.observe((notify) => MyExtension.newConsumer(notify).close)
Insert cell
Insert cell
Generators.observe((notify) => MyExtension.newConsumer(notify).close)
Insert cell
Insert cell
Insert cell
{
await restart;

let dispose = [];
for (let i = 0; i < providersNumber; i++) {
// Creates a new provider. The close method from this providers is added
// to the list of all disposal functions.
dispose.push(startProvider(i));
}
// Close all service providers when this cell is invalidated
invalidation.then(() => Promise.all(dispose.map((_) => _())));

// This method creates a new service provider and starts service generation.
function startProvider(n) {
// Create a new service provider
const provider = MyExtension.newProvider(null);
(async function () {
// Asynchronously start pushing new services.
// We check that the service is not closed yet.
for (let i = 0; !provider.closed && i < iterationsNumber; i++) {
await Promises.delay(Math.round(Math.random() * 1000));
await provider(`Foobar - ${n}:${i}`);
}
// Close the service provider. So consumers will see that the last service
// pushed by this provider dissapears from the service list.
provider.close();
})();
// Close the service provider
return provider.close;
}
return "";
}
Insert cell
iterationsNumber=100
Insert cell
Insert cell
Insert cell
dot = require("@observablehq/graphviz@0.2")
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more