@agentshouse/mdruntime 0.1.0-alpha.26 → 0.1.0-alpha.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,6 @@
1
1
  import { verify } from '@agentshouse/mdmodel';
2
2
  import { cached, modelTypes, NO_MODEL, sameModel } from './model.js';
3
+ import { opened } from './observation.js';
3
4
  import { filterOf, identityOf, planOf } from './query.js';
4
5
  import { createReadSide } from './read-side.js';
5
6
  import { createSearchIndex, searchPlanOf } from './search.js';
@@ -37,6 +38,9 @@ function declaredSelection(room) {
37
38
  return selection.roots.every((root, index)=>validRoot(root) && root !== '' && !within(room.collectionRoot, root) && selection.roots.every((other, at)=>at === index || !within(other, root)));
38
39
  }
39
40
  export function createMdRuntime(options) {
41
+ return composeRuntime(options).md;
42
+ }
43
+ export function composeRuntime(options) {
40
44
  const period = options.discoveryIntervalMs ?? DEFAULT_DISCOVERY_INTERVAL_MS;
41
45
  let authorization = options.authorization;
42
46
  let opening;
@@ -1769,7 +1773,89 @@ export function createMdRuntime(options) {
1769
1773
  notifyReady();
1770
1774
  });
1771
1775
  void enqueue();
1772
- return {
1776
+ function watch(topic, input, deliver) {
1777
+ if (topic === 'room-drafts') {
1778
+ const listener = {
1779
+ notify: deliver,
1780
+ ready: false
1781
+ };
1782
+ roomDraftListeners.add(listener);
1783
+ void exclusive(async ()=>{
1784
+ await openLocal();
1785
+ if (closed || !roomDraftListeners.has(listener)) return;
1786
+ listener.ready = true;
1787
+ }).then(()=>{
1788
+ if (closed || !listener.ready || !roomDraftListeners.has(listener)) return;
1789
+ listener.notify(snapshotRoomDrafts());
1790
+ }, ()=>{});
1791
+ return ()=>{
1792
+ roomDraftListeners.delete(listener);
1793
+ };
1794
+ }
1795
+ if (topic === 'conversation') {
1796
+ return conversations.watch(input[0], input[1], deliver);
1797
+ }
1798
+ if (topic === 'presentation') {
1799
+ const listener = {
1800
+ app: input[0],
1801
+ notify: deliver,
1802
+ ready: false
1803
+ };
1804
+ presentationListeners.add(listener);
1805
+ void exclusive(async ()=>{
1806
+ await openLocal();
1807
+ }).then(()=>{
1808
+ if (closed || !presentationListeners.has(listener)) return;
1809
+ listener.ready = true;
1810
+ listener.notify(presentationOf(listener.app));
1811
+ }, ()=>{});
1812
+ return ()=>{
1813
+ presentationListeners.delete(listener);
1814
+ };
1815
+ }
1816
+ if (topic === 'rooms') {
1817
+ roomListeners.add(deliver);
1818
+ deliver(listed());
1819
+ return ()=>{
1820
+ roomListeners.delete(deliver);
1821
+ };
1822
+ }
1823
+ if (topic === 'connection-refusal') {
1824
+ refusalListeners.add(deliver);
1825
+ deliver(refusal);
1826
+ return ()=>{
1827
+ refusalListeners.delete(deliver);
1828
+ };
1829
+ }
1830
+ if (topic === 'storage-loss') {
1831
+ storageLossListeners.add(deliver);
1832
+ deliver(storageLoss);
1833
+ return ()=>{
1834
+ storageLossListeners.delete(deliver);
1835
+ };
1836
+ }
1837
+ if (topic === 'sync-status') {
1838
+ statusListeners.add(deliver);
1839
+ deliver(shown);
1840
+ return ()=>{
1841
+ statusListeners.delete(deliver);
1842
+ };
1843
+ }
1844
+ if (topic === 'queue') {
1845
+ const queue = {
1846
+ locator: input[0],
1847
+ notify: deliver
1848
+ };
1849
+ queueSubscriptions.add(queue);
1850
+ queue.notify(entriesOf(queue.locator));
1851
+ return ()=>{
1852
+ queueSubscriptions.delete(queue);
1853
+ };
1854
+ }
1855
+ const plan = planOf(input[0], roomFacts());
1856
+ return readSide.watch(plan, identityOf(plan), deliver);
1857
+ }
1858
+ const md = {
1773
1859
  loaded () {
1774
1860
  return openLocal().then(()=>undefined);
1775
1861
  },
@@ -1913,93 +1999,9 @@ export function createMdRuntime(options) {
1913
1999
  persistence () {
1914
2000
  return options.shell.persistentStorage;
1915
2001
  },
1916
- observe (topic, second, third, fourth) {
1917
- if (topic === 'room-drafts') {
1918
- const notify = second;
1919
- const listener = {
1920
- notify,
1921
- ready: false
1922
- };
1923
- roomDraftListeners.add(listener);
1924
- void exclusive(async ()=>{
1925
- await openLocal();
1926
- if (closed || !roomDraftListeners.has(listener)) return;
1927
- listener.ready = true;
1928
- }).then(()=>{
1929
- if (closed || !listener.ready || !roomDraftListeners.has(listener)) return;
1930
- listener.notify(snapshotRoomDrafts());
1931
- }, ()=>{});
1932
- return ()=>{
1933
- roomDraftListeners.delete(listener);
1934
- };
1935
- }
1936
- if (topic === 'conversation') {
1937
- return conversations.observe(second, third, fourth);
1938
- }
1939
- if (topic === 'presentation') {
1940
- const listener = {
1941
- app: second,
1942
- notify: third,
1943
- ready: false
1944
- };
1945
- presentationListeners.add(listener);
1946
- void exclusive(async ()=>{
1947
- await openLocal();
1948
- }).then(()=>{
1949
- if (closed || !presentationListeners.has(listener)) return;
1950
- listener.ready = true;
1951
- listener.notify(presentationOf(listener.app));
1952
- }, ()=>{});
1953
- return ()=>{
1954
- presentationListeners.delete(listener);
1955
- };
1956
- }
1957
- if (topic === 'rooms') {
1958
- const notify = second;
1959
- roomListeners.add(notify);
1960
- notify(listed());
1961
- return ()=>{
1962
- roomListeners.delete(notify);
1963
- };
1964
- }
1965
- if (topic === 'connection-refusal') {
1966
- const notify = second;
1967
- refusalListeners.add(notify);
1968
- notify(refusal);
1969
- return ()=>{
1970
- refusalListeners.delete(notify);
1971
- };
1972
- }
1973
- if (topic === 'storage-loss') {
1974
- const notify = second;
1975
- storageLossListeners.add(notify);
1976
- notify(storageLoss);
1977
- return ()=>{
1978
- storageLossListeners.delete(notify);
1979
- };
1980
- }
1981
- if (topic === 'sync-status') {
1982
- const notify = second;
1983
- statusListeners.add(notify);
1984
- notify(shown);
1985
- return ()=>{
1986
- statusListeners.delete(notify);
1987
- };
1988
- }
1989
- if (topic === 'queue') {
1990
- const queue = {
1991
- locator: second,
1992
- notify: third
1993
- };
1994
- queueSubscriptions.add(queue);
1995
- queue.notify(entriesOf(queue.locator));
1996
- return ()=>{
1997
- queueSubscriptions.delete(queue);
1998
- };
1999
- }
2000
- const plan = planOf(second, roomFacts());
2001
- return readSide.observe(plan, identityOf(plan), third);
2002
- },
2002
+ observe: (topic, ...input)=>opened((deliver)=>watch(topic, input, (message)=>{
2003
+ deliver(structuredClone(message));
2004
+ }), topic === 'query'),
2003
2005
  async reauthorize () {
2004
2006
  if (refusal === null || !refusal.reauthorizationOffered) return;
2005
2007
  journal.record('connection reauthorize');
@@ -2009,6 +2011,10 @@ export function createMdRuntime(options) {
2009
2011
  await enqueue();
2010
2012
  }
2011
2013
  };
2014
+ return {
2015
+ md,
2016
+ watch
2017
+ };
2012
2018
  }
2013
2019
 
2014
2020
  //# sourceMappingURL=connection.js.map