@@ -12,12 +12,21 @@ import {
1212import { authenticate , fetchWebSocket , startWebsocket } from './websockets.js' ;
1313
1414/** Function called when a resource is updated or removed */
15- type Callback = ( resource : Resource ) => void ;
15+ type ResourceCallback = ( resource : Resource ) => void ;
16+ /** Callback called when the stores agent changes */
17+ type AgentCallback = ( agent : Agent | undefined ) => void ;
1618
1719/** Returns True if the client has WebSocket support */
1820const supportsWebSockets = ( ) => typeof WebSocket !== 'undefined' ;
1921type Fetch = typeof fetch ;
2022
23+ export interface StoreOpts {
24+ /** The default store URL, where to send commits and where to create new instances */
25+ serverUrl ?: string ;
26+ /** Default Agent, used for signing commits. Is required for posting things. */
27+ agent ?: Agent ;
28+ }
29+
2130export enum StoreEvents {
2231 /**
2332 * Whenever `Resource.save()` is called, so only when the user of this library
@@ -31,15 +40,18 @@ export enum StoreEvents {
3140 * the SideBar.
3241 */
3342 ResourceManuallyCreated = 'resource-manually-created' ,
43+ /** Event that gets called whenever the stores agent changes */
44+ AgentChanged = 'agent-changed' ,
3445}
3546
3647/**
3748 * Handlers are functions that are called when a certain event occurs.
3849 */
3950type StoreEventHandlers = {
40- [ StoreEvents . ResourceSaved ] : Callback ;
41- [ StoreEvents . ResourceRemoved ] : Callback ;
42- [ StoreEvents . ResourceManuallyCreated ] : Callback ;
51+ [ StoreEvents . ResourceSaved ] : ResourceCallback ;
52+ [ StoreEvents . ResourceRemoved ] : ResourceCallback ;
53+ [ StoreEvents . ResourceManuallyCreated ] : ResourceCallback ;
54+ [ StoreEvents . AgentChanged ] : AgentCallback ;
4355} ;
4456
4557/**
@@ -50,7 +62,7 @@ type StoreEventHandlers = {
5062 */
5163export class Store {
5264 /** A list of all functions that need to be called when a certain resource is updated */
53- public subscribers : Map < string , Array < Callback > > ;
65+ public subscribers : Map < string , Array < ResourceCallback > > ;
5466 /**
5567 * Is called when the store encounters an error. By default simply throws the
5668 * error, but can be overwritten
@@ -73,14 +85,7 @@ export class Store {
7385
7486 private client : Client ;
7587
76- public constructor (
77- opts : {
78- /** The default store URL, where to send commits and where to create new instances */
79- serverUrl ?: string ;
80- /** Default Agent, used for signing commits. Is required for posting things. */
81- agent ?: Agent ;
82- } = { } ,
83- ) {
88+ public constructor ( opts : StoreOpts = { } ) {
8489 this . _resources = new Map ( ) ;
8590 this . webSockets = new Map ( ) ;
8691 this . subscribers = new Map ( ) ;
@@ -91,6 +96,10 @@ export class Store {
9196 this . errorHandler = ( e : Error ) => {
9297 throw e ;
9398 } ;
99+
100+ // We need to bind this method because it is passed down by other functions
101+ this . getAgent = this . getAgent . bind ( this ) ;
102+ this . setAgent = this . setAgent . bind ( this ) ;
94103 }
95104
96105 /** All the resources of the store */
@@ -195,10 +204,15 @@ export class Store {
195204 ) {
196205 fetchWebSocket ( ws , subject ) ;
197206 } else {
207+ const signInfo = this . agent
208+ ? { agent : this . agent , serverURL : this . getServerUrl ( ) }
209+ : undefined ;
210+
198211 const [ _ , createdResources ] = await this . client . fetchResourceHTTP (
199212 subject ,
200213 {
201214 from : opts . fromProxy ? this . getServerUrl ( ) : undefined ,
215+ signInfo,
202216 } ,
203217 ) ;
204218
@@ -440,15 +454,19 @@ export class Store {
440454
441455 if ( agent ) {
442456 setCookieAuthentication ( this . serverUrl , agent ) ;
457+
443458 this . webSockets . forEach ( ws => {
444459 authenticate ( ws , this ) ;
445460 } ) ;
461+
446462 this . resources . forEach ( r => {
447463 if ( r . isUnauthorized ( ) ) {
448464 this . fetchResourceFromServer ( r . getSubject ( ) ) ;
449465 }
450466 } ) ;
451467 }
468+
469+ this . eventManager . emit ( StoreEvents . AgentChanged , agent ) ;
452470 }
453471
454472 /** Sets the Server base URL, without the trailing slash. */
@@ -483,7 +501,7 @@ export class Store {
483501 * this, you should probably also call .unsubscribe some time later.
484502 */
485503 // TODO: consider subscribing to properties, maybe add a second subscribe function, use that in useValue
486- public subscribe ( subject : string , callback : Callback ) : void {
504+ public subscribe ( subject : string , callback : ResourceCallback ) : void {
487505 if ( subject === undefined ) {
488506 throw Error ( 'Cannot subscribe to undefined subject' ) ;
489507 }
@@ -533,7 +551,7 @@ export class Store {
533551 }
534552
535553 /** Unregisters the callback (see `subscribe()`) */
536- public unsubscribe ( subject : string , callback : Callback ) : void {
554+ public unsubscribe ( subject : string , callback : ResourceCallback ) : void {
537555 if ( subject === undefined ) {
538556 return ;
539557 }
0 commit comments