@@ -17,14 +17,14 @@ import { Schema } from "./Schema";
17
17
import {
18
18
ComponentRegistration ,
19
19
DataObject ,
20
+ Dispatcher ,
20
21
EmptyRequestStatus ,
21
22
FetchOpts ,
22
23
FulfilledRequestStatus ,
23
24
LazyNNArgument ,
24
25
LinkedActionResponse ,
25
26
LinkedRenderStoreOptions ,
26
27
MiddlewareActionHandler ,
27
- MiddlewareFn ,
28
28
NamespaceMap ,
29
29
SomeNode ,
30
30
SubscriptionRegistration ,
@@ -39,7 +39,7 @@ declare global {
39
39
}
40
40
}
41
41
42
- export class LinkedRenderStore < T > {
42
+ export class LinkedRenderStore < T > implements Dispatcher {
43
43
public static registerRenderer < T > (
44
44
component : T ,
45
45
type : LazyNNArgument ,
@@ -61,7 +61,7 @@ export class LinkedRenderStore<T> {
61
61
62
62
private api : LinkedDataAPI ;
63
63
private mapping : ComponentStore < T > ;
64
- private middleware : MiddlewareActionHandler ;
64
+ private _dispatch ? : MiddlewareActionHandler ;
65
65
private schema : Schema ;
66
66
private store : RDFStore = new RDFStore ( ) ;
67
67
private subscriptions : SubscriptionRegistration [ ] = [ ] ;
@@ -74,16 +74,29 @@ export class LinkedRenderStore<T> {
74
74
}
75
75
76
76
this . api = opts . api || new DataProcessor ( {
77
- requestNotifier : this . touch . bind ( this ) ,
77
+ dispatch : opts . dispatch ,
78
78
store : this . store ,
79
79
} ) ;
80
+ if ( opts . dispatch ) {
81
+ this . dispatch = opts . dispatch ;
82
+ }
80
83
this . defaultType = opts . defaultType || defaultNS . schema ( "Thing" ) ;
81
84
this . namespaces = opts . namespaces || { ...defaultNS } ;
82
85
this . schema = opts . schema || new Schema ( this . store ) ;
83
86
this . mapping = opts . mapping || new ComponentStore ( this . schema ) ;
84
- // tslint:disable-next-line typedef
85
- const actionMiddleware : MiddlewareFn < T > = ( ) => ( ) => this . execActionByIRI . bind ( this ) ;
86
- this . middleware = this . applyMiddleware ( ...( opts . middleware || [ ] ) , actionMiddleware ) ;
87
+ }
88
+
89
+ public get dispatch ( ) : MiddlewareActionHandler {
90
+ if ( typeof this . _dispatch === "undefined" ) {
91
+ throw new Error ( "Invariant: cannot call `dispatch` before initialization is complete" ) ;
92
+ }
93
+
94
+ return this . _dispatch ;
95
+ }
96
+
97
+ public set dispatch ( value : MiddlewareActionHandler ) {
98
+ this . _dispatch = value ;
99
+ this . api . dispatch = value ;
87
100
}
88
101
89
102
/**
@@ -128,7 +141,7 @@ export class LinkedRenderStore<T> {
128
141
* @param {Object } args The arguments to the function defined by the subject.
129
142
*/
130
143
public async exec ( subject : NamedNode , args ?: DataObject ) : Promise < any > {
131
- return this . middleware ( subject , args ) ;
144
+ return this . dispatch ( subject , args ) ;
132
145
}
133
146
134
147
/**
@@ -317,6 +330,14 @@ export class LinkedRenderStore<T> {
317
330
this . subscriptions . push ( registration ) ;
318
331
}
319
332
333
+ /** @internal */
334
+ public touch ( iri : string | NamedNode , _err ?: Error ) : boolean {
335
+ const resource = typeof iri === "string" ? namedNodeByIRI ( iri ) : iri ;
336
+ this . store . addStatements ( [ new Statement ( resource , defaultNS . ll ( "nop" ) , Literal . fromValue ( 0 ) ) ] ) ;
337
+ this . broadcast ( ) ;
338
+ return true ;
339
+ }
340
+
320
341
/**
321
342
* Returns an entity from the cache directly.
322
343
* This won't cause any network requests even if the entity can't be found.
@@ -329,18 +350,6 @@ export class LinkedRenderStore<T> {
329
350
return this . store . statementsFor ( iri ) ;
330
351
}
331
352
332
- /**
333
- * Binds and reduces an array of middleware function down to a handler.
334
- * @param layers
335
- */
336
- private applyMiddleware ( ...layers : Array < MiddlewareFn < T > > ) : MiddlewareActionHandler {
337
- const storeBound = layers . map ( ( middleware ) => middleware ( this ) ) ;
338
-
339
- const dispatch : MiddlewareActionHandler = ( a : NamedNode , _o : any ) : Promise < any > => Promise . resolve ( a ) ;
340
-
341
- return storeBound . reduceRight ( ( composed , f ) => f ( composed ) , dispatch ) ;
342
- }
343
-
344
353
/**
345
354
* Broadcasts buffered to all subscribers.
346
355
* The actual broadcast might be executed asynchronously to prevent lag.
@@ -389,11 +398,4 @@ export class LinkedRenderStore<T> {
389
398
}
390
399
} ) ;
391
400
}
392
-
393
- private touch ( iri : string | NamedNode , _err ?: Error ) : boolean {
394
- const resource = typeof iri === "string" ? namedNodeByIRI ( iri ) : iri ;
395
- this . store . addStatements ( [ new Statement ( resource , defaultNS . ll ( "nop" ) , Literal . fromValue ( 0 ) ) ] ) ;
396
- this . broadcast ( ) ;
397
- return true ;
398
- }
399
401
}
0 commit comments