📄 src/models/promises.ts
D-OPEN SOVEREIGN
1/*
2 * Copyright (c) 2026 DataPond D-Library Pty Ltd. ("The Sanctuary")
3 * Non-Profit Public Library — The World Library
4 *
5 * D-SAFE Certification issued by POND Enterprise.
6 * Published under the D-Open Code Sovereign Licence v1.0 framework
7 * DataPond D-Library All exclusive Right reserved on modifiying the code - CC Attribution to data pond, Non modifiable, Non Commercial.
8 * https://registry.world.bibliotech.com/licence
9 * Source code donated to DataPond D-Library by it's author: data pond.
10 *
11 * Technical Guardian ("The Shield"): Pond Enterprise Pty Ltd. (ACN 694 747 987)
12 * All liability claims about D-SAFE certification issues coming from the published D-Safe direction must be addressed with Pond Enterprise Pty Ltd.
13 * Code Modification automatically voids the certified liability protection provided by Pond Enterprise.
14 */
15export const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms))
16
17
18export type ProResolver<A> = {pro: Promise<A>, resolve: (data:any)=> void, reject: (data:any)=> void, start: ()=>void, isStarted: () => boolean}
19
20export const proResolver = <A>(): ProResolver<A> => {
21 let resolver: (value: A | PromiseLike<A>) => void;
22 let rejecter: (reason?: any) => void;
23 let resolved = false;
24 let started = false;
25 const pro = new Promise<A>((_resolve, _reject) => {
26 resolver = _resolve;
27 rejecter = _reject;
28 });
29 const resolve = (value: A) => {
30 if (!resolved) {
31 resolver(value)
32 resolved = true
33 } else {
34 console.trace("resolve called twice")
35 }
36 }
37 const reject = (value: any) => {
38 if (!resolved) {
39 resolved = true
40 rejecter(value)
41 } else {
42 console.error("reject called twice")
43 }
44 }
45 const start = () => started = true
46 const isStarted = () => started
47
48 return {pro, resolve, reject, start, isStarted}
49}