Skip to content

Commit 1446b2f

Browse files
[skipruntime] Allow to wait for external resources
1 parent abc23e6 commit 1446b2f

File tree

15 files changed

+1106
-276
lines changed

15 files changed

+1106
-276
lines changed

skipruntime-ts/core/native/src/BaseTypes.sk

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,20 @@ base class Service(
9191
): Map<String, Collection>;
9292
}
9393

94+
value class ReactiveResponse(id: String, watermark: SKStore.Tick)
95+
96+
value class Values(
97+
values: Array<(SKJSON.CJSON, Array<SKJSON.CJSON>)>,
98+
reactive: ReactiveResponse,
99+
)
100+
101+
base class Request {
102+
children =
103+
| Identified(id: String)
104+
}
105+
106+
base class Checker extends Request {
107+
fun check(request: String): void;
108+
}
109+
94110
module end;

skipruntime-ts/core/native/src/Extern.sk

Lines changed: 131 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,24 @@ fun updateOfCollectionWriter(
234234
};
235235
}
236236

237+
@export("SkipRuntime_CollectionWriter__loading")
238+
fun loadingOfCollectionWriter(collection: String): Float {
239+
writer = CollectionWriter(SKStore.DirName::create(collection));
240+
writer.loading() match {
241+
| Success _ -> 0.0
242+
| Failure(err) -> getErrorHdl(err)
243+
};
244+
}
245+
246+
@export("SkipRuntime_CollectionWriter__error")
247+
fun errorOfCollectionWriter(collection: String, error: SKJSON.CJSON): Float {
248+
writer = CollectionWriter(SKStore.DirName::create(collection));
249+
writer.error(error) match {
250+
| Success _ -> 0.0
251+
| Failure(err) -> getErrorHdl(err)
252+
};
253+
}
254+
237255
/************ Resource ****************/
238256

239257
@cpp_extern("SkipRuntime_Resource__reactiveCompute")
@@ -295,6 +313,32 @@ class ExternResourceBuilder(
295313
}
296314
}
297315

316+
/************ Checker ****************/
317+
318+
@cpp_extern("SkipRuntime_Checker__check")
319+
@debug
320+
native fun checkOfChecker(executor: UInt32, request: String): void;
321+
322+
@cpp_extern("SkipRuntime_deleteChecker")
323+
@debug
324+
native fun deleteChecker(mapper: UInt32): void;
325+
326+
@export("SkipRuntime_createChecker")
327+
fun createChecker(executor: UInt32): ExternChecker {
328+
ExternChecker(SKStore.ExternalPointer::create(executor, deleteChecker))
329+
}
330+
331+
@export("SkipRuntime_createIdentified")
332+
fun createIdentified(request: String): Identified {
333+
Identified(request)
334+
}
335+
336+
class ExternChecker(eptr: SKStore.ExternalPointer) extends Checker {
337+
fun check(request: String): void {
338+
checkOfChecker(this.eptr.value, request)
339+
}
340+
}
341+
298342
/************ Service ****************/
299343

300344
@export("SkipRuntime_createService")
@@ -598,28 +642,63 @@ fun getAllOfRuntime(
598642
resource: String,
599643
jsonParams: SKJSON.CJObject,
600644
reactiveAuth: ?Array<UInt8>,
645+
optRequest: ?Request,
601646
): SKJSON.CJSON {
602-
SKStore.runWithResult(context ~> {
603-
getAll(context, resource, params(jsonParams), reactiveAuth)
647+
(getContext() match {
648+
| Some(context) ->
649+
try {
650+
Success(
651+
getAll(context, resource, params(jsonParams), reactiveAuth, optRequest),
652+
)
653+
} catch {
654+
| ex -> Failure(ex)
655+
}
656+
| _ ->
657+
SKStore.runWithResult(context ~> {
658+
getAll(context, resource, params(jsonParams), reactiveAuth, optRequest)
659+
})
604660
}) match {
605-
| Success(values) ->
606-
SKJSON.CJArray(
607-
Array[
661+
| Success(result) ->
662+
reactive = Array<(String, SKJSON.CJSON)>[
663+
("collection", SKJSON.CJString(result.values.reactive.id)),
664+
(
665+
"watermark",
666+
SKJSON.CJString(result.values.reactive.watermark.value.toString()),
667+
),
668+
];
669+
response = Array<(String, SKJSON.CJSON)>[
670+
(
671+
"values",
608672
SKJSON.CJArray(
609-
values.values.map(v ->
673+
result.values.values.map(v ->
610674
SKJSON.CJArray(Array[v.i0, SKJSON.CJArray(v.i1)])
611675
),
612676
),
613-
SKJSON.CJArray(
614-
Array[
615-
SKJSON.CJString(values.reactive.id),
616-
SKJSON.CJString(values.reactive.watermark.value.toString()),
617-
],
677+
),
678+
(
679+
"reactive",
680+
SKJSON.CJObject(
681+
SKJSON.CJFields::create(reactive.sortedBy(x ~> x.i0), x -> x),
618682
),
619-
],
683+
),
684+
];
685+
fields = mutable Vector<(String, SKJSON.CJSON)>[
686+
(
687+
"values",
688+
SKJSON.CJObject(
689+
SKJSON.CJFields::create(response.sortedBy(x ~> x.i0), x -> x),
690+
),
691+
),
692+
("errors", SKJSON.CJArray(result.errors)),
693+
];
694+
result.request.each(request ->
695+
fields.push(("request", SKJSON.CJString(request)))
696+
);
697+
SKJSON.CJObject(
698+
SKJSON.CJFields::create(fields.sortedBy(x ~> x.i0).toArray(), x -> x),
620699
)
621700
| Failure(err) -> SKJSON.CJFloat(getErrorHdl(err))
622-
};
701+
}
623702
}
624703

625704
@export("SkipRuntime_Runtime__getForKey")
@@ -628,11 +707,47 @@ fun getForKeyOfRuntime(
628707
jsonParams: SKJSON.CJObject,
629708
key: SKJSON.CJSON,
630709
reactiveAuth: ?Array<UInt8>,
710+
optRequest: ?Request,
631711
): SKJSON.CJSON {
632-
SKStore.runWithResult(context ~> {
633-
getForKey(context, resource, params(jsonParams), key, reactiveAuth)
712+
(getContext() match {
713+
| Some(context) ->
714+
try {
715+
Success(
716+
getForKey(
717+
context,
718+
resource,
719+
params(jsonParams),
720+
key,
721+
reactiveAuth,
722+
optRequest,
723+
),
724+
)
725+
} catch {
726+
| ex -> Failure(ex)
727+
}
728+
| _ ->
729+
SKStore.runWithResult(context ~> {
730+
getForKey(
731+
context,
732+
resource,
733+
params(jsonParams),
734+
key,
735+
reactiveAuth,
736+
optRequest,
737+
)
738+
})
634739
}) match {
635-
| Success(value) -> SKJSON.CJArray(value)
740+
| Success(result) ->
741+
fields = mutable Vector<(String, SKJSON.CJSON)>[
742+
("values", SKJSON.CJArray(result.values)),
743+
("errors", SKJSON.CJArray(result.errors)),
744+
];
745+
result.request.each(request ->
746+
fields.push(("request", SKJSON.CJString(request)))
747+
);
748+
SKJSON.CJObject(
749+
SKJSON.CJFields::create(fields.sortedBy(x ~> x.i0).toArray(), x -> x),
750+
)
636751
| Failure(err) -> SKJSON.CJFloat(getErrorHdl(err))
637752
};
638753
}

0 commit comments

Comments
 (0)